Skip to content

Commit ca5b4a3

Browse files
committed
fixed caching issue for eth_getStorageAt
1 parent f196f0d commit ca5b4a3

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

test/cache-utils.js

+20
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,23 @@ test('cacheIdentifierForPayload for latest block', function (t) {
1010
t.notEqual(cacheId1, cacheId2, 'cacheIds are unique')
1111
t.end()
1212
})
13+
14+
test('blockTagForPayload for different methods', function (t) {
15+
const payloads = [
16+
{jsonrpc: '2.0', method: 'eth_getBalance', params: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x1234'], id: 1},
17+
{jsonrpc: '2.0', method: 'eth_getCode', params: ['0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', '0x1234'], id: 1},
18+
{jsonrpc: '2.0', method: 'eth_getTransactionCount', params: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1','0x1234'], id: 1},
19+
{jsonrpc: '2.0', method: 'eth_getStorageAt', params: ['0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x0', '0x1234'], id: 1},
20+
{jsonrpc: '2.0', method: 'eth_call', params: [{to: '0x295a70b2de5e3953354a6a8344e616ed314d7251'}, '0x1234'], id: 1},
21+
{jsonrpc: '2.0', method: 'eth_estimateGas', params: [{to: '0x295a70b2de5e3953354a6a8344e616ed314d7251'}, '0x1234'], id: 1},
22+
{jsonrpc: '2.0', method: 'eth_getBlockByNumber', params: ['0x1234', true], id: 1},
23+
]
24+
25+
26+
payloads.forEach(function (payload) {
27+
const blockTag = cacheUtils.blockTagForPayload(payload)
28+
t.isEqual(blockTag, '0x1234', 'block tag for ' + payload.method + ' is correct')
29+
})
30+
31+
t.end()
32+
})

test/cache.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ cacheTest('getBlockForNumber for block 1', [{
128128
params: ['0x1'],
129129
}], true)
130130

131+
// storage
132+
133+
cacheTest('getStorageAt for same block should cache', [{
134+
method: 'eth_getStorageAt',
135+
params: ['0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x0', '0x1234'],
136+
}, {
137+
method: 'eth_getStorageAt',
138+
params: ['0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x0', '0x1234'],
139+
}], true)
140+
141+
cacheTest('getStorageAt for different block should not cache', [{
142+
method: 'eth_getStorageAt',
143+
params: ['0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x0', '0x1234'],
144+
}, {
145+
method: 'eth_getStorageAt',
146+
params: ['0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x0', '0x4321'],
147+
}], false)
148+
131149

132150
// test helper for caching
133151
// 1. Sets up caching and data provider
@@ -182,7 +200,8 @@ function cacheTest(label, payloads, shouldHitCacheOnSecondRequest){
182200
input: '0x',
183201
})
184202
}
185-
}
203+
},
204+
eth_getStorageAt: '0x00000000000000000000000000000000000000000000000000000000deadbeef',
186205
}))
187206
// handle dummy block
188207
var blockProvider = injectMetrics(new TestBlockProvider())

util/rpc-cache-utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ function paramsWithoutBlockTag(payload){
4949

5050
function blockTagParamIndex(payload){
5151
switch(payload.method) {
52+
// blockTag is third param
53+
case 'eth_getStorageAt':
54+
return 2
5255
// blockTag is second param
5356
case 'eth_getBalance':
5457
case 'eth_getCode':
5558
case 'eth_getTransactionCount':
56-
case 'eth_getStorageAt':
5759
case 'eth_call':
5860
case 'eth_estimateGas':
5961
return 1

0 commit comments

Comments
 (0)