Skip to content

Commit 0c3466c

Browse files
committed
Add seed
1 parent e1bf7e2 commit 0c3466c

File tree

19 files changed

+185
-235
lines changed

19 files changed

+185
-235
lines changed

executor/wasm-host/src/host.rs

+65-178
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,6 @@ pub fn casper_env_balance<S: GlobalStateReader, E: Executor>(
830830

831831
pub fn casper_transfer<S: GlobalStateReader + 'static, E: Executor>(
832832
mut caller: impl Caller<Context = Context<S, E>>,
833-
entity_kind: u32,
834833
entity_addr_ptr: u32,
835834
entity_addr_len: u32,
836835
amount_ptr: u32,
@@ -840,57 +839,39 @@ pub fn casper_transfer<S: GlobalStateReader + 'static, E: Executor>(
840839
return Ok(u32_from_host_result(Err(HostError::NotCallable)));
841840
}
842841

843-
let entity_kind = match EntityKindTag::from_u32(entity_kind) {
844-
Some(entity_kind) => entity_kind,
845-
None => {
846-
// Unknown target entity kind; failing to proceed with the transfer
847-
return Ok(u32_from_host_result(Err(HostError::NotCallable))); // fail
848-
}
849-
};
850-
851842
let amount: u128 = {
852843
let mut amount_bytes = [0u8; 16];
853844
caller.memory_read_into(amount_ptr, &mut amount_bytes)?;
854845
u128::from_le_bytes(amount_bytes)
855846
};
856847

857-
let target_entity_addr = match entity_kind {
858-
EntityKindTag::Account => {
859-
let entity_addr = caller.memory_read(entity_addr_ptr, entity_addr_len as usize)?;
860-
debug_assert_eq!(entity_addr.len(), 32);
861-
862-
// SAFETY: entity_addr is 32 bytes long
863-
let account_hash: AccountHash = AccountHash::new(entity_addr.try_into().unwrap());
864-
865-
// Resolve indirection to get to the actual addressable entity
866-
let account_key = Key::Account(account_hash);
867-
match caller.context_mut().tracking_copy.read(&account_key) {
868-
Ok(Some(StoredValue::CLValue(indirect))) => {
869-
// is it an account?
870-
let addressable_entity_key = indirect.into_t::<Key>().expect("should be key");
871-
addressable_entity_key
872-
.into_entity_addr()
873-
.expect("should be entity addr")
874-
}
875-
Ok(Some(other)) => panic!("should be cl value but got {other:?}"),
876-
Ok(None) => return Ok(u32_from_host_result(Err(HostError::NotCallable))),
877-
Err(error) => {
878-
error!(
879-
?error,
880-
?account_key,
881-
"Error while reading from storage; aborting"
882-
);
883-
panic!("Error while reading from storage")
884-
}
848+
let target_entity_addr = {
849+
let entity_addr = caller.memory_read(entity_addr_ptr, entity_addr_len as usize)?;
850+
debug_assert_eq!(entity_addr.len(), 32);
851+
852+
// SAFETY: entity_addr is 32 bytes long
853+
let account_hash: AccountHash = AccountHash::new(entity_addr.try_into().unwrap());
854+
855+
// Resolve indirection to get to the actual addressable entity
856+
let account_key = Key::Account(account_hash);
857+
match caller.context_mut().tracking_copy.read(&account_key) {
858+
Ok(Some(StoredValue::CLValue(indirect))) => {
859+
// is it an account?
860+
let addressable_entity_key = indirect.into_t::<Key>().expect("should be key");
861+
addressable_entity_key
862+
.into_entity_addr()
863+
.expect("should be entity addr")
864+
}
865+
Ok(Some(other)) => panic!("should be cl value but got {other:?}"),
866+
Ok(None) => return Ok(u32_from_host_result(Err(HostError::NotCallable))),
867+
Err(error) => {
868+
error!(
869+
?error,
870+
?account_key,
871+
"Error while reading from storage; aborting"
872+
);
873+
panic!("Error while reading from storage")
885874
}
886-
}
887-
EntityKindTag::Contract => {
888-
let entity_addr = caller.memory_read(entity_addr_ptr, entity_addr_len as usize)?;
889-
debug_assert_eq!(entity_addr.len(), 32);
890-
891-
// SAFETY: entity_addr is 32 bytes long
892-
let entity_addr: HashAddr = entity_addr.try_into().unwrap();
893-
EntityAddr::SmartContract(entity_addr)
894875
}
895876
};
896877

@@ -918,10 +899,6 @@ pub fn casper_transfer<S: GlobalStateReader + 'static, E: Executor>(
918899
other => panic!("should be account or addressable entity but got {other:?}"),
919900
};
920901

921-
let _callee_entity_addr = callee_addressable_entity_key
922-
.into_entity_addr()
923-
.expect("should be entity addr");
924-
925902
let callee_stored_value = caller
926903
.context_mut()
927904
.tracking_copy
@@ -933,138 +910,48 @@ pub fn casper_transfer<S: GlobalStateReader + 'static, E: Executor>(
933910
.expect("should be addressable entity");
934911
let callee_purse = callee_addressable_entity.main_purse();
935912

936-
match entity_kind {
937-
EntityKindTag::Account => {
938-
let target_purse = match caller
939-
.context_mut()
940-
.tracking_copy
941-
.read(&Key::AddressableEntity(target_entity_addr))
942-
{
943-
Ok(Some(StoredValue::Account(account))) => {
944-
panic!("Expected AddressableEntity but got {:?}", account)
945-
}
946-
Ok(Some(StoredValue::AddressableEntity(addressable_entity))) => {
947-
addressable_entity.main_purse()
948-
}
949-
Ok(Some(other_entity)) => {
950-
panic!("Unexpected entity type: {:?}", other_entity)
951-
}
952-
Ok(None) => {
953-
panic!("Addressable entity not found for key={target_entity_addr:?}");
954-
}
955-
Err(error) => {
956-
error!(?error, "Error while reading from storage; aborting");
957-
panic!("Error while reading from storage; aborting")
958-
}
959-
};
960-
// We don't execute anything as it does not make sense to execute an account as there
961-
// are no entry points.
962-
let transaction_hash = caller.context().transaction_hash;
963-
let address_generator = Arc::clone(&caller.context().address_generator);
964-
let args = MintTransferArgs {
965-
source: callee_purse,
966-
target: target_purse,
967-
amount: U512::from(amount),
968-
maybe_to: None,
969-
id: None,
970-
};
971-
972-
let result = system::mint_transfer(
973-
&mut caller.context_mut().tracking_copy,
974-
transaction_hash,
975-
address_generator,
976-
args,
977-
);
978-
979-
Ok(u32_from_host_result(result))
913+
let target_purse = match caller
914+
.context_mut()
915+
.tracking_copy
916+
.read(&Key::AddressableEntity(target_entity_addr))
917+
{
918+
Ok(Some(StoredValue::Account(account))) => {
919+
panic!("Expected AddressableEntity but got {:?}", account)
980920
}
981-
EntityKindTag::Contract => {
982-
// let callee_purse = callee_stored_value.main_purse();
983-
984-
let transaction_hash = caller.context().transaction_hash;
985-
let _address_generator = Arc::clone(&caller.context().address_generator);
986-
987-
let tracking_copy = caller.context().tracking_copy.fork2();
988-
989-
// Take the gas spent so far and use it as a limit for the new VM.
990-
let gas_limit = caller
991-
.gas_consumed()
992-
.try_into_remaining()
993-
.expect("should be remaining");
994-
995-
let address_generator = Arc::clone(&caller.context().address_generator);
996-
let execute_request = ExecuteRequestBuilder::default()
997-
.with_initiator(caller.context().initiator)
998-
.with_caller_key(caller.context().callee)
999-
// .with_callee_key(Key::AddressableEntity(EntityAddr::new_smart_contract(
1000-
// address,
1001-
// )))
1002-
.with_callee_key(Key::AddressableEntity(target_entity_addr))
1003-
.with_gas_limit(gas_limit)
1004-
.with_target(ExecutionKind::Stored {
1005-
address: target_entity_addr,
1006-
entry_point: "__casper_fallback".to_string(),
1007-
})
1008-
.with_transferred_value(amount)
1009-
.with_input(Bytes::new())
1010-
.with_transaction_hash(transaction_hash)
1011-
// We're using shared address generator there as we need to preserve and advance the
1012-
// state of deterministic address generator across chain of calls.
1013-
.with_shared_address_generator(address_generator)
1014-
.with_chain_name(caller.context().chain_name.clone())
1015-
.with_block_time(caller.context().block_time)
1016-
.build()
1017-
.expect("should build");
921+
Ok(Some(StoredValue::AddressableEntity(addressable_entity))) => {
922+
addressable_entity.main_purse()
923+
}
924+
Ok(Some(other_entity)) => {
925+
panic!("Unexpected entity type: {:?}", other_entity)
926+
}
927+
Ok(None) => {
928+
panic!("Addressable entity not found for key={target_entity_addr:?}");
929+
}
930+
Err(error) => {
931+
error!(?error, "Error while reading from storage; aborting");
932+
panic!("Error while reading from storage; aborting")
933+
}
934+
};
935+
// We don't execute anything as it does not make sense to execute an account as there
936+
// are no entry points.
937+
let transaction_hash = caller.context().transaction_hash;
938+
let address_generator = Arc::clone(&caller.context().address_generator);
939+
let args = MintTransferArgs {
940+
source: callee_purse,
941+
target: target_purse,
942+
amount: U512::from(amount),
943+
maybe_to: None,
944+
id: None,
945+
};
1018946

1019-
match caller
1020-
.context()
1021-
.executor
1022-
.execute(tracking_copy, execute_request)
1023-
{
1024-
Ok(ExecuteResult {
1025-
host_error,
1026-
output,
1027-
gas_usage,
1028-
effects,
1029-
cache,
1030-
}) => {
1031-
caller.consume_gas(gas_usage.gas_spent());
1032-
let _ = output; // TODO: What to do with the output of a fallback code? Need to emit a message
1033-
// with this
1034-
1035-
let host_result = match host_error {
1036-
Some(host_error) => Err(host_error),
1037-
None => {
1038-
caller
1039-
.context_mut()
1040-
.tracking_copy
1041-
.apply_changes(effects, cache);
1042-
Ok(())
1043-
}
1044-
};
1045-
1046-
let gas_spent = gas_usage
1047-
.gas_limit()
1048-
.checked_sub(gas_usage.remaining_points())
1049-
.expect("remaining points always below or equal to the limit");
1050-
1051-
match caller.consume_gas(gas_spent) {
1052-
MeteringPoints::Remaining(_) => {}
1053-
MeteringPoints::Exhausted => {
1054-
todo!("exhausted")
1055-
}
1056-
}
947+
let result = system::mint_transfer(
948+
&mut caller.context_mut().tracking_copy,
949+
transaction_hash,
950+
address_generator,
951+
args,
952+
);
1057953

1058-
Ok(u32_from_host_result(host_result))
1059-
}
1060-
Err(ExecuteError::WasmPreparation(preparation_error)) => {
1061-
// This is a bug in the EE, as it should have been caught during the preparation
1062-
// phase when the contract was stored in the global state.
1063-
unreachable!("Preparation error: {:?}", preparation_error)
1064-
}
1065-
}
1066-
}
1067-
}
954+
Ok(u32_from_host_result(result))
1068955
}
1069956

1070957
pub fn casper_upgrade<S: GlobalStateReader + 'static, E: Executor>(

executor/wasm/src/install.rs

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub struct InstallContractRequestBuilder {
5252
address_generator: Option<Arc<RwLock<AddressGenerator>>>,
5353
chain_name: Option<Arc<str>>,
5454
block_time: Option<Timestamp>,
55+
seed: Option<[u8; 32]>,
5556
}
5657

5758
impl InstallContractRequestBuilder {
@@ -113,6 +114,11 @@ impl InstallContractRequestBuilder {
113114
self
114115
}
115116

117+
pub(crate) fn with_seed(mut self, seed: [u8; 32]) -> Self {
118+
self.seed = Some(seed);
119+
self
120+
}
121+
116122
pub fn build(self) -> Result<InstallContractRequest, &'static str> {
117123
let initiator = self.initiator.ok_or("Initiator not set")?;
118124
let gas_limit = self.gas_limit.ok_or("Gas limit not set")?;
@@ -124,6 +130,7 @@ impl InstallContractRequestBuilder {
124130
let transaction_hash = self.transaction_hash.ok_or("Transaction hash not set")?;
125131
let chain_name = self.chain_name.ok_or("Chain name not set")?;
126132
let block_time = self.block_time.ok_or("Block time not set")?;
133+
let seed = self.seed;
127134
Ok(InstallContractRequest {
128135
initiator,
129136
gas_limit,
@@ -135,6 +142,7 @@ impl InstallContractRequestBuilder {
135142
transaction_hash,
136143
chain_name,
137144
block_time,
145+
seed,
138146
})
139147
}
140148
}

executor/wasm/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl ExecutorV2 {
146146
transaction_hash,
147147
chain_name,
148148
block_time,
149+
seed,
149150
} = install_request;
150151

151152
let caller_key = Key::Account(initiator);
@@ -166,6 +167,7 @@ impl ExecutorV2 {
166167
chain_name.as_bytes(),
167168
initiator,
168169
bytecode_hash,
170+
seed,
169171
);
170172

171173
{
@@ -880,6 +882,7 @@ impl WasmV2Request {
880882
Install {
881883
module_bytes: Bytes,
882884
entry_point: String,
885+
seed: Option<[u8; 32]>,
883886
},
884887
Session {
885888
module_bytes: Bytes,
@@ -907,13 +910,15 @@ impl WasmV2Request {
907910
module_bytes,
908911
runtime: _,
909912
transferred_value: _,
913+
seed,
910914
} => match transaction_v1.body().entry_point() {
911915
TransactionEntryPoint::Call => Target::Session {
912916
module_bytes: module_bytes.clone().take_inner().into(),
913917
},
914918
TransactionEntryPoint::Custom(entry_point) => Target::Install {
915919
module_bytes: module_bytes.clone().take_inner().into(),
916920
entry_point: entry_point.to_string(),
921+
seed: *seed,
917922
},
918923
_ => todo!(),
919924
},
@@ -925,6 +930,7 @@ impl WasmV2Request {
925930
Target::Install {
926931
module_bytes,
927932
entry_point,
933+
seed,
928934
} => {
929935
let mut builder = InstallContractRequestBuilder::default();
930936

@@ -946,6 +952,10 @@ impl WasmV2Request {
946952
}
947953
}
948954

955+
if let Some(seed) = seed {
956+
builder = builder.with_seed(seed);
957+
}
958+
949959
let install_request = builder
950960
.with_initiator(initiator_addr.account_hash())
951961
.with_gas_limit(gas_limit)

executor/wasmer-backend/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,12 @@ where
578578
&mut store,
579579
&function_env,
580580
|env: FunctionEnvMut<WasmerEnv<S, E>>,
581-
entity_kind,
582581
address_ptr,
583582
address_len,
584583
amount: WasmPtr<u128>| {
585584
let wasmer_caller = WasmerCaller { env };
586585
host::casper_transfer(
587586
wasmer_caller,
588-
entity_kind,
589587
address_ptr,
590588
address_len,
591589
amount.offset(),

0 commit comments

Comments
 (0)