From 781db209761987dd9dd1c7991c984d21596a5ea3 Mon Sep 17 00:00:00 2001 From: Warren He Date: Fri, 30 Apr 2021 14:44:11 -0700 Subject: [PATCH 1/2] modules/consensus_accounts: add gas costs for transactions --- .../src/modules/consensus_accounts/mod.rs | 46 +++++++++++++++++-- tests/runtimes/simple-consensus/src/lib.rs | 18 +++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/runtime-sdk/src/modules/consensus_accounts/mod.rs b/runtime-sdk/src/modules/consensus_accounts/mod.rs index 121ccecfcf..0784fb7035 100644 --- a/runtime-sdk/src/modules/consensus_accounts/mod.rs +++ b/runtime-sdk/src/modules/consensus_accounts/mod.rs @@ -13,6 +13,7 @@ use crate::{ module, module::{CallableMethodInfo, Module as _, QueryMethodInfo}, modules, + modules::core::{Module as Core, API as _}, types::{ address::Address, message::{MessageEvent, MessageEventHookInvocation}, @@ -45,6 +46,32 @@ pub enum Error { #[error("consensus: {0}")] #[sdk_error(transparent)] Consensus(#[from] modules::consensus::Error), + + #[error("core: {0}")] + #[sdk_error(transparent)] + Core(#[from] modules::core::Error), +} + +/// Gas costs. +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GasCosts { + #[serde(rename = "tx_deposit")] + pub tx_deposit: u64, + #[serde(rename = "tx_withdraw")] + pub tx_withdraw: u64, +} + +/// Parameters for the consensus module. +#[derive(Clone, Default, Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Parameters { + #[serde(rename = "gas_costs")] + pub gas_costs: GasCosts, +} + +impl module::Parameters for Parameters { + type Error = (); } /// Events emitted by the consensus module (none so far). @@ -55,7 +82,10 @@ pub enum Event {} /// Genesis state for the consensus module. #[derive(Clone, Debug, Default, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct Genesis {} +pub struct Genesis { + #[serde(rename = "parameters")] + pub parameters: Parameters, +} /// Interface that can be called from other modules. pub trait API { @@ -163,6 +193,9 @@ impl { /// Deposit in the runtime. fn tx_deposit(ctx: &mut TxContext<'_, '_>, body: types::Deposit) -> Result<(), Error> { + let params = Self::params(ctx.runtime_state()); + Core::use_gas(ctx, params.gas_costs.tx_deposit)?; + let signer = &ctx .tx_auth_info() .expect("should be called with a transaction ctx") @@ -175,6 +208,9 @@ impl /// Withdraw from the runtime. fn tx_withdraw(ctx: &mut TxContext<'_, '_>, body: types::Withdraw) -> Result<(), Error> { + let params = Self::params(ctx.runtime_state()); + Core::use_gas(ctx, params.gas_costs.tx_withdraw)?; + // Signer. let signer = &ctx .tx_auth_info() @@ -269,7 +305,7 @@ impl modul const VERSION: u32 = 1; type Error = Error; type Event = Event; - type Parameters = (); + type Parameters = Parameters; } /// Module methods. @@ -360,13 +396,15 @@ impl modul type Genesis = Genesis; fn init_or_migrate( - _ctx: &mut DispatchContext<'_>, + ctx: &mut DispatchContext<'_>, meta: &mut modules::core::types::Metadata, - _genesis: &Self::Genesis, + genesis: &Self::Genesis, ) -> bool { let version = meta.versions.get(Self::NAME).copied().unwrap_or_default(); if version == 0 { // Initialize state from genesis. + // Set genesis parameters. + Self::set_params(ctx.runtime_state(), &genesis.parameters); meta.versions.insert(Self::NAME.to_owned(), Self::VERSION); return true; } diff --git a/tests/runtimes/simple-consensus/src/lib.rs b/tests/runtimes/simple-consensus/src/lib.rs index cb82efc771..b04fde985c 100644 --- a/tests/runtimes/simple-consensus/src/lib.rs +++ b/tests/runtimes/simple-consensus/src/lib.rs @@ -10,9 +10,25 @@ impl sdk::Runtime for Runtime { type Modules = ( modules::accounts::Module, modules::consensus_accounts::Module, + modules::core::Module, ); fn genesis_state() -> ::Genesis { - Default::default() + ( + Default::default(), + modules::consensus_accounts::Genesis { + parameters: modules::consensus_accounts::Parameters { + gas_costs: modules::consensus_accounts::GasCosts { + tx_deposit: 100, + tx_withdraw: 100, + }, + }, + }, + modules::core::Genesis { + parameters: modules::core::Parameters { + max_batch_gas: 10_000, + }, + }, + ) } } From 97cec8282280dc278632598c2189ca763a509079 Mon Sep 17 00:00:00 2001 From: Warren He Date: Wed, 5 May 2021 10:19:02 -0700 Subject: [PATCH 2/2] tests: make simple-consensus free --- tests/runtimes/simple-consensus/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/runtimes/simple-consensus/src/lib.rs b/tests/runtimes/simple-consensus/src/lib.rs index b04fde985c..33e321cdbc 100644 --- a/tests/runtimes/simple-consensus/src/lib.rs +++ b/tests/runtimes/simple-consensus/src/lib.rs @@ -19,8 +19,10 @@ impl sdk::Runtime for Runtime { modules::consensus_accounts::Genesis { parameters: modules::consensus_accounts::Parameters { gas_costs: modules::consensus_accounts::GasCosts { - tx_deposit: 100, - tx_withdraw: 100, + // These are free, in order to simplify testing. We do test gas accounting + // with other methods elsewhere though. + tx_deposit: 0, + tx_withdraw: 0, }, }, },