Skip to content

Commit

Permalink
Merge pull request #1601 from oasisprotocol/kostko/feature/state-refa…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
kostko authored Jan 9, 2024
2 parents da9d86d + 126b36e commit 0a8c744
Show file tree
Hide file tree
Showing 49 changed files with 4,426 additions and 4,699 deletions.
14 changes: 7 additions & 7 deletions runtime-sdk-macros/src/module_derive/method_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl super::Deriver for DeriveMethodHandler {
if h.attrs.is_internal {
quote! {
|ctx, body| {
if !ctx.is_internal() {
if !sdk::state::CurrentState::with_env(|env| env.is_internal()) {
return Err(sdk::modules::core::Error::Forbidden.into());
}
Self::#ident(ctx, body)
Expand All @@ -128,8 +128,8 @@ impl super::Deriver for DeriveMethodHandler {
quote! {}
} else {
quote! {
fn dispatch_call<C: TxContext>(
ctx: &mut C,
fn dispatch_call<C: Context>(
ctx: &C,
method: &str,
body: cbor::Value,
) -> DispatchResult<cbor::Value, CallResult> {
Expand All @@ -146,7 +146,7 @@ impl super::Deriver for DeriveMethodHandler {

let query_parameters_impl = {
quote! {
fn query_parameters<C: Context>(_ctx: &mut C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
fn query_parameters<C: Context>(_ctx: &C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
Ok(Self::params())
}
}
Expand All @@ -158,7 +158,7 @@ impl super::Deriver for DeriveMethodHandler {
if handler_names.is_empty() {
quote! {
fn dispatch_query<C: Context>(
ctx: &mut C,
ctx: &C,
method: &str,
args: cbor::Value,
) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>> {
Expand All @@ -171,7 +171,7 @@ impl super::Deriver for DeriveMethodHandler {
} else {
quote! {
fn dispatch_query<C: Context>(
ctx: &mut C,
ctx: &C,
method: &str,
args: cbor::Value,
) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>> {
Expand All @@ -196,7 +196,7 @@ impl super::Deriver for DeriveMethodHandler {
} else {
quote! {
fn dispatch_message_result<C: Context>(
ctx: &mut C,
ctx: &C,
handler_name: &str,
result: MessageResult,
) -> DispatchResult<MessageResult, ()> {
Expand Down
2 changes: 1 addition & 1 deletion runtime-sdk-macros/src/module_derive/migration_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl super::Deriver for DeriveMigrationHandler {
#genesis_ty

fn init_or_migrate<C: Context>(
_ctx: &mut C,
_ctx: &C,
meta: &mut sdk::modules::core::types::Metadata,
genesis: Self::Genesis,
) -> bool {
Expand Down
28 changes: 14 additions & 14 deletions runtime-sdk-macros/src/module_derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod tests {
#[automatically_derived]
impl<C: Cfg> sdk::module::MethodHandler for MyModule<C> {
fn dispatch_query<C: Context>(
ctx: &mut C,
ctx: &C,
method: &str,
args: cbor::Value,
) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>>
Expand All @@ -135,7 +135,7 @@ mod tests {
}
#[automatically_derived]
impl<C: Cfg> MyModule<C> {
fn query_parameters<C: Context>(_ctx: &mut C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
fn query_parameters<C: Context>(_ctx: &C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
Ok(Self::params())
}
}
Expand Down Expand Up @@ -190,8 +190,8 @@ mod tests {
_ => module::DispatchResult::Unhandled(body),
}
}
fn dispatch_call<C: TxContext>(
ctx: &mut C,
fn dispatch_call<C: Context>(
ctx: &C,
method: &str,
body: cbor::Value,
) -> DispatchResult<cbor::Value, CallResult> {
Expand All @@ -201,7 +201,7 @@ mod tests {
module::dispatch_call(ctx, body, Self::my_other_call)
}
"my_module.MyInternalCall" => module::dispatch_call(ctx, body, |ctx, body| {
if !ctx.is_internal() {
if !sdk::state::CurrentState::with_env(|env| env.is_internal()) {
return Err(sdk::modules::core::Error::Forbidden.into());
}
Self::my_internal_call(ctx, body)
Expand All @@ -210,7 +210,7 @@ mod tests {
}
}
fn dispatch_query<C: Context>(
ctx: &mut C,
ctx: &C,
method: &str,
args: cbor::Value,
) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>>
Expand Down Expand Up @@ -241,7 +241,7 @@ mod tests {
}
#[automatically_derived]
impl<C: Cfg> MyModule<C> {
fn query_parameters<C: Context>(_ctx: &mut C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
fn query_parameters<C: Context>(_ctx: &C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
Ok(Self::params())
}
#[handler(prefetch = "my_module.MyCall")]
Expand Down Expand Up @@ -281,7 +281,7 @@ mod tests {
#[automatically_derived]
impl<C: Cfg> sdk::module::MethodHandler for MyModule<C> {
fn dispatch_query<C: Context>(
ctx: &mut C,
ctx: &C,
method: &str,
args: cbor::Value,
) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>>
Expand Down Expand Up @@ -322,7 +322,7 @@ mod tests {
#[automatically_derived]
impl<C: Cfg> MyModule<C> {
fn query_parameters<C: Context>(
_ctx: &mut C,
_ctx: &C,
_args: (),
) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error>
{
Expand Down Expand Up @@ -359,7 +359,7 @@ mod tests {
#[automatically_derived]
impl<C: Cfg> sdk::module::MethodHandler for MyModule<C> {
fn dispatch_query<C: Context>(
ctx: &mut C,
ctx: &C,
method: &str,
args: cbor::Value,
) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>>
Expand All @@ -381,7 +381,7 @@ mod tests {
}
#[automatically_derived]
impl<C: Cfg> MyModule<C> {
fn query_parameters<C: Context>(_ctx: &mut C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
fn query_parameters<C: Context>(_ctx: &C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
Ok(Self::params())
}
#[handler(query = "my_module.MyMC")]
Expand Down Expand Up @@ -438,7 +438,7 @@ mod tests {
impl<C: Cfg> sdk::module::MigrationHandler for MyModule<C> {
type Genesis = Genesis;
fn init_or_migrate<C: Context>(
_ctx: &mut C,
_ctx: &C,
meta: &mut sdk::modules::core::types::Metadata,
genesis: Self::Genesis,
) -> bool {
Expand Down Expand Up @@ -482,7 +482,7 @@ mod tests {
#[automatically_derived]
impl<C: Cfg> sdk::module::MethodHandler for MyModule<C> {
fn dispatch_query<C: Context>(
ctx: &mut C,
ctx: &C,
method: &str,
args: cbor::Value,
) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>>
Expand All @@ -504,7 +504,7 @@ mod tests {
}
#[automatically_derived]
impl<C: Cfg> MyModule<C> {
fn query_parameters<C: Context>(_ctx: &mut C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
fn query_parameters<C: Context>(_ctx: &C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
Ok(Self::params())
}
#[handler(query = "my_module.MyMC")]
Expand Down
4 changes: 2 additions & 2 deletions runtime-sdk/modules/contracts/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct Info {
/// Execution context.
pub struct ExecutionContext<'ctx, C: Context> {
/// Transaction context.
pub tx_context: &'ctx mut C,
pub tx_context: &'ctx C,
/// Contracts module parameters.
pub params: &'ctx Parameters,

Expand Down Expand Up @@ -127,7 +127,7 @@ impl<'ctx, C: Context> ExecutionContext<'ctx, C> {
caller_address: Address,
read_only: bool,
call_format: CallFormat,
tx_context: &'ctx mut C,
tx_context: &'ctx C,
) -> Self {
Self {
tx_context,
Expand Down
12 changes: 7 additions & 5 deletions runtime-sdk/modules/contracts/src/abi/oasis/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::convert::TryInto;

use oasis_contract_sdk_crypto as crypto;
use oasis_contract_sdk_types::crypto::SignatureKind;
use oasis_runtime_sdk::{context::Context, crypto::signature};
use oasis_runtime_sdk::{context::Context, crypto::signature, state::CurrentState};

use super::{memory::Region, OasisV1};
use crate::{
Expand Down Expand Up @@ -145,10 +145,12 @@ impl<Cfg: Config> OasisV1<Cfg> {
let pers = Region::from_arg((pers_ptr, pers_len))
.as_slice(&memory)
.map_err(|_| wasm3::Trap::Abort)?;
let mut rng = ec.tx_context.rng(pers).map_err(|e| {
ec.aborted = Some(e.into());
wasm3::Trap::Abort
})?;
let mut rng = CurrentState::with(|state| state.rng().fork(ec.tx_context, pers))
.map_err(|e| {
ec.aborted = Some(Error::ExecutionFailed(e.into()));
wasm3::Trap::Abort
})?;

let output = Region::from_arg((dst_ptr, num_bytes))
.as_slice_mut(&mut memory)
.map_err(|_| wasm3::Trap::Abort)?;
Expand Down
4 changes: 2 additions & 2 deletions runtime-sdk/modules/contracts/src/abi/oasis/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<Cfg: Config> OasisV1<Cfg> {
}

/// Perform environment query dispatch.
fn dispatch_query<Cfg: Config, C: Context>(ctx: &mut C, query: QueryRequest) -> QueryResponse {
fn dispatch_query<Cfg: Config, C: Context>(ctx: &C, query: QueryRequest) -> QueryResponse {
match query {
// Information about the current runtime block.
QueryRequest::BlockInfo => QueryResponse::BlockInfo {
Expand All @@ -124,7 +124,7 @@ fn dispatch_query<Cfg: Config, C: Context>(ctx: &mut C, query: QueryRequest) ->

/// Perform accounts API query dispatch.
fn dispatch_accounts_query<Cfg: Config, C: Context>(
_ctx: &mut C,
_ctx: &C,
query: AccountsQuery,
) -> QueryResponse {
match query {
Expand Down
Loading

0 comments on commit 0a8c744

Please sign in to comment.