From b5b3ba5f44f76e2d7a7099c724c19cc3a8965039 Mon Sep 17 00:00:00 2001 From: Warren He Date: Fri, 11 Mar 2022 12:56:30 -0800 Subject: [PATCH] modules/consensus_accounts: handle incoming message tokens --- runtime-sdk/src/modules/accounts/mod.rs | 20 +++++++++ .../src/modules/consensus_accounts/mod.rs | 44 ++++++++++++++++++- runtime-sdk/src/types/address.rs | 6 +++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/runtime-sdk/src/modules/accounts/mod.rs b/runtime-sdk/src/modules/accounts/mod.rs index bda154829b..bb69f7ef70 100644 --- a/runtime-sdk/src/modules/accounts/mod.rs +++ b/runtime-sdk/src/modules/accounts/mod.rs @@ -212,6 +212,11 @@ pub trait API { amount: &token::BaseUnits, ) -> Result<(), modules::core::Error>; + fn mint_into_fee_accumulator( + ctx: &mut C, + amount: &token::BaseUnits, + ) -> Result<(), modules::core::Error>; + /// Move amount from fee accumulator into address. fn move_from_fee_accumulator( ctx: &mut C, @@ -587,6 +592,21 @@ impl API for Module { Ok(()) } + fn mint_into_fee_accumulator( + ctx: &mut C, + amount: &token::BaseUnits, + ) -> Result<(), modules::core::Error> { + if ctx.is_simulation() { + return Ok(()); + } + + ctx.value::(CONTEXT_KEY_FEE_ACCUMULATOR) + .or_default() + .add(amount); + + Ok(()) + } + fn move_from_fee_accumulator( ctx: &mut C, to: Address, diff --git a/runtime-sdk/src/modules/consensus_accounts/mod.rs b/runtime-sdk/src/modules/consensus_accounts/mod.rs index f16ce0c711..c0c95209db 100644 --- a/runtime-sdk/src/modules/consensus_accounts/mod.rs +++ b/runtime-sdk/src/modules/consensus_accounts/mod.rs @@ -4,10 +4,16 @@ //! while keeping track of amount deposited per account. use std::{collections::BTreeSet, convert::TryInto}; +use num_traits::Zero; use once_cell::sync::Lazy; use thiserror::Error; -use oasis_core_runtime::consensus::staking::Account as ConsensusAccount; +use oasis_core_runtime::{ + consensus::{ + staking::Account as ConsensusAccount, + roothash, + }, +}; use oasis_runtime_sdk_macros::{handler, sdk_derive}; use crate::{ @@ -20,9 +26,10 @@ use crate::{ storage::Prefix, types::{ address::Address, + in_msg::IncomingMessageData, message::{MessageEvent, MessageEventHookInvocation}, token, - transaction::AuthInfo, + transaction::{AuthInfo, Transaction}, }, }; @@ -416,6 +423,39 @@ impl { } +impl + module::IncomingMessageHandler for Module +{ + fn prefetch_in_msg( + _prefixes: &mut BTreeSet, + _in_msg: &roothash::IncomingMessage, + _data: &IncomingMessageData, + _tx: &Option, + ) -> Result<(), error::RuntimeError> { + // todo: their account + Ok(()) + } + + fn execute_in_msg( + ctx: &mut C, + in_msg: &roothash::IncomingMessage, + _data: &IncomingMessageData, + _tx: &Option, + ) -> Result<(), error::RuntimeError> { + if !in_msg.fee.is_zero() { + let amount = token::BaseUnits(Consensus::amount_from_consensus(ctx, (&in_msg.fee).try_into().unwrap()).unwrap(), Consensus::consensus_denomination(ctx).unwrap()); + Accounts::mint_into_fee_accumulator(ctx, &amount).unwrap(); + // TODO: Emit event that fee has been paid. + } + if !in_msg.tokens.is_zero() { + let amount = token::BaseUnits(Consensus::amount_from_consensus(ctx, (&in_msg.tokens).try_into().unwrap()).unwrap(), Consensus::consensus_denomination(ctx).unwrap()); + Accounts::mint(ctx, (&in_msg.caller).into(), &amount).unwrap(); + // TODO: Emit event. + } + Ok(()) + } +} + impl module::BlockHandler for Module { diff --git a/runtime-sdk/src/types/address.rs b/runtime-sdk/src/types/address.rs index d4ff0a340f..09e6be284b 100644 --- a/runtime-sdk/src/types/address.rs +++ b/runtime-sdk/src/types/address.rs @@ -250,6 +250,12 @@ impl From
for ConsensusAddress { } } +impl From<&ConsensusAddress> for Address { + fn from(addr: &ConsensusAddress) -> Address { + Address::from_bytes(addr.as_ref()).unwrap() + } +} + #[cfg(test)] mod test { use super::*;