@@ -13,50 +13,54 @@ const { JSON_RPC_METHODS } = require('./constants');
1313function sendTransactionOnNode ( node , p2pServer , args , done , isDryrun ) {
1414 const beginTime = Date . now ( ) ;
1515 const txBytesLimit = node . getBlockchainParam ( 'resource/tx_bytes_limit' ) ;
16- if ( sizeof ( args ) > txBytesLimit ) {
16+ if ( ! args . tx_body || ! args . signature ) {
17+ const latency = Date . now ( ) - beginTime ;
18+ trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
19+ done ( null , JsonRpcUtil . addProtocolVersion ( {
20+ result : null ,
21+ code : JsonRpcApiResultCode . TX_MISSING_PROPERTIES ,
22+ message : 'Missing properties.'
23+ } ) ) ;
24+ return ;
25+ }
26+ if ( sizeof ( args . tx_body ) > txBytesLimit ) {
1727 const latency = Date . now ( ) - beginTime ;
1828 trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
1929 done ( null , JsonRpcUtil . addProtocolVersion ( {
2030 result : null ,
2131 code : JsonRpcApiResultCode . TX_EXCEEDS_SIZE_LIMIT ,
2232 message : `Transaction size exceeds its limit: ${ txBytesLimit } bytes.`
2333 } ) ) ;
24- } else if ( ! args . tx_body || ! args . signature ) {
34+ return ;
35+ }
36+ const chainId = node . getBlockchainParam ( 'genesis/chain_id' ) ;
37+ const createdTx = Transaction . create ( args . tx_body , args . signature , chainId ) ;
38+ if ( ! createdTx ) {
2539 const latency = Date . now ( ) - beginTime ;
2640 trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
2741 done ( null , JsonRpcUtil . addProtocolVersion ( {
2842 result : null ,
29- code : JsonRpcApiResultCode . TX_MISSING_PROPERTIES ,
30- message : 'Missing properties .'
43+ code : JsonRpcApiResultCode . TX_INVALID_FORMAT ,
44+ message : 'Invalid transaction format .'
3145 } ) ) ;
32- } else {
33- const chainId = node . getBlockchainParam ( 'genesis/chain_id' ) ;
34- const createdTx = Transaction . create ( args . tx_body , args . signature , chainId ) ;
35- if ( ! createdTx ) {
36- const latency = Date . now ( ) - beginTime ;
37- trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
38- done ( null , JsonRpcUtil . addProtocolVersion ( {
39- result : null ,
40- code : JsonRpcApiResultCode . TX_INVALID_FORMAT ,
41- message : 'Invalid transaction format.'
42- } ) ) ;
43- } else {
44- if ( ! NodeConfigs . LIGHTWEIGHT &&
45- NodeConfigs . ENABLE_EARLY_TX_SIG_VERIF &&
46- ! Transaction . verifyTransaction ( createdTx , chainId ) ) {
47- done ( null , JsonRpcUtil . addProtocolVersion ( {
48- result : null ,
49- code : JsonRpcApiResultCode . TX_INVALID_SIGNATURE ,
50- message : 'Invalid transaction signature.'
51- } ) ) ;
52- } else {
53- const result = p2pServer . executeAndBroadcastTransaction ( createdTx , isDryrun ) ;
54- const latency = Date . now ( ) - beginTime ;
55- trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
56- done ( null , JsonRpcUtil . addProtocolVersion ( { result } ) ) ;
57- }
58- }
46+ return ;
47+ }
48+ if ( ! NodeConfigs . LIGHTWEIGHT &&
49+ NodeConfigs . ENABLE_EARLY_TX_SIG_VERIF &&
50+ ! Transaction . verifyTransaction ( createdTx , chainId ) ) {
51+ const latency = Date . now ( ) - beginTime ;
52+ trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
53+ done ( null , JsonRpcUtil . addProtocolVersion ( {
54+ result : null ,
55+ code : JsonRpcApiResultCode . TX_INVALID_SIGNATURE ,
56+ message : 'Invalid transaction signature.'
57+ } ) ) ;
58+ return ;
5959 }
60+ const result = p2pServer . executeAndBroadcastTransaction ( createdTx , isDryrun ) ;
61+ const latency = Date . now ( ) - beginTime ;
62+ trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
63+ done ( null , JsonRpcUtil . addProtocolVersion ( { result } ) ) ;
6064}
6165
6266module . exports = function getTransactionApis ( node , p2pServer ) {
@@ -149,67 +153,70 @@ module.exports = function getTransactionApis(node, p2pServer) {
149153 code : JsonRpcApiResultCode . BATCH_INVALID_FORMAT ,
150154 message : 'Invalid batch transaction format.'
151155 } ) ) ;
152- } else if ( args . tx_list . length > batchTxListSizeLimit ) {
156+ return ;
157+ }
158+ if ( args . tx_list . length > batchTxListSizeLimit ) {
153159 const latency = Date . now ( ) - beginTime ;
154160 trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
155161 done ( null , JsonRpcUtil . addProtocolVersion ( {
156162 result : null ,
157163 code : JsonRpcApiResultCode . BATCH_TX_LIST_EXCEEDS_SIZE_LIMIT ,
158164 message : `Batch transaction list size exceeds its limit: ${ batchTxListSizeLimit } .`
159165 } ) ) ;
160- } else {
161- const txBytesLimit = node . getBlockchainParam ( 'resource/tx_bytes_limit' ) ;
162- const chainId = node . getBlockchainParam ( 'genesis/chain_id' ) ;
163- const txList = [ ] ;
164- for ( let i = 0 ; i < args . tx_list . length ; i ++ ) {
165- const tx = args . tx_list [ i ] ;
166- if ( sizeof ( tx ) > txBytesLimit ) {
167- const latency = Date . now ( ) - beginTime ;
168- trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
169- done ( null , JsonRpcUtil . addProtocolVersion ( {
170- result : null ,
171- code : JsonRpcApiResultCode . BATCH_TX_EXCEEDS_SIZE_LIMIT ,
172- message : `Transaction[${ i } ]'s size exceededs its limit: ${ txBytesLimit } bytes.`
173- } ) ) ;
174- return ;
175- } else if ( ! tx . tx_body || ! tx . signature ) {
176- const latency = Date . now ( ) - beginTime ;
177- trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
178- done ( null , JsonRpcUtil . addProtocolVersion ( {
179- result : null ,
180- code : JsonRpcApiResultCode . BATCH_TX_MISSING_PROPERTIES ,
181- message : `Missing properties of transaction[${ i } ].`
182- } ) ) ;
183- return ;
184- }
185- const createdTx = Transaction . create ( tx . tx_body , tx . signature , chainId ) ;
186- if ( ! createdTx ) {
187- const latency = Date . now ( ) - beginTime ;
188- trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
189- done ( null , JsonRpcUtil . addProtocolVersion ( {
190- result : null ,
191- code : JsonRpcApiResultCode . BATCH_TX_INVALID_FORMAT ,
192- message : `Invalid format of transaction[${ i } ].`
193- } ) ) ;
194- return ;
195- }
196- if ( ! NodeConfigs . LIGHTWEIGHT &&
197- NodeConfigs . ENABLE_EARLY_TX_SIG_VERIF &&
198- ! Transaction . verifyTransaction ( createdTx , chainId ) ) {
199- done ( null , JsonRpcUtil . addProtocolVersion ( {
200- result : null ,
201- code : JsonRpcApiResultCode . BATCH_TX_INVALID_SIGNATURE ,
202- message : `Invalid signature of transaction[${ i } ].`
203- } ) ) ;
204- return ;
205- }
206- txList . push ( createdTx ) ;
166+ return ;
167+ }
168+ const txBytesLimit = node . getBlockchainParam ( 'resource/tx_bytes_limit' ) ;
169+ const chainId = node . getBlockchainParam ( 'genesis/chain_id' ) ;
170+ const txList = [ ] ;
171+ for ( let i = 0 ; i < args . tx_list . length ; i ++ ) {
172+ const tx = args . tx_list [ i ] ;
173+ if ( ! tx . tx_body || ! tx . signature ) {
174+ const latency = Date . now ( ) - beginTime ;
175+ trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
176+ done ( null , JsonRpcUtil . addProtocolVersion ( {
177+ result : null ,
178+ code : JsonRpcApiResultCode . BATCH_TX_MISSING_PROPERTIES ,
179+ message : `Missing properties of transaction[${ i } ].`
180+ } ) ) ;
181+ return ;
207182 }
208- const result = p2pServer . executeAndBroadcastTransaction ( { tx_list : txList } ) ;
209- const latency = Date . now ( ) - beginTime ;
210- trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
211- done ( null , JsonRpcUtil . addProtocolVersion ( { result } ) ) ;
183+ if ( sizeof ( tx . tx_body ) > txBytesLimit ) {
184+ const latency = Date . now ( ) - beginTime ;
185+ trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
186+ done ( null , JsonRpcUtil . addProtocolVersion ( {
187+ result : null ,
188+ code : JsonRpcApiResultCode . BATCH_TX_EXCEEDS_SIZE_LIMIT ,
189+ message : `Transaction[${ i } ]'s size exceededs its limit: ${ txBytesLimit } bytes.`
190+ } ) ) ;
191+ return ;
192+ }
193+ const createdTx = Transaction . create ( tx . tx_body , tx . signature , chainId ) ;
194+ if ( ! createdTx ) {
195+ const latency = Date . now ( ) - beginTime ;
196+ trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
197+ done ( null , JsonRpcUtil . addProtocolVersion ( {
198+ result : null ,
199+ code : JsonRpcApiResultCode . BATCH_TX_INVALID_FORMAT ,
200+ message : `Invalid format of transaction[${ i } ].`
201+ } ) ) ;
202+ return ;
203+ }
204+ if ( ! NodeConfigs . LIGHTWEIGHT &&
205+ NodeConfigs . ENABLE_EARLY_TX_SIG_VERIF &&
206+ ! Transaction . verifyTransaction ( createdTx , chainId ) ) {
207+ done ( null , JsonRpcUtil . addProtocolVersion ( {
208+ result : null ,
209+ code : JsonRpcApiResultCode . BATCH_TX_INVALID_SIGNATURE ,
210+ message : `Invalid signature of transaction[${ i } ].`
211+ } ) ) ;
212+ return ;
213+ }
214+ txList . push ( createdTx ) ;
212215 }
213- } ,
216+ const result = p2pServer . executeAndBroadcastTransaction ( { tx_list : txList } ) ;
217+ const latency = Date . now ( ) - beginTime ;
218+ trafficStatsManager . addEvent ( TrafficEventTypes . JSON_RPC_SET , latency ) ;
219+ done ( null , JsonRpcUtil . addProtocolVersion ( { result } ) ) ;
220+ }
214221 } ;
215222} ;
0 commit comments