Skip to content

Commit

Permalink
Merge pull request #117 from oasisprotocol/pro-wh/feature/consensusgas
Browse files Browse the repository at this point in the history
modules/consensus_accounts: add gas costs for transactions
  • Loading branch information
pro-wh authored May 5, 2021
2 parents b561308 + 97cec82 commit 82812f1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
46 changes: 42 additions & 4 deletions runtime-sdk/src/modules/consensus_accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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).
Expand All @@ -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 {
Expand Down Expand Up @@ -163,6 +193,9 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
{
/// 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")
Expand All @@ -175,6 +208,9 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>

/// 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()
Expand Down Expand Up @@ -269,7 +305,7 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API> modul
const VERSION: u32 = 1;
type Error = Error;
type Event = Event;
type Parameters = ();
type Parameters = Parameters;
}

/// Module methods.
Expand Down Expand Up @@ -360,13 +396,15 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API> 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;
}
Expand Down
20 changes: 19 additions & 1 deletion tests/runtimes/simple-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ impl sdk::Runtime for Runtime {
type Modules = (
modules::accounts::Module,
modules::consensus_accounts::Module<modules::accounts::Module, modules::consensus::Module>,
modules::core::Module,
);

fn genesis_state() -> <Self::Modules as sdk::module::MigrationHandler>::Genesis {
Default::default()
(
Default::default(),
modules::consensus_accounts::Genesis {
parameters: modules::consensus_accounts::Parameters {
gas_costs: modules::consensus_accounts::GasCosts {
// These are free, in order to simplify testing. We do test gas accounting
// with other methods elsewhere though.
tx_deposit: 0,
tx_withdraw: 0,
},
},
},
modules::core::Genesis {
parameters: modules::core::Parameters {
max_batch_gas: 10_000,
},
},
)
}
}

0 comments on commit 82812f1

Please sign in to comment.