@@ -165,19 +165,33 @@ impl reth_codecs::Compact for Bytecode {
165
165
166
166
use compact_ids:: * ;
167
167
168
- let len = buf. read_u32 :: < byteorder:: BigEndian > ( ) . expect ( "could not read bytecode length" ) ;
169
- let bytes = Bytes :: from ( buf. copy_to_bytes ( len as usize ) ) ;
168
+ let len = buf. read_u32 :: < byteorder:: BigEndian > ( ) . expect ( "could not read bytecode length" )
169
+ as usize ;
170
+ let bytes = Bytes :: from ( buf. copy_to_bytes ( len) ) ;
170
171
let variant = buf. read_u8 ( ) . expect ( "could not read bytecode variant" ) ;
171
172
let decoded = match variant {
172
173
LEGACY_RAW_BYTECODE_ID => Self ( RevmBytecode :: new_raw ( bytes) ) ,
173
174
REMOVED_BYTECODE_ID => {
174
175
unreachable ! ( "Junk data in database: checked Bytecode variant was removed" )
175
176
}
176
- LEGACY_ANALYZED_BYTECODE_ID => Self ( RevmBytecode :: new_analyzed (
177
- bytes,
178
- buf. read_u64 :: < byteorder:: BigEndian > ( ) . unwrap ( ) as usize ,
179
- revm_bytecode:: JumpTable :: from_slice ( buf) ,
180
- ) ) ,
177
+ LEGACY_ANALYZED_BYTECODE_ID => {
178
+ let original_len = buf. read_u64 :: < byteorder:: BigEndian > ( ) . unwrap ( ) as usize ;
179
+ // When saving jumptable, its length is getting aligned to u8 boundary. Thus, we
180
+ // need to re-calculate the internal length of bitvec and truncate it when loading
181
+ // jumptables to avoid inconsistencies during `Compact` roundtrip.
182
+ let jump_table_len = if buf. len ( ) * 8 >= bytes. len ( ) {
183
+ // Use length of padded bytecode if we can fit it
184
+ bytes. len ( )
185
+ } else {
186
+ // Otherwise, use original_len
187
+ original_len
188
+ } ;
189
+ Self ( RevmBytecode :: new_analyzed (
190
+ bytes,
191
+ original_len,
192
+ revm_bytecode:: JumpTable :: from_slice ( buf, jump_table_len) ,
193
+ ) )
194
+ }
181
195
EOF_BYTECODE_ID | EIP7702_BYTECODE_ID => {
182
196
// EOF and EIP-7702 bytecode objects will be decoded from the raw bytecode
183
197
Self ( RevmBytecode :: new_raw ( bytes) )
@@ -293,7 +307,7 @@ mod tests {
293
307
let bytecode = Bytecode ( RevmBytecode :: LegacyAnalyzed ( LegacyAnalyzedBytecode :: new (
294
308
Bytes :: from ( & hex ! ( "ff00" ) ) ,
295
309
2 ,
296
- JumpTable :: from_slice ( & [ 0 ] ) ,
310
+ JumpTable :: from_slice ( & [ 0 ] , 2 ) ,
297
311
) ) ) ;
298
312
let len = bytecode. to_compact ( & mut buf) ;
299
313
assert_eq ! ( len, 16 ) ;
0 commit comments