-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add waitForBlock method * fix: add test * fix: fix typo * fix: add comments * fix: improve naming and tests * fix: improve assert * fix: change client to module for block in thor * fix: use fixture * fix: fix typos * fix: add the maximum timeout option * fix: update waitForBlock * fix: improve waitForBlock options param * fix: remove unused test * fix: fix test
- Loading branch information
1 parent
e9580d0
commit f222463
Showing
8 changed files
with
193 additions
and
6 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
packages/network/src/clients/thor-client/blocks/blocks-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { DATA, assert } from '@vechainfoundation/vechain-sdk-errors'; | ||
import { type HttpClient, Poll } from '../../../utils'; | ||
import { type BlockDetail, BlocksClient } from '../../thorest-client'; | ||
import { type WaitForBlockOptions } from './types'; | ||
|
||
/** The `BlocksModule` class encapsulates functionality for interacting with blocks | ||
* on the VechainThor blockchain. | ||
*/ | ||
class BlocksModule { | ||
/** | ||
* Internal blocks client instance used for interacting with block-related endpoints. | ||
*/ | ||
private readonly blocksClient: BlocksClient; | ||
|
||
/** | ||
* Initializes a new instance of the `NodeModule` class. | ||
* @param httpClient - The HTTP client instance used for making HTTP requests. | ||
*/ | ||
constructor(readonly httpClient: HttpClient) { | ||
// Create an instance of BlocksClient using the provided HTTP client | ||
this.blocksClient = new BlocksClient(httpClient); | ||
} | ||
|
||
/** | ||
* Synchronously waits for a specific block revision using polling. | ||
* | ||
* @param revision - The block number or ID to wait for. | ||
* @returns A promise that resolves to an object containing the block details. | ||
*/ | ||
public async waitForBlock( | ||
blockNumber: number, | ||
options?: WaitForBlockOptions | ||
): Promise<BlockDetail | null> { | ||
assert( | ||
blockNumber === undefined || | ||
blockNumber === null || | ||
typeof blockNumber !== 'number' || | ||
blockNumber >= 0, | ||
DATA.INVALID_DATA_TYPE, | ||
'Invalid blockNumber. The blockNumber must be a number representing a block number.', | ||
{ blockNumber } | ||
); | ||
|
||
// Use the Poll.SyncPoll utility to repeatedly call getBestBlock with a specified interval | ||
const block = await Poll.SyncPoll( | ||
async () => await this.blocksClient.getBestBlock(), | ||
{ | ||
requestIntervalInMilliseconds: options?.intervalMs, | ||
maximumWaitingTimeInMilliseconds: options?.timeoutMs | ||
} | ||
).waitUntil((result) => { | ||
// Continue polling until the result's block number matches the specified revision | ||
return result != null && result?.number >= blockNumber; | ||
}); | ||
|
||
return block; | ||
} | ||
} | ||
|
||
export { BlocksModule }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './types.d'; | ||
export * from './blocks-module'; |
21 changes: 21 additions & 0 deletions
21
packages/network/src/clients/thor-client/blocks/types.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* --- Input options start --- */ | ||
|
||
/** | ||
* Options for `waitForBlock` method. | ||
*/ | ||
interface WaitForBlockOptions { | ||
/** | ||
* Timeout in milliseconds. | ||
* After this time, the method will throw an error. | ||
*/ | ||
timeoutMs?: number; | ||
/** | ||
* Interval in milliseconds. | ||
* The method will check the blocks status every `intervalMs` milliseconds. | ||
*/ | ||
intervalMs?: number; | ||
} | ||
|
||
/* --- Input options end --- */ | ||
|
||
export type { WaitForBlockOptions }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/network/tests/clients/thor-client/blocks/blocks.testnet.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
import { thorClient, thorestClient } from '../../../fixture'; | ||
import { waitForBlockTestCases } from './fixture'; | ||
|
||
/** | ||
* Blocks Module integration tests | ||
* | ||
* @group integration/clients/thor-client/blocks | ||
*/ | ||
describe('Blocks Module', () => { | ||
/** | ||
* Test suite for waitForBlock method | ||
*/ | ||
describe('waitForBlock', () => { | ||
waitForBlockTestCases.forEach(({ description, options }) => { | ||
test( | ||
description, | ||
async () => { | ||
// Get best block | ||
const bestBlock = await thorestClient.blocks.getBestBlock(); | ||
if (bestBlock != null) { | ||
const expectedBlock = | ||
await thorClient.blocks.waitForBlock( | ||
bestBlock?.number + 1, | ||
options | ||
); | ||
expect(expectedBlock?.number).toBe( | ||
bestBlock?.number + 1 | ||
); | ||
} | ||
}, | ||
15000 | ||
); | ||
}); | ||
}); | ||
|
||
test('waitForBlock - invalid blockNumber', async () => { | ||
await expect( | ||
async () => await thorClient.blocks.waitForBlock(-2) | ||
).rejects.toThrowError( | ||
'Invalid blockNumber. The blockNumber must be a number representing a block number.' | ||
); | ||
}); | ||
|
||
test('waitForBlock - maximumWaitingTimeInMilliseconds', async () => { | ||
// Get best block | ||
const bestBlock = await thorestClient.blocks.getBestBlock(); | ||
if (bestBlock != null) { | ||
const block = await thorClient.blocks.waitForBlock( | ||
bestBlock?.number + 2, | ||
{ | ||
timeoutMs: 1000 | ||
} | ||
); | ||
expect(block).toBeDefined(); | ||
} | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/network/tests/clients/thor-client/blocks/fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* waitForBlock test cases | ||
*/ | ||
const waitForBlockTestCases = [ | ||
{ | ||
description: | ||
'Should wait for block without timeoutMs and intervalMs and return BlockDetail', | ||
options: { | ||
timeoutMs: undefined, | ||
intervalMs: undefined | ||
} | ||
}, | ||
{ | ||
description: | ||
'Should wait for block with timeoutMs and return BlockDetail', | ||
options: { | ||
timeoutMs: 10000, | ||
intervalMs: undefined | ||
} | ||
}, | ||
{ | ||
description: | ||
'Should wait for transaction with intervalMs and return BlockDetail', | ||
options: { | ||
timeoutMs: undefined, | ||
intervalMs: 1000 | ||
} | ||
}, | ||
{ | ||
description: | ||
'Should wait for transaction with intervalMs & timeoutMs and return BlockDetail', | ||
options: { | ||
timeoutMs: 10000, | ||
intervalMs: 1000 | ||
} | ||
} | ||
]; | ||
|
||
export { waitForBlockTestCases }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f222463
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Coverage
Summary