@@ -22,8 +22,10 @@ use casper_executor_wasm_host::{
2222} ;
2323use casper_executor_wasm_interface:: {
2424 executor:: {
25- ExecuteError , ExecuteRequest , ExecuteRequestBuilder , ExecuteResult ,
26- ExecuteWithProviderError , ExecuteWithProviderResult , ExecutionKind , Executor , SystemMenu ,
25+ AuctionMethods , ControlMethods , CryptoMethods , EmitMethods , ExecuteError , ExecuteRequest ,
26+ ExecuteRequestBuilder , ExecuteResult , ExecuteWithProviderError , ExecuteWithProviderResult ,
27+ ExecutionKind , Executor , FFIMenu , GlobalStateMethods , IOMethods , MintMethods ,
28+ SystemContractMenu ,
2729 } ,
2830 sandboxed_execution:: {
2931 SandboxedExecutionError , SandboxedExecutionRequest , SandboxedExecutionResult ,
@@ -53,13 +55,14 @@ use casper_types::{
5355 AddressableEntity , AuctionCosts , ByteCode , ByteCodeAddr , ByteCodeHash , ByteCodeKind , CLType ,
5456 CLValue , Contract , ContractRuntimeTag , ContractWasmHash , Digest , EntityAddr , EntityKind ,
5557 EntryPointAccess , EntryPointAddr , EntryPointPayment , EntryPointType , EntryPointValue , Gas ,
56- Groups , InitiatorAddr , Key , MessageLimits , MintCosts , NamedKeys , Package , PackageAddr ,
57- PackageStatus , Parameters , Phase , ProtocolVersion , StorageCosts , StoredValue , TransactionHash ,
58- TransactionInvocationTarget , URef , WasmV2Config , NAME_FOR_V2_CONTRACT_MAIN_PURSE ,
58+ Groups , HostFFIFunctionCost , InitiatorAddr , Key , MessageLimits , MintCosts , NamedKeys , Package ,
59+ PackageAddr , PackageStatus , Parameters , Phase , ProtocolVersion , StorageCosts , StoredValue ,
60+ TransactionHash , TransactionInvocationTarget , URef , WasmV2Config ,
61+ NAME_FOR_V2_CONTRACT_MAIN_PURSE ,
5962} ;
6063use install:: { InstallContractError , InstallContractRequest , InstallContractResult } ;
6164use parking_lot:: RwLock ;
62- use tracing:: { debug, error, info , warn} ;
65+ use tracing:: { debug, error, warn} ;
6366
6467#[ cfg( any( feature = "testing" , test) ) ]
6568pub mod chainspec_config;
@@ -529,9 +532,9 @@ impl ExecutorV2 {
529532 }
530533 }
531534
532- fn execute_system_contract < R : GlobalStateReader + ' static > (
535+ fn execute_ffi < R : GlobalStateReader + ' static > (
533536 & self ,
534- menu_selection : SystemMenu ,
537+ menu_selection : SystemContractMenu ,
535538 tracking_copy : TrackingCopy < R > ,
536539 execute_request : ExecuteRequest ,
537540 ) -> Result < ExecuteResult , ExecuteError > {
@@ -542,16 +545,10 @@ impl ExecutorV2 {
542545 input,
543546 transaction_hash,
544547 address_generator,
545- sandboxed,
546548 runtime_native_config,
547549 ..
548550 } = execute_request;
549551
550- if sandboxed {
551- info ! ( "attempt to call system contract while sandboxed" ) ;
552- return Err ( ExecuteError :: SandboxedSystemContractCall ) ;
553- }
554-
555552 let gas_usage = GasUsage :: new ( gas_limit, gas_limit) ;
556553
557554 native_exec :: < TransferArgs , ( ) , R > (
@@ -572,13 +569,8 @@ impl ExecutorV2 {
572569 mut tracking_copy : TrackingCopy < R > ,
573570 execute_request : ExecuteRequest ,
574571 ) -> Result < ExecuteResult , ExecuteError > {
575- if let Some ( system_menu_selection) = execute_request. execution_kind . system_menu_selection ( )
576- {
577- return self . execute_system_contract (
578- system_menu_selection,
579- tracking_copy,
580- execute_request,
581- ) ;
572+ if let Some ( ffi_menu_selection) = execute_request. execution_kind . ffi_selection ( ) {
573+ return self . execute_ffi ( ffi_menu_selection, tracking_copy, execute_request) ;
582574 }
583575
584576 let ExecuteRequest {
@@ -937,13 +929,11 @@ impl ExecutorV2 {
937929 return Err ( ExecuteError :: Fatal ( FatalHostError :: DispatchSystemContract ) ) ;
938930 }
939931 } ;
940-
932+ let ffi_call_costs = self . build_ffi_call_costs ( & self . config ) ;
941933 let context = Context {
942934 initiator,
943935 config : self . config . wasm_config ,
944936 storage_costs : self . config . storage_costs ,
945- mint_costs : self . config . mint_costs ,
946- auction_costs : self . config . auction_costs ,
947937 baseline_motes_amount : self . config . baseline_motes_amount ,
948938 caller : caller_key,
949939 callee : callee_key,
@@ -960,6 +950,7 @@ impl ExecutorV2 {
960950 parent_block_hash : parent_block_hash. inner ( ) . value ( ) ,
961951 block_height,
962952 authorization_keys,
953+ ffi_call_costs,
963954 } ;
964955
965956 // Check that the input argument size does not exceed the VM memory limit
@@ -1102,6 +1093,87 @@ impl ExecutorV2 {
11021093 }
11031094 }
11041095 }
1096+
1097+ fn build_ffi_call_costs ( & self , config : & ExecutorConfig ) -> BTreeMap < u32 , HostFFIFunctionCost > {
1098+ FFIMenu :: all_ffi_options ( )
1099+ . map ( |ffi_opt| {
1100+ let ffi_function_cost = match & ffi_opt {
1101+ FFIMenu :: Mint ( mint_methods) => {
1102+ let base_cost = match mint_methods {
1103+ MintMethods :: Burn => config. mint_costs . burn ,
1104+ MintMethods :: Transfer => config. mint_costs . transfer ,
1105+ MintMethods :: TransferPurse => config. mint_costs . transfer ,
1106+ } ;
1107+ HostFFIFunctionCost :: fixed ( base_cost as u64 )
1108+ }
1109+ FFIMenu :: Auction ( auction_methods) => {
1110+ let base_cost = match auction_methods {
1111+ AuctionMethods :: Activate => config. auction_costs . activate_bid ,
1112+ AuctionMethods :: Bid => config. auction_costs . add_bid ,
1113+ AuctionMethods :: Withdraw => config. auction_costs . withdraw_bid ,
1114+ AuctionMethods :: Delegate => config. auction_costs . delegate ,
1115+ AuctionMethods :: Undelegate => config. auction_costs . undelegate ,
1116+ AuctionMethods :: Redelegate => config. auction_costs . redelegate ,
1117+ AuctionMethods :: AddReservation => config. auction_costs . add_reservations ,
1118+ AuctionMethods :: CancelReservation => {
1119+ config. auction_costs . cancel_reservations
1120+ }
1121+ AuctionMethods :: ChangePublicKey => {
1122+ config. auction_costs . change_bid_public_key
1123+ }
1124+ } ;
1125+ HostFFIFunctionCost :: fixed ( base_cost)
1126+ }
1127+ FFIMenu :: Crypto ( crypto_methods) => match crypto_methods {
1128+ CryptoMethods :: AltBn128Add => {
1129+ config. wasm_config . host_ffi_opt_costs ( ) . alt_bn128_add
1130+ }
1131+ CryptoMethods :: AltBn128Multiply => {
1132+ config. wasm_config . host_ffi_opt_costs ( ) . alt_bn128_mul
1133+ }
1134+ CryptoMethods :: AltBn128Pairing => {
1135+ config. wasm_config . host_ffi_opt_costs ( ) . alt_bn128_pairing
1136+ }
1137+ CryptoMethods :: GenericHash => {
1138+ config. wasm_config . host_ffi_opt_costs ( ) . generic_hash
1139+ }
1140+ CryptoMethods :: RecoverSecp256K1 => {
1141+ config. wasm_config . host_ffi_opt_costs ( ) . recover_secp256k1
1142+ }
1143+ } ,
1144+ FFIMenu :: Emit ( emit_methods) => match emit_methods {
1145+ EmitMethods :: PrintStd => config. wasm_config . host_ffi_opt_costs ( ) . print ,
1146+ EmitMethods :: Native => config. wasm_config . host_ffi_opt_costs ( ) . emit ,
1147+ } ,
1148+ FFIMenu :: GlobalState ( global_state_methods) => match global_state_methods {
1149+ GlobalStateMethods :: Read => config. wasm_config . host_ffi_opt_costs ( ) . read ,
1150+ GlobalStateMethods :: Write => config. wasm_config . host_ffi_opt_costs ( ) . write ,
1151+ GlobalStateMethods :: Remove => {
1152+ config. wasm_config . host_ffi_opt_costs ( ) . remove
1153+ }
1154+ GlobalStateMethods :: GetBalance => {
1155+ config. wasm_config . host_ffi_opt_costs ( ) . env_balance
1156+ }
1157+ GlobalStateMethods :: GetInfo => {
1158+ config. wasm_config . host_ffi_opt_costs ( ) . env_info
1159+ }
1160+ GlobalStateMethods :: Create => {
1161+ config. wasm_config . host_ffi_opt_costs ( ) . create
1162+ }
1163+ } ,
1164+ FFIMenu :: Control ( control_methods) => match control_methods {
1165+ ControlMethods :: Call => config. wasm_config . host_ffi_opt_costs ( ) . call ,
1166+ ControlMethods :: Upgrade => config. wasm_config . host_ffi_opt_costs ( ) . upgrade ,
1167+ } ,
1168+ FFIMenu :: IO ( iomethods) => match iomethods {
1169+ IOMethods :: Return => config. wasm_config . host_ffi_opt_costs ( ) . ret ,
1170+ IOMethods :: CopyInput => config. wasm_config . host_ffi_opt_costs ( ) . copy_input ,
1171+ } ,
1172+ } ;
1173+ ( ffi_opt. into ( ) , ffi_function_cost)
1174+ } )
1175+ . collect ( )
1176+ }
11051177 #[ allow( clippy:: too_many_arguments) ]
11061178 fn execute_vm1_wasm_byte_code < R > (
11071179 & self ,
@@ -1354,6 +1426,7 @@ impl Executor for ExecutorV2 {
13541426 CallError :: CalleeGasDepleted => SandboxedExecutionError :: CalleeGasDepleted ,
13551427 CallError :: NotCallable => SandboxedExecutionError :: NotCallable ,
13561428 CallError :: Api ( api_error) => SandboxedExecutionError :: Api ( api_error) ,
1429+ CallError :: InputInvalid => SandboxedExecutionError :: InputInvalid ,
13571430 } ) ,
13581431 output : output_bytes. map ( |x| x. into ( ) ) ,
13591432 gas_usage : Gas :: new ( execute_result. gas_usage . gas_spent ( ) ) ,
0 commit comments