@@ -104,12 +104,11 @@ impl<'db> ApplyEnv<'db> {
104104 Transaction < ' db , TransactionDB < MultiThreaded > > ,
105105 Vec < consensus_muts:: Mutation > ,
106106 Vec < consensus_muts:: Mutation > ,
107- Vec < HashMap < String , String > > ,
108107 Vec < protocol:: ExecutionReceipt > ,
109108 [ u8 ; 32 ] ,
110109 [ u8 ; 32 ] ,
111110 ) {
112- ( self . txn , self . muts_final , self . muts_final_rev , self . result_log , self . receipts , root_receipts, root_contractstate)
111+ ( self . txn , self . muts_final , self . muts_final_rev , self . receipts , root_receipts, root_contractstate)
113112 }
114113}
115114
@@ -155,28 +154,37 @@ pub fn set_apply_env_tx<'db>(env: &mut ApplyEnv<'db>, tx_hash: &[u8; 32], tx_sig
155154 env. caller_env . account_origin = tx_signer. to_vec ( ) ;
156155}
157156
158- pub fn apply_entry < ' db , ' a > ( db : & ' db TransactionDB < MultiThreaded > , pk : & [ u8 ] , sk : & [ u8 ] ,
159- entry_signer : & [ u8 ; 48 ] , entry_prev_hash : & [ u8 ; 32 ] ,
160- entry_slot : u64 , entry_prev_slot : u64 , entry_height : u64 , entry_epoch : u64 ,
161- entry_vr : & [ u8 ; 96 ] , entry_vr_b3 : & [ u8 ; 32 ] , entry_dr : & [ u8 ; 32 ] ,
162- txus : Vec < rustler:: Term < ' a > > , txn : Transaction < ' db , TransactionDB < MultiThreaded > > ,
157+ pub fn apply_entry < ' db , ' a > ( db : & ' db TransactionDB < MultiThreaded > , txn : Transaction < ' db , TransactionDB < MultiThreaded > > ,
158+ entry : crate :: model:: entry:: Entry , pk : & [ u8 ] , sk : & [ u8 ] ,
163159 testnet : bool , testnet_peddlebikes : Vec < Vec < u8 > > ,
164- ) -> ( Transaction < ' db , TransactionDB < MultiThreaded > > , Vec < consensus_muts:: Mutation > , Vec < consensus_muts:: Mutation > , Vec < HashMap < String , String > > , Vec < protocol:: ExecutionReceipt > , [ u8 ; 32 ] , [ u8 ; 32 ] ) {
160+ ) -> ( Transaction < ' db , TransactionDB < MultiThreaded > > , Vec < consensus_muts:: Mutation > , Vec < consensus_muts:: Mutation > , Vec < protocol:: ExecutionReceipt > , [ u8 ; 32 ] , [ u8 ; 32 ] ) {
165161 let cf_h = db. cf_handle ( "contractstate" ) . unwrap ( ) ;
166162 let cf2_h = db. cf_handle ( "contractstate" ) . unwrap ( ) ;
167163 let cf_tree_h = db. cf_handle ( "contractstate_tree" ) . unwrap ( ) ;
164+
165+ let entry_signer = entry. header . signer . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "entry_signer_len_wrong" ) ) ;
166+ let entry_prev_hash = entry. header . prev_hash . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "entry_prev_hash_len_wrong" ) ) ;
167+ let entry_vr = entry. header . vr . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "entry_vr_len_wrong" ) ) ;
168+ let entry_vr_b3_binding = blake3:: hash ( & entry. header . vr ) ;
169+ let entry_vr_b3 = entry_vr_b3_binding. as_bytes ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "entry_vr_len_wrong" ) ) ;
170+ let entry_dr = entry. header . dr . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "entry_dr_len_wrong" ) ) ;
171+
172+
173+ let entry_epoch = entry. header . height / 100_000 ;
168174 let mut applyenv = make_apply_env ( db, txn, cf_h, b"contractstate" . to_vec ( ) , cf2_h, cf_tree_h,
169- entry_signer, entry_prev_hash, entry_slot, entry_prev_slot, entry_height, entry_epoch, entry_vr, entry_vr_b3, entry_dr,
175+ entry_signer, entry_prev_hash, entry. header . slot , entry. header . prev_slot , entry. header . height ,
176+ entry_epoch, entry_vr, entry_vr_b3, entry_dr,
170177 testnet, testnet_peddlebikes) ;
171178
172- call_txs_pre_upfront_cost ( & mut applyenv, & txus ) ;
179+ call_txs_pre_upfront_cost ( & mut applyenv, & entry . txs ) ;
173180
174- for ( i, txu) in txus. clone ( ) . into_iter ( ) . enumerate ( ) {
175- let tx_hash = crate :: fixed :: < 32 > ( txu. map_get ( crate :: atoms:: hash ( ) ) . unwrap ( ) ) . unwrap ( ) ;
176- let tx = txu. map_get ( crate :: atoms:: tx ( ) ) . unwrap ( ) ;
177- let tx_signer = crate :: fixed :: < 48 > ( tx. map_get ( crate :: atoms:: signer ( ) ) . unwrap ( ) ) . unwrap ( ) ;
178- let tx_nonce = tx. map_get ( crate :: atoms:: nonce ( ) ) . unwrap ( ) . decode :: < u64 > ( ) . unwrap ( ) ;
179- let action = tx. map_get ( crate :: atoms:: action ( ) ) . unwrap ( ) . decode :: < rustler:: Term < ' a > > ( ) . unwrap ( ) ;
181+ for ( i, txu) in entry. txs . clone ( ) . into_iter ( ) . enumerate ( ) {
182+ let tx_historical_cost = crate :: consensus:: bic:: protocol:: tx_historical_cost ( & txu) ;
183+
184+ let tx_hash = txu. hash . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "tx_hash_len_wrong" ) ) ;
185+ let tx_signer = txu. tx . signer . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "tx_signer_len_wrong" ) ) ;
186+ let tx_nonce = txu. tx . nonce ;
187+ let action = txu. tx . action ;
180188
181189 applyenv. caller_env . tx_index = i as u64 ;
182190 applyenv. caller_env . tx_hash = tx_hash;
@@ -186,24 +194,11 @@ pub fn apply_entry<'db, 'a>(db: &'db TransactionDB<MultiThreaded>, pk: &[u8], sk
186194 applyenv. caller_env . account_caller = tx_signer. to_vec ( ) ;
187195
188196 //let op = action.map_get(crate::atoms::op()).unwrap().decode::<rustler::Binary>().unwrap().as_slice();
189- let contract = action. map_get ( crate :: atoms:: contract ( ) ) . unwrap ( ) . decode :: < rustler:: Binary > ( ) . unwrap ( ) . to_vec ( ) ;
190- let function = action. map_get ( crate :: atoms:: function ( ) ) . unwrap ( ) . decode :: < rustler:: Binary > ( ) . unwrap ( ) . to_vec ( ) ;
191-
192- let args = action. map_get ( crate :: atoms:: args ( ) ) . unwrap ( ) . decode :: < Vec < rustler:: Binary > > ( ) . unwrap ( ) . into_iter ( ) . map ( |b| b. as_slice ( ) . to_vec ( ) ) . collect ( ) ;
193- let attached_symbol = match action. map_get ( crate :: atoms:: attached_symbol ( ) ) . ok ( ) {
194- None => None ,
195- Some ( t) => match t. decode :: < Option < rustler:: Binary > > ( ) . ok ( ) . flatten ( ) {
196- None => None ,
197- Some ( bin) => Some ( bin. as_slice ( ) . to_vec ( ) ) ,
198- } ,
199- } ;
200- let attached_amount = match action. map_get ( crate :: atoms:: attached_amount ( ) ) . ok ( ) {
201- None => None ,
202- Some ( t) => match t. decode :: < Option < rustler:: Binary > > ( ) . ok ( ) . flatten ( ) {
203- None => None ,
204- Some ( bin) => Some ( bin. as_slice ( ) . to_vec ( ) ) ,
205- } ,
206- } ;
197+ let contract = action. contract ;
198+ let function = action. function ;
199+ let args = action. args ;
200+ let attached_symbol = action. attached_symbol . clone ( ) ;
201+ let attached_amount = action. attached_amount . clone ( ) ;
207202
208203 applyenv. caller_env . account_current = contract. to_vec ( ) ;
209204 applyenv. muts = Vec :: new ( ) ;
@@ -234,7 +229,6 @@ pub fn apply_entry<'db, 'a>(db: &'db TransactionDB<MultiThreaded>, pk: &[u8], sk
234229
235230 applyenv. exec_track = false ;
236231
237- let tx_historical_cost = txu. map_get ( crate :: atoms:: tx_historical_cost ( ) ) . unwrap ( ) . decode :: < i128 > ( ) . unwrap ( ) ;
238232 let exec_cost_total = ( ( tx_historical_cost + ( applyenv. exec_max - applyenv. exec_left ) + ( applyenv. storage_max - applyenv. storage_left ) ) as u64 ) . to_string ( ) ;
239233
240234 match res {
@@ -318,7 +312,7 @@ pub fn apply_entry<'db, 'a>(db: &'db TransactionDB<MultiThreaded>, pk: &[u8], sk
318312
319313 call_exit ( & mut applyenv) ;
320314
321- let root_receipts = root_receipts ( txus . clone ( ) , applyenv. result_log . clone ( ) ) ;
315+ let root_receipts = root_receipts ( entry . txs . clone ( ) , applyenv. result_log . clone ( ) ) ;
322316 let root_contractstate = update_and_root_contractstate ( & mut applyenv) ;
323317
324318 //println!("r{:?} {}", applyenv.caller_env.entry_height, root_receipts(txus.clone(), applyenv.result_log.clone()).iter().map(|b| format!("{:02x}", b)).collect::<String>() );
@@ -376,15 +370,15 @@ fn update_and_root_contractstate(applyenv: &mut ApplyEnv) -> [u8; 32] {
376370 root_contractstate
377371}
378372
379- fn root_receipts ( txus : Vec < rustler :: Term > , result_log : Vec < HashMap < String , String > > ) -> [ u8 ; 32 ] {
373+ fn root_receipts ( txus : Vec < crate :: model :: tx :: TXU > , result_log : Vec < HashMap < String , String > > ) -> [ u8 ; 32 ] {
380374 use sha2:: { Sha256 , Digest } ;
381375 let mut hubt = bintree:: Hubt :: new ( ) ;
382376 let mut kvs = Vec :: new ( ) ;
383377
384378 let count = txus. len ( ) ;
385379
386380 for ( txu, log) in txus. into_iter ( ) . zip ( result_log. into_iter ( ) ) {
387- let tx_hash = crate :: fixed :: < 32 > ( txu. map_get ( crate :: atoms :: hash ( ) ) . unwrap ( ) ) . unwrap ( ) ;
381+ let tx_hash = txu. hash ;
388382
389383 let error = log. get ( "error" )
390384 . expect ( "no_error_key_in_receipt" )
@@ -476,27 +470,22 @@ fn refund_exec_storage_deposit(applyenv: &mut ApplyEnv) {
476470 applyenv. muts_final_rev . append ( & mut applyenv. muts_rev ) ;
477471}
478472
479- fn call_txs_pre_upfront_cost < ' a > ( env : & mut ApplyEnv , txus : & [ rustler :: Term < ' a > ] ) {
473+ fn call_txs_pre_upfront_cost < ' a > ( env : & mut ApplyEnv , txus : & [ crate :: model :: tx :: TXU ] ) {
480474 env. muts = Vec :: new ( ) ;
481475 env. muts_rev = Vec :: new ( ) ;
482476 for txu in txus {
483- let tx_hash = crate :: fixed :: < 32 > ( txu. map_get ( crate :: atoms:: hash ( ) ) . unwrap ( ) ) . unwrap ( ) ;
484- let tx = txu. map_get ( crate :: atoms:: tx ( ) ) . unwrap ( ) ;
485- let tx_signer = crate :: fixed :: < 48 > ( tx. map_get ( crate :: atoms:: signer ( ) ) . unwrap ( ) ) . unwrap ( ) ;
486- let tx_nonce = tx. map_get ( crate :: atoms:: nonce ( ) ) . unwrap ( ) . decode :: < u64 > ( ) . unwrap ( ) ;
477+ let tx_hash = txu. hash . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "tx_hash_len_wrong" ) ) ;
478+ let tx_signer = txu. tx . signer . as_slice ( ) . try_into ( ) . unwrap_or_else ( |_| panic ! ( "tx_signer_len_wrong" ) ) ;
479+ let tx_nonce = txu. tx . nonce ;
487480
488481 set_apply_env_tx ( env, & tx_hash, & tx_signer, tx_nonce) ;
489482
490483 // Update nonce
491484 consensus_kv:: kv_put ( env, & crate :: bcat ( & [ b"account:" , & tx_signer, b":attribute:nonce" ] ) , & tx_nonce. to_string ( ) . into_bytes ( ) ) ;
492485
493486 // Deduct tx historical cost
494- let tx_historical_cost = txu . map_get ( crate :: atoms :: tx_historical_cost ( ) ) . unwrap ( ) . decode :: < i128 > ( ) . unwrap ( ) ;
487+ let tx_historical_cost = crate :: consensus :: bic :: protocol :: tx_historical_cost ( txu ) ;
495488 protocol:: pay_cost ( env, tx_historical_cost) ;
496- // Deduct fixed call costs
497- let action = tx. map_get ( crate :: atoms:: action ( ) ) . unwrap ( ) . decode :: < rustler:: Term < ' a > > ( ) . unwrap ( ) ;
498- let contract = action. map_get ( crate :: atoms:: contract ( ) ) . unwrap ( ) . decode :: < rustler:: Binary > ( ) . unwrap ( ) . to_vec ( ) ;
499- let function = action. map_get ( crate :: atoms:: function ( ) ) . unwrap ( ) . decode :: < rustler:: Binary > ( ) . unwrap ( ) . to_vec ( ) ;
500489
501490 //lock 0.1 AMA during execution
502491 consensus_kv:: kv_increment ( env, & crate :: bcat ( & [ b"account:" , & env. caller_env . account_origin , b":balance:AMA" ] ) , -protocol:: AMA_10_CENT ) ;
@@ -634,7 +623,6 @@ fn migrate_db(env: &mut ApplyEnv) {
634623 }
635624}
636625
637-
638626pub fn valid_bic_action ( contract : Vec < u8 > , function : Vec < u8 > ) -> bool {
639627 let c = contract. as_slice ( ) ;
640628 let f = function. as_slice ( ) ;
0 commit comments