diff --git a/Cargo.lock b/Cargo.lock index 5b76d69af5..0ec6bf5ccb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3679,7 +3679,7 @@ dependencies = [ [[package]] name = "test-runtime-components-rofl" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "async-trait", diff --git a/runtime-sdk/src/modules/rofl/app/client.rs b/runtime-sdk/src/modules/rofl/app/client.rs index 4f4eae1b4e..2997b6e9b6 100644 --- a/runtime-sdk/src/modules/rofl/app/client.rs +++ b/runtime-sdk/src/modules/rofl/app/client.rs @@ -1,4 +1,7 @@ -use std::{collections::HashSet, sync::Arc}; +use std::{ + collections::{BTreeMap, HashSet}, + sync::Arc, +}; use anyhow::{anyhow, Result}; use tokio::sync::{mpsc, oneshot}; @@ -16,10 +19,7 @@ use crate::{ }, crypto::signature::{PublicKey, Signer}, enclave_rpc::{QueryRequest, METHOD_QUERY}, - modules::{ - accounts::API as _, - core::{types::EstimateGasQuery, API as _}, - }, + modules::{accounts::types::NonceQuery, core::types::EstimateGasQuery}, state::CurrentState, storage::HostStore, types::{ @@ -27,7 +27,6 @@ use crate::{ token, transaction::{self, CallerAddress}, }, - Runtime, }; use super::{processor, App}; @@ -66,19 +65,17 @@ where /// Retrieve the nonce for the given account. pub async fn account_nonce(&self, round: u64, address: Address) -> Result { - self.with_store_for_round(round, move || { - Ok(::Accounts::get_nonce(address)?) - }) - .await + self.query(round, "accounts.Nonce", NonceQuery { address }) + .await } /// Retrieve the gas price in the given denomination. - pub async fn gas_price(&self, round: u64, denom: token::Denomination) -> Result { - self.with_store_for_round(round, move || { - ::Core::min_gas_price(&denom) - .ok_or(anyhow!("denomination not supported")) - }) - .await + pub async fn gas_price(&self, round: u64, denom: &token::Denomination) -> Result { + let mgp: BTreeMap = + self.query(round, "core.MinGasPrice", ()).await?; + mgp.get(denom) + .ok_or(anyhow!("denomination not supported")) + .copied() } /// Securely query the on-chain runtime component. @@ -194,7 +191,7 @@ where } // Determine gas price. Currently we always use the native denomination. - let mgp = self.gas_price(round, token::Denomination::NATIVE).await?; + let mgp = self.gas_price(round, &token::Denomination::NATIVE).await?; let fee = mgp.saturating_mul(tx.fee_gas().into()); tx.set_fee_amount(token::BaseUnits::new(fee, token::Denomination::NATIVE)); @@ -231,7 +228,7 @@ where } /// Run a closure inside a `CurrentState` context with store for the given round. - async fn with_store_for_round(&self, round: u64, f: F) -> Result + pub async fn with_store_for_round(&self, round: u64, f: F) -> Result where F: FnOnce() -> Result + Send + 'static, R: Send + 'static, @@ -242,7 +239,7 @@ where } /// Return a store corresponding to the given round. - async fn store_for_round(&self, round: u64) -> Result { + pub async fn store_for_round(&self, round: u64) -> Result { HostStore::new_for_round( self.state.host.clone(), &self.state.consensus_verifier, diff --git a/runtime-sdk/src/modules/rofl/app/mod.rs b/runtime-sdk/src/modules/rofl/app/mod.rs index bddc7eb023..fef1fb25b3 100644 --- a/runtime-sdk/src/modules/rofl/app/mod.rs +++ b/runtime-sdk/src/modules/rofl/app/mod.rs @@ -7,14 +7,14 @@ use tokio::sync::mpsc; use crate::{ core::{ + common::version, config::Config, - consensus::roothash, + consensus::{roothash, verifier::TrustRoot}, dispatcher::{PostInitState, PreInitState}, rofl, start_runtime, }, crypto, types::transaction, - Runtime, }; mod client; @@ -31,14 +31,18 @@ pub use env::Environment; #[allow(unused_variables)] #[async_trait] pub trait App: Send + Sync + 'static { - /// Runtime to attach the application to. - /// - /// The runtime must have the ROFL module enabled so that the application can register to it. - type AttachTo: Runtime; + /// ROFL application version. + const VERSION: version::Version; /// Identifier of the application (used for registrations). fn id() -> AppId; + /// Return the consensus layer trust root for this runtime; if `None`, consensus layer integrity + /// verification will not be performed. + fn consensus_trust_root() -> Option { + None + } + /// Create a new unsigned transaction. fn new_transaction(&self, method: &str, body: B) -> transaction::Transaction where @@ -86,10 +90,8 @@ pub trait App: Send + Sync + 'static { } }), Config { - // Use the same version as the runtime we are attaching to. - version: Self::AttachTo::VERSION, - // Use the same trust root as the runtime we are attaching to. - trust_root: Self::AttachTo::consensus_trust_root(), + version: Self::VERSION, + trust_root: Self::consensus_trust_root(), ..Default::default() }, ); diff --git a/tests/runtimes/components-rofl/Cargo.toml b/tests/runtimes/components-rofl/Cargo.toml index 305a21443d..f5a10fb896 100644 --- a/tests/runtimes/components-rofl/Cargo.toml +++ b/tests/runtimes/components-rofl/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "test-runtime-components-rofl" -version = "0.1.0" +version = "0.2.0" authors = ["Oasis Protocol Foundation "] edition = "2021" license = "Apache-2.0" diff --git a/tests/runtimes/components-rofl/src/main.rs b/tests/runtimes/components-rofl/src/main.rs index 73c7791fd9..c4272dd900 100644 --- a/tests/runtimes/components-rofl/src/main.rs +++ b/tests/runtimes/components-rofl/src/main.rs @@ -3,14 +3,18 @@ use std::sync::Arc; use anyhow::Result; use async_trait::async_trait; -use oasis_runtime_sdk::modules::rofl::app::{App, AppId, Environment}; +use oasis_runtime_sdk::{ + self as sdk, + modules::rofl::app::{App, AppId, Environment}, + Version, +}; struct TestApp; #[async_trait] impl App for TestApp { - /// Runtime to attach the application to. - type AttachTo = components_ronl::Runtime; + /// Application version. + const VERSION: Version = sdk::version_from_cargo!(); /// Identifier of the application (used for registrations). ///