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

Remove unnecessary call to get_block_header_state #703

Merged
merged 2 commits into from
Apr 6, 2020
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
26 changes: 14 additions & 12 deletions src/eosjs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,13 @@ export class Api {
info = await this.rpc.get_info();
}

let taposBlockNumber: number;
if (useLastIrreversible) {
taposBlockNumber = info.last_irreversible_block_num;
} else {
taposBlockNumber = info.head_block_num - blocksBehind;
}
const taposBlockNumber: number = useLastIrreversible
? info.last_irreversible_block_num : info.head_block_num - blocksBehind;

let refBlock: GetBlockHeaderStateResult | GetBlockResult;
try {
refBlock = await this.rpc.get_block_header_state(taposBlockNumber);
} catch (error) {
refBlock = await this.rpc.get_block(taposBlockNumber);
}
const refBlock: GetBlockHeaderStateResult | GetBlockResult =
taposBlockNumber <= info.last_irreversible_block_num
? await this.rpc.get_block(taposBlockNumber)
: await this.tryGetBlockHeaderState(taposBlockNumber);

return { ...ser.transactionHeader(refBlock, expireSeconds), ...transaction };
}
Expand All @@ -384,4 +378,12 @@ export class Api {
return !!(expiration && typeof(ref_block_num) === 'number' && typeof(ref_block_prefix) === 'number');
}

private async tryGetBlockHeaderState(taposBlockNumber: number):
Promise<GetBlockHeaderStateResult | GetBlockResult> {
try {
return await this.rpc.get_block_header_state(taposBlockNumber);
} catch (error) {
return await this.rpc.get_block(taposBlockNumber);
}
}
} // Api
Loading