Skip to content

Commit

Permalink
fix: account_info rpc address conversion (#4144)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
AlastairHolmes and dandanlen authored Oct 23, 2023
1 parent fd975b9 commit c54d754
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
36 changes: 19 additions & 17 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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},
};
Expand All @@ -43,7 +47,7 @@ pub enum RpcAccountInfo {
},
LiquidityProvider {
balances: HashMap<ForeignChain, HashMap<Asset, NumberOrHex>>,
refund_addresses: HashMap<ForeignChain, Option<String>>,
refund_addresses: HashMap<ForeignChain, Option<ForeignChainAddressHumanreadable>>,
flip_balance: NumberOrHex,
},
Validator {
Expand Down Expand Up @@ -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 {
Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -184,7 +181,7 @@ pub struct RpcEnvironment {
impl From<Environment> 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,
}
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -974,6 +975,7 @@ mod test {
(Asset::Flip, u128::MAX / 2),
],
},
cf_primitives::NetworkEnvironment::Mainnet,
0,
);

Expand All @@ -990,9 +992,9 @@ mod test {
"Bitcoin": { "Btc": "0x0" },
},
"refund_addresses": {
"Ethereum": "0x0101010101010101010101010101010101010101",
"Ethereum": { "Eth" : "0x0101010101010101010101010101010101010101" },
"Bitcoin": null,
"Polkadot": "0x0000000000000000000000000000000000000000000000000000000000000000"
"Polkadot": { "Dot": "111111111111111111111111111111111HC1" }
}
})
);
Expand Down
2 changes: 1 addition & 1 deletion state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
9 changes: 4 additions & 5 deletions state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit c54d754

Please sign in to comment.