forked from MetaMask/eth-json-rpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache-utils.js
152 lines (137 loc) · 3.9 KB
/
cache-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const stringify = require('json-stable-stringify')
module.exports = {
cacheIdentifierForPayload: cacheIdentifierForPayload,
canCache: canCache,
blockTagForPayload: blockTagForPayload,
paramsWithoutBlockTag: paramsWithoutBlockTag,
blockTagParamIndex: blockTagParamIndex,
cacheTypeForPayload: cacheTypeForPayload
}
function cacheIdentifierForPayload (payload, skipBlockRef) {
const simpleParams = skipBlockRef ? paramsWithoutBlockTag(payload) : payload.params
if (canCache(payload)) {
return payload.method + ':' + stringify(simpleParams)
} else {
return null
}
}
function canCache (payload) {
return cacheTypeForPayload(payload) !== 'never'
}
function blockTagForPayload (payload) {
let index = blockTagParamIndex(payload)
// Block tag param not passed.
if (index >= payload.params.length) {
return null
}
return payload.params[index]
}
function paramsWithoutBlockTag (payload) {
const index = blockTagParamIndex(payload)
// Block tag param not passed.
if (index >= payload.params.length) {
return payload.params
}
// eth_getBlockByNumber has the block tag first, then the optional includeTx? param
if (payload.method === 'eth_getBlockByNumber') {
return payload.params.slice(1)
}
return payload.params.slice(0, index)
}
function blockTagParamIndex (payload) {
switch (payload.method) {
// blockTag is at index 2
case 'eth_getStorageAt':
return 2
// blockTag is at index 1
case 'eth_getBalance':
case 'eth_getCode':
case 'eth_getTransactionCount':
case 'eth_call':
return 1
// blockTag is at index 0
case 'eth_getBlockByNumber':
return 0
// there is no blockTag
default:
return undefined
}
}
function cacheTypeForPayload (payload) {
switch (payload.method) {
// cache permanently
case 'web3_clientVersion':
case 'web3_sha3':
case 'eth_protocolVersion':
case 'eth_getBlockTransactionCountByHash':
case 'eth_getUncleCountByBlockHash':
case 'eth_getCode':
case 'eth_getBlockByHash':
case 'eth_getTransactionByHash':
case 'eth_getTransactionByBlockHashAndIndex':
case 'eth_getTransactionReceipt':
case 'eth_getUncleByBlockHashAndIndex':
case 'eth_getCompilers':
case 'eth_compileLLL':
case 'eth_compileSolidity':
case 'eth_compileSerpent':
case 'shh_version':
case 'test_permaCache':
return 'perma'
// cache until fork
case 'eth_getBlockByNumber':
case 'eth_getBlockTransactionCountByNumber':
case 'eth_getUncleCountByBlockNumber':
case 'eth_getTransactionByBlockNumberAndIndex':
case 'eth_getUncleByBlockNumberAndIndex':
case 'test_forkCache':
return 'fork'
// cache for block
case 'eth_gasPrice':
case 'eth_blockNumber':
case 'eth_getBalance':
case 'eth_getStorageAt':
case 'eth_getTransactionCount':
case 'eth_call':
case 'eth_estimateGas':
case 'eth_getFilterLogs':
case 'eth_getLogs':
case 'test_blockCache':
return 'block'
// never cache
case 'net_version':
case 'net_peerCount':
case 'net_listening':
case 'eth_syncing':
case 'eth_sign':
case 'eth_coinbase':
case 'eth_mining':
case 'eth_hashrate':
case 'eth_accounts':
case 'eth_sendTransaction':
case 'eth_sendRawTransaction':
case 'eth_newFilter':
case 'eth_newBlockFilter':
case 'eth_newPendingTransactionFilter':
case 'eth_uninstallFilter':
case 'eth_getFilterChanges':
case 'eth_getWork':
case 'eth_submitWork':
case 'eth_submitHashrate':
case 'db_putString':
case 'db_getString':
case 'db_putHex':
case 'db_getHex':
case 'shh_post':
case 'shh_newIdentity':
case 'shh_hasIdentity':
case 'shh_newGroup':
case 'shh_addToGroup':
case 'shh_newFilter':
case 'shh_uninstallFilter':
case 'shh_getFilterChanges':
case 'shh_getMessages':
case 'test_neverCache':
return 'never'
}
}