@@ -3,20 +3,22 @@ use std::convert::TryInto;
3
3
use std:: fmt;
4
4
use std:: pin:: Pin ;
5
5
6
+ use anyhow:: anyhow;
6
7
use chrono:: { DateTime , Utc } ;
7
8
use futures:: Future ;
8
9
use jsonrpc_core:: { Error , ErrorCode } ;
9
10
use multivm:: interface:: { ExecutionResult , VmExecutionResultAndLogs , VmInterface } ;
10
11
use multivm:: vm_latest:: HistoryDisabled ;
11
12
use multivm:: vm_latest:: Vm ;
13
+ use zkevm_opcode_defs:: utils:: bytecode_to_code_hash;
12
14
use zksync_basic_types:: { H256 , U256 , U64 } ;
13
15
use zksync_state:: WriteStorage ;
14
16
use zksync_types:: api:: { BlockNumber , DebugCall , DebugCallType } ;
15
17
use zksync_types:: l2:: L2Tx ;
16
18
use zksync_types:: vm_trace:: Call ;
17
19
use zksync_types:: CONTRACT_DEPLOYER_ADDRESS ;
20
+ use zksync_utils:: bytes_to_be_words;
18
21
use zksync_utils:: u256_to_h256;
19
- use zksync_utils:: { bytecode:: hash_bytecode, bytes_to_be_words} ;
20
22
use zksync_web3_decl:: error:: Web3Error ;
21
23
22
24
use crate :: deps:: storage_view:: StorageView ;
@@ -55,13 +57,36 @@ pub fn to_human_size(input: U256) -> String {
55
57
tmp. iter ( ) . rev ( ) . collect ( )
56
58
}
57
59
58
- pub fn bytecode_to_factory_dep ( bytecode : Vec < u8 > ) -> ( U256 , Vec < U256 > ) {
59
- let bytecode_hash = hash_bytecode ( & bytecode) ;
60
+ pub fn bytes_to_chunks ( bytes : & [ u8 ] ) -> Vec < [ u8 ; 32 ] > {
61
+ bytes
62
+ . chunks ( 32 )
63
+ . map ( |el| {
64
+ let mut chunk = [ 0u8 ; 32 ] ;
65
+ chunk. copy_from_slice ( el) ;
66
+ chunk
67
+ } )
68
+ . collect ( )
69
+ }
70
+
71
+ pub fn hash_bytecode ( code : & [ u8 ] ) -> Result < H256 , anyhow:: Error > {
72
+ if code. len ( ) % 32 != 0 {
73
+ return Err ( anyhow ! ( "bytes must be divisible by 32" ) ) ;
74
+ }
75
+
76
+ let chunked_code = bytes_to_chunks ( code) ;
77
+ match bytecode_to_code_hash ( & chunked_code) {
78
+ Ok ( hash) => Ok ( H256 ( hash) ) ,
79
+ Err ( _) => Err ( anyhow ! ( "invalid bytecode" ) ) ,
80
+ }
81
+ }
82
+
83
+ pub fn bytecode_to_factory_dep ( bytecode : Vec < u8 > ) -> Result < ( U256 , Vec < U256 > ) , anyhow:: Error > {
84
+ let bytecode_hash = hash_bytecode ( & bytecode) ?;
60
85
let bytecode_hash = U256 :: from_big_endian ( bytecode_hash. as_bytes ( ) ) ;
61
86
62
87
let bytecode_words = bytes_to_be_words ( bytecode) ;
63
88
64
- ( bytecode_hash, bytecode_words)
89
+ Ok ( ( bytecode_hash, bytecode_words) )
65
90
}
66
91
67
92
/// Creates and inserts a given number of empty blocks into the node, with a given interval between them.
@@ -72,7 +97,7 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
72
97
node : & mut InMemoryNodeInner < S > ,
73
98
num_blocks : u64 ,
74
99
interval_ms : u64 ,
75
- ) {
100
+ ) -> Result < ( ) , anyhow :: Error > {
76
101
// build and insert new blocks
77
102
for i in 0 ..num_blocks {
78
103
// roll the vm
@@ -98,11 +123,11 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
98
123
99
124
vm. execute ( multivm:: interface:: VmExecutionMode :: Bootloader ) ;
100
125
101
- let bytecodes: HashMap < U256 , Vec < U256 > > = vm
102
- . get_last_tx_compressed_bytecodes ( )
103
- . iter ( )
104
- . map ( |b| bytecode_to_factory_dep ( b . original . clone ( ) ) )
105
- . collect ( ) ;
126
+ let mut bytecodes = HashMap :: new ( ) ;
127
+ for b in vm . get_last_tx_compressed_bytecodes ( ) . iter ( ) {
128
+ let hashcode = bytecode_to_factory_dep ( b . original . clone ( ) ) ? ;
129
+ bytecodes . insert ( hashcode . 0 , hashcode . 1 ) ;
130
+ }
106
131
let modified_keys = storage. borrow ( ) . modified_storage_keys ( ) . clone ( ) ;
107
132
( modified_keys, bytecodes, block_ctx)
108
133
} ;
@@ -140,6 +165,8 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
140
165
node. current_miniblock = block_ctx. miniblock ;
141
166
node. current_timestamp = block_ctx. timestamp ;
142
167
}
168
+
169
+ Ok ( ( ) )
143
170
}
144
171
145
172
/// Returns the actual [U64] block number from [BlockNumber].
@@ -265,6 +292,14 @@ pub fn into_jsrpc_error(err: Web3Error) -> Error {
265
292
}
266
293
}
267
294
295
+ pub fn into_jsrpc_error_message ( msg : String ) -> Error {
296
+ Error {
297
+ code : ErrorCode :: InternalError ,
298
+ message : msg,
299
+ data : None ,
300
+ }
301
+ }
302
+
268
303
pub fn internal_error ( method_name : & ' static str , error : impl fmt:: Display ) -> Web3Error {
269
304
tracing:: error!( "Internal error in method {method_name}: {error}" ) ;
270
305
Web3Error :: InternalError ( anyhow:: Error :: msg ( error. to_string ( ) ) )
@@ -362,7 +397,7 @@ mod tests {
362
397
363
398
{
364
399
let mut writer = inner. write ( ) . expect ( "failed acquiring write lock" ) ;
365
- mine_empty_blocks ( & mut writer, 1 , 1000 ) ;
400
+ mine_empty_blocks ( & mut writer, 1 , 1000 ) . unwrap ( ) ;
366
401
}
367
402
368
403
let reader = inner. read ( ) . expect ( "failed acquiring reader" ) ;
@@ -396,7 +431,7 @@ mod tests {
396
431
397
432
{
398
433
let mut writer = inner. write ( ) . expect ( "failed acquiring write lock" ) ;
399
- mine_empty_blocks ( & mut writer, 2 , 1000 ) ;
434
+ mine_empty_blocks ( & mut writer, 2 , 1000 ) . unwrap ( ) ;
400
435
}
401
436
402
437
let reader = inner. read ( ) . expect ( "failed acquiring reader" ) ;
@@ -439,7 +474,7 @@ mod tests {
439
474
440
475
{
441
476
let mut writer = inner. write ( ) . expect ( "failed acquiring write lock" ) ;
442
- mine_empty_blocks ( & mut writer, 2 , 1000 ) ;
477
+ mine_empty_blocks ( & mut writer, 2 , 1000 ) . unwrap ( ) ;
443
478
}
444
479
445
480
{
0 commit comments