Skip to content

Commit d39e797

Browse files
authored
Merge pull request #5347 from zajko/vm2_single_ffi
[VM2] One FFI
2 parents 2f496d3 + e4cc978 commit d39e797

File tree

60 files changed

+3378
-3491
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3378
-3491
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

binary_port/src/sandboxed_execution.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub enum SandboxedExecutionError {
2222
InternalHostError,
2323
/// Api error occurred.
2424
Api(String),
25+
/// Input invalid
26+
InputInvalid,
2527
}
2628

2729
impl core::fmt::Display for SandboxedExecutionError {
@@ -34,6 +36,7 @@ impl core::fmt::Display for SandboxedExecutionError {
3436
SandboxedExecutionError::CodeNotFound => write!(f, "contract code not found"),
3537
SandboxedExecutionError::InternalHostError => write!(f, "internal host error"),
3638
SandboxedExecutionError::Api(api_error) => write!(f, "{}", api_error),
39+
SandboxedExecutionError::InputInvalid => write!(f, "input invalid"),
3740
}
3841
}
3942
}

execution_engine_testing/tests/src/test/contract_messages.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use casper_types::{
1313
bytesrepr::ToBytes,
1414
contract_messages::{MessageChecksum, MessagePayload, MessageTopicSummary, TopicNameHash},
1515
runtime_args, AddressableEntityHash, BlockGlobalAddr, BlockTime, CLValue, CoreConfig, Digest,
16-
EntityAddr, HashAddr, HostFunction, HostFunctionCostsV1, HostFunctionCostsV2, Key,
16+
EntityAddr, HashAddr, HostFFIFunctionCosts, HostFunction, HostFunctionCostsV1, Key,
1717
MessageLimits, OpcodeCosts, PublicKey, RuntimeArgs, StorageCosts, StoredValue, SystemConfig,
1818
WasmConfig, WasmV1Config, WasmV2Config, DEFAULT_MAX_STACK_HEIGHT, DEFAULT_WASM_MAX_MEMORY,
1919
U512,
@@ -551,7 +551,7 @@ fn should_not_exceed_configured_limits() {
551551
let wasm_v2_config = WasmV2Config::new(
552552
default_wasm_v2_config.max_memory(),
553553
default_wasm_v2_config.opcode_costs(),
554-
default_wasm_v2_config.take_host_function_costs(),
554+
default_wasm_v2_config.take_host_ffi_opt_costs(),
555555
);
556556
let wasm_config = WasmConfig::new(
557557
MessageLimits {
@@ -729,7 +729,7 @@ fn should_charge_expected_gas_for_storage() {
729729
let wasm_v2_config = WasmV2Config::new(
730730
DEFAULT_WASM_MAX_MEMORY,
731731
OpcodeCosts::zero(),
732-
HostFunctionCostsV2::zero(),
732+
HostFFIFunctionCosts::zero(),
733733
);
734734
let wasm_config = WasmConfig::new(MessageLimits::default(), wasm_v1_config, wasm_v2_config);
735735
ChainspecConfig {
@@ -850,7 +850,7 @@ fn should_charge_increasing_gas_consumed_for_multiple_messages_emitted() {
850850
let wasm_v2_config = WasmV2Config::new(
851851
DEFAULT_WASM_MAX_MEMORY,
852852
OpcodeCosts::zero(),
853-
HostFunctionCostsV2::default(),
853+
HostFFIFunctionCosts::default(),
854854
);
855855
let wasm_config = WasmConfig::new(MessageLimits::default(), wasm_v1_config, wasm_v2_config);
856856
ChainspecConfig {
@@ -970,7 +970,7 @@ fn should_not_exceed_configured_topic_name_limits_on_contract_upgrade_no_init()
970970
let wasm_v2_config = WasmV2Config::new(
971971
default_wasm_v2_config.max_memory(),
972972
default_wasm_v2_config.opcode_costs(),
973-
default_wasm_v2_config.take_host_function_costs(),
973+
default_wasm_v2_config.take_host_ffi_opt_costs(),
974974
);
975975
let wasm_config = WasmConfig::new(
976976
MessageLimits {
@@ -1013,7 +1013,7 @@ fn should_not_exceed_configured_max_topics_per_contract_upgrade_no_init() {
10131013
let wasm_v2_config = WasmV2Config::new(
10141014
default_wasm_v2_config.max_memory(),
10151015
default_wasm_v2_config.opcode_costs(),
1016-
default_wasm_v2_config.take_host_function_costs(),
1016+
default_wasm_v2_config.take_host_ffi_opt_costs(),
10171017
);
10181018
let wasm_config = WasmConfig::new(
10191019
MessageLimits {
@@ -1244,7 +1244,7 @@ fn emit_message_should_consume_variable_gas_based_on_topic_and_message_size() {
12441244
let wasm_v2_config = WasmV2Config::new(
12451245
DEFAULT_WASM_MAX_MEMORY,
12461246
OpcodeCosts::zero(),
1247-
HostFunctionCostsV2::default(),
1247+
HostFFIFunctionCosts::default(),
12481248
);
12491249
let wasm_config = WasmConfig::new(MessageLimits::default(), wasm_v1_config, wasm_v2_config);
12501250
ChainspecConfig {

execution_engine_testing/tests/src/test/regression/ee_966.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use casper_engine_test_support::{
1010
use casper_execution_engine::{engine_state::Error, execution::ExecError};
1111
use casper_types::{
1212
addressable_entity::DEFAULT_ENTRY_POINT_NAME, runtime_args, ApiError, EraId,
13-
HostFunctionCostsV1, HostFunctionCostsV2, MessageLimits, OpcodeCosts, ProtocolVersion,
13+
HostFFIFunctionCosts, HostFunctionCostsV1, MessageLimits, OpcodeCosts, ProtocolVersion,
1414
RuntimeArgs, WasmConfig, WasmV1Config, WasmV2Config, DEFAULT_MAX_STACK_HEIGHT,
1515
DEFAULT_WASM_MAX_MEMORY,
1616
};
@@ -29,7 +29,7 @@ static DOUBLED_WASM_MEMORY_LIMIT: Lazy<WasmConfig> = Lazy::new(|| {
2929
let wasm_v2_config = WasmV2Config::new(
3030
DEFAULT_WASM_MAX_MEMORY * 2,
3131
OpcodeCosts::default(),
32-
HostFunctionCostsV2::default(),
32+
HostFFIFunctionCosts::default(),
3333
);
3434
WasmConfig::new(MessageLimits::default(), wasm_v1_config, wasm_v2_config)
3535
});

execution_engine_testing/tests/src/test/storage_costs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use casper_types::{
1717
AUCTION,
1818
},
1919
AddressableEntityHash, BrTableCost, CLValue, ControlFlowCosts, EraId, Gas, Group, Groups,
20-
HostFunctionCostsV1, HostFunctionCostsV2, Key, MessageLimits, OpcodeCosts, ProtocolVersion,
20+
HostFFIFunctionCosts, HostFunctionCostsV1, Key, MessageLimits, OpcodeCosts, ProtocolVersion,
2121
RuntimeArgs, StorageCosts, StoredValue, URef, WasmConfig, WasmV1Config, WasmV2Config,
2222
DEFAULT_ADD_BID_COST, DEFAULT_MAX_STACK_HEIGHT, DEFAULT_WASM_MAX_MEMORY, U512,
2323
};
@@ -85,7 +85,8 @@ const NEW_OPCODE_COSTS: OpcodeCosts = OpcodeCosts {
8585
};
8686

8787
static NEW_HOST_FUNCTION_COSTS: Lazy<HostFunctionCostsV1> = Lazy::new(HostFunctionCostsV1::zero);
88-
static NEW_HOST_FUNCTION_COSTS_V2: Lazy<HostFunctionCostsV2> = Lazy::new(HostFunctionCostsV2::zero);
88+
static NEW_HOST_FUNCTION_COSTS_V2: Lazy<HostFFIFunctionCosts> =
89+
Lazy::new(HostFFIFunctionCosts::zero);
8990
static NO_COSTS_WASM_CONFIG: Lazy<WasmConfig> = Lazy::new(|| {
9091
let wasm_v1_config = WasmV1Config::new(
9192
DEFAULT_WASM_MAX_MEMORY,

execution_engine_testing/tests/src/test/system_costs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use casper_types::{
1515
handle_payment, mint, AUCTION,
1616
},
1717
AuctionCosts, BrTableCost, ControlFlowCosts, CoreConfig, EraId, Gas, GenesisAccount,
18-
GenesisValidator, HandlePaymentCosts, HostFunction, HostFunctionCost, HostFunctionCostsV1,
19-
HostFunctionCostsV2, MessageLimits, MintCosts, Motes, OpcodeCosts, ProtocolVersion, PublicKey,
18+
GenesisValidator, HandlePaymentCosts, HostFFIFunctionCosts, HostFunction, HostFunctionCost,
19+
HostFunctionCostsV1, MessageLimits, MintCosts, Motes, OpcodeCosts, ProtocolVersion, PublicKey,
2020
RuntimeArgs, SecretKey, StandardPaymentCosts, StorageCosts, SystemConfig, WasmConfig,
2121
WasmV1Config, WasmV2Config, DEFAULT_ADD_BID_COST, DEFAULT_MAX_STACK_HEIGHT,
2222
DEFAULT_MINIMUM_BID_AMOUNT, DEFAULT_WASM_MAX_MEMORY, U512,
@@ -849,7 +849,7 @@ fn should_verify_wasm_add_bid_wasm_cost_is_not_recursive() {
849849
let wasm_v2_config = WasmV2Config::new(
850850
DEFAULT_WASM_MAX_MEMORY,
851851
OpcodeCosts::default(),
852-
HostFunctionCostsV2::default(),
852+
HostFFIFunctionCosts::default(),
853853
);
854854
let wasm_config = WasmConfig::new(MessageLimits::default(), wasm_v1_config, wasm_v2_config);
855855

executor/wasm/src/lib.rs

Lines changed: 97 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ use casper_executor_wasm_host::{
2222
};
2323
use 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
};
6063
use install::{InstallContractError, InstallContractRequest, InstallContractResult};
6164
use parking_lot::RwLock;
62-
use tracing::{debug, error, info, warn};
65+
use tracing::{debug, error, warn};
6366

6467
#[cfg(any(feature = "testing", test))]
6568
pub 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

Comments
 (0)