Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VP update using actions enum written by txs to a temp storage #2934

Merged
merged 34 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9e26065
ibc/tx: rm compatibility wrapper
tzemanovic Mar 7, 2024
d87aa92
changelog: add #2848
tzemanovic Mar 8, 2024
1aaeb67
state/write_log: only trigger VPs from a fst addr of a key segment
tzemanovic Mar 20, 2024
2550d2d
wasm/tx: insert_verifier from txs where needed
tzemanovic Mar 20, 2024
c9ed295
benches: fix VPs exec to include verifiers from tx
tzemanovic Mar 22, 2024
38c9cc2
changelog: add #2928
tzemanovic Mar 22, 2024
d602f7d
Add a simple allocated storage err
sug0 Mar 8, 2024
9708463
Remove `eyre` from Ethereum Bridge param errs
sug0 Mar 8, 2024
440c76d
Remove `eyre` from Ethereum Bridge VP
sug0 Mar 8, 2024
3bcfe7b
Remove `eyre` from NUT VP
sug0 Mar 8, 2024
29e44be
Remove `eyre` from Ethereum Bridge Pool VP
sug0 Mar 8, 2024
18b2f75
Changelog for #2852
sug0 Mar 8, 2024
c5e8a3b
Extend bools and results with new helper methods
sug0 Mar 25, 2024
686c622
Return a result of unit from VPs rather than of bools
sug0 Mar 25, 2024
69385c3
Rebuild wasms for tests
sug0 Mar 25, 2024
e84759d
Remove explicit out-of-gas checks during VP execution
sug0 Mar 25, 2024
9747a71
Changelog for #2940
sug0 Mar 25, 2024
edc3344
Merge branch 'tiago/wasm-errs-ux' (#2940)
tzemanovic Mar 25, 2024
c86ed99
Merge branch 'tomas/ibc-tx-simplify' (#2848)
tzemanovic Mar 28, 2024
894c5f6
core/address: add `InternalAddress::TempStorage`
tzemanovic Mar 20, 2024
b146e04
tx: add actions to indicate them from tx to VPs
tzemanovic Mar 20, 2024
9b76266
tx,vp_prelude,native_vp: impl tx action Read/Write traits
tzemanovic Mar 21, 2024
b5a516a
namada/vm/host_env: add `tx_read_temp`
tzemanovic Mar 21, 2024
52e1f3f
tx_prelude/pos: push tx actions to indicate to VP
tzemanovic Mar 21, 2024
853f8e2
native_vp/PoS: update to use actions from txs
tzemanovic Mar 21, 2024
9a47733
write_log: skip temp storage keys in changed keys set
tzemanovic Mar 22, 2024
e354d82
gov: add tx actions to check authorization
tzemanovic Mar 22, 2024
705566e
pgf: add tx actions to check authorization
tzemanovic Mar 22, 2024
8d3feda
multitoken: replace write_log specialization with standard VP rules
tzemanovic Mar 25, 2024
ed1dd25
vp_prelude: re-export tx mod
tzemanovic Mar 26, 2024
914bf3b
wasm/vp_implicit+vp_user: update using tx action
tzemanovic Mar 26, 2024
c37b663
IBC: insert NUT token as verifier in IBC transfer
tzemanovic Apr 1, 2024
b2aabf4
wasm/vp_implicit+vp_user: re-order to read the main entrypoint first
tzemanovic Mar 26, 2024
9ffcd2e
changelog: add #2934
tzemanovic Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions crates/benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,12 +1157,16 @@ fn ibc_vp_validate_action(c: &mut Criterion) {
shell.vp_wasm_cache.clone(),
),
};
// Use an empty verifiers set placeholder for validation, this is only
// needed in actual txs to addresses whose VPs should be triggered
let verifiers = Rc::new(RefCell::new(BTreeSet::<Address>::new()));

let exec_ctx = PseudoExecutionContext::new(ibc.ctx.pre());
let ctx = Rc::new(RefCell::new(exec_ctx));
let mut actions = IbcActions::new(ctx.clone());
let mut actions = IbcActions::new(ctx.clone(), verifiers.clone());
actions.set_validation_params(ibc.validation_params().unwrap());

let module = TransferModule::new(ctx);
let module = TransferModule::new(ctx, verifiers);
actions.add_transfer_module(module.module_id(), module);

group.bench_function(bench_name, |b| {
Expand Down Expand Up @@ -1255,12 +1259,17 @@ fn ibc_vp_execute_action(c: &mut Criterion) {
shell.vp_wasm_cache.clone(),
),
};
// Use an empty verifiers set placeholder for validation, this is only
// needed in actual txs to addresses whose VPs should be triggered
let verifiers = Rc::new(RefCell::new(BTreeSet::<Address>::new()));

let exec_ctx = PseudoExecutionContext::new(ibc.ctx.pre());
let ctx = Rc::new(RefCell::new(exec_ctx));
let mut actions = IbcActions::new(ctx.clone());

let mut actions = IbcActions::new(ctx.clone(), verifiers.clone());
actions.set_validation_params(ibc.validation_params().unwrap());

let module = TransferModule::new(ctx);
let module = TransferModule::new(ctx, verifiers);
actions.add_transfer_module(module.module_id(), module);

group.bench_function(bench_name, |b| {
Expand Down
27 changes: 25 additions & 2 deletions crates/core/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ pub const MASP: Address = Address::Internal(InternalAddress::Masp);
pub const MULTITOKEN: Address = Address::Internal(InternalAddress::Multitoken);
/// Internal Eth bridge address
pub const ETH_BRIDGE: Address = Address::Internal(InternalAddress::EthBridge);
/// Address with temporary storage is used to pass data from txs to VPs which is
/// never committed to DB
pub const TEMP_STORAGE: Address =
Address::Internal(InternalAddress::TempStorage);

/// Error from decoding address from string
pub type DecodeError = string_encoding::DecodeError;
Expand Down Expand Up @@ -135,6 +139,9 @@ impl From<raw::Address<'_, raw::Validated>> for Address {
InternalAddress::IbcToken(IbcTokenHash(*raw_addr.data())),
),
raw::Discriminant::Masp => Address::Internal(InternalAddress::Masp),
raw::Discriminant::TempStorage => {
Address::Internal(InternalAddress::TempStorage)
}
}
}
}
Expand Down Expand Up @@ -229,6 +236,11 @@ impl<'addr> From<&'addr Address> for raw::Address<'addr, raw::Validated> {
.validate()
.expect("This raw address is valid")
}
Address::Internal(InternalAddress::TempStorage) => {
raw::Address::from_discriminant(raw::Discriminant::TempStorage)
.validate()
.expect("This raw address is valid")
}
}
}
}
Expand Down Expand Up @@ -303,6 +315,11 @@ impl Address {
pub fn is_implicit(&self) -> bool {
matches!(self, Address::Implicit(_))
}

/// If the address internal?
pub fn is_internal(&self) -> bool {
matches!(self, Address::Internal(_))
}
}

impl string_encoding::Format for Address {
Expand Down Expand Up @@ -557,6 +574,9 @@ pub enum InternalAddress {
Pgf,
/// Masp
Masp,
/// Address with temporary storage is used to pass data from txs to VPs
/// which is never committed to DB
TempStorage,
}

impl Display for InternalAddress {
Expand All @@ -578,6 +598,7 @@ impl Display for InternalAddress {
Self::Multitoken => "Multitoken".to_string(),
Self::Pgf => "PublicGoodFundings".to_string(),
Self::Masp => "MASP".to_string(),
Self::TempStorage => "TempStorage".to_string(),
}
)
}
Expand Down Expand Up @@ -802,8 +823,9 @@ pub mod testing {
InternalAddress::Nut(_) => {}
InternalAddress::Pgf => {}
InternalAddress::Masp => {}
InternalAddress::Multitoken => {} /* Add new addresses in the
* `prop_oneof` below. */
InternalAddress::Multitoken => {}
InternalAddress::TempStorage => {} /* Add new addresses in the
* `prop_oneof` below. */
};
prop_oneof![
Just(InternalAddress::PoS),
Expand All @@ -819,6 +841,7 @@ pub mod testing {
Just(InternalAddress::Multitoken),
Just(InternalAddress::Pgf),
Just(InternalAddress::Masp),
Just(InternalAddress::TempStorage),
]
}

Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/address/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum Discriminant {
IbcToken = 13,
/// MASP raw address.
Masp = 14,
/// Temporary storage address.
TempStorage = 15,
}

/// Raw address representation.
Expand Down
6 changes: 5 additions & 1 deletion crates/ibc/src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Implementation of `IbcActions` with the protocol storage

use std::cell::RefCell;
use std::collections::BTreeSet;
use std::rc::Rc;

use namada_core::address::{Address, InternalAddress};
Expand Down Expand Up @@ -306,6 +307,9 @@ where
prost::Message::encode(&any_msg, &mut data).into_storage_result()?;

let ctx = IbcProtocolContext { state };
let mut actions = IbcActions::new(Rc::new(RefCell::new(ctx)));
// Use an empty verifiers set placeholder for validation, this is only
// needed in txs and not protocol
let verifiers = Rc::new(RefCell::new(BTreeSet::<Address>::new()));
let mut actions = IbcActions::new(Rc::new(RefCell::new(ctx)), verifiers);
actions.execute(&data).into_storage_result()
}
35 changes: 33 additions & 2 deletions crates/ibc/src/context/token_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! IBC token transfer context

use std::cell::RefCell;
use std::collections::BTreeSet;
use std::rc::Rc;

use namada_core::address::{Address, InternalAddress};
Expand All @@ -26,15 +27,24 @@ where
C: IbcCommonContext,
{
inner: Rc<RefCell<C>>,
verifiers: Rc<RefCell<BTreeSet<Address>>>,
}

impl<C> TokenTransferContext<C>
where
C: IbcCommonContext,
{
/// Make new token transfer context
pub fn new(inner: Rc<RefCell<C>>) -> Self {
Self { inner }
pub fn new(
inner: Rc<RefCell<C>>,
verifiers: Rc<RefCell<BTreeSet<Address>>>,
) -> Self {
Self { inner, verifiers }
}

/// Insert a verifier address whose VP will verify the tx.
fn insert_verifier(&mut self, addr: &Address) {
self.verifiers.borrow_mut().insert(addr.clone());
}

/// Get the token address and the amount from PrefixedCoin. If the base
Expand Down Expand Up @@ -143,6 +153,13 @@ where
// has no prefix
let (ibc_token, amount) = self.get_token_amount(coin)?;

// A transfer of NUT tokens must be verified by their VP
if ibc_token.is_internal()
&& matches!(ibc_token, Address::Internal(InternalAddress::Nut(_)))
{
self.insert_verifier(&ibc_token);
}

self.inner
.borrow_mut()
.transfer_token(from, to, &ibc_token, amount)
Expand All @@ -157,6 +174,13 @@ where
// The trace path of the denom is already updated if receiving the token
let (ibc_token, amount) = self.get_token_amount(coin)?;

// A transfer of NUT tokens must be verified by their VP
if ibc_token.is_internal()
&& matches!(ibc_token, Address::Internal(InternalAddress::Nut(_)))
{
self.insert_verifier(&ibc_token);
}

self.inner
.borrow_mut()
.mint_token(account, &ibc_token, amount)
Expand All @@ -170,6 +194,13 @@ where
) -> Result<(), TokenTransferError> {
let (ibc_token, amount) = self.get_token_amount(coin)?;

// A transfer of NUT tokens must be verified by their VP
if ibc_token.is_internal()
&& matches!(ibc_token, Address::Internal(InternalAddress::Nut(_)))
{
self.insert_verifier(&ibc_token);
}

// The burn is "unminting" from the minted balance
self.inner
.borrow_mut()
Expand Down
9 changes: 7 additions & 2 deletions crates/ibc/src/context/transfer_mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! IBC module for token transfer

use std::cell::RefCell;
use std::collections::BTreeSet;
use std::fmt::Debug;
use std::rc::Rc;

use namada_core::address::Address;
use namada_core::ibc::apps::transfer::module::{
on_acknowledgement_packet_execute, on_acknowledgement_packet_validate,
on_chan_close_confirm_execute, on_chan_close_confirm_validate,
Expand Down Expand Up @@ -58,9 +60,12 @@ where
C: IbcCommonContext,
{
/// Make a new module
pub fn new(ctx: Rc<RefCell<C>>) -> Self {
pub fn new(
ctx: Rc<RefCell<C>>,
verifiers: Rc<RefCell<BTreeSet<Address>>>,
) -> Self {
Self {
ctx: TokenTransferContext::new(ctx),
ctx: TokenTransferContext::new(ctx, verifiers),
}
}

Expand Down
36 changes: 27 additions & 9 deletions crates/ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod context;
pub mod storage;

use std::cell::RefCell;
use std::collections::BTreeSet;
use std::fmt::Debug;
use std::rc::Rc;
use std::str::FromStr;
Expand Down Expand Up @@ -68,17 +69,22 @@ where
{
ctx: IbcContext<C>,
router: IbcRouter<'a>,
verifiers: Rc<RefCell<BTreeSet<Address>>>,
}

impl<'a, C> IbcActions<'a, C>
where
C: IbcCommonContext + Debug,
{
/// Make new IBC actions
pub fn new(ctx: Rc<RefCell<C>>) -> Self {
pub fn new(
ctx: Rc<RefCell<C>>,
verifiers: Rc<RefCell<BTreeSet<Address>>>,
) -> Self {
Self {
ctx: IbcContext::new(ctx),
router: IbcRouter::new(),
verifiers,
}
}

Expand All @@ -101,8 +107,10 @@ where
let message = decode_message(tx_data)?;
match &message {
IbcMessage::Transfer(msg) => {
let mut token_transfer_ctx =
TokenTransferContext::new(self.ctx.inner.clone());
let mut token_transfer_ctx = TokenTransferContext::new(
self.ctx.inner.clone(),
self.verifiers.clone(),
);
send_transfer_execute(
&mut self.ctx,
&mut token_transfer_ctx,
Expand All @@ -111,8 +119,10 @@ where
.map_err(Error::TokenTransfer)
}
IbcMessage::ShieldedTransfer(msg) => {
let mut token_transfer_ctx =
TokenTransferContext::new(self.ctx.inner.clone());
let mut token_transfer_ctx = TokenTransferContext::new(
self.ctx.inner.clone(),
self.verifiers.clone(),
);
send_transfer_execute(
&mut self.ctx,
&mut token_transfer_ctx,
Expand Down Expand Up @@ -226,17 +236,25 @@ where

/// Validate according to the message in IBC VP
pub fn validate(&self, tx_data: &[u8]) -> Result<(), Error> {
// Use an empty verifiers set placeholder for validation, this is only
// needed in actual txs to addresses whose VPs should be triggered
let verifiers = Rc::new(RefCell::new(BTreeSet::<Address>::new()));

let message = decode_message(tx_data)?;
match message {
IbcMessage::Transfer(msg) => {
let token_transfer_ctx =
TokenTransferContext::new(self.ctx.inner.clone());
let token_transfer_ctx = TokenTransferContext::new(
self.ctx.inner.clone(),
verifiers.clone(),
);
send_transfer_validate(&self.ctx, &token_transfer_ctx, msg)
.map_err(Error::TokenTransfer)
}
IbcMessage::ShieldedTransfer(msg) => {
let token_transfer_ctx =
TokenTransferContext::new(self.ctx.inner.clone());
let token_transfer_ctx = TokenTransferContext::new(
self.ctx.inner.clone(),
verifiers.clone(),
);
send_transfer_validate(
&self.ctx,
&token_transfer_ctx,
Expand Down
Loading