11"use strict" ;
22
3+ const { Level } = require ( "level" ) ;
34const crypto = require ( "crypto" ) , SHA256 = message => crypto . createHash ( "sha256" ) . update ( message ) . digest ( "hex" ) ;
5+ const fs = require ( "fs" ) ;
46const Transaction = require ( "../core/transaction" ) ;
57const Block = require ( "../core/block" ) ;
6- const { deserializeState, serializeState } = require ( "../utils/utils" ) ;
8+ const { deserializeState } = require ( "../utils/utils" ) ;
79
810const fastify = require ( "fastify" ) ( ) ;
911
@@ -88,15 +90,13 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
8890 if ( typeof req . body . params !== "object" || typeof req . body . params . hash !== "string" ) {
8991 throwError ( "Invalid request." ) ;
9092 } 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 {
9694 const blockNumber = parseInt ( ( await bhashDB . get ( req . body . params . hash ) ) . toString ( "hex" ) , 16 ) . toString ( ) ;
9795 const block = [ ...await blockDB . get ( blockNumber ) ] ;
9896
9997 respond ( { block : Block . deserialize ( block ) } ) ;
98+ } catch ( e ) {
99+ throwError ( "Invalid block hash." , 400 ) ;
100100 }
101101 }
102102
@@ -106,14 +106,12 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
106106 if ( typeof req . body . params !== "object" || typeof req . body . params . blockNumber !== "number" ) {
107107 throwError ( "Invalid request." ) ;
108108 } 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 {
114110 const block = [ ...await blockDB . get ( req . body . params . blockNumber . toString ( ) ) ] ;
115111
116112 respond ( { block : Block . deserialize ( block ) } ) ;
113+ } catch ( e ) {
114+ throwError ( "Invalid block number." , 400 ) ;
117115 }
118116 }
119117
@@ -123,15 +121,13 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
123121 if ( typeof req . body . params !== "object" || typeof req . body . params . hash !== "string" ) {
124122 throwError ( "Invalid request." , 400 ) ;
125123 } 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 {
131125 const blockNumber = parseInt ( ( await bhashDB . get ( req . body . params . hash ) ) . toString ( "hex" ) , 16 ) . toString ( ) ;
132126 const block = Block . deserialize ( [ ...await blockDB . get ( blockNumber ) ] ) ;
133127
134128 respond ( { count : block . transactions . length } ) ;
129+ } catch ( e ) {
130+ throwError ( "Invalid block hash." , 400 ) ;
135131 }
136132 }
137133
@@ -141,74 +137,72 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
141137 if ( typeof req . body . params !== "object" || typeof req . body . params . blockNumber !== "number" ) {
142138 throwError ( "Invalid request." , 400 ) ;
143139 } 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 {
149141 const block = Block . deserialize ( [ ...await blockDB . get ( req . body . params . blockNumber . toString ( ) ) ] ) ;
150142
151143 respond ( { count : block . transactions . length } ) ;
144+ } catch ( e ) {
145+ throwError ( "Invalid block number." , 400 ) ;
152146 }
153147 }
154148
155149 break ;
156150
157151 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" ) {
163153 throwError ( "Invalid request." , 400 ) ;
164154 } 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
167158
168- respond ( { balance : targetBalance } ) ;
159+ respond ( { balance : targetBalance } ) ;
160+ } catch ( e ) {
161+ throwError ( "Can not find account." , 400 ) ;
162+ }
169163 }
170164
171165 break ;
172166
173167 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" ) {
179169 throwError ( "Invalid request." , 400 ) ;
180170 } 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+ }
182176 }
183177
184178 break ;
185179
186180 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" ) {
192182 throwError ( "Invalid request." , 400 ) ;
193183 } 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
195186
196- respond ( { codeHash : dataFromTarget . codeHash } ) ;
187+ respond ( { codeHash : dataFromTarget . codeHash } ) ;
188+ } catch ( e ) {
189+ throwError ( "Can not find account." , 400 ) ;
190+ }
197191 }
198-
192+
199193 break ;
200194
201195 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" ) {
207197 throwError ( "Invalid request." , 400 ) ;
208198 } 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
210201
211- respond ( { nonce : dataFromTarget . nonce } ) ;
202+ respond ( { nonce : dataFromTarget . nonce } ) ;
203+ } catch ( e ) {
204+ throwError ( "Can not find account." , 400 ) ;
205+ }
212206 }
213207
214208 break ;
@@ -218,41 +212,48 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
218212 typeof req . body . params !== "object" ||
219213 typeof req . body . params . address !== "string" ||
220214 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 )
222216 ) {
223217 throwError ( "Invalid request." , 400 ) ;
224218 } else {
225- const storageDB = new Level ( "./log/accountStore/" + contractInfo . address ) ;
219+ try {
220+ const storageDB = new Level ( "./log/accountStore/" + req . body . params . address ) ;
226221
227- respond ( { storage : await storageDB . get ( req . body . params . key ) } ) ;
222+ respond ( { storage : await storageDB . get ( req . body . params . key ) } ) ;
228223
229- storageDB . close ( ) ;
224+ storageDB . close ( ) ;
225+ } catch ( e ) {
226+ throwError ( "Can not find storage slot." , 400 ) ;
227+ }
230228 }
231229
232230 break ;
233-
231+
234232 case "get_storageKeys" :
235233 if (
236234 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 )
238236 ) {
239237 throwError ( "Invalid request." , 400 ) ;
240238 } else {
241- const storageDB = new Level ( "./log/accountStore/" + contractInfo . address ) ;
239+ const storageDB = new Level ( "./log/accountStore/" + req . body . params . address ) ;
242240
243241 respond ( { storage : await storageDB . keys ( ) . all ( ) } ) ;
242+
243+ storageDB . close ( ) ;
244244 }
245245
246246 break ;
247247
248248 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" ) {
253250 throwError ( "Invalid request." , 400 ) ;
254251 } 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+ }
256257 }
257258
258259 break ;
@@ -265,18 +266,16 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
265266 ) {
266267 throwError ( "Invalid request." , 400 ) ;
267268 } 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 {
273270 const block = Block . deserialize ( [ ...await blockDB . get ( req . body . params . blockNumber . toString ( ) ) ] ) ;
274271
275272 if ( req . body . params . index < 0 || req . body . params . index >= block . transactions . length ) {
276273 throwError ( "Invalid transaction index." , 400 ) ;
277274 } else {
278275 respond ( { transaction : block . transactions [ req . body . params . index ] } ) ;
279276 }
277+ } catch ( e ) {
278+ throwError ( "Invalid block number." , 400 ) ;
280279 }
281280 }
282281
@@ -290,11 +289,7 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
290289 ) {
291290 throwError ( "Invalid request." , 400 ) ;
292291 } 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 {
298293 const blockNumber = parseInt ( ( await bhashDB . get ( req . body . params . hash ) ) . toString ( "hex" ) , 16 ) . toString ( ) ;
299294 const block = Block . deserialize ( [ ...await blockDB . get ( blockNumber ) ] ) ;
300295
@@ -303,6 +298,8 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
303298 } else {
304299 respond ( { transaction : block . transactions [ req . body . params . index ] } ) ;
305300 }
301+ } catch ( e ) {
302+ throwError ( "Invalid block hash." , 400 ) ;
306303 }
307304 }
308305
0 commit comments