@@ -17,7 +17,6 @@ use fvm_shared::{
1717 MethodNum , Response , IPLD_RAW , METHOD_SEND ,
1818} ;
1919use multihash:: Code ;
20- use once_cell:: unsync:: OnceCell ;
2120
2221use crate :: state:: { State , Tombstone } ;
2322use crate :: BytecodeHash ;
@@ -29,33 +28,27 @@ use {
2928 fvm_ipld_kamt:: { AsHashedKey , Config as KamtConfig , Kamt } ,
3029} ;
3130
32- lazy_static:: lazy_static! {
33- // The Solidity compiler creates contiguous array item keys.
34- // To prevent the tree from going very deep we use extensions,
35- // which the Kamt supports and does in all cases.
36- //
37- // There are maximum 32 levels in the tree with the default bit width of 8.
38- // The top few levels will have a higher level of overlap in their hashes.
39- // Intuitively these levels should be used for routing, not storing data.
40- //
41- // The only exception to this is the top level variables in the contract
42- // which solidity puts in the first few slots. There having to do extra
43- // lookups is burdensome, and they will always be accessed even for arrays
44- // because that's where the array length is stored.
45- //
46- // However, for Solidity, the size of the KV pairs is 2x256, which is
47- // comparable to a size of a CID pointer plus extension metadata.
48- // We can keep the root small either by force-pushing data down,
49- // or by not allowing many KV pairs in a slot.
50- //
51- // The following values have been set by looking at how the charts evolved
52- // with the test contract. They might not be the best for other contracts.
53- static ref KAMT_CONFIG : KamtConfig = KamtConfig {
54- min_data_depth: 0 ,
55- bit_width: 5 ,
56- max_array_width: 1
57- } ;
58- }
31+ // The Solidity compiler creates contiguous array item keys.
32+ // To prevent the tree from going very deep we use extensions,
33+ // which the Kamt supports and does in all cases.
34+ //
35+ // There are maximum 32 levels in the tree with the default bit width of 8.
36+ // The top few levels will have a higher level of overlap in their hashes.
37+ // Intuitively these levels should be used for routing, not storing data.
38+ //
39+ // The only exception to this is the top level variables in the contract
40+ // which solidity puts in the first few slots. There having to do extra
41+ // lookups is burdensome, and they will always be accessed even for arrays
42+ // because that's where the array length is stored.
43+ //
44+ // However, for Solidity, the size of the KV pairs is 2x256, which is
45+ // comparable to a size of a CID pointer plus extension metadata.
46+ // We can keep the root small either by force-pushing data down,
47+ // or by not allowing many KV pairs in a slot.
48+ //
49+ // The following values have been set by looking at how the charts evolved
50+ // with the test contract. They might not be the best for other contracts.
51+ const KAMT_CONFIG : KamtConfig = KamtConfig { min_data_depth : 0 , bit_width : 5 , max_array_width : 1 } ;
5952
6053pub struct StateHashAlgorithm ;
6154
@@ -109,7 +102,7 @@ pub struct System<'r, RT: Runtime> {
109102 /// Read Only context (staticcall)
110103 pub readonly : bool ,
111104 /// Randomness taken from the current epoch of chain randomness
112- randomness : OnceCell < [ u8 ; 32 ] > ,
105+ randomness : Option < [ u8 ; 32 ] > ,
113106
114107 /// This is "some" if the actor is currently a "zombie". I.e., it has selfdestructed, but the
115108 /// current message is still executing. `System` cannot load a contracts state with a
@@ -129,7 +122,7 @@ impl<'r, RT: Runtime> System<'r, RT> {
129122 saved_state_root : None ,
130123 bytecode : None ,
131124 readonly,
132- randomness : OnceCell :: new ( ) ,
125+ randomness : None ,
133126 tombstone : None ,
134127 }
135128 }
@@ -197,7 +190,7 @@ impl<'r, RT: Runtime> System<'r, RT> {
197190 saved_state_root : Some ( state_root) ,
198191 bytecode : Some ( EvmBytecode :: new ( state. bytecode , state. bytecode_hash ) ) ,
199192 readonly : read_only,
200- randomness : OnceCell :: new ( ) ,
193+ randomness : None ,
201194 tombstone : state. tombstone ,
202195 } )
203196 }
@@ -441,14 +434,15 @@ impl<'r, RT: Runtime> System<'r, RT> {
441434 /// Gets the cached EVM randomness seed of the current epoch
442435 pub fn get_randomness ( & mut self ) -> Result < & [ u8 ; 32 ] , ActorError > {
443436 const ENTROPY : & [ u8 ] = b"prevrandao" ;
444- self . randomness . get_or_try_init ( || {
437+ match & mut self . randomness {
438+ Some ( rand) => Ok ( & * rand) ,
445439 // get randomness from current beacon epoch with entropy of "prevrandao"
446- self . rt . get_randomness_from_beacon (
440+ cache => Ok ( cache . insert ( self . rt . get_randomness_from_beacon (
447441 fil_actors_runtime:: runtime:: DomainSeparationTag :: EvmPrevRandao ,
448442 self . rt . curr_epoch ( ) ,
449443 ENTROPY ,
450- )
451- } )
444+ ) ? ) ) ,
445+ }
452446 }
453447
454448 /// Mark ourselves as "selfdestructed".
0 commit comments