@@ -72,35 +72,29 @@ export class RunestoneIndexer {
72
72
return ;
73
73
}
74
74
75
- this . _updateInProgress = this . _rpc . getblockhashbyheight
76
- ? this . updateRuneUtxoBalancesWithBlockHeightImpl (
77
- this . _rpc . getblockhashbyheight . bind ( this . _rpc )
78
- )
79
- : this . updateRuneUtxoBalancesImpl ( ) ;
75
+ this . _updateInProgress = this . updateRuneUtxoBalancesImpl ( ) ;
80
76
try {
81
77
await this . _updateInProgress ;
82
78
} finally {
83
79
this . _updateInProgress = null ;
84
80
}
85
81
}
86
82
87
- private async updateRuneUtxoBalancesWithBlockHeightImpl (
88
- getblockhashbyheight : ( blockheight : number ) => Promise < string | null >
89
- ) {
83
+ private async updateRuneUtxoBalancesImpl ( ) {
90
84
const currentStorageBlock = await this . _storage . getCurrentBlock ( ) ;
91
85
if ( currentStorageBlock ) {
92
86
// walk down until matching hash is found
93
87
const reorgBlockhashesToIndex : string [ ] = [ ] ;
94
88
let blockheight = currentStorageBlock . height ;
95
- let blockhash = await getblockhashbyheight ( blockheight ) ;
89
+ let blockhash = ( await this . _rpc . getblockhash ( { height : blockheight } ) ) . result ;
96
90
let storageBlockHash : string | null = currentStorageBlock . hash ;
97
91
while ( storageBlockHash !== blockhash ) {
98
92
if ( blockhash ) {
99
93
reorgBlockhashesToIndex . push ( blockhash ) ;
100
94
}
101
95
102
96
blockheight -- ;
103
- blockhash = await getblockhashbyheight ( blockheight ) ;
97
+ blockhash = ( await this . _rpc . getblockhash ( { height : blockheight } ) ) . result ;
104
98
storageBlockHash = await this . _storage . getBlockhash ( blockheight ) ;
105
99
}
106
100
@@ -127,7 +121,7 @@ export class RunestoneIndexer {
127
121
Network . getFirstRuneHeight ( this . _network ) ,
128
122
currentStorageBlock ? currentStorageBlock . height + 1 : 0
129
123
) ;
130
- let blockhash = await getblockhashbyheight ( blockheight ) ;
124
+ let blockhash = ( await this . _rpc . getblockhash ( { height : blockheight } ) ) . result ;
131
125
while ( blockhash !== null ) {
132
126
const blockResult = await this . _rpc . getblock ( { blockhash, verbosity : 2 } ) ;
133
127
if ( blockResult . error !== null ) {
@@ -144,124 +138,7 @@ export class RunestoneIndexer {
144
138
await this . _storage . saveBlockIndex ( runeUpdater ) ;
145
139
146
140
blockheight ++ ;
147
- blockhash = await getblockhashbyheight ( blockheight ) ;
148
- }
149
- }
150
-
151
- private async updateRuneUtxoBalancesImpl ( ) {
152
- const newBlockhashesToIndex : string [ ] = [ ] ;
153
-
154
- const currentStorageBlock = await this . _storage . getCurrentBlock ( ) ;
155
- if ( currentStorageBlock !== null ) {
156
- // If rpc block indexing is ahead of our storage, let's save up all block hashes
157
- // until we arrive back to the current storage's block tip.
158
- const bestblockhashResult = await this . _rpc . getbestblockhash ( ) ;
159
- if ( bestblockhashResult . error !== null ) {
160
- throw bestblockhashResult . error ;
161
- }
162
- const bestblockhash = bestblockhashResult . result ;
163
-
164
- let rpcBlockResult = await this . _rpc . getblock ( {
165
- blockhash : bestblockhash ,
166
- verbosity : 1 ,
167
- } ) ;
168
- if ( rpcBlockResult . error !== null ) {
169
- throw rpcBlockResult . error ;
170
- }
171
- let rpcBlock = rpcBlockResult . result ;
172
-
173
- while ( rpcBlock . height > currentStorageBlock . height ) {
174
- newBlockhashesToIndex . push ( rpcBlock . hash ) ;
175
-
176
- rpcBlockResult = await this . _rpc . getblock ( {
177
- blockhash : rpcBlock . previousblockhash ,
178
- verbosity : 1 ,
179
- } ) ;
180
- if ( rpcBlockResult . error !== null ) {
181
- throw rpcBlockResult . error ;
182
- }
183
- rpcBlock = rpcBlockResult . result ;
184
- }
185
-
186
- // Handle edge case where storage block height is higher than rpc node block
187
- // (such as pointing to a newly indexing rpc node)
188
- let storageBlockhash =
189
- currentStorageBlock && currentStorageBlock . height === rpcBlock . height
190
- ? currentStorageBlock . hash
191
- : await this . _storage . getBlockhash ( rpcBlock . height ) ;
192
-
193
- // Now rpc and storage blocks are at the same height,
194
- // iterate until they are also the same hash
195
- while ( rpcBlock . hash !== storageBlockhash ) {
196
- newBlockhashesToIndex . push ( rpcBlock . hash ) ;
197
-
198
- rpcBlockResult = await this . _rpc . getblock ( {
199
- blockhash : rpcBlock . previousblockhash ,
200
- verbosity : 1 ,
201
- } ) ;
202
- if ( rpcBlockResult . error !== null ) {
203
- throw rpcBlockResult . error ;
204
- }
205
- rpcBlock = rpcBlockResult . result ;
206
-
207
- storageBlockhash = await this . _storage . getBlockhash ( rpcBlock . height ) ;
208
- }
209
-
210
- // We can reset our storage state to where rpc node and storage matches
211
- if ( currentStorageBlock && currentStorageBlock . hash !== rpcBlock . hash ) {
212
- await this . _storage . resetCurrentBlock ( rpcBlock ) ;
213
- }
214
- } else {
215
- const firstRuneHeight = Network . getFirstRuneHeight ( this . _network ) ;
216
-
217
- // Iterate through the rpc blocks until we reach first rune height
218
- const bestblockhashResult = await this . _rpc . getbestblockhash ( ) ;
219
- if ( bestblockhashResult . error !== null ) {
220
- throw bestblockhashResult . error ;
221
- }
222
- const bestblockhash = bestblockhashResult . result ;
223
-
224
- let rpcBlockResult = await this . _rpc . getblock ( {
225
- blockhash : bestblockhash ,
226
- verbosity : 1 ,
227
- } ) ;
228
- if ( rpcBlockResult . error !== null ) {
229
- throw rpcBlockResult . error ;
230
- }
231
- let rpcBlock = rpcBlockResult . result ;
232
-
233
- while ( rpcBlock . height >= firstRuneHeight ) {
234
- newBlockhashesToIndex . push ( rpcBlock . hash ) ;
235
-
236
- rpcBlockResult = await this . _rpc . getblock ( {
237
- blockhash : rpcBlock . previousblockhash ,
238
- verbosity : 1 ,
239
- } ) ;
240
- if ( rpcBlockResult . error !== null ) {
241
- throw rpcBlockResult . error ;
242
- }
243
- rpcBlock = rpcBlockResult . result ;
244
- }
245
- }
246
-
247
- // Finally start processing balances using newBlockhashesToIndex
248
- let blockhash = newBlockhashesToIndex . pop ( ) ;
249
- while ( blockhash !== undefined ) {
250
- const blockResult = await this . _rpc . getblock ( { blockhash, verbosity : 2 } ) ;
251
- if ( blockResult . error !== null ) {
252
- throw blockResult . error ;
253
- }
254
- const block = blockResult . result ;
255
- const reorg = currentStorageBlock ? currentStorageBlock . height >= block . height : false ;
256
-
257
- const runeUpdater = new RuneUpdater ( this . _network , block , reorg , this . _storage , this . _rpc ) ;
258
-
259
- for ( const [ txIndex , tx ] of block . tx . entries ( ) ) {
260
- await runeUpdater . indexRunes ( tx , txIndex ) ;
261
- }
262
-
263
- await this . _storage . saveBlockIndex ( runeUpdater ) ;
264
- blockhash = newBlockhashesToIndex . pop ( ) ;
141
+ blockhash = ( await this . _rpc . getblockhash ( { height : blockheight } ) ) . result ;
265
142
}
266
143
}
267
144
}
0 commit comments