Skip to content

Commit

Permalink
runtime-sdk: Refactor the RNG, mix in local entropy in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Aug 2, 2023
1 parent 3f6e320 commit 63e2510
Show file tree
Hide file tree
Showing 22 changed files with 697 additions and 259 deletions.
4 changes: 2 additions & 2 deletions runtime-sdk/modules/contracts/src/abi/oasis/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ fn test_validate_and_transform() {
let mut mock = mock::Mock::default();
let mut ctx = mock.create_ctx();
let params = Parameters::default();
ctx.with_tx(0, 0, mock::transaction(), |ctx, _| {
ctx.with_tx(mock::transaction().into(), |ctx, _| {
test::<ContractsConfig, _>(ctx, &params);
});
}
Expand Down Expand Up @@ -331,7 +331,7 @@ fn run_contract_with_defaults(
let mut tx = mock::transaction();
tx.auth_info.fee.gas = gas_limit;

ctx.with_tx(0, 0, tx, |mut ctx, _| -> Result<cbor::Value, Error> {
ctx.with_tx(tx.into(), |mut ctx, _| -> Result<cbor::Value, Error> {
fn transform<C: TxContext>(
_ctx: &mut C,
code: &[u8],
Expand Down
38 changes: 19 additions & 19 deletions runtime-sdk/modules/contracts/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn upload_hello_contract<C: BatchContext>(ctx: &mut C) -> types::CodeId {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let code_id = Contracts::tx_upload(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("upload should succeed")
.id;
Expand Down Expand Up @@ -115,7 +115,7 @@ fn deploy_hello_contract<C: BatchContext>(
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let instance_id =
Contracts::tx_instantiate(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("instantiate should succeed")
Expand Down Expand Up @@ -232,7 +232,7 @@ fn test_hello_contract_call() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");

Expand Down Expand Up @@ -329,7 +329,7 @@ fn test_hello_contract_call() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");

Expand Down Expand Up @@ -482,7 +482,7 @@ fn test_hello_contract_call() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let _result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");
tx_ctx.commit();
Expand Down Expand Up @@ -569,7 +569,7 @@ fn test_hello_contract_call() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let _result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");
tx_ctx.commit();
Expand Down Expand Up @@ -667,13 +667,13 @@ fn test_hello_contract_call() {
..Default::default()
},
};
ctx.with_tx(0, 0, invalid_tx.clone(), |mut tx_ctx, call| {
ctx.with_tx(invalid_tx.clone().into(), |mut tx_ctx, call| {
Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect_err("invalid call should fail");
});

ctx.with_child(context::Mode::CheckTx, |mut check_ctx| {
check_ctx.with_tx(0, 0, invalid_tx, |mut tx_ctx, call| {
check_ctx.with_tx(invalid_tx.into(), |mut tx_ctx, call| {
Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("invalid call should succeed check-tx");
});
Expand Down Expand Up @@ -769,7 +769,7 @@ fn test_hello_contract_subcalls_overflow() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect_err("call should fail");

Expand Down Expand Up @@ -823,7 +823,7 @@ fn test_hello_contract_subcalls() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx.clone(), |mut tx_ctx, call| {
ctx.with_tx(tx.clone().into(), |mut tx_ctx, call| {
let result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");

Expand Down Expand Up @@ -891,7 +891,7 @@ fn test_hello_contract_query() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");

Expand Down Expand Up @@ -933,7 +933,7 @@ fn test_hello_contract_query() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");

Expand Down Expand Up @@ -975,7 +975,7 @@ fn test_hello_contract_query() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed");

Expand Down Expand Up @@ -1029,7 +1029,7 @@ fn test_hello_contract_upgrade() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
Contracts::tx_upgrade(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("upgrade should succeed");

Expand Down Expand Up @@ -1074,7 +1074,7 @@ fn test_hello_contract_upgrade_fail_policy() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_upgrade(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect_err("upgrade should fail");

Expand Down Expand Up @@ -1121,7 +1121,7 @@ fn test_hello_contract_upgrade_fail_pre() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_upgrade(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect_err("upgrade should fail");

Expand Down Expand Up @@ -1171,7 +1171,7 @@ fn test_hello_contract_upgrade_fail_post() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result = Contracts::tx_upgrade(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect_err("upgrade should fail");

Expand Down Expand Up @@ -1218,7 +1218,7 @@ fn test_hello_contract_change_upgrade_policy() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
Contracts::tx_change_upgrade_policy(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("upgrade should succeed");

Expand Down Expand Up @@ -1260,7 +1260,7 @@ fn test_hello_contract_change_upgrade_policy_fail() {
..Default::default()
},
};
ctx.with_tx(0, 0, tx, |mut tx_ctx, call| {
ctx.with_tx(tx.into(), |mut tx_ctx, call| {
let result =
Contracts::tx_change_upgrade_policy(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect_err("change upgrade policy should fail");
Expand Down
2 changes: 1 addition & 1 deletion runtime-sdk/modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl<Cfg: Config> API for Module<Cfg> {
..Default::default()
},
};
sctx.with_tx(0, 0, call_tx, |mut txctx, _call| {
sctx.with_tx(call_tx.into(), |mut txctx, _call| {
Self::do_evm(
caller,
&mut txctx,
Expand Down
22 changes: 11 additions & 11 deletions runtime-sdk/modules/evm/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn do_test_evm_calls<C: Config>(force_plain: bool) {
// Run authentication handler to simulate nonce increments.
Accounts::authenticate_tx(&mut ctx, &create_tx).unwrap();

let erc20_addr = ctx.with_tx(0, 0, create_tx, |mut tx_ctx, call| {
let erc20_addr = ctx.with_tx(create_tx.into(), |mut tx_ctx, call| {
let addr = H160::from_slice(
&EVMModule::<C>::tx_create(&mut tx_ctx, cbor::from_value(call.body).unwrap()).unwrap(),
);
Expand Down Expand Up @@ -258,7 +258,7 @@ fn do_test_evm_calls<C: Config>(force_plain: bool) {
// Run authentication handler to simulate nonce increments.
Accounts::authenticate_tx(&mut ctx, &call_name_tx).unwrap();

let erc20_name = ctx.with_tx(0, 0, call_name_tx, |mut tx_ctx, call| {
let erc20_name = ctx.with_tx(call_name_tx.into(), |mut tx_ctx, call| {
let name: Vec<u8> = cbor::from_value(
decode_result!(
tx_ctx,
Expand Down Expand Up @@ -360,7 +360,7 @@ fn test_c10l_evm_balance_transfer() {
// Run authentication handler to simulate nonce increments.
Accounts::authenticate_tx(&mut ctx, &transfer_tx).unwrap();

ctx.with_tx(0, 0, transfer_tx, |mut tx_ctx, call| {
ctx.with_tx(transfer_tx.into(), |mut tx_ctx, call| {
EVMModule::<ConfidentialEVMConfig>::tx_call(
&mut tx_ctx,
cbor::from_value(call.body).unwrap(),
Expand Down Expand Up @@ -535,7 +535,7 @@ fn do_test_evm_runtime<C: Config>() {
// Run authentication handler to simulate nonce increments.
<EVMRuntime<C> as Runtime>::Modules::authenticate_tx(&mut ctx, &create_tx).unwrap();

let erc20_addr = ctx.with_tx(0, 0, create_tx, |mut tx_ctx, call| {
let erc20_addr = ctx.with_tx(create_tx.into(), |mut tx_ctx, call| {
let addr = H160::from_slice(
&EVMModule::<C>::tx_create(&mut tx_ctx, cbor::from_value(call.body).unwrap()).unwrap(),
);
Expand Down Expand Up @@ -572,7 +572,7 @@ fn do_test_evm_runtime<C: Config>() {
// Run authentication handler to simulate nonce increments.
<EVMRuntime<C> as Runtime>::Modules::authenticate_tx(&mut ctx, &out_of_gas_create).unwrap();

ctx.with_tx(0, 0, out_of_gas_create.clone(), |mut tx_ctx, call| {
ctx.with_tx(out_of_gas_create.clone().into(), |mut tx_ctx, call| {
assert!(!decode_result!(
tx_ctx,
EVMModule::<C>::tx_create(&mut tx_ctx, cbor::from_value(call.body).unwrap())
Expand All @@ -582,7 +582,7 @@ fn do_test_evm_runtime<C: Config>() {

// CheckTx should not fail.
ctx.with_child(context::Mode::CheckTx, |mut check_ctx| {
check_ctx.with_tx(0, 0, out_of_gas_create, |mut tx_ctx, call| {
check_ctx.with_tx(out_of_gas_create.into(), |mut tx_ctx, call| {
let rsp = EVMModule::<C>::tx_create(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed with empty result");

Expand Down Expand Up @@ -627,7 +627,7 @@ fn do_test_evm_runtime<C: Config>() {
// Test transaction call in simulate mode.
CurrentStore::with_transaction(|| {
ctx.with_simulation(|mut sim_ctx| {
let erc20_name = sim_ctx.with_tx(0, 0, call_name_tx.clone(), |mut tx_ctx, call| {
let erc20_name = sim_ctx.with_tx(call_name_tx.clone().into(), |mut tx_ctx, call| {
let name: Vec<u8> = cbor::from_value(
decode_result!(
tx_ctx,
Expand All @@ -651,7 +651,7 @@ fn do_test_evm_runtime<C: Config>() {
TransactionResult::Rollback(()) // Ignore simulation results.
});

let erc20_name = ctx.with_tx(0, 0, call_name_tx.clone(), |mut tx_ctx, call| {
let erc20_name = ctx.with_tx(call_name_tx.clone().into(), |mut tx_ctx, call| {
let name: Vec<u8> = cbor::from_value(
decode_result!(
tx_ctx,
Expand Down Expand Up @@ -709,7 +709,7 @@ fn do_test_evm_runtime<C: Config>() {
// Run authentication handler to simulate nonce increments.
<EVMRuntime<C> as Runtime>::Modules::authenticate_tx(&mut ctx, &call_transfer_tx).unwrap();

let transfer_ret = ctx.with_tx(0, 0, call_transfer_tx.clone(), |mut tx_ctx, call| {
let transfer_ret = ctx.with_tx(call_transfer_tx.clone().into(), |mut tx_ctx, call| {
let ret: Vec<u8> = cbor::from_value(
decode_result!(
tx_ctx,
Expand Down Expand Up @@ -758,7 +758,7 @@ fn do_test_evm_runtime<C: Config>() {
};
<EVMRuntime<C> as Runtime>::Modules::authenticate_tx(&mut ctx, &out_of_gas_tx).unwrap();

ctx.with_tx(0, 0, out_of_gas_tx.clone(), |mut tx_ctx, call| {
ctx.with_tx(out_of_gas_tx.clone().into(), |mut tx_ctx, call| {
assert!(!decode_result!(
tx_ctx,
EVMModule::<C>::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
Expand All @@ -768,7 +768,7 @@ fn do_test_evm_runtime<C: Config>() {

// CheckTx should not fail.
ctx.with_child(context::Mode::CheckTx, |mut check_ctx| {
check_ctx.with_tx(0, 0, out_of_gas_tx, |mut tx_ctx, call| {
check_ctx.with_tx(out_of_gas_tx.into(), |mut tx_ctx, call| {
let rsp = EVMModule::<C>::tx_call(&mut tx_ctx, cbor::from_value(call.body).unwrap())
.expect("call should succeed with empty result");

Expand Down
Loading

0 comments on commit 63e2510

Please sign in to comment.