From c54d75442284731ef6682f4f9b8938ff22b3fa50 Mon Sep 17 00:00:00 2001 From: Alastair Holmes <42404303+AlastairHolmes@users.noreply.github.com> Date: Mon, 23 Oct 2023 16:49:13 +0200 Subject: [PATCH] fix: account_info rpc address conversion (#4144) Previously, address conversion in the rpc was causing the node to crash because it was trying to access a runtime storage item outside of the runtime environment Co-authored-by: dandanlen <3168260+dandanlen@users.noreply.github.com> --- state-chain/custom-rpc/src/lib.rs | 36 +++++++++++++------------ state-chain/runtime/src/lib.rs | 2 +- state-chain/runtime/src/runtime_apis.rs | 9 +++---- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/state-chain/custom-rpc/src/lib.rs b/state-chain/custom-rpc/src/lib.rs index c84f498663..24ccd68dc6 100644 --- a/state-chain/custom-rpc/src/lib.rs +++ b/state-chain/custom-rpc/src/lib.rs @@ -3,10 +3,14 @@ use cf_amm::{ range_orders::Liquidity, }; use cf_chains::{ - address::AddressConverter, btc::BitcoinNetwork, dot::PolkadotHash, + address::{ForeignChainAddressHumanreadable, ToHumanreadableAddress}, + btc::BitcoinNetwork, + dot::PolkadotHash, eth::Address as EthereumAddress, }; -use cf_primitives::{AccountRole, Asset, AssetAmount, ForeignChain, SemVer, SwapOutput}; +use cf_primitives::{ + AccountRole, Asset, AssetAmount, ForeignChain, NetworkEnvironment, SemVer, SwapOutput, +}; use core::ops::Range; use jsonrpsee::{ core::RpcResult, @@ -22,7 +26,7 @@ use sp_api::BlockT; use sp_rpc::number::NumberOrHex; use sp_runtime::DispatchError; use state_chain_runtime::{ - chainflip::{ChainAddressConverter, Offence}, + chainflip::Offence, constants::common::TX_FEE_MULTIPLIER, runtime_apis::{CustomRuntimeApi, Environment, LiquidityProviderInfo, RuntimeApiAccountInfoV2}, }; @@ -43,7 +47,7 @@ pub enum RpcAccountInfo { }, LiquidityProvider { balances: HashMap>, - refund_addresses: HashMap>, + refund_addresses: HashMap>, flip_balance: NumberOrHex, }, Validator { @@ -72,7 +76,7 @@ impl RpcAccountInfo { Self::Broker { flip_balance: balance.into() } } - fn lp(info: LiquidityProviderInfo, balance: u128) -> Self { + fn lp(info: LiquidityProviderInfo, network: NetworkEnvironment, balance: u128) -> Self { let mut balances = HashMap::new(); for (asset, balance) in info.balances { @@ -88,14 +92,7 @@ impl RpcAccountInfo { refund_addresses: info .refund_addresses .into_iter() - .map(|(chain, address)| { - ( - chain, - address - .map(ChainAddressConverter::to_encoded_address) - .map(|a| format!("{}", a)), - ) - }) + .map(|(chain, address)| (chain, address.map(|a| a.to_humanreadable(network)))) .collect(), } } @@ -184,7 +181,7 @@ pub struct RpcEnvironment { impl From for RpcEnvironment { fn from(environment: Environment) -> Self { Self { - bitcoin_network: environment.bitcoin_network, + bitcoin_network: environment.network.into(), ethereum_chain_id: environment.ethereum_chain_id, polkadot_genesis_hash: environment.polkadot_genesis_hash, } @@ -553,7 +550,11 @@ where .map_err(to_rpc_error)? .expect("role already validated"); - RpcAccountInfo::lp(info, balance) + RpcAccountInfo::lp( + info, + api.cf_environment(hash).map_err(to_rpc_error)?.network, + balance, + ) }, AccountRole::Validator => { let info = api.cf_account_info_v2(hash, &account_id).map_err(to_rpc_error)?; @@ -974,6 +975,7 @@ mod test { (Asset::Flip, u128::MAX / 2), ], }, + cf_primitives::NetworkEnvironment::Mainnet, 0, ); @@ -990,9 +992,9 @@ mod test { "Bitcoin": { "Btc": "0x0" }, }, "refund_addresses": { - "Ethereum": "0x0101010101010101010101010101010101010101", + "Ethereum": { "Eth" : "0x0101010101010101010101010101010101010101" }, "Bitcoin": null, - "Polkadot": "0x0000000000000000000000000000000000000000000000000000000000000000" + "Polkadot": { "Dot": "111111111111111111111111111111111HC1" } } }) ); diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index 7b0068f22b..117767653b 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -1039,7 +1039,7 @@ impl_runtime_apis! { fn cf_environment() -> runtime_apis::Environment { runtime_apis::Environment { - bitcoin_network: Environment::network_environment().into(), + network: Environment::network_environment(), ethereum_chain_id: Environment::ethereum_chain_id(), polkadot_genesis_hash: Environment::polkadot_genesis_hash(), } diff --git a/state-chain/runtime/src/runtime_apis.rs b/state-chain/runtime/src/runtime_apis.rs index 57cb57e347..f667752c38 100644 --- a/state-chain/runtime/src/runtime_apis.rs +++ b/state-chain/runtime/src/runtime_apis.rs @@ -3,11 +3,10 @@ use cf_amm::{ common::{Amount, Price, Tick}, range_orders::Liquidity, }; -use cf_chains::{ - btc::BitcoinNetwork, dot::PolkadotHash, eth::Address as EthereumAddress, ForeignChainAddress, -}; +use cf_chains::{dot::PolkadotHash, eth::Address as EthereumAddress, ForeignChainAddress}; use cf_primitives::{ - AccountRole, Asset, AssetAmount, EpochIndex, ForeignChain, SemVer, SwapOutput, + AccountRole, Asset, AssetAmount, EpochIndex, ForeignChain, NetworkEnvironment, SemVer, + SwapOutput, }; use codec::{Decode, Encode}; use core::ops::Range; @@ -69,7 +68,7 @@ pub struct AuctionState { #[derive(Encode, Decode, Eq, PartialEq, TypeInfo)] pub struct Environment { - pub bitcoin_network: BitcoinNetwork, + pub network: NetworkEnvironment, pub ethereum_chain_id: cf_chains::evm::api::EvmChainId, pub polkadot_genesis_hash: PolkadotHash, }