Skip to content

Commit

Permalink
Merge of #3733
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] committed Sep 6, 2024
2 parents 80e767e + 8271805 commit ed10c2f
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/3733-typed-chain-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Switched to use typed ChainId instead of a string in all `fn get_chain_id`.
([\#3733](https://github.com/anoma/namada/pull/3733))
5 changes: 2 additions & 3 deletions crates/governance/src/finalize_block.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Governance logic applied on an end of a block.

use std::collections::BTreeSet;
use std::str::FromStr;

use borsh::BorshDeserialize;
use namada_core::address::Address;
use namada_core::chain::{ChainId, Epoch};
use namada_core::chain::Epoch;
use namada_core::collections::HashMap;
use namada_core::encode;
use namada_core::ibc::PGFIbcTarget;
Expand Down Expand Up @@ -374,7 +373,7 @@ where
state.write(&pending_execution_key, ())?;

let mut tx = Tx::from_type(TxType::Raw);
tx.header.chain_id = ChainId::from_str(&state.get_chain_id()?).unwrap();
tx.header.chain_id = state.get_chain_id()?;
tx.set_data(Data::new(encode(&id)));
tx.set_code(Code::new(proposal_code, None));

Expand Down
3 changes: 2 additions & 1 deletion crates/ibc/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ibc::apps::transfer::types::PrefixedCoin;
use ibc::core::channel::types::timeout::TimeoutHeight;
use namada_core::address::Address;
use namada_core::borsh::{BorshSerialize, BorshSerializeExt};
use namada_core::chain::ChainId;
use namada_core::ibc::PGFIbcTarget;
use namada_core::tendermint::Time as TmTime;
use namada_core::token::Amount;
Expand Down Expand Up @@ -65,7 +66,7 @@ where
self.state.iter_next(iter)
}

fn get_chain_id(&self) -> Result<String> {
fn get_chain_id(&self) -> Result<ChainId> {
self.state.get_chain_id()
}

Expand Down
6 changes: 3 additions & 3 deletions crates/ibc/src/vp/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::marker::PhantomData;

use namada_core::address::Address;
use namada_core::arith::checked;
use namada_core::chain::{BlockHeader, BlockHeight, Epoch, Epochs};
use namada_core::chain::{BlockHeader, BlockHeight, ChainId, Epoch, Epochs};
use namada_core::collections::{HashMap, HashSet};
use namada_core::storage::{Key, TxIndex};
use namada_events::Event;
Expand Down Expand Up @@ -142,7 +142,7 @@ Self: 'iter;
self.ctx.iter_next(iter)
}

fn get_chain_id(&self) -> Result<String> {
fn get_chain_id(&self) -> Result<ChainId> {
self.ctx.get_chain_id()
}

Expand Down Expand Up @@ -332,7 +332,7 @@ where
self.ctx.iter_next(iter)
}

fn get_chain_id(&self) -> Result<String> {
fn get_chain_id(&self) -> Result<ChainId> {
self.ctx.get_chain_id()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ibc/src/vp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ where
let unbonding_period_secs =
checked!(pipeline_len * epoch_duration.min_duration.0)?;
Ok(ValidationParams {
chain_id: IbcChainId::from_str(&chain_id)
chain_id: IbcChainId::from_str(chain_id.as_str())
.map_err(ActionError::ChainId)?,
proof_specs: proof_specs
.try_into()
Expand Down
4 changes: 2 additions & 2 deletions crates/state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ where
}

/// Get the chain ID as a raw string
pub fn get_chain_id(&self) -> (String, u64) {
pub fn get_chain_id(&self) -> (ChainId, u64) {
// Adding consts that cannot overflow
#[allow(clippy::arithmetic_side_effects)]
(
self.chain_id.to_string(),
self.chain_id.clone(),
CHAIN_ID_LENGTH as u64 * MEMORY_ACCESS_GAS_PER_BYTE,
)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use in_memory::{
};
use namada_core::address::Address;
use namada_core::arith::checked;
use namada_core::chain::ChainId;
pub use namada_core::chain::{
BlockHash, BlockHeader, BlockHeight, Epoch, Epochs, BLOCK_HASH_LENGTH,
BLOCK_HEIGHT_LENGTH,
Expand Down Expand Up @@ -290,7 +291,7 @@ macro_rules! impl_storage_read {

fn get_chain_id(
&self,
) -> std::result::Result<String, namada_storage::Error> {
) -> std::result::Result<ChainId, namada_storage::Error> {
let (chain_id, gas) = self.in_mem().get_chain_id();
self.charge_gas(gas).into_storage_result()?;
Ok(chain_id)
Expand Down Expand Up @@ -587,7 +588,6 @@ pub mod testing {
use clru::CLruCache;
use namada_core::address;
use namada_core::address::EstablishedAddressGen;
use namada_core::chain::ChainId;
use namada_core::time::DateTimeUtc;
pub use namada_storage::testing::{PrefixIter, *};
use namada_storage::tx_queue::ExpiredTxsQueue;
Expand Down
7 changes: 4 additions & 3 deletions crates/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub use db::{Error as DbError, Result as DbResult, *};
pub use error::{CustomError, Error, OptionExt, Result, ResultExt};
use namada_core::address::Address;
use namada_core::borsh::{BorshDeserialize, BorshSerialize, BorshSerializeExt};
use namada_core::chain::ChainId;
pub use namada_core::chain::{
BlockHash, BlockHeader, BlockHeight, Epoch, Epochs,
};
Expand Down Expand Up @@ -85,7 +86,7 @@ pub trait StorageRead {
) -> Result<Option<(String, Vec<u8>)>>;

/// Getting the chain ID.
fn get_chain_id(&self) -> Result<String>;
fn get_chain_id(&self) -> Result<ChainId>;

/// Getting the block height. The height is that of the block to which the
/// current transaction is being applied.
Expand Down Expand Up @@ -385,8 +386,8 @@ pub mod testing {
Ok(iter.next())
}

fn get_chain_id(&self) -> Result<String> {
Ok(self.chain_id.to_string())
fn get_chain_id(&self) -> Result<ChainId> {
Ok(self.chain_id.clone())
}

fn get_block_height(&self) -> Result<BlockHeight> {
Expand Down
10 changes: 7 additions & 3 deletions crates/tx_prelude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub mod token;

use core::slice;
use std::marker::PhantomData;
use std::str::FromStr;

use chain::ChainId;
use namada_account::AccountPublicKeysMap;
pub use namada_core::address::Address;
pub use namada_core::borsh::{
Expand Down Expand Up @@ -156,15 +158,17 @@ impl StorageRead for Ctx {
Ok(HostEnvResult::is_success(found))
}

fn get_chain_id(&self) -> Result<String> {
fn get_chain_id(&self) -> Result<ChainId> {
let result = Vec::with_capacity(CHAIN_ID_LENGTH);
unsafe {
namada_tx_get_chain_id(result.as_ptr() as _);
}
let slice =
unsafe { slice::from_raw_parts(result.as_ptr(), CHAIN_ID_LENGTH) };
Ok(String::from_utf8(slice.to_vec())
.expect("Cannot convert the ID string"))
Ok(ChainId::from_str(
std::str::from_utf8(slice).expect("Chain ID must be valid utf8"),
)
.expect("Chain ID must be valid"))
}

fn get_block_height(&self) -> Result<BlockHeight> {
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ where
tx_charge_gas::<MEM, D, H, CA>(env, gas)?;
let gas = env
.memory
.write_string(result_ptr, chain_id)
.write_string(result_ptr, chain_id.to_string())
.map_err(|e| TxRuntimeError::MemoryError(Box::new(e)))?;
tx_charge_gas::<MEM, D, H, CA>(env, gas)
}
Expand Down Expand Up @@ -1748,7 +1748,7 @@ where
let chain_id = vp_host_fns::get_chain_id(gas_meter, &state)?;
let gas = env
.memory
.write_string(result_ptr, chain_id)
.write_string(result_ptr, chain_id.to_string())
.map_err(Into::into)?;
vp_host_fns::add_gas(gas_meter, gas)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/vp/src/native_vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::marker::PhantomData;

use namada_core::borsh;
use namada_core::borsh::BorshDeserialize;
use namada_core::chain::Epochs;
use namada_core::chain::{ChainId, Epochs};
use namada_gas::{GasMetering, VpGasMeter};
use namada_tx::{BatchedTxRef, Tx, TxCommitments};

Expand Down Expand Up @@ -202,7 +202,7 @@ where
.into_storage_result()
}

fn get_chain_id(&self) -> Result<String> {
fn get_chain_id(&self) -> Result<ChainId> {
self.ctx.get_chain_id()
}

Expand Down Expand Up @@ -277,7 +277,7 @@ where
.into_storage_result()
}

fn get_chain_id(&self) -> Result<String> {
fn get_chain_id(&self) -> Result<ChainId> {
self.ctx.get_chain_id()
}

Expand Down Expand Up @@ -341,7 +341,7 @@ where
.into_storage_result()
}

fn get_chain_id(&self) -> Result<String> {
fn get_chain_id(&self) -> Result<ChainId> {
vp_host_fns::get_chain_id(self.gas_meter, self.state)
.into_storage_result()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/vp/src/vp_host_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt::Debug;

use namada_core::address::{Address, ESTABLISHED_ADDRESS_BYTES_LEN};
use namada_core::arith::checked;
use namada_core::chain::{BlockHeader, BlockHeight, Epoch, Epochs};
use namada_core::chain::{BlockHeader, BlockHeight, ChainId, Epoch, Epochs};
use namada_core::hash::{Hash, HASH_LENGTH};
use namada_core::storage::{Key, TxIndex, TX_INDEX_LENGTH};
use namada_events::{Event, EventTypeBuilder};
Expand Down Expand Up @@ -201,7 +201,7 @@ where
pub fn get_chain_id<S>(
gas_meter: &RefCell<VpGasMeter>,
state: &S,
) -> Result<String>
) -> Result<ChainId>
where
S: StateRead + Debug,
{
Expand Down
3 changes: 2 additions & 1 deletion crates/vp_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod collection_validation;

use namada_core::address::Address;
use namada_core::borsh::BorshDeserialize;
use namada_core::chain::ChainId;
pub use namada_core::chain::{BlockHeader, BlockHeight, Epoch, Epochs};
use namada_core::hash::Hash;
pub use namada_core::storage::{Key, TxIndex};
Expand Down Expand Up @@ -67,7 +68,7 @@ where
) -> Result<Option<Vec<u8>>, namada_storage::Error>;

/// Getting the chain ID.
fn get_chain_id(&self) -> Result<String, namada_storage::Error>;
fn get_chain_id(&self) -> Result<ChainId, namada_storage::Error>;

/// Getting the block height. The height is that of the block to which the
/// current transaction is being applied.
Expand Down
16 changes: 9 additions & 7 deletions crates/vp_prelude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub mod ibc {
use core::slice;
pub use std::collections::BTreeSet;
use std::marker::PhantomData;
use std::str::FromStr;

use chain::ChainId;
pub use namada_core::address::Address;
pub use namada_core::borsh::{
BorshDeserialize, BorshSerialize, BorshSerializeExt,
Expand Down Expand Up @@ -296,7 +298,7 @@ impl<'view> VpEnv<'view> for Ctx {
Ok(read_from_buffer(read_result, namada_vp_result_buffer))
}

fn get_chain_id(&self) -> Result<String, Error> {
fn get_chain_id(&self) -> Result<ChainId, Error> {
// Both `CtxPreStorageRead` and `CtxPostStorageRead` have the same impl
get_chain_id()
}
Expand Down Expand Up @@ -447,7 +449,7 @@ impl StorageRead for CtxPreStorageRead<'_> {
))
}

fn get_chain_id(&self) -> Result<String, Error> {
fn get_chain_id(&self) -> Result<ChainId, Error> {
get_chain_id()
}

Expand Down Expand Up @@ -517,7 +519,7 @@ impl StorageRead for CtxPostStorageRead<'_> {
))
}

fn get_chain_id(&self) -> Result<String, Error> {
fn get_chain_id(&self) -> Result<ChainId, Error> {
get_chain_id()
}

Expand Down Expand Up @@ -569,17 +571,17 @@ fn iter_prefix_post_impl(
Ok(KeyValIterator(iter_id, PhantomData))
}

fn get_chain_id() -> Result<String, Error> {
fn get_chain_id() -> Result<ChainId, Error> {
let result = Vec::with_capacity(CHAIN_ID_LENGTH);
unsafe {
namada_vp_get_chain_id(result.as_ptr() as _);
}
let slice =
unsafe { slice::from_raw_parts(result.as_ptr(), CHAIN_ID_LENGTH) };
Ok(
String::from_utf8(slice.to_vec())
.expect("Cannot convert the ID string"),
Ok(ChainId::from_str(
std::str::from_utf8(slice).expect("Chain ID must be valid utf8"),
)
.expect("Chain ID must be valid"))
}

fn get_block_height() -> Result<BlockHeight, Error> {
Expand Down

0 comments on commit ed10c2f

Please sign in to comment.