-
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.
Implement rpc method eth_syncing (#531)
* feat: add eth_syncing RPC method * fix: add mock tests for eth_syncing * fix: update comment for RPC methods to implement * fix: add test for eth_syncing * fix: add test for eth_syncing * fix: proper type for eth_syncing.ts --------- Co-authored-by: rodolfopietro97 <[email protected]>
- Loading branch information
1 parent
2a22a5c
commit 716bb75
Showing
6 changed files
with
170 additions
and
59 deletions.
There are no files selected for viewing
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
71 changes: 52 additions & 19 deletions
71
packages/provider/src/utils/rpc-mapper/methods-map/methods/eth_syncing.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 |
---|---|---|
@@ -1,32 +1,65 @@ | ||
import { type ThorClient } from '@vechain/vechain-sdk-network'; | ||
import { buildError, FUNCTION } from '@vechain/vechain-sdk-errors'; | ||
import { | ||
blocksFormatter, | ||
RPC_METHODS, | ||
RPCMethodsMap, | ||
type SyncBlockRPC | ||
} from '../../../../provider'; | ||
import { JSONRPC, buildProviderError } from '@vechain/vechain-sdk-errors'; | ||
|
||
/** | ||
* RPC Method eth_syncing implementation | ||
* | ||
* @link [eth_syncing](https://docs.infura.io/networks/ethereum/json-rpc-methods/eth_syncing) | ||
* | ||
* @param thorClient - The thor client instance to use. | ||
* @param params - The standard array of rpc call parameters. | ||
* @note: | ||
* * params[0]: ... | ||
* * params[1]: ... | ||
* * params[n]: ... | ||
* | ||
* @returns Returns an object with the sync status of the node if the node is out-of-sync and is syncing. Returns false when the node is already in sync. | ||
*/ | ||
const ethSyncing = async ( | ||
thorClient: ThorClient, | ||
params: unknown[] | ||
): Promise<void> => { | ||
// To avoid eslint error | ||
await Promise.resolve(0); | ||
thorClient: ThorClient | ||
): Promise<boolean | SyncBlockRPC> => { | ||
try { | ||
const bestBlock = await thorClient.blocks.getBestBlock(); | ||
const genesisBlock = await thorClient.blocks.getGenesisBlock(); | ||
|
||
// Not implemented yet | ||
throw buildError( | ||
FUNCTION.NOT_IMPLEMENTED, | ||
'Method "eth_syncing" not not implemented yet', | ||
{ | ||
params, | ||
thorClient | ||
// Check if the node is already in sync | ||
if ( | ||
bestBlock != null && | ||
Math.floor(Date.now() / 1000) - bestBlock.timestamp < 11000 | ||
) { | ||
return false; | ||
} | ||
); | ||
|
||
// Calculate the chainId | ||
const chainId = (await RPCMethodsMap(thorClient)[ | ||
RPC_METHODS.eth_chainId | ||
]([])) as string; | ||
|
||
const highestBlock = | ||
genesisBlock != null | ||
? Math.floor((Date.now() - genesisBlock.timestamp) / 10000) | ||
: null; | ||
|
||
return { | ||
startingBlock: null, | ||
currentBlock: | ||
bestBlock != null | ||
? blocksFormatter.formatToRPCStandard(bestBlock, chainId) | ||
: null, | ||
highestBlock: | ||
highestBlock != null ? highestBlock.toString(16) : null | ||
}; | ||
} catch (e) { | ||
throw buildProviderError( | ||
JSONRPC.INTERNAL_ERROR, | ||
`Method 'eth_syncing' failed: Error while getting last block\n | ||
URL: ${thorClient.httpClient.baseURL}`, | ||
{ | ||
innerError: JSON.stringify(e) | ||
} | ||
); | ||
} | ||
}; | ||
|
||
export { ethSyncing }; |
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
95 changes: 95 additions & 0 deletions
95
packages/provider/tests/rpc-mapper/methods/eth_syncing/eth_syncing.mock.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,95 @@ | ||
import { beforeEach, describe, expect, jest, test } from '@jest/globals'; | ||
import { ProviderRpcError } from '@vechain/vechain-sdk-errors'; | ||
import { RPC_METHODS, RPCMethodsMap } from '../../../../src'; | ||
import { ThorClient } from '@vechain/vechain-sdk-network'; | ||
import { soloNetwork } from '../../../fixture'; | ||
|
||
/** | ||
* RPC Mapper integration tests for 'eth_syncing' method with Solo Network and mocked functionality | ||
* | ||
* @group integration/rpc-mapper/methods/eth_syncing | ||
*/ | ||
describe('RPC Mapper - eth_syncing method tests', () => { | ||
/** | ||
* Thor client instance | ||
*/ | ||
let thorClient: ThorClient; | ||
|
||
/** | ||
* Init thor client before each test | ||
*/ | ||
beforeEach(() => { | ||
// Init thor client | ||
thorClient = new ThorClient(soloNetwork); | ||
}); | ||
|
||
/** | ||
* eth_syncing RPC call tests - Negative cases | ||
*/ | ||
describe('eth_syncing - Negative cases', () => { | ||
/** | ||
* Test case that mocks an error thrown by the getBestBlock method | ||
*/ | ||
test('Should throw `ProviderRpcError` if an error occurs while retrieving the best block', async () => { | ||
// Mock the getGenesisBlock method to return null | ||
jest.spyOn(thorClient.blocks, 'getBestBlock').mockRejectedValue( | ||
new Error() | ||
); | ||
|
||
await expect( | ||
RPCMethodsMap(thorClient)[RPC_METHODS.eth_syncing]([]) | ||
).rejects.toThrowError(ProviderRpcError); | ||
}); | ||
|
||
/** | ||
* Test case that mocks an error thrown by the getGenesisBlock method | ||
*/ | ||
test('Should throw `ProviderRpcError` if an error occurs while retrieving the genesis block', async () => { | ||
// Mock the getGenesisBlock method to return null | ||
jest.spyOn(thorClient.blocks, 'getGenesisBlock').mockRejectedValue( | ||
new Error() | ||
); | ||
|
||
await expect( | ||
RPCMethodsMap(thorClient)[RPC_METHODS.eth_syncing]([]) | ||
).rejects.toThrowError(ProviderRpcError); | ||
}); | ||
|
||
/** | ||
* Test case where the best block is not defined | ||
*/ | ||
test('Should return an object with the sync status of the node if the node is out-of-sync', async () => { | ||
// Mock the getBestBlock method to return null | ||
jest.spyOn(thorClient.blocks, 'getBestBlock').mockResolvedValue( | ||
null | ||
); | ||
|
||
const status = (await RPCMethodsMap(thorClient)[ | ||
RPC_METHODS.eth_syncing | ||
]([])) as string; | ||
|
||
expect(status).not.toBe(false); | ||
}); | ||
|
||
/** | ||
* Test case where the best block and genesis block are not defined | ||
*/ | ||
test('Should return an object with the sync status of the node if the node is out-of-sync', async () => { | ||
// Mock the getBestBlock method to return null | ||
jest.spyOn(thorClient.blocks, 'getBestBlock').mockResolvedValue( | ||
null | ||
); | ||
|
||
// Mock the getGenesisBlock method to return null | ||
jest.spyOn(thorClient.blocks, 'getGenesisBlock').mockResolvedValue( | ||
null | ||
); | ||
|
||
const status = (await RPCMethodsMap(thorClient)[ | ||
RPC_METHODS.eth_syncing | ||
]([])) as string; | ||
|
||
expect(status).not.toBe(false); | ||
}); | ||
}); | ||
}); |
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
716bb75
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