Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
impl: use Payable trait (#153)
Browse files Browse the repository at this point in the history
* changes for impl Payable

* update impl Payable args

* use ModuleId

* use StateItemEncoder and StateItemDecoder

* update mocks

* rm unused import

* use symlinked constants.json

* use ibc_transfer module id

* fn compute_module_address

* use module address

* update compute_module_address

* update ChainStateConfig

* impl Debug and Clone separately

* use custom clone method

* fix clippy

* fix tests

* bump sov-sdk rev

* fix compile

* update cargo lockfile

* update sov-sdk-wip rev in nix flake

* update nix-flake lockfile

* update comment
  • Loading branch information
rnbguy authored Apr 29, 2024
1 parent 3cce447 commit 5a67e4b
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 257 deletions.
335 changes: 175 additions & 160 deletions Cargo.lock

Large diffs are not rendered by default.

33 changes: 0 additions & 33 deletions constants.json

This file was deleted.

1 change: 1 addition & 0 deletions constants.json
22 changes: 11 additions & 11 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

sovereign-sdk-src = {
flake = false;
url = git+ssh://[email protected]/informalsystems/sovereign-sdk-wip?rev=20bfd68c8c740b447d51eb50381b5590102258bb;
url = git+ssh://[email protected]/informalsystems/sovereign-sdk-wip?rev=63fa5f110ebb323100ff740e6b152ba42a6ae84c;
};
};

Expand Down
2 changes: 1 addition & 1 deletion mocks/src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<S: Spec, Da: DaService> TestSetupConfig<S, Da> {

pub fn kernel_genesis_config(&self) -> BasicKernelGenesisConfig<S, Da::Spec> {
BasicKernelGenesisConfig {
chain_state: self.rollup_genesis_config.chain_state_config.clone(),
chain_state: self.rollup_genesis_config.cloned_chain_state_config(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions mocks/src/sovereign/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::{Arc, Mutex};
use ibc_client_tendermint::types::Header;
use ibc_core::client::types::Height;
use ibc_core::host::types::identifiers::ChainId;
use sov_bank::{CallMessage as BankCallMessage, TokenConfig, TokenId};
use sov_bank::{CallMessage as BankCallMessage, Payable, TokenConfig, TokenId};
use sov_celestia_client::types::client_message::test_util::dummy_sov_header;
use sov_celestia_client::types::client_message::SovTmHeader;
use sov_consensus_state_tracker::{ConsensusStateTracker, HasConsensusState};
Expand Down Expand Up @@ -144,12 +144,12 @@ where
}

/// Returns the balance of a user for a given token
pub fn get_balance_of(&self, user_address: S::Address, token_id: TokenId) -> u64 {
pub fn get_balance_of(&self, user_address: &S::Address, token_id: TokenId) -> u64 {
let mut working_set: WorkingSet<S> = WorkingSet::new(self.prover_storage());

self.runtime()
.bank
.get_balance_of(user_address, token_id, &mut working_set)
.get_balance_of(user_address.as_token_holder(), token_id, &mut working_set)
.unwrap()
}

Expand Down
68 changes: 62 additions & 6 deletions mocks/src/sovereign/runtime/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};

use sov_bank::{BankConfig, GasTokenConfig};
use sov_chain_state::ChainStateConfig;
use sov_ibc::ExampleModuleConfig;
use sov_ibc_transfer::TransferConfig;
use sov_modules_api::utils::generate_address as gen_address_generic;
use sov_modules_api::{Gas, GasArray, Spec};
use sov_modules_api::{Spec, Zkvm};
use sov_rollup_interface::da::Time;
use sov_rollup_interface::zk::CodeCommitment;

/// The default initial slot height.
pub const DEFAULT_INIT_HEIGHT: u64 = 1;
Expand All @@ -23,14 +25,65 @@ pub const DEFAULT_GAS_TOKEN_NAME: &str = "sov-gas-token";
/// The default salt.
pub const DEFAULT_SALT: u64 = 0;

#[derive(Clone, Debug)]
pub struct RollupGenesisConfig<S: Spec> {
pub chain_state_config: ChainStateConfig<S>,
pub bank_config: BankConfig<S>,
pub ibc_config: ExampleModuleConfig,
pub ibc_transfer_config: TransferConfig,
}

impl<S: Spec> RollupGenesisConfig<S> {
pub fn cloned_chain_state_config(&self) -> ChainStateConfig<S> {
ChainStateConfig {
current_time: self.chain_state_config.current_time.clone(),
genesis_da_height: self.chain_state_config.genesis_da_height,
inner_code_commitment: self.chain_state_config.inner_code_commitment.clone(),
outer_code_commitment: self.chain_state_config.outer_code_commitment.clone(),
}
}
}

impl<S: Spec> Clone for RollupGenesisConfig<S> {
fn clone(&self) -> Self {
Self {
chain_state_config: self.cloned_chain_state_config(),
bank_config: self.bank_config.clone(),
ibc_config: self.ibc_config.clone(),
ibc_transfer_config: self.ibc_transfer_config.clone(),
}
}
}

impl<S: Spec> fmt::Debug for RollupGenesisConfig<S>
where
<S::InnerZkvm as Zkvm>::CodeCommitment: fmt::Debug,
<S::OuterZkvm as Zkvm>::CodeCommitment: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RollupGenesisConfig")
.field(
"chain_state_config.current_time",
&self.chain_state_config.current_time,
)
.field(
"chain_state_config.inner_code_commitment",
&self.chain_state_config.inner_code_commitment,
)
.field(
"chain_state_config.outer_code_commitment",
&self.chain_state_config.outer_code_commitment,
)
.field(
"chain_state_config.genesis_da_height",
&self.chain_state_config.genesis_da_height,
)
.field("bank_config", &self.bank_config)
.field("ibc_config", &self.ibc_config)
.field("ibc_transfer_config", &self.ibc_transfer_config)
.finish()
}
}

impl<S: Spec> RollupGenesisConfig<S> {
pub fn new(
chain_state_config: ChainStateConfig<S>,
Expand Down Expand Up @@ -77,10 +130,13 @@ pub fn create_chain_state_config<S: Spec>() -> ChainStateConfig<S> {

ChainStateConfig {
current_time: Time::from_secs(seconds.try_into().unwrap()),
gas_price_blocks_depth: 10,
gas_price_maximum_elasticity: 1,
initial_gas_price: <<S as Spec>::Gas as Gas>::Price::ZEROED,
minimum_gas_price: <<S as Spec>::Gas as Gas>::Price::ZEROED,
genesis_da_height: 0,
// These are for `MockCodeCommitment`, works with `MockZkVerifier`, which
// is being used in `DefaultSpec`. These may fail for other zk verifiers.
inner_code_commitment: <S::InnerZkvm as Zkvm>::CodeCommitment::decode([0u8; 32].as_slice())
.unwrap(),
outer_code_commitment: <S::OuterZkvm as Zkvm>::CodeCommitment::decode([0u8; 32].as_slice())
.unwrap(),
}
}

Expand Down
14 changes: 7 additions & 7 deletions mocks/src/tests/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn test_escrow_unescrow_on_sov() {
let sender_balance = rly
.src_chain_ctx()
.service()
.get_balance_of(cfg.sov_address, gas_token.token_id);
.get_balance_of(&cfg.sov_address, gas_token.token_id);

assert_eq!(sender_balance, expected_sender_balance);

Expand Down Expand Up @@ -100,7 +100,7 @@ async fn test_escrow_unescrow_on_sov() {
let fake_token_sender_initial_balance = rly
.src_chain_ctx()
.service()
.get_balance_of(cfg.sov_address, fake_token_id);
.get_balance_of(&cfg.sov_address, fake_token_id);

let msg_transfer_on_sov = rly.build_msg_transfer_for_sov(&cfg);

Expand All @@ -116,14 +116,14 @@ async fn test_escrow_unescrow_on_sov() {
let gas_token_sender_balance = rly
.src_chain_ctx()
.service()
.get_balance_of(cfg.sov_address, gas_token.token_id);
.get_balance_of(&cfg.sov_address, gas_token.token_id);

assert_eq!(gas_token_sender_balance, expected_sender_balance);

let fake_token_sender_balance = rly
.src_chain_ctx()
.service()
.get_balance_of(cfg.sov_address, fake_token_id);
.get_balance_of(&cfg.sov_address, fake_token_id);

assert_eq!(
fake_token_sender_balance,
Expand Down Expand Up @@ -256,7 +256,7 @@ async fn test_mint_burn_on_sov() {
let receiver_balance = rly
.src_chain_ctx()
.service()
.get_balance_of(cfg.sov_address, minted_token_id);
.get_balance_of(&cfg.sov_address, minted_token_id);

let mut expected_receiver_balance = cfg.amount * 2;

Expand Down Expand Up @@ -307,7 +307,7 @@ async fn test_mint_burn_on_sov() {
let receiver_balance = rly
.src_chain_ctx()
.service()
.get_balance_of(cfg.sov_address, minted_token_id);
.get_balance_of(&cfg.sov_address, minted_token_id);

expected_receiver_balance -= cfg.amount;

Expand Down Expand Up @@ -339,7 +339,7 @@ async fn test_mint_burn_on_sov() {
let receiver_balance = rly
.src_chain_ctx()
.service()
.get_balance_of(cfg.sov_address, minted_token_id);
.get_balance_of(&cfg.sov_address, minted_token_id);

assert_eq!(receiver_balance, expected_receiver_balance);
}
30 changes: 17 additions & 13 deletions modules/sov-ibc-transfer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use ibc_core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
use ibc_core::primitives::Signer;
use ibc_core::router::module::Module;
use ibc_core::router::types::module::ModuleExtras;
use sov_bank::{Coins, TokenId};
use sov_bank::{Coins, IntoPayable, Payable, TokenId};
use sov_modules_api::{Context, Spec, WorkingSet};
use uint::FromDecStrErr;

use super::IbcTransfer;
use crate::utils::compute_escrow_address;
use crate::utils::{compute_escrow_address, compute_module_address};

/// Using a different salt will result in a different token address. Since
/// ICS-20 tokens coming from other chains are guaranteed to have unique names,
Expand Down Expand Up @@ -160,13 +160,17 @@ impl<'ws, S: Spec> IbcTransferContext<'ws, S> {
fn validate_balance(
&self,
token_id: TokenId,
address: S::Address,
address: &S::Address,
amount: Amount,
) -> Result<Amount, TokenTransferError> {
let sender_balance: u64 = self
.ibc_transfer
.bank
.get_balance_of(address, token_id, *self.working_set.borrow_mut())
.get_balance_of(
address.as_token_holder(),
token_id,
*self.working_set.borrow_mut(),
)
.ok_or(TokenTransferError::Other(format!(
"No balance for token with ID: '{token_id}'"
)))?;
Expand All @@ -193,11 +197,11 @@ impl<'ws, S: Spec> IbcTransferContext<'ws, S> {
fn create_token(
&self,
token_name: String,
minter_address: S::Address,
minter_address: &S::Address,
) -> Result<TokenId, TokenTransferError> {
// Make sure to use `ibc_transfer` address as the sender
let context = Context::new(
self.ibc_transfer.address.clone(),
compute_module_address::<S>(self.ibc_transfer.id.as_bytes()),
self.sdk_context.sequencer().clone(),
self.sdk_context.visible_slot_number(),
);
Expand All @@ -209,8 +213,8 @@ impl<'ws, S: Spec> IbcTransferContext<'ws, S> {
token_name.clone(),
SALT,
0,
minter_address,
vec![self.ibc_transfer.address.clone()],
minter_address.as_token_holder(),
vec![self.ibc_transfer.id.to_payable()],
&context,
&mut self.working_set.borrow_mut(),
)
Expand Down Expand Up @@ -306,7 +310,7 @@ where

let minted_token_id = self.get_ibc_token_id(coin)?;

self.validate_balance(minted_token_id, account.address.clone(), coin.amount)?;
self.validate_balance(minted_token_id, &account.address, coin.amount)?;

Ok(())
}
Expand All @@ -329,7 +333,7 @@ where

let token_id = self.get_native_token_id(coin, port_id, channel_id)?;

self.validate_balance(token_id, from_account.address.clone(), coin.amount)?;
self.validate_balance(token_id, &from_account.address, coin.amount)?;

Ok(())
}
Expand Down Expand Up @@ -359,7 +363,7 @@ where

let escrow_address = self.obtain_escrow_address(port_id, channel_id);

self.validate_balance(token_id, escrow_address, coin.amount)?;
self.validate_balance(token_id, &escrow_address, coin.amount)?;

Ok(())
}
Expand All @@ -377,7 +381,7 @@ impl<'ws, S: Spec> TokenTransferExecutionContext for IbcTransferContext<'ws, S>
// create a new token and store in the maps
let token_id = match self.get_ibc_token_id(coin) {
Ok(token_id) => token_id,
Err(_) => self.create_token(coin.denom.to_string(), account.address.clone())?,
Err(_) => self.create_token(coin.denom.to_string(), &account.address)?,
};

// 2. mint tokens
Expand All @@ -391,7 +395,7 @@ impl<'ws, S: Spec> TokenTransferExecutionContext for IbcTransferContext<'ws, S>
.mint(
&sdk_coins,
&account.address,
&self.ibc_transfer.address,
self.ibc_transfer.id.to_payable(),
&mut self.working_set.borrow_mut(),
)
.map_err(|err| TokenTransferError::Other(err.to_string()))?;
Expand Down
Loading

0 comments on commit 5a67e4b

Please sign in to comment.