Skip to content

Commit e598d01

Browse files
committed
update comments, add connect logic to fetchBlock
1 parent df50cb1 commit e598d01

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/stores/useBaseStore.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,15 @@ export const useBaseStore = defineStore('baseStore', {
9090
//check if the block exists in recents
9191
if (this.recents.findIndex((x) => x?.block_id?.hash === this.latest?.block_id?.hash) === -1) {
9292
const newBlocks = await this.fetchNewBlocks();
93-
if (this.recents.length + newBlocks.length > 50) {
94-
this.recents.splice(0, this.recents.length + newBlocks.length - 50);
95-
}
96-
this.recents.push(...newBlocks);
93+
const combined = [...this.recents, ...newBlocks];
94+
this.recents = combined.slice(-50);
9795
}
9896
return this.latest;
9997
},
10098
/**
101-
* Fetches all recent blocks since the current latest block and adds them to recents.
102-
* Only fetches blocks with height greater than this.latest.block.header.height.
103-
* Returns an array of new blocks added to recents.
99+
* Fetches all blocks since the last block in recents.
100+
* Only fetches blocks with height greater than this.recents[-1].block.header.height.
101+
* Returns an array of new blocks to be added to recents.
104102
*/
105103
async fetchNewBlocks() {
106104
if (!this.latest?.block?.header?.height) return [];
@@ -110,6 +108,7 @@ export const useBaseStore = defineStore('baseStore', {
110108
// Fetch all blocks between oldHeight+1 and less than newHeight
111109
for (let h = oldHeight + 1; h < newHeight; h++) {
112110
const block = await this.fetchBlock(h);
111+
if (!block?.block?.header?.height) continue; // skip if block not found
113112
newBlocks.push(block);
114113
}
115114
// Add the latest block
@@ -123,7 +122,15 @@ export const useBaseStore = defineStore('baseStore', {
123122
return this.blockchain.rpc.getBaseValidatorsetLatest(offset);
124123
},
125124
async fetchBlock(height?: number | string) {
126-
return this.blockchain.rpc.getBaseBlockAt(String(height));
125+
try {
126+
const block = await this.blockchain.rpc.getBaseBlockAt(String(height));
127+
this.connected = true;
128+
return block;
129+
} catch (error) {
130+
console.error('Error fetching latest block:', error);
131+
this.connected = false;
132+
}
133+
return {} as Block;
127134
},
128135
async fetchAbciInfo() {
129136
return this.blockchain.rpc.getBaseNodeInfo();

0 commit comments

Comments
 (0)