Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #812 from EOSIO/block_info_changes
Browse files Browse the repository at this point in the history
New chain_api endpoints and more efficient Tapos fields
  • Loading branch information
Brad Hart authored Nov 20, 2020
2 parents 2e51a04 + 6611d9c commit 3664985
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 15 deletions.
45 changes: 39 additions & 6 deletions src/eosjs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
import { JsonRpc } from './eosjs-jsonrpc';
import {
Abi,
BlockTaposInfo,
GetInfoResult,
PushTransactionArgs,
GetBlockHeaderStateResult,
GetBlockInfoResult,
GetBlockResult
} from './eosjs-rpc-interfaces';
import * as ser from './eosjs-serialize';
Expand Down Expand Up @@ -349,8 +351,7 @@ export class Api {
{ sign, requiredKeys, authorization = [] }: QueryConfig
): Promise<any> {
const info = await this.rpc.get_info();
// TODO: replace get_block; needs rodeos changes
const refBlock = await this.rpc.get_block(info.last_irreversible_block_num);
const refBlock = await this.tryRefBlockFromGetInfo(info);
const queryBuffer = new ser.SerialBuffer({ textEncoder: this.textEncoder, textDecoder: this.textDecoder });
ser.serializeQuery(queryBuffer, query);

Expand Down Expand Up @@ -439,13 +440,16 @@ export class Api {
if (!info) {
info = await this.rpc.get_info();
}
if (useLastIrreversible) {
const block = await this.tryRefBlockFromGetInfo(info);
return { ...ser.transactionHeader(block, expireSeconds), ...transaction };
}

const taposBlockNumber: number = useLastIrreversible
? info.last_irreversible_block_num : info.head_block_num - blocksBehind;
const taposBlockNumber: number = info.head_block_num - blocksBehind;

const refBlock: GetBlockHeaderStateResult | GetBlockResult =
taposBlockNumber <= info.last_irreversible_block_num
? await this.rpc.get_block(taposBlockNumber)
? await this.tryGetBlockInfo(taposBlockNumber)
: await this.tryGetBlockHeaderState(taposBlockNumber);

return { ...ser.transactionHeader(refBlock, expireSeconds), ...transaction };
Expand All @@ -461,7 +465,36 @@ export class Api {
try {
return await this.rpc.get_block_header_state(taposBlockNumber);
} catch (error) {
return await this.rpc.get_block(taposBlockNumber);
return await this.tryGetBlockInfo(taposBlockNumber);
}
}

private async tryGetBlockInfo(blockNumber: number): Promise<GetBlockInfoResult | GetBlockResult> {
try {
return await this.rpc.get_block_info(blockNumber);
} catch (error) {
return await this.rpc.get_block(blockNumber);
}
}

private async tryRefBlockFromGetInfo(info: GetInfoResult): Promise<BlockTaposInfo | GetBlockInfoResult | GetBlockResult> {
if (
info.hasOwnProperty('last_irreversible_block_id') &&
info.hasOwnProperty('last_irreversible_block_num') &&
info.hasOwnProperty('last_irreversible_block_time')
) {
return {
block_num: info.last_irreversible_block_num,
id: info.last_irreversible_block_id,
timestamp: info.last_irreversible_block_time,
};
} else {
const block = await this.tryGetBlockInfo(info.last_irreversible_block_num);
return {
block_num: block.block_num,
id: block.id,
timestamp: block.timestamp,
};
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/eosjs-jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { AbiProvider, AuthorityProvider, AuthorityProviderArgs, BinaryAbi } from
import { base64ToBinary, convertLegacyPublicKeys } from './eosjs-numeric';
import {
GetAbiResult,
GetBlockInfoResult,
GetBlockResult,
GetCodeResult,
GetInfoResult,
GetRawCodeAndAbiResult,
GetRawAbiResult,
PushTransactionArgs,
GetBlockHeaderStateResult
} from './eosjs-rpc-interfaces';
Expand Down Expand Up @@ -88,6 +90,11 @@ export class JsonRpc implements AuthorityProvider, AbiProvider {
return await this.fetch('/v1/chain/get_block_header_state', { block_num_or_id: blockNumOrId });
}

/** Raw call to `/v1/chain/get_block_info` */
public async get_block_info(blockNum: number): Promise<GetBlockInfoResult> {
return await this.fetch('/v1/chain/get_block_info', { block_num: blockNum });
}

/** Raw call to `/v1/chain/get_block` */
public async get_block(blockNumOrId: number | string): Promise<GetBlockResult> {
return await this.fetch('/v1/chain/get_block', { block_num_or_id: blockNumOrId });
Expand Down Expand Up @@ -132,11 +139,15 @@ export class JsonRpc implements AuthorityProvider, AbiProvider {
}

/** calls `/v1/chain/get_raw_code_and_abi` and pulls out unneeded raw wasm code */
// TODO: use `/v1/chain/get_raw_abi` directly when it becomes available
public async getRawAbi(accountName: string): Promise<BinaryAbi> {
const rawCodeAndAbi = await this.get_raw_code_and_abi(accountName);
const abi = base64ToBinary(rawCodeAndAbi.abi);
return { accountName: rawCodeAndAbi.account_name, abi };
const rawAbi = await this.get_raw_abi(accountName);
const abi = base64ToBinary(rawAbi.abi);
return { accountName: rawAbi.account_name, abi };
}

/** Raw call to `/v1/chain/get_raw_abi` */
public async get_raw_abi(accountName: string): Promise<GetRawAbiResult> {
return await this.fetch('/v1/chain/get_raw_abi', { account_name: accountName });
}

/** Raw call to `/v1/chain/get_scheduled_transactions` */
Expand Down
23 changes: 23 additions & 0 deletions src/eosjs-rpc-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ export interface GetAbiResult {
abi: Abi;
}

/** Return value of `/v1/chain/get_block_info` */
export interface GetBlockInfoResult {
timestamp: string;
producer: string;
confirmed: number;
previous: string;
transaction_mroot: string;
action_mroot: string;
schedule_version: number;
producer_signature: string;
id: string;
block_num: number;
ref_block_prefix: number;
}

/** Return value of `/v1/chain/get_block` */
export interface GetBlockResult {
timestamp: string;
Expand Down Expand Up @@ -101,6 +116,7 @@ export interface GetInfoResult {
head_block_num: number;
last_irreversible_block_num: number;
last_irreversible_block_id: string;
last_irreversible_block_time: string;
head_block_id: string;
head_block_time: string;
head_block_producer: string;
Expand All @@ -117,6 +133,13 @@ export interface GetRawCodeAndAbiResult {
abi: string;
}

export interface GetRawAbiResult {
account_name: string;
code_hash: string;
abi_hash: string;
abi: string;
}

/** Arguments for `push_transaction` */
export interface PushTransactionArgs {
signatures: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/tests/eosjs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('eosjs-api', () => {
const fetch = async (input: any, init: any): Promise<any> => ({
ok: true,
json: async () => {
if (input === '/v1/chain/get_raw_code_and_abi') {
if (input === '/v1/chain/get_raw_abi') {
return {
account_name: 'testeostoken',
abi: 'DmVvc2lvOjphYmkvMS4wAQxhY2NvdW50X25hbWUEbmFtZQUIdHJhbnNmZXIABARmcm9tDGFjY291bnRfbmFtZQJ0bwxhY2NvdW50X25hbWUIcXVhbnRpdHkFYXNzZXQEbWVtbwZzdHJpbmcGY3JlYXRlAAIGaXNzdWVyDGFjY291bnRfbmFtZQ5tYXhpbXVtX3N1cHBseQVhc3NldAVpc3N1ZQADAnRvDGFjY291bnRfbmFtZQhxdWFudGl0eQVhc3NldARtZW1vBnN0cmluZwdhY2NvdW50AAEHYmFsYW5jZQVhc3NldA5jdXJyZW5jeV9zdGF0cwADBnN1cHBseQVhc3NldAptYXhfc3VwcGx5BWFzc2V0Bmlzc3VlcgxhY2NvdW50X25hbWUDAAAAVy08zc0IdHJhbnNmZXLnBSMjIFRyYW5zZmVyIFRlcm1zICYgQ29uZGl0aW9ucwoKSSwge3tmcm9tfX0sIGNlcnRpZnkgdGhlIGZvbGxvd2luZyB0byBiZSB0cnVlIHRvIHRoZSBiZXN0IG9mIG15IGtub3dsZWRnZToKCjEuIEkgY2VydGlmeSB0aGF0IHt7cXVhbnRpdHl9fSBpcyBub3QgdGhlIHByb2NlZWRzIG9mIGZyYXVkdWxlbnQgb3IgdmlvbGVudCBhY3Rpdml0aWVzLgoyLiBJIGNlcnRpZnkgdGhhdCwgdG8gdGhlIGJlc3Qgb2YgbXkga25vd2xlZGdlLCB7e3RvfX0gaXMgbm90IHN1cHBvcnRpbmcgaW5pdGlhdGlvbiBvZiB2aW9sZW5jZSBhZ2FpbnN0IG90aGVycy4KMy4gSSBoYXZlIGRpc2Nsb3NlZCBhbnkgY29udHJhY3R1YWwgdGVybXMgJiBjb25kaXRpb25zIHdpdGggcmVzcGVjdCB0byB7e3F1YW50aXR5fX0gdG8ge3t0b319LgoKSSB1bmRlcnN0YW5kIHRoYXQgZnVuZHMgdHJhbnNmZXJzIGFyZSBub3QgcmV2ZXJzaWJsZSBhZnRlciB0aGUge3t0cmFuc2FjdGlvbi5kZWxheX19IHNlY29uZHMgb3Igb3RoZXIgZGVsYXkgYXMgY29uZmlndXJlZCBieSB7e2Zyb219fSdzIHBlcm1pc3Npb25zLgoKSWYgdGhpcyBhY3Rpb24gZmFpbHMgdG8gYmUgaXJyZXZlcnNpYmx5IGNvbmZpcm1lZCBhZnRlciByZWNlaXZpbmcgZ29vZHMgb3Igc2VydmljZXMgZnJvbSAne3t0b319JywgSSBhZ3JlZSB0byBlaXRoZXIgcmV0dXJuIHRoZSBnb29kcyBvciBzZXJ2aWNlcyBvciByZXNlbmQge3txdWFudGl0eX19IGluIGEgdGltZWx5IG1hbm5lci4KAAAAAAClMXYFaXNzdWUAAAAAAKhs1EUGY3JlYXRlAAIAAAA4T00RMgNpNjQBCGN1cnJlbmN5AQZ1aW50NjQHYWNjb3VudAAAAAAAkE3GA2k2NAEIY3VycmVuY3kBBnVpbnQ2NA5jdXJyZW5jeV9zdGF0cwAAAA===', // eslint-disable-line
Expand Down
19 changes: 19 additions & 0 deletions src/tests/eosjs-jsonrpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ describe('JSON RPC', () => {
expect(fetch).toBeCalledWith(endpoint + expPath, expParams);
});

it('calls get_block_info', async () => {
const expPath = '/v1/chain/get_block_info';
const blockNum = 1234;
const expReturn = { data: '12345' };
const expParams = {
body: JSON.stringify({
block_num: blockNum,
}),
method: 'POST',
};

fetchMock.once(JSON.stringify(expReturn));

const response = await jsonRpc.get_block_info(blockNum);

expect(response).toEqual(expReturn);
expect(fetch).toBeCalledWith(endpoint + expPath, expParams);
});

it('calls get_block', async () => {
const expPath = '/v1/chain/get_block';
const blockNumOrId = 1234;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const transactWithConfig = async (config, memo, from = 'bob', to = 'alice') => {

const transactWithoutConfig = async () => {
const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30}, 'transactWithoutConfig');
const blockInfo = await rpc.get_block(transactionResponse.processed.block_num - 3);
const blockInfo = await rpc.get_block_info(transactionResponse.processed.block_num - 3);
const currentDate = new Date();
const timePlusTen = currentDate.getTime() + 10000;
const timeInISOString = (new Date(timePlusTen)).toISOString();
Expand Down Expand Up @@ -196,7 +196,7 @@ const transactShouldFail = async () => await api.transact({
}]
});

const rpcShouldFail = async () => await rpc.get_block(-1);
const rpcShouldFail = async () => await rpc.get_block_info(-1);

module.exports = {
transactWithConfig,
Expand Down
4 changes: 2 additions & 2 deletions src/tests/web.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

const transactWithoutConfig = async () => {
const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30 }, 'transactWithoutConfig');
const blockInfo = await rpc.get_block(transactionResponse.processed.block_num - 3);
const blockInfo = await rpc.get_block_info(transactionResponse.processed.block_num - 3);
const currentDate = new Date();
const timePlusTen = currentDate.getTime() + 10000;
const timeInISOString = (new Date(timePlusTen)).toISOString();
Expand Down Expand Up @@ -509,7 +509,7 @@
return false;
}

const rpcShouldFail = async () => await rpc.get_block(-1);
const rpcShouldFail = async () => await rpc.get_block_info(-1);

const testRpcShouldFail = async (e) => {
resultsLabel = e.target;
Expand Down

0 comments on commit 3664985

Please sign in to comment.