From 665fc3ba8465664c5c0289d3346994534dc46320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Livora?= Date: Thu, 29 Jun 2023 16:28:12 +0200 Subject: [PATCH] fix(tendermint-rpc): add earliest block to sync info * fix issue #1448 --- CHANGELOG.md | 5 +++++ .../src/tendermint34/adaptor/responses.ts | 19 ++++++++++++++++++- .../src/tendermint34/responses.ts | 4 ++++ .../tendermint34/tendermint34client.spec.ts | 11 +++++++++++ .../src/tendermint37/adaptor/responses.ts | 19 ++++++++++++++++++- .../src/tendermint37/responses.ts | 4 ++++ .../tendermint37/tendermint37client.spec.ts | 11 +++++++++++ 7 files changed, 71 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94836e3784..152e6db8f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to ## [Unreleased] +### Fixed + +- @cosmjs/tendermint-rpc: Add missing `earliest_*` fields to `SyncInfo` record + returned from the `/status` RPC endpoint ([#1448]). + ## [0.31.0] - 2023-06-22 ### Fixed diff --git a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts index 315322017f..bc8d2051b3 100644 --- a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts +++ b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts @@ -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 */ @@ -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)), diff --git a/packages/tendermint-rpc/src/tendermint34/responses.ts b/packages/tendermint-rpc/src/tendermint34/responses.ts index 9bf0bd8e0c..4dec9c5df1 100644 --- a/packages/tendermint-rpc/src/tendermint34/responses.ts +++ b/packages/tendermint-rpc/src/tendermint34/responses.ts @@ -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; diff --git a/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts b/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts index 11c5d1428d..a3d64e5e77 100644 --- a/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts +++ b/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts @@ -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(); diff --git a/packages/tendermint-rpc/src/tendermint37/adaptor/responses.ts b/packages/tendermint-rpc/src/tendermint37/adaptor/responses.ts index 05aecedf8e..8db6e3cb38 100644 --- a/packages/tendermint-rpc/src/tendermint37/adaptor/responses.ts +++ b/packages/tendermint-rpc/src/tendermint37/adaptor/responses.ts @@ -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 */ @@ -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)), diff --git a/packages/tendermint-rpc/src/tendermint37/responses.ts b/packages/tendermint-rpc/src/tendermint37/responses.ts index 66eb464c77..0b8d547009 100644 --- a/packages/tendermint-rpc/src/tendermint37/responses.ts +++ b/packages/tendermint-rpc/src/tendermint37/responses.ts @@ -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; diff --git a/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts b/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts index 73458aa645..9389ffa995 100644 --- a/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts +++ b/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts @@ -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();