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

replace get_block with get_block_header_state #582

Merged
merged 2 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/eosjs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class Api {
if (!info) {
info = await this.rpc.get_info();
}
const refBlock = await this.rpc.get_block(info.head_block_num - blocksBehind);
const refBlock = await this.rpc.get_block_header_state(info.head_block_num - blocksBehind);
transaction = { ...ser.transactionHeader(refBlock, expireSeconds), ...transaction };
}

Expand Down
4 changes: 2 additions & 2 deletions src/eosjs-jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { AbiProvider, AuthorityProvider, AuthorityProviderArgs, BinaryAbi } from './eosjs-api-interfaces';
import { base64ToBinary, convertLegacyPublicKeys } from './eosjs-numeric';
import { GetAbiResult, GetBlockResult, GetCodeResult, GetInfoResult, GetRawCodeAndAbiResult, PushTransactionArgs } from "./eosjs-rpc-interfaces" // tslint:disable-line
import { GetAbiResult, GetBlockResult, GetCodeResult, GetInfoResult, GetRawCodeAndAbiResult, PushTransactionArgs, GetBlockHeaderStateResult } from "./eosjs-rpc-interfaces" // tslint:disable-line
import { RpcError } from './eosjs-rpcerror';

function arrayToHex(data: Uint8Array) {
Expand Down Expand Up @@ -73,7 +73,7 @@ export class JsonRpc implements AuthorityProvider, AbiProvider {
}

/** Raw call to `/v1/chain/get_block_header_state` */
public async get_block_header_state(blockNumOrId: number | string): Promise<any> {
public async get_block_header_state(blockNumOrId: number | string): Promise<GetBlockHeaderStateResult> {
return await this.fetch('/v1/chain/get_block_header_state', { block_num_or_id: blockNumOrId });
}

Expand Down
55 changes: 48 additions & 7 deletions src/eosjs-rpc-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@ export interface Abi {
variants?: Array<{ name: string, types: string[] }>;
}

export interface BlockHeader {
timestamp: string;
producer: string;
confirmed: number;
previous: string;
transaction_mroot: string;
action_mroot: string;
schedule_version: number;
new_producers: any;
header_extensions: any;
}

export interface SignedBlockHeader extends BlockHeader {
producer_signature: string;
}

/** Return value of `/v1/chain/get_abi` */
export interface GetAbiResult {
account_name: string;
abi: Abi;
}

/** Subset of `GetBlockResult` needed to calculate TAPoS fields in transactions */
export interface BlockTaposInfo {
timestamp: string;
block_num: number;
ref_block_prefix: number;
}

/** Return value of `/v1/chain/get_block` */
export interface GetBlockResult {
timestamp: string;
Expand All @@ -41,6 +50,38 @@ export interface GetBlockResult {
ref_block_prefix: number;
}

/** Used to calculate TAPoS fields in transactions */
export interface BlockTaposInfo {
block_num: number;
id: string;
timestamp?: string;
header?: BlockHeader;
}

/** Return value of `v1/chain/get_block_header_state */
export interface GetBlockHeaderStateResult {
id: string;
header: SignedBlockHeader;
pending_schedule: any;
activated_protocol_features: any;
block_num: number;
dpos_proposed_irreversible_blocknum: number;
dpos_irreversible_blocknum: number;
active_schedule: any;
blockroot_merkle: any;
producer_to_last_produced: any;
producer_to_last_implied_irb: any;
block_signing_key: string;
confirm_count: any;
}

/** Subset of `GetBlockHeaderStateResult` used to calculate TAPoS fields in transactions */
export interface BlockHeaderStateTaposInfo {
block_num: number;
id: string;
header: SignedBlockHeader;
}

/** Return value of `/v1/chain/get_code` */
export interface GetCodeResult {
account_name: string;
Expand Down
15 changes: 11 additions & 4 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// copyright defined in eosjs/LICENSE.txt

import * as numeric from './eosjs-numeric';
import { Abi, BlockTaposInfo } from './eosjs-rpc-interfaces';
import { Abi, BlockTaposInfo, BlockHeaderStateTaposInfo } from './eosjs-rpc-interfaces';

/** A field in an abi */
export interface Field {
Expand Down Expand Up @@ -1073,12 +1073,19 @@ export function getTypesFromAbi(initialTypes: Map<string, Type>, abi: Abi) {
return types;
} // getTypesFromAbi

/** TAPoS: Return transaction fields which reference `refBlock` and expire `expireSeconds` after `refBlock.timestamp` */
function reverseHex(h: string) {
return h.substr(6, 2) + h.substr(4, 2) + h.substr(2, 2) + h.substr(0, 2);
}

/** TAPoS: Return transaction fields which reference `refBlock` and expire `expireSeconds` after `timestamp` */
export function transactionHeader(refBlock: BlockTaposInfo, expireSeconds: number) {
const timestamp = refBlock.header ? refBlock.header.timestamp : refBlock.timestamp;
const prefix = parseInt(reverseHex(refBlock.id.substr(16, 8)), 16);

return {
expiration: timePointSecToDate(dateToTimePointSec(refBlock.timestamp) + expireSeconds),
expiration: timePointSecToDate(dateToTimePointSec(timestamp) + expireSeconds),
ref_block_num: refBlock.block_num & 0xffff,
ref_block_prefix: refBlock.ref_block_prefix,
ref_block_prefix: prefix,
};
}

Expand Down