@@ -117,27 +117,31 @@ export class Eth {
117
117
async call ( params : [ RpcCallTx , string ] ) {
118
118
const [ transaction , blockOpt ] = params
119
119
120
+ if ( ! this . _vm ) {
121
+ throw new Error ( 'missing vm' )
122
+ }
123
+
124
+ // use a copy of the vm in case new blocks are executed,
125
+ // and to not make any underlying changes during the call
126
+ const vm = this . _vm . copy ( )
127
+
120
128
if ( blockOpt !== 'latest' ) {
121
- const latest = await this . blockNumber ( )
122
- if ( blockOpt !== latest ) {
129
+ const latest = await vm . blockchain . getLatestHeader ( )
130
+ const number = latest . number . toString ( 16 )
131
+ if ( blockOpt !== `0x${ number } ` ) {
123
132
return {
124
133
code : INVALID_PARAMS ,
125
134
message : `Currently only "latest" block supported` ,
126
135
}
127
136
}
128
137
}
129
138
130
- if ( ! this . _vm ) {
131
- throw new Error ( 'missing vm' )
132
- }
133
-
134
139
if ( ! transaction . gas ) {
135
140
// If no gas limit is specified use the last block gas limit as an upper bound.
136
- const latestHeader = await this . _chain . getLatestHeader ( )
137
- transaction . gas = latestHeader . gasLimit as any
141
+ const latest = await vm . blockchain . getLatestHeader ( )
142
+ transaction . gas = latest . gasLimit as any
138
143
}
139
144
140
- const vm = this . _vm . copy ( )
141
145
const txData = { ...transaction , gasLimit : transaction . gas }
142
146
const tx = Transaction . fromTxData ( txData , { common : vm . _common , freeze : false } )
143
147
@@ -171,27 +175,30 @@ export class Eth {
171
175
async estimateGas ( params : [ RpcCallTx , string ] ) {
172
176
const [ transaction , blockOpt ] = params
173
177
178
+ if ( ! this . _vm ) {
179
+ throw new Error ( 'missing vm' )
180
+ }
181
+
182
+ // use a copy of the vm in case new blocks are executed
183
+ const vm = this . _vm . copy ( )
184
+
174
185
if ( blockOpt !== 'latest' ) {
175
- const latest = await this . blockNumber ( )
176
- if ( blockOpt !== latest ) {
186
+ const latest = await vm . blockchain . getLatestHeader ( )
187
+ const number = latest . number . toString ( 16 )
188
+ if ( blockOpt !== `0x${ number } ` ) {
177
189
return {
178
190
code : INVALID_PARAMS ,
179
191
message : `Currently only "latest" block supported` ,
180
192
}
181
193
}
182
194
}
183
195
184
- if ( ! this . _vm ) {
185
- throw new Error ( 'missing vm' )
186
- }
187
-
188
196
if ( ! transaction . gas ) {
189
197
// If no gas limit is specified use the last block gas limit as an upper bound.
190
- const latestHeader = await this . _chain . getLatestHeader ( )
191
- transaction . gas = latestHeader . gasLimit as any
198
+ const latest = await this . _chain . getLatestHeader ( )
199
+ transaction . gas = latest . gasLimit as any
192
200
}
193
201
194
- const vm = this . _vm . copy ( )
195
202
const txData = { ...transaction , gasLimit : transaction . gas }
196
203
const tx = Transaction . fromTxData ( txData , { common : vm . _common , freeze : false } )
197
204
@@ -220,22 +227,26 @@ export class Eth {
220
227
async getBalance ( params : [ string , string ] ) {
221
228
const [ addressHex , blockOpt ] = params
222
229
230
+ if ( ! this . _vm ) {
231
+ throw new Error ( 'missing vm' )
232
+ }
233
+
234
+ // use a copy of the vm in case new blocks are sync'd
235
+ const vm = this . _vm . copy ( )
236
+
223
237
if ( blockOpt !== 'latest' ) {
224
- const latest = await this . blockNumber ( )
225
- if ( blockOpt !== latest ) {
238
+ const latest = await vm . blockchain . getLatestHeader ( )
239
+ const number = latest . number . toString ( 16 )
240
+ if ( blockOpt !== `0x${ number } ` ) {
226
241
return {
227
242
code : INVALID_PARAMS ,
228
243
message : `Currently only "latest" block supported` ,
229
244
}
230
245
}
231
246
}
232
247
233
- if ( ! this . _vm ) {
234
- throw new Error ( 'missing vm' )
235
- }
236
-
237
248
const address = Address . fromString ( addressHex )
238
- const account : Account = await ( this . _vm . stateManager as any ) . getAccount ( address )
249
+ const account : Account = await vm . stateManager . getAccount ( address )
239
250
return `0x${ account . balance . toString ( 16 ) } `
240
251
}
241
252
@@ -294,21 +305,26 @@ export class Eth {
294
305
async getCode ( params : [ string , string ] ) {
295
306
const [ addressHex , blockOpt ] = params
296
307
308
+ if ( ! this . _vm ) {
309
+ throw new Error ( 'missing vm' )
310
+ }
311
+
312
+ // use a copy of the vm in case new blocks are sync'd
313
+ const vm = this . _vm . copy ( )
314
+
297
315
if ( blockOpt !== 'latest' ) {
298
- const latest = await this . blockNumber ( )
299
- if ( blockOpt !== latest ) {
316
+ const latest = await vm . blockchain . getLatestHeader ( )
317
+ const number = latest . number . toString ( 16 )
318
+ if ( blockOpt !== `0x${ number } ` ) {
300
319
return {
301
320
code : INVALID_PARAMS ,
302
321
message : `Currently only "latest" block supported` ,
303
322
}
304
323
}
305
324
}
306
325
307
- if ( ! this . _vm ) {
308
- throw new Error ( 'missing vm' )
309
- }
310
326
const address = Address . fromString ( addressHex )
311
- const code = await ( this . _vm . stateManager as any ) . getContractCode ( address )
327
+ const code = await vm . stateManager . getContractCode ( address )
312
328
return bufferToHex ( code )
313
329
}
314
330
@@ -323,22 +339,26 @@ export class Eth {
323
339
async getStorageAt ( params : [ string , string , string ] ) {
324
340
const [ addressHex , positionHex , blockOpt ] = params
325
341
342
+ if ( ! this . _vm ) {
343
+ throw new Error ( 'missing vm' )
344
+ }
345
+
346
+ // use a copy of the vm in case new blocks are executed
347
+ const vm = this . _vm . copy ( )
348
+
326
349
if ( blockOpt !== 'latest' ) {
327
- const latest = await this . blockNumber ( )
328
- if ( blockOpt !== latest ) {
350
+ const latest = await vm . blockchain . getLatestHeader ( )
351
+ const number = latest . number . toString ( 16 )
352
+ if ( blockOpt !== `0x${ number } ` ) {
329
353
return {
330
354
code : INVALID_PARAMS ,
331
355
message : `Currently only "latest" block supported` ,
332
356
}
333
357
}
334
358
}
335
359
336
- if ( ! this . _vm ) {
337
- throw new Error ( 'missing vm' )
338
- }
339
-
340
360
const address = Address . fromString ( addressHex )
341
- const storageTrie = await ( this . _vm . stateManager as any ) . _getStorageTrie ( address )
361
+ const storageTrie = await ( vm . stateManager as any ) . _getStorageTrie ( address )
342
362
const position = setLengthLeft ( toBuffer ( positionHex ) , 32 )
343
363
const storage = await storageTrie . get ( position )
344
364
return storage ? bufferToHex ( setLengthLeft ( decode ( storage ) , 32 ) ) : '0x'
@@ -354,22 +374,26 @@ export class Eth {
354
374
async getTransactionCount ( params : [ string , string ] ) {
355
375
const [ addressHex , blockOpt ] = params
356
376
377
+ if ( ! this . _vm ) {
378
+ throw new Error ( 'missing vm' )
379
+ }
380
+
381
+ // use a copy of the vm in case new blocks are executed
382
+ const vm = this . _vm . copy ( )
383
+
357
384
if ( blockOpt !== 'latest' ) {
358
- const latest = await this . blockNumber ( )
359
- if ( blockOpt !== latest ) {
385
+ const latest = await vm . blockchain . getLatestHeader ( )
386
+ const number = latest . number . toString ( 16 )
387
+ if ( blockOpt !== `0x${ number } ` ) {
360
388
return {
361
389
code : INVALID_PARAMS ,
362
390
message : `Currently only "latest" block supported` ,
363
391
}
364
392
}
365
393
}
366
394
367
- if ( ! this . _vm ) {
368
- throw new Error ( 'missing vm' )
369
- }
370
-
371
395
const address = Address . fromString ( addressHex )
372
- const account : Account = await ( this . _vm . stateManager as any ) . getAccount ( address )
396
+ const account : Account = await vm . stateManager . getAccount ( address )
373
397
return `0x${ account . nonce . toString ( 16 ) } `
374
398
}
375
399
0 commit comments