Skip to content

Commit

Permalink
Merge pull request #1420 from oasisprotocol/kostko/feature/rng-refactor
Browse files Browse the repository at this point in the history
runtime-sdk: Refactor the RNG, mix in local entropy in queries
  • Loading branch information
kostko committed Aug 4, 2023
2 parents 86a22a2 + fbf0cac commit 03fe715
Show file tree
Hide file tree
Showing 41 changed files with 931 additions and 473 deletions.
144 changes: 71 additions & 73 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions client-sdk/go/callformat/callformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
type EncodeConfig struct {
// PublicKey is an optional runtime's call data public key to use for encrypted call formats.
PublicKey *types.SignedPublicKey
// Epoch is the epoch of the ephemeral runtime key (when PublicKey is set).
Epoch uint64
}

type metaEncryptedX25519DeoxysII struct {
Expand Down Expand Up @@ -60,6 +62,7 @@ func EncodeCall(call *types.Call, cf types.CallFormat, cfg *EncodeConfig) (*type
Body: cbor.Marshal(&types.CallEnvelopeX25519DeoxysII{
Pk: *pk,
Nonce: nonce,
Epoch: cfg.Epoch,
Data: sealedCall,
}),
ReadOnly: call.ReadOnly,
Expand Down
4 changes: 4 additions & 0 deletions client-sdk/go/client/callformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
const methodCallDataPublicKey = "core.CallDataPublicKey"

type callDataPublicKeyQueryResponse struct {
// PublicKey is the ephemeral X25519 runtime public key.
PublicKey types.SignedPublicKey `json:"public_key"`
// Epoch is the epoch of the ephemeral runtime key.
Epoch uint64 `json:"epoch,omitempty"`
}

// encodeCall performs call encoding based on the specified call format.
Expand All @@ -30,6 +33,7 @@ func (tb *TransactionBuilder) encodeCall(ctx context.Context, call *types.Call,
// TODO: In case the node we are connecting to is not trusted, validate the key manager signature.

cfg.PublicKey = &rsp.PublicKey
cfg.Epoch = rsp.Epoch
default:
}

Expand Down
2 changes: 2 additions & 0 deletions client-sdk/go/modules/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ const (
type CallDataPublicKeyResponse struct {
// PublicKey is the signed runtime call data public key.
PublicKey types.SignedPublicKey `json:"public_key"`
// Epoch is the epoch of the ephemeral runtime key.
Epoch uint64 `json:"epoch,omitempty"`
}

// ExecuteReadOnlyTxQuery is the body of the core.ExecuteReadOnlyTx query.
Expand Down
2 changes: 2 additions & 0 deletions client-sdk/go/types/callformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type CallEnvelopeX25519DeoxysII struct {
Pk [32]byte `json:"pk"`
// Nonce.
Nonce [deoxysii.NonceSize]byte `json:"nonce"`
// Epoch is the epoch of the ephemeral runtime key.
Epoch uint64 `json:"epoch,omitempty"`
// Data is the encrypted call data.
Data []byte `json:"data"`
}
Expand Down
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/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ rand_core = { version = "0.6.4", default-features = false }
# Ethereum.
ethabi = { version = "18.0.0", default-features = false, features = ["std"] }
ethereum = "0.14"
evm = "0.37.0"
evm = "0.39.1"
fixed-hash = "0.8.0"
primitive-types = { version = "0.12", default-features = false, features = ["rlp", "num-traits"] }
rlp = "0.5.2"
Expand Down
9 changes: 7 additions & 2 deletions runtime-sdk/modules/evm/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ impl<'ctx, 'backend, 'config, C: TxContext, Cfg: Config> Backend
U256::zero()
}

fn block_randomness(&self) -> Option<H256> {
None
}

fn block_gas_limit(&self) -> U256 {
<C::Runtime as Runtime>::Core::max_batch_gas(&mut self.backend.ctx.borrow_mut()).into()
}
Expand Down Expand Up @@ -466,14 +470,15 @@ impl<'ctx, 'backend, 'config, C: TxContext, Cfg: Config> StackState<'config>
.recursive_is_cold(&|a: &Accessed| a.accessed_storage.contains(&(address, key)))
}

fn inc_nonce(&mut self, address: H160) {
fn inc_nonce(&mut self, address: H160) -> Result<(), ExitError> {
// Do not increment the origin nonce as that has already been handled by the SDK.
if address == self.origin() {
return;
return Ok(());
}

let address = Cfg::map_address(address);
Cfg::Accounts::inc_nonce(address);
Ok(())
}

fn set_storage(&mut self, address: H160, key: H256, value: H256) {
Expand Down
44 changes: 24 additions & 20 deletions runtime-sdk/modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use thiserror::Error;

use oasis_runtime_sdk::{
callformat,
context::{BatchContext, Context, TxContext},
context::{BatchContext, Context, TransactionWithMeta, TxContext},
handler,
module::{self, Module as _},
modules::{
Expand Down Expand Up @@ -107,7 +107,7 @@ pub trait Config: 'static {
}
})
} else {
EVM_CONFIG.get_or_init(EVMConfig::london)
EVM_CONFIG.get_or_init(EVMConfig::shanghai)
}
}
}
Expand Down Expand Up @@ -186,6 +186,7 @@ impl From<evm::ExitError> for Error {
PCUnderflow => "PC underflow",

CreateEmpty => "create empty",
MaxNonce => "nonce overflow",

Other(msg) => return Error::ExecutionFailed(msg.to_string()),
};
Expand Down Expand Up @@ -494,24 +495,27 @@ impl<Cfg: Config> API for Module<Cfg> {
..Default::default()
},
};
sctx.with_tx(0, 0, call_tx, |mut txctx, _call| {
Self::do_evm(
caller,
&mut txctx,
|exec, gas_limit| {
exec.transact_call(
caller.into(),
address.into(),
value.into(),
data,
gas_limit,
vec![],
)
},
// Simulate call is never called from EstimateGas.
false,
)
})
sctx.with_tx(
TransactionWithMeta::internal(call_tx),
|mut txctx, _call| {
Self::do_evm(
caller,
&mut txctx,
|exec, gas_limit| {
exec.transact_call(
caller.into(),
address.into(),
value.into(),
data,
gas_limit,
vec![],
)
},
// Simulate call is never called from EstimateGas.
false,
)
},
)
});
Self::encode_evm_result(ctx, evm_result, tx_metadata)
}
Expand Down
Loading

0 comments on commit 03fe715

Please sign in to comment.