From 2fef9d40c63fd8e064298ca8fdb43a6e45786ca3 Mon Sep 17 00:00:00 2001 From: Troy Kessler <43882936+troykessler@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:04:32 +0200 Subject: [PATCH] chore: add more snapshot metadata to bundle summary (#139) * chore: added chunks in bundle summary * chore: added format to bundle summary * chore: fetch snapshot in bundle summary * chore: reset comment * chore: use method --- integrations/tendermint-ssync/src/runtime.ts | 49 ++++++++++---------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/integrations/tendermint-ssync/src/runtime.ts b/integrations/tendermint-ssync/src/runtime.ts index da961a87..ff20104d 100644 --- a/integrations/tendermint-ssync/src/runtime.ts +++ b/integrations/tendermint-ssync/src/runtime.ts @@ -43,21 +43,8 @@ export default class TendermintSSync implements IRuntime { // fetch snapshot chunk from given height, format and chunk derived from key const [height, chunkIndex] = key.split('/').map((k) => +k); - const { data: snapshots } = await axios.get( - `${this.config.api}/list_snapshots` - ); - - if (!snapshots) { - throw new Error(`404: Snapshot with height ${height} not found`); - } - - const snapshot: ISnapshot = snapshots.find( - (s: ISnapshot) => +s.height === height - ); - - if (!snapshot) { - throw new Error(`404: Snapshot with height ${height} not found`); - } + // fetch snapshot metadata from height + const snapshot = await this.getSnapshot(height); // fetch snapshot chunk const { data: chunk } = await axios.get( @@ -194,13 +181,34 @@ export default class TendermintSSync implements IRuntime { } async summarizeDataBundle(_: Validator, bundle: DataItem[]): Promise { - return bundle.at(-1)?.key ?? ''; + // return format "height/format/chunkIndex/chunks" + const dataItem = bundle.at(-1); + if (!dataItem) { + return ''; + } + + const [height, chunkIndex] = dataItem.key.split('/').map((k) => +k); + const snapshot = await this.getSnapshot(height); + return `${height}/${snapshot.format}/${chunkIndex}/${snapshot.chunks}`; } async nextKey(_: Validator, key: string): Promise { // since we only need to fetch if we continue with the next snapshot const [height, chunkIndex] = key.split('/').map((k) => +k); + const snapshot = await this.getSnapshot(height); + + // move on to next snapshot and start at first chunk + // if we have already reached all chunks in current snapshot + if (snapshot.chunks - 1 === chunkIndex) { + return `${height + this.config.interval}/0`; + } + + // stay on current snapshot and continue with next snapshot chunk + return `${height}/${chunkIndex + 1}`; + } + + private async getSnapshot(height: number): Promise { const { data: snapshots } = await axios.get( `${this.config.api}/list_snapshots` ); @@ -217,13 +225,6 @@ export default class TendermintSSync implements IRuntime { throw new Error(`404: Snapshot with height ${height} not found`); } - // move on to next snapshot and start at first chunk - // if we have already reached all chunks in current snapshot - if (snapshot.chunks - 1 === chunkIndex) { - return `${height + this.config.interval}/0`; - } - - // stay on current snapshot and continue with next snapshot chunk - return `${height}/${chunkIndex + 1}`; + return snapshot; } }