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 | 17 +++++++++++++++++ .../src/tendermint34/responses.ts | 4 ++++ .../src/tendermint34/tendermint34client.spec.ts | 11 +++++++++++ .../src/tendermint37/adaptor/responses.ts | 17 +++++++++++++++++ .../src/tendermint37/responses.ts | 4 ++++ .../src/tendermint37/tendermint37client.spec.ts | 11 +++++++++++ 7 files changed, 69 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94836e37..152e6db8 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 31532201..bc8d2051 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 { + 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 9bf0bd8e..4dec9c5d 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 11c5d142..a3d64e5e 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 05aecedf..8db6e3cb 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 { + 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 66eb464c..0b8d5470 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 73458aa6..9389ffa9 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();