1
1
"use strict" ;
2
2
3
+ const { Level } = require ( "level" ) ;
3
4
const crypto = require ( "crypto" ) , SHA256 = message => crypto . createHash ( "sha256" ) . update ( message ) . digest ( "hex" ) ;
5
+ const fs = require ( "fs" ) ;
4
6
const Transaction = require ( "../core/transaction" ) ;
5
7
const Block = require ( "../core/block" ) ;
6
- const { deserializeState, serializeState } = require ( "../utils/utils" ) ;
8
+ const { deserializeState } = require ( "../utils/utils" ) ;
7
9
8
10
const fastify = require ( "fastify" ) ( ) ;
9
11
@@ -88,15 +90,13 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
88
90
if ( typeof req . body . params !== "object" || typeof req . body . params . hash !== "string" ) {
89
91
throwError ( "Invalid request." ) ;
90
92
} else {
91
- const hashes = ( await bhashDB . keys ( ) . all ( ) ) ;
92
-
93
- if ( ! hashes . find ( hash => hash === req . body . params . hash ) ) {
94
- throwError ( "Invalid block hash." , 400 ) ;
95
- } else {
93
+ try {
96
94
const blockNumber = parseInt ( ( await bhashDB . get ( req . body . params . hash ) ) . toString ( "hex" ) , 16 ) . toString ( ) ;
97
95
const block = [ ...await blockDB . get ( blockNumber ) ] ;
98
96
99
97
respond ( { block : Block . deserialize ( block ) } ) ;
98
+ } catch ( e ) {
99
+ throwError ( "Invalid block hash." , 400 ) ;
100
100
}
101
101
}
102
102
@@ -106,14 +106,12 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
106
106
if ( typeof req . body . params !== "object" || typeof req . body . params . blockNumber !== "number" ) {
107
107
throwError ( "Invalid request." ) ;
108
108
} else {
109
- const currentBlockNumber = Math . max ( ...( await blockDB . keys ( ) . all ( ) ) . map ( key => parseInt ( key ) ) ) ;
110
-
111
- if ( req . body . params . blockNumber <= 0 || req . body . params . blockNumber > currentBlockNumber ) {
112
- throwError ( "Invalid block number." , 400 ) ;
113
- } else {
109
+ try {
114
110
const block = [ ...await blockDB . get ( req . body . params . blockNumber . toString ( ) ) ] ;
115
111
116
112
respond ( { block : Block . deserialize ( block ) } ) ;
113
+ } catch ( e ) {
114
+ throwError ( "Invalid block number." , 400 ) ;
117
115
}
118
116
}
119
117
@@ -123,15 +121,13 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
123
121
if ( typeof req . body . params !== "object" || typeof req . body . params . hash !== "string" ) {
124
122
throwError ( "Invalid request." , 400 ) ;
125
123
} else {
126
- const hashes = ( await bhashDB . keys ( ) . all ( ) ) ;
127
-
128
- if ( ! hashes . find ( hash => hash === req . body . params . hash ) ) {
129
- throwError ( "Invalid block hash." , 400 ) ;
130
- } else {
124
+ try {
131
125
const blockNumber = parseInt ( ( await bhashDB . get ( req . body . params . hash ) ) . toString ( "hex" ) , 16 ) . toString ( ) ;
132
126
const block = Block . deserialize ( [ ...await blockDB . get ( blockNumber ) ] ) ;
133
127
134
128
respond ( { count : block . transactions . length } ) ;
129
+ } catch ( e ) {
130
+ throwError ( "Invalid block hash." , 400 ) ;
135
131
}
136
132
}
137
133
@@ -141,74 +137,72 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
141
137
if ( typeof req . body . params !== "object" || typeof req . body . params . blockNumber !== "number" ) {
142
138
throwError ( "Invalid request." , 400 ) ;
143
139
} else {
144
- const currentBlockNumber = Math . max ( ...( await blockDB . keys ( ) . all ( ) ) . map ( key => parseInt ( key ) ) ) ;
145
-
146
- if ( req . body . params . blockNumber <= 0 || req . body . params . blockNumber > currentBlockNumber ) {
147
- throwError ( "Invalid block number." , 400 ) ;
148
- } else {
140
+ try {
149
141
const block = Block . deserialize ( [ ...await blockDB . get ( req . body . params . blockNumber . toString ( ) ) ] ) ;
150
142
151
143
respond ( { count : block . transactions . length } ) ;
144
+ } catch ( e ) {
145
+ throwError ( "Invalid block number." , 400 ) ;
152
146
}
153
147
}
154
148
155
149
break ;
156
150
157
151
case "get_balance" :
158
- if (
159
- typeof req . body . params !== "object" ||
160
- typeof req . body . params . address !== "string" ||
161
- ! ( await stateDB . keys ( ) . all ( ) ) . includes ( req . body . params . address )
162
- ) {
152
+ if ( typeof req . body . params !== "object" || typeof req . body . params . address !== "string" ) {
163
153
throwError ( "Invalid request." , 400 ) ;
164
154
} else {
165
- const dataFromTarget = deserializeState ( await stateDB . get ( req . body . params . address ) ) ; // Fetch target's state object
166
- const targetBalance = dataFromTarget . balance ; // Get target's balance
155
+ try {
156
+ const dataFromTarget = deserializeState ( await stateDB . get ( req . body . params . address ) ) ; // Fetch target's state object
157
+ const targetBalance = dataFromTarget . balance ; // Get target's balance
167
158
168
- respond ( { balance : targetBalance } ) ;
159
+ respond ( { balance : targetBalance } ) ;
160
+ } catch ( e ) {
161
+ throwError ( "Can not find account." , 400 ) ;
162
+ }
169
163
}
170
164
171
165
break ;
172
166
173
167
case "get_code" :
174
- if (
175
- typeof req . body . params !== "object" ||
176
- typeof req . body . params . codeHash !== "string" ||
177
- ! ( await codeDB . keys ( ) . all ( ) ) . includes ( req . body . params . codeHash )
178
- ) {
168
+ if ( typeof req . body . params !== "object" || typeof req . body . params . codeHash !== "string" ) {
179
169
throwError ( "Invalid request." , 400 ) ;
180
170
} else {
181
- respond ( { code : await codeDB . get ( req . body . params . codeHash ) } ) ;
171
+ try {
172
+ respond ( { code : await codeDB . get ( req . body . params . codeHash ) } ) ;
173
+ } catch ( e ) {
174
+ throwError ( "Can not find code." , 400 ) ;
175
+ }
182
176
}
183
177
184
178
break ;
185
179
186
180
case "get_codeHash" :
187
- if (
188
- typeof req . body . params !== "object" ||
189
- typeof req . body . params . address !== "string" ||
190
- ! ( await stateDB . keys ( ) . all ( ) ) . includes ( req . body . params . address )
191
- ) {
181
+ if ( typeof req . body . params !== "object" || typeof req . body . params . address !== "string" ) {
192
182
throwError ( "Invalid request." , 400 ) ;
193
183
} else {
194
- const dataFromTarget = deserializeState ( await stateDB . get ( req . body . params . address ) ) ; // Fetch target's state object
184
+ try {
185
+ const dataFromTarget = deserializeState ( await stateDB . get ( req . body . params . address ) ) ; // Fetch target's state object
195
186
196
- respond ( { codeHash : dataFromTarget . codeHash } ) ;
187
+ respond ( { codeHash : dataFromTarget . codeHash } ) ;
188
+ } catch ( e ) {
189
+ throwError ( "Can not find account." , 400 ) ;
190
+ }
197
191
}
198
-
192
+
199
193
break ;
200
194
201
195
case "get_nonce" :
202
- if (
203
- typeof req . body . params !== "object" ||
204
- typeof req . body . params . address !== "string" ||
205
- ! ( await stateDB . keys ( ) . all ( ) ) . includes ( req . body . params . address )
206
- ) {
196
+ if ( typeof req . body . params !== "object" || typeof req . body . params . address !== "string" ) {
207
197
throwError ( "Invalid request." , 400 ) ;
208
198
} else {
209
- const dataFromTarget = deserializeState ( await stateDB . get ( req . body . params . address ) ) ; // Fetch target's state object
199
+ try {
200
+ const dataFromTarget = deserializeState ( await stateDB . get ( req . body . params . address ) ) ; // Fetch target's state object
210
201
211
- respond ( { nonce : dataFromTarget . nonce } ) ;
202
+ respond ( { nonce : dataFromTarget . nonce } ) ;
203
+ } catch ( e ) {
204
+ throwError ( "Can not find account." , 400 ) ;
205
+ }
212
206
}
213
207
214
208
break ;
@@ -218,41 +212,48 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
218
212
typeof req . body . params !== "object" ||
219
213
typeof req . body . params . address !== "string" ||
220
214
typeof req . body . params . key !== "string" ||
221
- ! ( await stateDB . keys ( ) . all ( ) ) . includes ( req . body . params . address )
215
+ ! fs . existsSync ( "./log/accountStore/" + req . body . params . address )
222
216
) {
223
217
throwError ( "Invalid request." , 400 ) ;
224
218
} else {
225
- const storageDB = new Level ( "./log/accountStore/" + contractInfo . address ) ;
219
+ try {
220
+ const storageDB = new Level ( "./log/accountStore/" + req . body . params . address ) ;
226
221
227
- respond ( { storage : await storageDB . get ( req . body . params . key ) } ) ;
222
+ respond ( { storage : await storageDB . get ( req . body . params . key ) } ) ;
228
223
229
- storageDB . close ( ) ;
224
+ storageDB . close ( ) ;
225
+ } catch ( e ) {
226
+ throwError ( "Can not find storage slot." , 400 ) ;
227
+ }
230
228
}
231
229
232
230
break ;
233
-
231
+
234
232
case "get_storageKeys" :
235
233
if (
236
234
typeof req . body . params . address !== "string" ||
237
- ! ( await stateDB . keys ( ) . all ( ) ) . includes ( req . body . params . address )
235
+ ! fs . existsSync ( "./log/accountStore/" + req . body . params . address )
238
236
) {
239
237
throwError ( "Invalid request." , 400 ) ;
240
238
} else {
241
- const storageDB = new Level ( "./log/accountStore/" + contractInfo . address ) ;
239
+ const storageDB = new Level ( "./log/accountStore/" + req . body . params . address ) ;
242
240
243
241
respond ( { storage : await storageDB . keys ( ) . all ( ) } ) ;
242
+
243
+ storageDB . close ( ) ;
244
244
}
245
245
246
246
break ;
247
247
248
248
case "get_storageRoot" :
249
- if (
250
- typeof req . body . params . address !== "string" ||
251
- ! ( await stateDB . keys ( ) . all ( ) ) . includes ( req . body . params . address )
252
- ) {
249
+ if ( typeof req . body . params . address !== "string" ) {
253
250
throwError ( "Invalid request." , 400 ) ;
254
251
} else {
255
- respond ( { storageRoot : ( deserializeState ( await stateDB . get ( contractInfo . address ) ) ) . storageRoot } ) ;
252
+ try {
253
+ respond ( { storageRoot : ( deserializeState ( await stateDB . get ( req . body . params . address ) ) ) . storageRoot } ) ;
254
+ } catch ( e ) {
255
+ throwError ( "Can not find account." , 400 ) ;
256
+ }
256
257
}
257
258
258
259
break ;
@@ -265,18 +266,16 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
265
266
) {
266
267
throwError ( "Invalid request." , 400 ) ;
267
268
} else {
268
- const currentBlockNumber = Math . max ( ...( await blockDB . keys ( ) . all ( ) ) . map ( key => parseInt ( key ) ) ) ;
269
-
270
- if ( req . body . params . blockNumber <= 0 || req . body . params . blockNumber > currentBlockNumber ) {
271
- throwError ( "Invalid block number." , 400 ) ;
272
- } else {
269
+ try {
273
270
const block = Block . deserialize ( [ ...await blockDB . get ( req . body . params . blockNumber . toString ( ) ) ] ) ;
274
271
275
272
if ( req . body . params . index < 0 || req . body . params . index >= block . transactions . length ) {
276
273
throwError ( "Invalid transaction index." , 400 ) ;
277
274
} else {
278
275
respond ( { transaction : block . transactions [ req . body . params . index ] } ) ;
279
276
}
277
+ } catch ( e ) {
278
+ throwError ( "Invalid block number." , 400 ) ;
280
279
}
281
280
}
282
281
@@ -290,11 +289,7 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
290
289
) {
291
290
throwError ( "Invalid request." , 400 ) ;
292
291
} else {
293
- const hashes = ( await bhashDB . keys ( ) . all ( ) ) ;
294
-
295
- if ( ! hashes . find ( hash => hash === req . body . params . hash ) ) {
296
- throwError ( "Invalid block hash." , 400 ) ;
297
- } else {
292
+ try {
298
293
const blockNumber = parseInt ( ( await bhashDB . get ( req . body . params . hash ) ) . toString ( "hex" ) , 16 ) . toString ( ) ;
299
294
const block = Block . deserialize ( [ ...await blockDB . get ( blockNumber ) ] ) ;
300
295
@@ -303,6 +298,8 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
303
298
} else {
304
299
respond ( { transaction : block . transactions [ req . body . params . index ] } ) ;
305
300
}
301
+ } catch ( e ) {
302
+ throwError ( "Invalid block hash." , 400 ) ;
306
303
}
307
304
}
308
305
0 commit comments