From 250a214a73415ad484c4df09cd7dc0ee1b206091 Mon Sep 17 00:00:00 2001 From: platfowner Date: Mon, 4 Mar 2024 14:57:29 +0900 Subject: [PATCH 1/4] Split getProposer() to getProposerByNumber() and getProposerByHash() --- __tests__/ain.test.ts | 18 ++++++++++++++---- src/ain.ts | 20 +++++++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/__tests__/ain.test.ts b/__tests__/ain.test.ts index b7e7a0d..df56451 100644 --- a/__tests__/ain.test.ts +++ b/__tests__/ain.test.ts @@ -549,10 +549,20 @@ describe('ain-js', function() { expect(txCount).not.toBeNull(); }); - it('getProposer', async function () { - const proposer = await ain.getProposer(1); - const hash = (await ain.getBlockByNumber(1)).hash || ""; - expect(await ain.getProposer(hash)).toBe(proposer); + it('getProposerByNumber', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.number).not.toBeNull(); + const proposer = await ain.getProposerByNumber(lastBlock.number); + expect(proposer).toBe(lastBlock.proposer); + }); + + it('getProposerByHash', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.hash).not.toBeNull(); + const proposer = await ain.getProposerByHash(lastBlock.hash); + expect(proposer).toBe(lastBlock.proposer); }); it('getValidators', async function () { diff --git a/src/ain.ts b/src/ain.ts index 36f5bc8..86555bb 100755 --- a/src/ain.ts +++ b/src/ain.ts @@ -165,15 +165,21 @@ export default class Ain { } /** - * Fetches the forger's address of a block with a block hash or block number. - * @param {string | number} blockHashOrBlockNumber The block hash or block number. + * Fetches the forger's address of a block with a block number. + * @param {number} blockNumber The block number. * @returns {Promise} */ - getProposer(blockHashOrBlockNumber: string | number): Promise { - const byHash = typeof blockHashOrBlockNumber === 'string' - const rpcMethod = byHash ? 'ain_getProposerByHash' : 'ain_getProposerByNumber'; - return this.provider.send(rpcMethod, - {[byHash ? 'hash' : 'number']: blockHashOrBlockNumber}); + getProposerByNumber(blockNumber: number): Promise { + return this.provider.send('ain_getProposerByNumber', { number: blockNumber }); + } + + /** + * Fetches the forger's address of a block with a block hash. + * @param {string} blockHash The block hash. + * @returns {Promise} + */ + getProposerByHash(blockHash: string): Promise { + return this.provider.send('ain_getProposerByHash', { hash: blockHash }); } /** From b734f7a4d5687483c8d49fd5dbb027b1c4467bce Mon Sep 17 00:00:00 2001 From: platfowner Date: Mon, 4 Mar 2024 15:14:05 +0900 Subject: [PATCH 2/4] Split getValidators() to getValidatorsByNumber() and getValidatorsByHash() --- __tests__/ain.test.ts | 22 ++++++++++++++++------ src/ain.ts | 30 ++++++++++++++++++------------ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/__tests__/ain.test.ts b/__tests__/ain.test.ts index df56451..99b8ae6 100644 --- a/__tests__/ain.test.ts +++ b/__tests__/ain.test.ts @@ -549,6 +549,22 @@ describe('ain-js', function() { expect(txCount).not.toBeNull(); }); + it('getValidatorsByNumber', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.number).not.toBeNull(); + const validators = await ain.getValidatorsByNumber(lastBlock.number); + expect(validators).toStrictEqual(lastBlock.validators); + }); + + it('getValidatorsByHash', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.number).not.toBeNull(); + const validators = await ain.getValidatorsByHash(lastBlock.hash); + expect(validators).toStrictEqual(lastBlock.validators); + }); + it('getProposerByNumber', async function () { const lastBlock = await ain.getLastBlock(); expect(lastBlock).not.toBeNull(); @@ -565,12 +581,6 @@ describe('ain-js', function() { expect(proposer).toBe(lastBlock.proposer); }); - it('getValidators', async function () { - const validators = await ain.getValidators(4); - const hash = (await ain.getBlockByNumber(4)).hash || ""; - expect(await ain.getValidators(hash)).toStrictEqual(validators); - }); - // TODO(liayoo): add getTransactionResult method and test case for it. // it('getTransactionResult', async function() { // expect(await ain.getTransactionResult('0xabcdefghijklmnop')).toMatchSnapshot(); diff --git a/src/ain.ts b/src/ain.ts index 86555bb..1bff0ca 100755 --- a/src/ain.ts +++ b/src/ain.ts @@ -164,6 +164,24 @@ export default class Ain { return this.provider.send('ain_getBlockTransactionCountByHash', { hash }); } + /** + * Fetches the validator list of a block with a block number. + * @param {number} blockNumber The block number. + * @returns {Promise} + */ + getValidatorsByNumber(blockNumber: number): Promise { + return this.provider.send('ain_getValidatorsByNumber', { number: blockNumber }); + } + + /** + * Fetches the validator list of a block with a block hash. + * @param {string} blockHash The block hash. + * @returns {Promise} + */ + getValidatorsByHash(blockHash: string): Promise { + return this.provider.send('ain_getValidatorsByHash', { hash: blockHash }); + } + /** * Fetches the forger's address of a block with a block number. * @param {number} blockNumber The block number. @@ -182,18 +200,6 @@ export default class Ain { return this.provider.send('ain_getProposerByHash', { hash: blockHash }); } - /** - * Fetches the validator list of a block with a block hash or block number. - * @param {string | number} blockHashOrBlockNumber The block hash or block number. - * @returns {Promise} - */ - getValidators(blockHashOrBlockNumber: string | number): Promise { - const byHash = typeof blockHashOrBlockNumber === 'string' - const rpcMethod = byHash ? 'ain_getValidatorsByHash' : 'ain_getValidatorsByNumber'; - return this.provider.send(rpcMethod, - {[byHash ? 'hash' : 'number']: blockHashOrBlockNumber}); - } - /** * Fetches pending transaction. * @returns {Promise} From bad198a9befff864978df543817dd9e395d8814f Mon Sep 17 00:00:00 2001 From: platfowner Date: Mon, 4 Mar 2024 15:37:29 +0900 Subject: [PATCH 3/4] Add getValidatorInfo() method --- __tests__/ain.test.ts | 8 ++++++++ src/ain.ts | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/__tests__/ain.test.ts b/__tests__/ain.test.ts index 99b8ae6..f586760 100644 --- a/__tests__/ain.test.ts +++ b/__tests__/ain.test.ts @@ -549,6 +549,14 @@ describe('ain-js', function() { expect(txCount).not.toBeNull(); }); + it('getValidatorInfo', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.proposer).not.toBeNull(); + const info = await ain.getValidatorInfo(lastBlock.proposer); + expect(info).not.toBeNull(); + }); + it('getValidatorsByNumber', async function () { const lastBlock = await ain.getLastBlock(); expect(lastBlock).not.toBeNull(); diff --git a/src/ain.ts b/src/ain.ts index 1bff0ca..a32a950 100755 --- a/src/ain.ts +++ b/src/ain.ts @@ -164,21 +164,30 @@ export default class Ain { return this.provider.send('ain_getBlockTransactionCountByHash', { hash }); } + /** + * Fetches the information of the given validator address. + * @param {string} address The validator address. + * @returns {Promise} + */ + getValidatorInfo(address: string): Promise { + return this.provider.send('ain_getValidatorInfo', { address }); + } + /** * Fetches the validator list of a block with a block number. * @param {number} blockNumber The block number. - * @returns {Promise} + * @returns {Promise} */ - getValidatorsByNumber(blockNumber: number): Promise { + getValidatorsByNumber(blockNumber: number): Promise { return this.provider.send('ain_getValidatorsByNumber', { number: blockNumber }); } /** * Fetches the validator list of a block with a block hash. * @param {string} blockHash The block hash. - * @returns {Promise} + * @returns {Promise} */ - getValidatorsByHash(blockHash: string): Promise { + getValidatorsByHash(blockHash: string): Promise { return this.provider.send('ain_getValidatorsByHash', { hash: blockHash }); } From c1fd6a958e9fbb44d935b3e8fabad27d16e87d9d Mon Sep 17 00:00:00 2001 From: platfowner Date: Mon, 4 Mar 2024 16:13:02 +0900 Subject: [PATCH 4/4] Tweak comments --- src/ain.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ain.ts b/src/ain.ts index a32a950..49c4411 100755 --- a/src/ain.ts +++ b/src/ain.ts @@ -192,7 +192,7 @@ export default class Ain { } /** - * Fetches the forger's address of a block with a block number. + * Fetches the block proproser's address of a block with a block number. * @param {number} blockNumber The block number. * @returns {Promise} */ @@ -201,7 +201,7 @@ export default class Ain { } /** - * Fetches the forger's address of a block with a block hash. + * Fetches the block proproser's address of a block with a block hash. * @param {string} blockHash The block hash. * @returns {Promise} */