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

Commit 3664985

Browse files
author
Brad Hart
authored
Merge pull request #812 from EOSIO/block_info_changes
New chain_api endpoints and more efficient Tapos fields
2 parents 2e51a04 + 6611d9c commit 3664985

File tree

7 files changed

+101
-15
lines changed

7 files changed

+101
-15
lines changed

src/eosjs-api.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import {
2323
import { JsonRpc } from './eosjs-jsonrpc';
2424
import {
2525
Abi,
26+
BlockTaposInfo,
2627
GetInfoResult,
2728
PushTransactionArgs,
2829
GetBlockHeaderStateResult,
30+
GetBlockInfoResult,
2931
GetBlockResult
3032
} from './eosjs-rpc-interfaces';
3133
import * as ser from './eosjs-serialize';
@@ -349,8 +351,7 @@ export class Api {
349351
{ sign, requiredKeys, authorization = [] }: QueryConfig
350352
): Promise<any> {
351353
const info = await this.rpc.get_info();
352-
// TODO: replace get_block; needs rodeos changes
353-
const refBlock = await this.rpc.get_block(info.last_irreversible_block_num);
354+
const refBlock = await this.tryRefBlockFromGetInfo(info);
354355
const queryBuffer = new ser.SerialBuffer({ textEncoder: this.textEncoder, textDecoder: this.textDecoder });
355356
ser.serializeQuery(queryBuffer, query);
356357

@@ -439,13 +440,16 @@ export class Api {
439440
if (!info) {
440441
info = await this.rpc.get_info();
441442
}
443+
if (useLastIrreversible) {
444+
const block = await this.tryRefBlockFromGetInfo(info);
445+
return { ...ser.transactionHeader(block, expireSeconds), ...transaction };
446+
}
442447

443-
const taposBlockNumber: number = useLastIrreversible
444-
? info.last_irreversible_block_num : info.head_block_num - blocksBehind;
448+
const taposBlockNumber: number = info.head_block_num - blocksBehind;
445449

446450
const refBlock: GetBlockHeaderStateResult | GetBlockResult =
447451
taposBlockNumber <= info.last_irreversible_block_num
448-
? await this.rpc.get_block(taposBlockNumber)
452+
? await this.tryGetBlockInfo(taposBlockNumber)
449453
: await this.tryGetBlockHeaderState(taposBlockNumber);
450454

451455
return { ...ser.transactionHeader(refBlock, expireSeconds), ...transaction };
@@ -461,7 +465,36 @@ export class Api {
461465
try {
462466
return await this.rpc.get_block_header_state(taposBlockNumber);
463467
} catch (error) {
464-
return await this.rpc.get_block(taposBlockNumber);
468+
return await this.tryGetBlockInfo(taposBlockNumber);
469+
}
470+
}
471+
472+
private async tryGetBlockInfo(blockNumber: number): Promise<GetBlockInfoResult | GetBlockResult> {
473+
try {
474+
return await this.rpc.get_block_info(blockNumber);
475+
} catch (error) {
476+
return await this.rpc.get_block(blockNumber);
477+
}
478+
}
479+
480+
private async tryRefBlockFromGetInfo(info: GetInfoResult): Promise<BlockTaposInfo | GetBlockInfoResult | GetBlockResult> {
481+
if (
482+
info.hasOwnProperty('last_irreversible_block_id') &&
483+
info.hasOwnProperty('last_irreversible_block_num') &&
484+
info.hasOwnProperty('last_irreversible_block_time')
485+
) {
486+
return {
487+
block_num: info.last_irreversible_block_num,
488+
id: info.last_irreversible_block_id,
489+
timestamp: info.last_irreversible_block_time,
490+
};
491+
} else {
492+
const block = await this.tryGetBlockInfo(info.last_irreversible_block_num);
493+
return {
494+
block_num: block.block_num,
495+
id: block.id,
496+
timestamp: block.timestamp,
497+
};
465498
}
466499
}
467500

src/eosjs-jsonrpc.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { AbiProvider, AuthorityProvider, AuthorityProviderArgs, BinaryAbi } from
77
import { base64ToBinary, convertLegacyPublicKeys } from './eosjs-numeric';
88
import {
99
GetAbiResult,
10+
GetBlockInfoResult,
1011
GetBlockResult,
1112
GetCodeResult,
1213
GetInfoResult,
1314
GetRawCodeAndAbiResult,
15+
GetRawAbiResult,
1416
PushTransactionArgs,
1517
GetBlockHeaderStateResult
1618
} from './eosjs-rpc-interfaces';
@@ -88,6 +90,11 @@ export class JsonRpc implements AuthorityProvider, AbiProvider {
8890
return await this.fetch('/v1/chain/get_block_header_state', { block_num_or_id: blockNumOrId });
8991
}
9092

93+
/** Raw call to `/v1/chain/get_block_info` */
94+
public async get_block_info(blockNum: number): Promise<GetBlockInfoResult> {
95+
return await this.fetch('/v1/chain/get_block_info', { block_num: blockNum });
96+
}
97+
9198
/** Raw call to `/v1/chain/get_block` */
9299
public async get_block(blockNumOrId: number | string): Promise<GetBlockResult> {
93100
return await this.fetch('/v1/chain/get_block', { block_num_or_id: blockNumOrId });
@@ -132,11 +139,15 @@ export class JsonRpc implements AuthorityProvider, AbiProvider {
132139
}
133140

134141
/** calls `/v1/chain/get_raw_code_and_abi` and pulls out unneeded raw wasm code */
135-
// TODO: use `/v1/chain/get_raw_abi` directly when it becomes available
136142
public async getRawAbi(accountName: string): Promise<BinaryAbi> {
137-
const rawCodeAndAbi = await this.get_raw_code_and_abi(accountName);
138-
const abi = base64ToBinary(rawCodeAndAbi.abi);
139-
return { accountName: rawCodeAndAbi.account_name, abi };
143+
const rawAbi = await this.get_raw_abi(accountName);
144+
const abi = base64ToBinary(rawAbi.abi);
145+
return { accountName: rawAbi.account_name, abi };
146+
}
147+
148+
/** Raw call to `/v1/chain/get_raw_abi` */
149+
public async get_raw_abi(accountName: string): Promise<GetRawAbiResult> {
150+
return await this.fetch('/v1/chain/get_raw_abi', { account_name: accountName });
140151
}
141152

142153
/** Raw call to `/v1/chain/get_scheduled_transactions` */

src/eosjs-rpc-interfaces.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ export interface GetAbiResult {
3838
abi: Abi;
3939
}
4040

41+
/** Return value of `/v1/chain/get_block_info` */
42+
export interface GetBlockInfoResult {
43+
timestamp: string;
44+
producer: string;
45+
confirmed: number;
46+
previous: string;
47+
transaction_mroot: string;
48+
action_mroot: string;
49+
schedule_version: number;
50+
producer_signature: string;
51+
id: string;
52+
block_num: number;
53+
ref_block_prefix: number;
54+
}
55+
4156
/** Return value of `/v1/chain/get_block` */
4257
export interface GetBlockResult {
4358
timestamp: string;
@@ -101,6 +116,7 @@ export interface GetInfoResult {
101116
head_block_num: number;
102117
last_irreversible_block_num: number;
103118
last_irreversible_block_id: string;
119+
last_irreversible_block_time: string;
104120
head_block_id: string;
105121
head_block_time: string;
106122
head_block_producer: string;
@@ -117,6 +133,13 @@ export interface GetRawCodeAndAbiResult {
117133
abi: string;
118134
}
119135

136+
export interface GetRawAbiResult {
137+
account_name: string;
138+
code_hash: string;
139+
abi_hash: string;
140+
abi: string;
141+
}
142+
120143
/** Arguments for `push_transaction` */
121144
export interface PushTransactionArgs {
122145
signatures: string[];

src/tests/eosjs-api.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('eosjs-api', () => {
135135
const fetch = async (input: any, init: any): Promise<any> => ({
136136
ok: true,
137137
json: async () => {
138-
if (input === '/v1/chain/get_raw_code_and_abi') {
138+
if (input === '/v1/chain/get_raw_abi') {
139139
return {
140140
account_name: 'testeostoken',
141141
abi: 'DmVvc2lvOjphYmkvMS4wAQxhY2NvdW50X25hbWUEbmFtZQUIdHJhbnNmZXIABARmcm9tDGFjY291bnRfbmFtZQJ0bwxhY2NvdW50X25hbWUIcXVhbnRpdHkFYXNzZXQEbWVtbwZzdHJpbmcGY3JlYXRlAAIGaXNzdWVyDGFjY291bnRfbmFtZQ5tYXhpbXVtX3N1cHBseQVhc3NldAVpc3N1ZQADAnRvDGFjY291bnRfbmFtZQhxdWFudGl0eQVhc3NldARtZW1vBnN0cmluZwdhY2NvdW50AAEHYmFsYW5jZQVhc3NldA5jdXJyZW5jeV9zdGF0cwADBnN1cHBseQVhc3NldAptYXhfc3VwcGx5BWFzc2V0Bmlzc3VlcgxhY2NvdW50X25hbWUDAAAAVy08zc0IdHJhbnNmZXLnBSMjIFRyYW5zZmVyIFRlcm1zICYgQ29uZGl0aW9ucwoKSSwge3tmcm9tfX0sIGNlcnRpZnkgdGhlIGZvbGxvd2luZyB0byBiZSB0cnVlIHRvIHRoZSBiZXN0IG9mIG15IGtub3dsZWRnZToKCjEuIEkgY2VydGlmeSB0aGF0IHt7cXVhbnRpdHl9fSBpcyBub3QgdGhlIHByb2NlZWRzIG9mIGZyYXVkdWxlbnQgb3IgdmlvbGVudCBhY3Rpdml0aWVzLgoyLiBJIGNlcnRpZnkgdGhhdCwgdG8gdGhlIGJlc3Qgb2YgbXkga25vd2xlZGdlLCB7e3RvfX0gaXMgbm90IHN1cHBvcnRpbmcgaW5pdGlhdGlvbiBvZiB2aW9sZW5jZSBhZ2FpbnN0IG90aGVycy4KMy4gSSBoYXZlIGRpc2Nsb3NlZCBhbnkgY29udHJhY3R1YWwgdGVybXMgJiBjb25kaXRpb25zIHdpdGggcmVzcGVjdCB0byB7e3F1YW50aXR5fX0gdG8ge3t0b319LgoKSSB1bmRlcnN0YW5kIHRoYXQgZnVuZHMgdHJhbnNmZXJzIGFyZSBub3QgcmV2ZXJzaWJsZSBhZnRlciB0aGUge3t0cmFuc2FjdGlvbi5kZWxheX19IHNlY29uZHMgb3Igb3RoZXIgZGVsYXkgYXMgY29uZmlndXJlZCBieSB7e2Zyb219fSdzIHBlcm1pc3Npb25zLgoKSWYgdGhpcyBhY3Rpb24gZmFpbHMgdG8gYmUgaXJyZXZlcnNpYmx5IGNvbmZpcm1lZCBhZnRlciByZWNlaXZpbmcgZ29vZHMgb3Igc2VydmljZXMgZnJvbSAne3t0b319JywgSSBhZ3JlZSB0byBlaXRoZXIgcmV0dXJuIHRoZSBnb29kcyBvciBzZXJ2aWNlcyBvciByZXNlbmQge3txdWFudGl0eX19IGluIGEgdGltZWx5IG1hbm5lci4KAAAAAAClMXYFaXNzdWUAAAAAAKhs1EUGY3JlYXRlAAIAAAA4T00RMgNpNjQBCGN1cnJlbmN5AQZ1aW50NjQHYWNjb3VudAAAAAAAkE3GA2k2NAEIY3VycmVuY3kBBnVpbnQ2NA5jdXJyZW5jeV9zdGF0cwAAAA===', // eslint-disable-line

src/tests/eosjs-jsonrpc.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ describe('JSON RPC', () => {
140140
expect(fetch).toBeCalledWith(endpoint + expPath, expParams);
141141
});
142142

143+
it('calls get_block_info', async () => {
144+
const expPath = '/v1/chain/get_block_info';
145+
const blockNum = 1234;
146+
const expReturn = { data: '12345' };
147+
const expParams = {
148+
body: JSON.stringify({
149+
block_num: blockNum,
150+
}),
151+
method: 'POST',
152+
};
153+
154+
fetchMock.once(JSON.stringify(expReturn));
155+
156+
const response = await jsonRpc.get_block_info(blockNum);
157+
158+
expect(response).toEqual(expReturn);
159+
expect(fetch).toBeCalledWith(endpoint + expPath, expParams);
160+
});
161+
143162
it('calls get_block', async () => {
144163
const expPath = '/v1/chain/get_block';
145164
const blockNumOrId = 1234;

src/tests/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const transactWithConfig = async (config, memo, from = 'bob', to = 'alice') => {
4040

4141
const transactWithoutConfig = async () => {
4242
const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30}, 'transactWithoutConfig');
43-
const blockInfo = await rpc.get_block(transactionResponse.processed.block_num - 3);
43+
const blockInfo = await rpc.get_block_info(transactionResponse.processed.block_num - 3);
4444
const currentDate = new Date();
4545
const timePlusTen = currentDate.getTime() + 10000;
4646
const timeInISOString = (new Date(timePlusTen)).toISOString();
@@ -196,7 +196,7 @@ const transactShouldFail = async () => await api.transact({
196196
}]
197197
});
198198

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

201201
module.exports = {
202202
transactWithConfig,

src/tests/web.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
const transactWithoutConfig = async () => {
9292
const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30 }, 'transactWithoutConfig');
93-
const blockInfo = await rpc.get_block(transactionResponse.processed.block_num - 3);
93+
const blockInfo = await rpc.get_block_info(transactionResponse.processed.block_num - 3);
9494
const currentDate = new Date();
9595
const timePlusTen = currentDate.getTime() + 10000;
9696
const timeInISOString = (new Date(timePlusTen)).toISOString();
@@ -509,7 +509,7 @@
509509
return false;
510510
}
511511

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

514514
const testRpcShouldFail = async (e) => {
515515
resultsLabel = e.target;

0 commit comments

Comments
 (0)