Skip to content

Commit

Permalink
Merge pull request #319 from rumos-io/azkr
Browse files Browse the repository at this point in the history
Small changes based on azkr feedback
  • Loading branch information
joneskm authored Nov 5, 2024
2 parents 92fa316 + 807d57e commit 6e8a099
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 51 deletions.
8 changes: 8 additions & 0 deletions gears/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ impl From<SubspaceParseError> for ProtobufError {
Self::Core(CoreError::DecodeGeneral(value.to_string()))
}
}


impl From<tendermint::types::time::duration::DurationError> for ProtobufError
{
fn from(value: tendermint::types::time::duration::DurationError) -> Self {
Self::Core(CoreError::DecodeGeneral(value.to_string()))
}
}
2 changes: 2 additions & 0 deletions gears/src/utils/node/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct MockNode<App, G> {
pub struct StepResponse {
pub app_hash: Bytes,
pub tx_responses: Vec<ResponseDeliverTx>,
pub height: u32,
}

impl<G: Clone, App: ABCIApplication<G>> MockNode<App, G> {
Expand Down Expand Up @@ -126,6 +127,7 @@ impl<G: Clone, App: ABCIApplication<G>> MockNode<App, G> {
StepResponse {
app_hash: self.app_hash.clone(),
tx_responses,
height: self.height,
}
}

Expand Down
10 changes: 10 additions & 0 deletions gears/src/utils/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ impl User {
pub fn address(&self) -> AccAddress {
self.key_pair.get_address()
}

pub fn from_bech32(mnemonic: impl AsRef<str>, account_number: u64) -> Option<Self> {
let mnemonic = bip32::Mnemonic::new(mnemonic, bip32::Language::English).ok()?;
let key_pair = KeyPair::from_mnemonic(&mnemonic);

Some(Self {
key_pair,
account_number,
})
}
}

#[derive(Debug, Clone)]
Expand Down
30 changes: 4 additions & 26 deletions gears/src/utils/node/presets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::PathBuf;

use database::MemDB;
use keyring::key::pair::KeyPair;
use tendermint::types::{
chain_id::ChainId,
proto::{
Expand All @@ -14,11 +13,10 @@ use tendermint::types::{
use crate::{
application::handlers::node::ABCIHandler,
baseapp::{genesis::Genesis, options::NodeOptions, BaseApp},
crypto::keys::ReadAccAddress,
params::ParamsSubspaceKey,
};

use super::{InitState, MockApplication, MockNode, User};
use super::{InitState, MockApplication, MockNode};

#[derive(Debug, Clone, Default)]
pub enum GenesisSource<GS> {
Expand Down Expand Up @@ -46,7 +44,7 @@ impl<PSK: ParamsSubspaceKey, H: ABCIHandler, GS: Genesis> From<MockOptionsFormer

pub fn init_node<PSK: ParamsSubspaceKey, H: ABCIHandler<Genesis = GS>, GS: Genesis>(
opt: impl Into<MockOptions<PSK, H, GS>>,
) -> (MockNode<BaseApp<MemDB, PSK, H, MockApplication>, GS>, User) {
) -> MockNode<BaseApp<MemDB, PSK, H, MockApplication>, GS> {
let MockOptions {
baseapp_sbs_key,
node_opt,
Expand All @@ -61,11 +59,6 @@ pub fn init_node<PSK: ParamsSubspaceKey, H: ABCIHandler<Genesis = GS>, GS: Genes
BaseApp::new(db, baseapp_sbs_key, abci_handler, node_options);
let chain_id = ChainId::default();

let mnemonic = "race draft rival universe maid cheese steel logic crowd fork comic easy truth drift tomorrow eye buddy head time cash swing swift midnight borrow";
let mnemonic =
bip32::Mnemonic::new(mnemonic, bip32::Language::English).expect("Invalid mnemonic");
let key_pair = KeyPair::from_mnemonic(&mnemonic);
let address = key_pair.get_address();
let consensus_key = tendermint::crypto::new_private_key();

let app_genesis = match genesis {
Expand All @@ -79,16 +72,7 @@ pub fn init_node<PSK: ParamsSubspaceKey, H: ABCIHandler<Genesis = GS>, GS: Genes
serde_json::from_str(&genesis_state).expect("failed to deserialize genesis state")
}
GenesisSource::Genesis(genesis) => genesis,
GenesisSource::Default => {
let mut genesis = GS::default();
genesis
.add_genesis_account(
address.clone(),
"34uatom".parse().expect("hard coded coin is valid"),
)
.expect("won't fail since there's no existing account");
genesis
}
GenesisSource::Default => GS::default(),
};

let init_state = InitState {
Expand All @@ -105,11 +89,5 @@ pub fn init_node<PSK: ParamsSubspaceKey, H: ABCIHandler<Genesis = GS>, GS: Genes
initial_height: 1,
};

(
MockNode::new(app, init_state),
User {
key_pair,
account_number: 2,
},
)
MockNode::new(app, init_state)
}
7 changes: 7 additions & 0 deletions gears/src/x/keepers/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
address::AccAddress,
base::{coin::UnsignedCoin, coins::UnsignedCoins},
denom::Denom,
msg::send::MsgSend,
store::gas::errors::GasStoreErrors,
tx::metadata::Metadata,
},
Expand Down Expand Up @@ -46,6 +47,12 @@ pub trait BankKeeper<SK: StoreKey, M: Module>: Clone + Send + Sync + 'static {
amount: UnsignedCoins,
) -> Result<(), BankKeeperError>;

fn send_coins_from_account_to_account<DB: Database, CTX: TransactionalContext<DB, SK>>(
&self,
ctx: &mut CTX,
msg: &MsgSend,
) -> Result<(), BankKeeperError>;

fn send_coins_from_module_to_module<DB: Database, CTX: TransactionalContext<DB, SK>>(
&self,
ctx: &mut CTX,
Expand Down
13 changes: 12 additions & 1 deletion gears/src/x/keepers/mocks/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
pub struct MockBankKeeper {
pub get_denom_metadata: Option<Metadata>,
pub balance_all: Vec<UnsignedCoin>,
pub balance: UnsignedCoin,
pub balance: Option<UnsignedCoin>,
pub supply: HashMap<Denom, UnsignedCoin>,
}

Expand Down Expand Up @@ -106,4 +106,15 @@ impl<SK: StoreKey, M: Module> BankKeeper<SK, M> for MockBankKeeper {
) -> Result<(), crate::x::errors::BankKeeperError> {
Ok(())
}

fn send_coins_from_account_to_account<
DB: database::Database,
CTX: crate::context::TransactionalContext<DB, SK>,
>(
&self,
_ctx: &mut CTX,
_msg: &crate::types::msg::send::MsgSend,
) -> Result<(), BankKeeperError> {
Ok(())
}
}
2 changes: 1 addition & 1 deletion gears/src/x/keepers/mocks/gov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ impl<SK: StoreKey, M: Module> GovernanceBankKeeper<SK, M> for MockBankKeeper {
_: &address::AccAddress,
_: &crate::types::denom::Denom,
) -> Result<UnsignedCoin, GasStoreErrors> {
Ok(self.balance.clone())
Ok(self.balance.clone().expect("balances field in mock is not set"))
}
}
2 changes: 1 addition & 1 deletion tendermint/src/types/response/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::Bytes;

use crate::types::proto::crypto::ProofOps;

#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ResponseQuery {
pub code: u32,
/// bytes data = 2; // use "value" instead.
Expand Down
2 changes: 1 addition & 1 deletion x/auth/tests/abci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn test_init_and_few_blocks() {
.baseapp_sbs_key(SubspaceKey::BaseApp)
.genesis(GenesisSource::Genesis(GenesisState::default()));

let (mut node, _) = init_node(opt);
let mut node = init_node(opt);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
Expand Down
17 changes: 17 additions & 0 deletions x/bank/src/keeper/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ impl<
Ok(())
}

fn send_coins_from_account_to_account<DB: Database, CTX: TransactionalContext<DB, SK>>(
&self,
ctx: &mut CTX,
msg: &MsgSend,
) -> Result<(), BankKeeperError> {
self.send_coins(ctx, msg.clone())?;

// Create account if recipient does not exist

if !self.auth_keeper.has_account(ctx, &msg.to_address)? {
self.auth_keeper
.create_new_base_account(ctx, &msg.to_address)?;
};

Ok(())
}

fn send_coins_from_module_to_module<DB: Database, CTX: TransactionalContext<DB, SK>>(
&self,
ctx: &mut CTX,
Expand Down
17 changes: 0 additions & 17 deletions x/bank/src/keeper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,6 @@ impl<
(p_result, store)
}

pub fn send_coins_from_account_to_account<DB: Database, CTX: TransactionalContext<DB, SK>>(
&self,
ctx: &mut CTX,
msg: &MsgSend,
) -> Result<(), BankKeeperError> {
self.send_coins(ctx, msg.clone())?;

// Create account if recipient does not exist

if !self.auth_keeper.has_account(ctx, &msg.to_address)? {
self.auth_keeper
.create_new_base_account(ctx, &msg.to_address)?;
};

Ok(())
}

fn send_coins<DB: Database, CTX: TransactionalContext<DB, SK>>(
&self,
ctx: &mut CTX,
Expand Down
8 changes: 5 additions & 3 deletions x/bank/tests/abci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use gears::{
},
msg::send::MsgSend,
},
utils::node::{acc_address, generate_tx, init_node, GenesisSource, MockOptionsFormer},
utils::node::{acc_address, generate_tx, init_node, GenesisSource, MockOptionsFormer, User},
x::{keepers::mocks::auth::MockAuthKeeper, module::Module},
};

Expand All @@ -34,7 +34,7 @@ fn test_init_and_few_blocks() {
.baseapp_sbs_key(SubspaceKey::BaseApp)
.genesis(GenesisSource::Genesis(GenesisState::default()));

let (mut node, _) = init_node(opt);
let mut node = init_node(opt);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
Expand Down Expand Up @@ -74,7 +74,9 @@ fn test_init_and_sending_tx() {
.baseapp_sbs_key(SubspaceKey::BaseApp)
.genesis(GenesisSource::Genesis(genesis));

let (mut node, user) = init_node(opt);
let mut node = init_node(opt);

let user = User::from_bech32("race draft rival universe maid cheese steel logic crowd fork comic easy truth drift tomorrow eye buddy head time cash swing swift midnight borrow", 1).unwrap_test();

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion x/staking/tests/abci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_init_and_few_blocks() {
.baseapp_sbs_key(SubspaceKey::BaseApp)
.genesis(GenesisSource::Default);

let (mut node, _) = init_node(opt);
let mut node = init_node(opt);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
Expand Down

0 comments on commit 6e8a099

Please sign in to comment.