Skip to content

Commit

Permalink
fix(tendermint-rpc): add earliest block to sync info
Browse files Browse the repository at this point in the history
* fix issue #1448
  • Loading branch information
livthomas committed Jun 30, 2023
1 parent 21d150e commit c813712
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,12 @@ function decodeNodeInfo(data: RpcNodeInfo): responses.NodeInfo {
}

interface RpcSyncInfo {
/** hex encoded */
readonly earliest_app_hash: string;
/** hex encoded */
readonly earliest_block_hash: string;
readonly earliest_block_height: string;
readonly earliest_block_time: string;
/** hex encoded */
readonly latest_block_hash: string;
/** hex encoded */
Expand All @@ -629,7 +635,18 @@ interface RpcSyncInfo {
}

function decodeSyncInfo(data: RpcSyncInfo): responses.SyncInfo {
return {
const earliestBlockHeight = data.earliest_block_height
? apiToSmallInt(data.earliest_block_height)
: undefined;
const earliestBlockTime = data.earliest_block_time
? fromRfc3339WithNanoseconds(data.earliest_block_time)
: undefined;

return {
earliestAppHash: data.earliest_app_hash ? fromHex(data.earliest_app_hash) : undefined,
earliestBlockHash: data.earliest_block_hash ? fromHex(data.earliest_block_hash) : undefined,
earliestBlockHeight: earliestBlockHeight || undefined,
earliestBlockTime: earliestBlockTime?.getTime() ? earliestBlockTime : undefined,
latestBlockHash: fromHex(assertNotEmpty(data.latest_block_hash)),
latestAppHash: fromHex(assertNotEmpty(data.latest_app_hash)),
latestBlockTime: fromRfc3339WithNanoseconds(assertNotEmpty(data.latest_block_time)),
Expand Down
4 changes: 4 additions & 0 deletions packages/tendermint-rpc/src/tendermint34/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ export interface NodeInfo {
}

export interface SyncInfo {
readonly earliestAppHash?: Uint8Array;
readonly earliestBlockHash?: Uint8Array;
readonly earliestBlockHeight?: number;
readonly earliestBlockTime?: ReadonlyDate;
readonly latestBlockHash: Uint8Array;
readonly latestAppHash: Uint8Array;
readonly latestBlockHeight: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
// sync info
expect(status.syncInfo.catchingUp).toEqual(false);
expect(status.syncInfo.latestBlockHeight).toBeGreaterThanOrEqual(1);
expect(status.syncInfo.latestBlockTime).toBeInstanceOf(Date);
if (status.syncInfo.earliestBlockHeight) {
expect(status.syncInfo.earliestBlockHeight).toBeGreaterThanOrEqual(1);
expect(status.syncInfo.earliestBlockHeight).toBeLessThanOrEqual(status.syncInfo.latestBlockHeight);
}
if (status.syncInfo.earliestBlockTime) {
expect(status.syncInfo.earliestBlockTime).toBeInstanceOf(Date);
expect(status.syncInfo.earliestBlockTime.getTime()).toBeLessThanOrEqual(
status.syncInfo.latestBlockTime.getTime(),
);
}

// validator info
expect(status.validatorInfo.pubkey).toBeTruthy();
Expand Down
19 changes: 18 additions & 1 deletion packages/tendermint-rpc/src/tendermint37/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,12 @@ function decodeNodeInfo(data: RpcNodeInfo): responses.NodeInfo {
}

interface RpcSyncInfo {
/** hex encoded */
readonly earliest_app_hash: string;
/** hex encoded */
readonly earliest_block_hash: string;
readonly earliest_block_height: string;
readonly earliest_block_time: string;
/** hex encoded */
readonly latest_block_hash: string;
/** hex encoded */
Expand All @@ -630,7 +636,18 @@ interface RpcSyncInfo {
}

function decodeSyncInfo(data: RpcSyncInfo): responses.SyncInfo {
return {
const earliestBlockHeight = data.earliest_block_height
? apiToSmallInt(data.earliest_block_height)
: undefined;
const earliestBlockTime = data.earliest_block_time
? fromRfc3339WithNanoseconds(data.earliest_block_time)
: undefined;

return {
earliestAppHash: data.earliest_app_hash ? fromHex(data.earliest_app_hash) : undefined,
earliestBlockHash: data.earliest_block_hash ? fromHex(data.earliest_block_hash) : undefined,
earliestBlockHeight: earliestBlockHeight || undefined,
earliestBlockTime: earliestBlockTime?.getTime() ? earliestBlockTime : undefined,
latestBlockHash: fromHex(assertNotEmpty(data.latest_block_hash)),
latestAppHash: fromHex(assertNotEmpty(data.latest_app_hash)),
latestBlockTime: fromRfc3339WithNanoseconds(assertNotEmpty(data.latest_block_time)),
Expand Down
4 changes: 4 additions & 0 deletions packages/tendermint-rpc/src/tendermint37/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ export interface NodeInfo {
}

export interface SyncInfo {
readonly earliestAppHash?: Uint8Array;
readonly earliestBlockHash?: Uint8Array;
readonly earliestBlockHeight?: number;
readonly earliestBlockTime?: ReadonlyDate;
readonly latestBlockHash: Uint8Array;
readonly latestAppHash: Uint8Array;
readonly latestBlockHeight: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
// sync info
expect(status.syncInfo.catchingUp).toEqual(false);
expect(status.syncInfo.latestBlockHeight).toBeGreaterThanOrEqual(1);
expect(status.syncInfo.latestBlockTime).toBeInstanceOf(Date);
if (status.syncInfo.earliestBlockHeight) {
expect(status.syncInfo.earliestBlockHeight).toBeGreaterThanOrEqual(1);
expect(status.syncInfo.earliestBlockHeight).toBeLessThanOrEqual(status.syncInfo.latestBlockHeight);
}
if (status.syncInfo.earliestBlockTime) {
expect(status.syncInfo.earliestBlockTime).toBeInstanceOf(Date);
expect(status.syncInfo.earliestBlockTime.getTime()).toBeLessThanOrEqual(
status.syncInfo.latestBlockTime.getTime(),
);
}

// validator info
expect(status.validatorInfo.pubkey).toBeTruthy();
Expand Down

0 comments on commit c813712

Please sign in to comment.