Skip to content

Commit

Permalink
parameters: fix VP post move
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jun 17, 2024
1 parent 73a29ac commit ede3b1d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ testing = [
[dependencies]
namada_core = { path = "../core" }
namada_macros = { path = "../macros" }
namada_state = { path = "../state" }
namada_storage = { path = "../storage" }
namada_tx = { path = "../tx" }
namada_vp = { path = "../vp" }

thiserror.workspace = true
1 change: 1 addition & 0 deletions crates/parameters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)]

pub mod storage;
pub mod vp;
mod wasm_allowlist;
use std::collections::BTreeMap;
use std::marker::PhantomData;
Expand Down
61 changes: 35 additions & 26 deletions crates/parameters/src/vp.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
//! Native VP for protocol parameters

use std::collections::BTreeSet;
use std::marker::PhantomData;

use namada_core::address::Address;
use namada_core::booleans::BoolResultUnitExt;
use namada_core::governance;
use namada_core::storage::Key;
use namada_state::StateRead;
use namada_state::{StateRead, StorageError};
use namada_tx::BatchedTxRef;
use namada_vp::native_vp::{
self, Ctx, CtxPreStorageRead, NativeVp, VpEvaluator,
};
use thiserror::Error;

use crate::ledger::native_vp::{self, Ctx, NativeVp};
use crate::vm::WasmCacheAccess;
use crate::storage;

#[allow(missing_docs)]
#[derive(Error, Debug)]
Expand All @@ -23,24 +27,31 @@ pub enum Error {
pub type Result<T> = std::result::Result<T, Error>;

/// Parameters VP
pub struct ParametersVp<'a, S, CA>
pub struct ParametersVp<'a, S, CA, EVAL, Gov>
where
S: StateRead,
CA: WasmCacheAccess,
S: 'static + StateRead,
EVAL: VpEvaluator<'a, S, CA, EVAL>,
{
/// Context to interact with the host structures.
pub ctx: Ctx<'a, S, CA>,
pub ctx: Ctx<'a, S, CA, EVAL>,
/// Governance type
pub gov: PhantomData<Gov>,
}

impl<'a, S, CA> NativeVp for ParametersVp<'a, S, CA>
impl<'a, S, CA, EVAL, Gov> NativeVp<'a> for ParametersVp<'a, S, CA, EVAL, Gov>
where
S: StateRead,
CA: 'static + WasmCacheAccess,
S: 'static + StateRead,
CA: 'static + Clone,
EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>,
Gov: governance::Read<
CtxPreStorageRead<'a, 'a, S, CA, EVAL>,
Err = StorageError,
>,
{
type Error = Error;

fn validate_tx(
&self,
&'a self,
batched_tx: &BatchedTxRef<'_>,
keys_changed: &BTreeSet<Key>,
_verifiers: &BTreeSet<Address>,
Expand All @@ -57,19 +68,17 @@ where
};
match key_type {
KeyType::PARAMETER | KeyType::UNKNOWN_PARAMETER => {
namada_governance::storage::is_proposal_accepted(
&self.ctx.pre(),
&data,
)
.map_err(Error::NativeVpError)?
.ok_or_else(|| {
native_vp::Error::new_alloc(format!(
"Attempted to change a protocol parameter from \
outside of a governance proposal, or from a \
non-accepted governance proposal: {key}",
))
.into()
})
Gov::is_proposal_accepted(&self.ctx.pre(), &data)
.map_err(Error::NativeVpError)?
.ok_or_else(|| {
native_vp::Error::new_alloc(format!(
"Attempted to change a protocol parameter \
from outside of a governance proposal, or \
from a non-accepted governance proposal: \
{key}",
))
.into()
})
}
KeyType::UNKNOWN => Ok(()),
}
Expand All @@ -90,9 +99,9 @@ enum KeyType {

impl From<&Key> for KeyType {
fn from(value: &Key) -> Self {
if namada_parameters::storage::is_protocol_parameter_key(value) {
if storage::is_protocol_parameter_key(value) {
KeyType::PARAMETER
} else if namada_parameters::storage::is_parameter_key(value) {
} else if storage::is_parameter_key(value) {
KeyType::UNKNOWN_PARAMETER
} else {
KeyType::UNKNOWN
Expand Down

0 comments on commit ede3b1d

Please sign in to comment.