@@ -9,18 +9,19 @@ import { fromBase64 } from '@cosmjs/encoding';
99export 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 } ,
0 commit comments