Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing Blockchain Node APIs to ain-js #159

Merged
merged 4 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions __tests__/ain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,44 @@ 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('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();
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();
expect(lastBlock.number).not.toBeNull();
const proposer = await ain.getProposerByNumber(lastBlock.number);
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);
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);
});

// TODO(liayoo): add getTransactionResult method and test case for it.
Expand Down
51 changes: 36 additions & 15 deletions src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,48 @@ 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 information of the given validator address.
* @param {string} address The validator address.
* @returns {Promise<any>}
*/
getValidatorInfo(address: string): Promise<any> {
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<any>}
*/
getValidatorsByNumber(blockNumber: number): Promise<any> {
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<any>}
*/
getValidatorsByHash(blockHash: string): Promise<any> {
return this.provider.send('ain_getValidatorsByHash', { hash: blockHash });
}

/**
* Fetches the forger's address of a block with a block number.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question) what is forger?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes block proposer is called block forger or block producer. just replaced with 'block proposer' for readability. thanks!

* @param {number} blockNumber The block number.
* @returns {Promise<string>}
*/
getProposer(blockHashOrBlockNumber: string | number): Promise<string> {
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<string> {
return this.provider.send('ain_getProposerByNumber', { number: blockNumber });
}

/**
* 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<string[]>}
* Fetches the forger's address of a block with a block hash.
* @param {string} blockHash The block hash.
* @returns {Promise<string>}
*/
getValidators(blockHashOrBlockNumber: string | number): Promise<string[]> {
const byHash = typeof blockHashOrBlockNumber === 'string'
const rpcMethod = byHash ? 'ain_getValidatorsByHash' : 'ain_getValidatorsByNumber';
return this.provider.send(rpcMethod,
{[byHash ? 'hash' : 'number']: blockHashOrBlockNumber});
getProposerByHash(blockHash: string): Promise<string> {
return this.provider.send('ain_getProposerByHash', { hash: blockHash });
}

/**
Expand Down
Loading