From c4d30751d315ff09d29c38342c6f0f042e41e80d Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli Date: Wed, 29 May 2024 11:05:59 +0200 Subject: [PATCH] allow governance to change ibc storage --- crates/ibc/src/storage.rs | 22 ++++++++++++++++ crates/namada/src/ledger/native_vp/ibc/mod.rs | 25 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/crates/ibc/src/storage.rs b/crates/ibc/src/storage.rs index c9fa19839a..5190962be5 100644 --- a/crates/ibc/src/storage.rs +++ b/crates/ibc/src/storage.rs @@ -542,6 +542,28 @@ pub fn is_ibc_trace_key(key: &Key) -> Option<(String, String)> { } } +/// Check if a key is an IBC parameter Key +pub fn is_params_key(key: &Key) -> bool { + match &key.segments[..] { + [ + DbKeySeg::AddressSeg(addr), + DbKeySeg::StringSeg(prefix), + DbKeySeg::StringSeg(_), + ] => { + if addr == &Address::Internal(InternalAddress::Ibc) + && (prefix == MINT_LIMIT + || prefix == THROUGHPUT_LIMIT + || prefix == PARAMS) + { + true + } else { + false + } + } + _ => false, + } +} + /// Returns true if the given key is for an IBC counter for clients, /// connections, or channelEnds pub fn is_ibc_counter_key(key: &Key) -> bool { diff --git a/crates/namada/src/ledger/native_vp/ibc/mod.rs b/crates/namada/src/ledger/native_vp/ibc/mod.rs index a0e1f41bbd..64cf7355f7 100644 --- a/crates/namada/src/ledger/native_vp/ibc/mod.rs +++ b/crates/namada/src/ledger/native_vp/ibc/mod.rs @@ -13,7 +13,9 @@ use namada_core::arith::{self, checked}; use namada_core::collections::HashSet; use namada_core::storage::Key; use namada_gas::{IBC_ACTION_EXECUTE_GAS, IBC_ACTION_VALIDATE_GAS}; +use namada_governance::is_proposal_accepted; use namada_ibc::event::IbcEvent; +use namada_ibc::storage::{is_params_key, params_key}; use namada_ibc::{ Error as ActionError, IbcActions, NftTransferModule, TransferModule, ValidationParams, @@ -43,6 +45,8 @@ pub enum Error { NativeVpError(#[from] native_vp::Error), #[error("IBC VP error: Decoding error: {0}")] Decoding(#[from] std::io::Error), + #[error("IBC VP error: governance proposal change is invalid")] + InvalidGovernanceChange, #[error("IBC VP error: IBC message is required as transaction data")] NoTxData, #[error("IBC VP error: IBC action error: {0}")] @@ -83,6 +87,27 @@ where keys_changed: &BTreeSet, _verifiers: &BTreeSet
, ) -> VpResult<()> { + // Is VP triggered by a governance proposal? + let is_governance_proposal = is_proposal_accepted( + &self.ctx.pre(), + batched_tx + .tx + .data(batched_tx.cmt) + .unwrap_or_default() + .as_ref(), + ) + .unwrap_or_default(); + + if is_governance_proposal { + let changed_keys_are_params = + keys_changed.iter().any(|key| is_ibc_key(key)); + if changed_keys_are_params { + return Ok(()); + } else { + return Err(Error::InvalidGovernanceChange); + } + } + let tx_data = batched_tx.tx.data(batched_tx.cmt).ok_or(Error::NoTxData)?;