Skip to content

Commit df50cb1

Browse files
committed
modify basestore to retain previous behavior but fetch missed blocks
1 parent 9e3f190 commit df50cb1

File tree

3 files changed

+58
-49
lines changed

3 files changed

+58
-49
lines changed

src/main.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import App from '@/App.vue';
33
import i18n from '@/plugins/i18n';
44
import '@/style.css';
5-
import { createApp, ref, watch } from 'vue';
5+
import { createApp, ref } from 'vue';
66
import { createPinia } from 'pinia';
77
import LazyLoad from 'lazy-load-vue3';
88

@@ -19,32 +19,15 @@ app.use(LazyLoad, { component: true });
1919
// Mount vue app
2020
app.mount('#app');
2121

22-
// fetch latest block every <blocktime> ms
23-
const baseStore = useBaseStore();
24-
const requestCounter = ref(0);
25-
26-
let intervalId: NodeJS.Timeout;
27-
28-
const startInterval = () => {
29-
clearInterval(intervalId); // Clear any existing interval
30-
// console.log('Starting interval with blocktime:', baseStore.blocktime);
22+
const REFRESH_INTERVAL = import.meta.env.VITE_REFRESH_INTERVAL || 6000; // 6 seconds
3123

32-
intervalId = setInterval(() => {
33-
requestCounter.value += 1;
34-
if (requestCounter.value < 5) {
35-
// max allowed request
36-
baseStore.fetchLatest().finally(() => (requestCounter.value -= 1));
37-
}
38-
}, baseStore.blocktime);
39-
};
40-
41-
// Call startInterval initially
42-
startInterval();
43-
44-
// Watch for changes to baseStore.blocktime
45-
watch(
46-
() => baseStore.blocktime,
47-
() => {
48-
startInterval(); // Restart the interval when baseStore.blocktime changes
24+
// fetch latest block every 6s
25+
const blockStore = useBaseStore();
26+
const requestCounter = ref(0);
27+
setInterval(() => {
28+
requestCounter.value += 1;
29+
if (requestCounter.value < 5) {
30+
// max allowed request
31+
blockStore.fetchLatest().finally(() => (requestCounter.value -= 1));
4932
}
50-
);
33+
}, REFRESH_INTERVAL);

src/stores/useBaseStore.ts

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ import { fromBase64 } from '@cosmjs/encoding';
99
export const useBaseStore = defineStore('baseStore', {
1010
state: () => {
1111
return {
12-
earlest: {} as Block,
12+
earliest: {} as Block,
1313
latest: {} as Block,
1414
recents: [] as Block[],
1515
theme: (window.localStorage.getItem('theme') || 'dark') as 'light' | 'dark',
16+
connected: false,
1617
};
1718
},
1819
getters: {
1920
blocktime(): number {
20-
if (this.earlest && this.latest) {
21-
if (this.latest.block?.header?.height !== this.earlest.block?.header?.height) {
22-
const diff = dayjs(this.latest.block?.header?.time).diff(this.earlest.block?.header?.time);
23-
const blocks = Number(this.latest.block.header.height) - Number(this.earlest.block.header.height);
21+
if (this.earliest && this.latest) {
22+
if (this.latest.block?.header?.height !== this.earliest.block?.header?.height) {
23+
const diff = dayjs(this.latest.block?.header?.time).diff(this.earliest.block?.header?.time);
24+
const blocks = Number(this.latest.block.header.height) - Number(this.earliest.block.header.height);
2425
return Math.round(diff / blocks);
2526
}
2627
}
@@ -29,7 +30,7 @@ export const useBaseStore = defineStore('baseStore', {
2930
blockchain() {
3031
return useBlockchain();
3132
},
32-
connected(): boolean {
33+
hasRpc(): boolean {
3334
return this.blockchain?.rpc as unknown as boolean;
3435
},
3536
currentChainId(): string {
@@ -64,7 +65,7 @@ export const useBaseStore = defineStore('baseStore', {
6465
},
6566
actions: {
6667
async initial() {
67-
while (!this.connected) {
68+
while (!this.hasRpc) {
6869
await new Promise((resolve) => setTimeout(resolve, 1000));
6970
}
7071
this.fetchLatest();
@@ -73,24 +74,48 @@ export const useBaseStore = defineStore('baseStore', {
7374
this.recents = [];
7475
},
7576
async fetchLatest() {
76-
if (this.connected) {
77+
if (!this.hasRpc) return this.latest;
78+
try {
7779
this.latest = await this.blockchain.rpc?.getBaseBlockLatest();
78-
if (!this.earlest || this.earlest?.block?.header?.chain_id != this.latest?.block?.header?.chain_id) {
79-
//reset earlest and recents
80-
this.earlest = this.latest;
81-
this.recents = [];
82-
}
83-
//check if the block exists in recents
84-
if (this.recents.findIndex((x) => x?.block_id?.hash === this.latest?.block_id?.hash) === -1) {
85-
if (this.recents.length >= 50) {
86-
this.recents.shift();
87-
}
88-
this.recents.push(this.latest);
80+
this.connected = true;
81+
} catch (error) {
82+
console.error('Error fetching latest block:', error);
83+
this.connected = false;
84+
}
85+
if (!this.earliest || this.earliest?.block?.header?.chain_id != this.latest?.block?.header?.chain_id) {
86+
//reset earliest and recents
87+
this.earliest = this.latest;
88+
this.recents = [];
89+
}
90+
//check if the block exists in recents
91+
if (this.recents.findIndex((x) => x?.block_id?.hash === this.latest?.block_id?.hash) === -1) {
92+
const newBlocks = await this.fetchNewBlocks();
93+
if (this.recents.length + newBlocks.length > 50) {
94+
this.recents.splice(0, this.recents.length + newBlocks.length - 50);
8995
}
96+
this.recents.push(...newBlocks);
9097
}
91-
return this.latest;
98+
return this.latest;
99+
},
100+
/**
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.
104+
*/
105+
async fetchNewBlocks() {
106+
if (!this.latest?.block?.header?.height) return [];
107+
const oldHeight = Number(this.recents[this.recents.length - 1]?.block?.header?.height);
108+
const newHeight = Number(this.latest.block.header.height);
109+
let newBlocks = [];
110+
// Fetch all blocks between oldHeight+1 and less than newHeight
111+
for (let h = oldHeight + 1; h < newHeight; h++) {
112+
const block = await this.fetchBlock(h);
113+
newBlocks.push(block);
114+
}
115+
// Add the latest block
116+
newBlocks.push(this.latest);
117+
return newBlocks;
92118
},
93-
94119
async fetchValidatorByHeight(height?: number, offset = 0) {
95120
return this.blockchain.rpc.getBaseValidatorsetAt(String(height), offset);
96121
},

src/types/chaindata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,5 @@ export interface ChainConfig {
139139
address_limit: number;
140140
fees: string;
141141
};
142+
blocktime_ms?: number;
142143
}

0 commit comments

Comments
 (0)