Skip to content

Commit

Permalink
chore: add more snapshot metadata to bundle summary (#139)
Browse files Browse the repository at this point in the history
* chore: added chunks in bundle summary

* chore: added format to bundle summary

* chore: fetch snapshot in bundle summary

* chore: reset comment

* chore: use method
  • Loading branch information
troykessler authored Jun 14, 2024
1 parent 4c88bf2 commit 2fef9d4
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions integrations/tendermint-ssync/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -194,13 +181,34 @@ export default class TendermintSSync implements IRuntime {
}

async summarizeDataBundle(_: Validator, bundle: DataItem[]): Promise<string> {
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<string> {
// 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<ISnapshot> {
const { data: snapshots } = await axios.get(
`${this.config.api}/list_snapshots`
);
Expand All @@ -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;
}
}

0 comments on commit 2fef9d4

Please sign in to comment.