From 61fb44af6830ee85ad5f91b15e66dd11f10a491d Mon Sep 17 00:00:00 2001 From: Web3 Smith <31099392+Wizdave97@users.noreply.github.com> Date: Thu, 2 Jun 2022 12:24:39 +0100 Subject: [PATCH] Update with new changes including ICS20 (#12) * Release v0.15.0 (#2234) * Bump crates to v0.15.0 and ibc-proto to v0.18.0 * Include ibc-test-framework in release * Slightly improve main Cargo doc for ibc-test-framework * Fix components names * Update changelog * Update lockfile Co-authored-by: Soares Chen * Add type ascription to fix build when `telemetry` feature is disabled (#2235) * Bump once_cell from 1.11.0 to 1.12.0 (#2237) * Add `ibc-test-framework` to the main README (#2236) * Add `ibc-test-framework` to the main README * Cleanup * Fix `execute_schedule` method leaking operational data (#2118) * Pull in upstream changes * Adding integration test for execute_schedule * Adding integration test for execute_schedule * Finish stubbing out execute_schedule test * Call `chains.shutdown` method * Correctly shut down chain * Shut down node b as well * Debugging execute_schedule test * Add Debug derivations * Update integration test so that its flow correctly tests `execute_schedule` * Attempt to perform a second IBC transfer * Remove info's * Increase sleep timeout duration * Incorportate new test framework features * Remove unnecessary `sleep` call * Correctly use new test framework features * Get assertions passing for now * Send two transactions, one in each direction * Add doc comment for test * Improve panic messages * Refactor test so that it is actually testing the desired behavior * Attempt at fixing `execute_schedule` leaky logic * Flesh out doc comments some more * Remove a duplicate function * Make use of OperationalDataTarget enum * Remove redundant enum * Remove some Debug derives * Remove one more debug derive * Add `try_fetch_scheduled_operational_data` back in * Give `do_execute_schedule` a more descriptive name * Improve `execute_schedule_for_target_chain` method's documentation * Add a bunch of clarifying comments * More clarification of comments * Flesh out `OperationalData` docs * Add changelog entry * Incorporate PR feedback Co-authored-by: Adi Seredinschi * Add `keys balance` command to query the balance for a key (#2232) * Added subcommand 'balance' for command 'keys' to output the account balance associated with a given key * Added changelog entry for new command feature * Updated Hermes guide to include the new keys balance command * Improved error log for the CLI command and added doc comment * fixed fmt by running cargo fmt * Refactored query_balance to take key_name parameter. Added JSON output to keys balance command. * Fixed typo in comment in CosmosSdkChain query_balance method * Updated keys balance command to take the key_name as an optional flag Co-authored-by: Luca Joss Co-authored-by: Romain Ruetschi * Model based testing on integration tests (#2072) * ics20 token transfer spec * fixes for tla functions * full spec * update tla specs * mbt driver code * prepare for itf adoption * itf deserializer * description in comments * fix type * hack for apalache v0.20.2 (informalsystems/apalache#1304) * updated tla spec * example itf.json * code refactor * prepare for mbt * fix and update * updated example * updated trace types * working mbt driver * fix mbt test * added mbt readme * BinaryChainTest over BinaryChannelTest * wait for established channel * added packet state assertions * improved mbt integration * few fixes * Refactor bootstrap chain and foreign client code * Introduce BootstrapChannel/ConnectionOptions * Increase timeout for assert_eventual_wallet_amount for CI * Upgrade Rust to 1.60.0 * Fix fmt in Rust 1.60.0 * Update Rust MSRV to 1.60 * Try running CI integration test single-threaded * Fix variable mixup * Add MBT to CI * Disable failing parse_itf test * Disable test_self_connected_ibc_transfer * Use gaia6 to run MBT test * Debug log on CI * save itf trace on failure * initialize nested maps * updated mbt code * Use new Nix URL * better fix to empty nested maps * Remove stale comments * Fix merge conflicts Co-authored-by: Soares Chen Co-authored-by: Adi Seredinschi * Bump uuid from 1.0.0 to 1.1.0 (#2247) Bumps [uuid](https://github.com/uuid-rs/uuid) from 1.0.0 to 1.1.0. - [Release notes](https://github.com/uuid-rs/uuid/releases) - [Commits](https://github.com/uuid-rs/uuid/compare/1.0.0...1.1.0) --- updated-dependencies: - dependency-name: uuid dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Add `query channel client` command to query a channel's client state (#2248) * Added new CLI command 'query channel client' * Updated Hermes guide to include new query channel client command and updated changelog * Added missing subcommand in Hermes guide, section 7.9.3 Co-authored-by: Luca Joss * Fix wrong `create channel` command in "Create a new path" page in the guide (#2245) Fix wrong `create channel` command in guide Co-authored-by: Adi Seredinschi * Disable MBT tests on CI due to flakiness (#2253) * Complete ICS20 Implementation (#1989) * Add ics26 Module trait * newtype for Acknowledgement * Define Router interface * Improve Router trait * Update mock router impl * Test for router API * Disallow duplicate module_ids in MockRouter * Add RouterBuilder for seal-style API * Fix failing test * Fix CI after merge * Chainable RouterBuilder::add_route() * Fix test * Add Router::has_route() trait method * Add comments * Separate mutating/non-mutating trait methods for shadow-paging style API * Make most Module trait methods optional * Default impl for MockModule::on_chan_open_try() * Acknowledgement trait * Extend router test * Simplify OnRechPacketResult using FnOnce() * Cleanup * Use Cow for CapabilityName::new() * Use Cow for CapabilityName::new() * Use newtype ModuleId instead of trait assoc type * Module callbacks' args as refs * Fix mock impl * WIP channel callbacks * Fix ModuleId ctor validation * TypedCapability * Use typed capabilities * Add Router::route_mut() * Implement pre-dispatch channel message validation * Avoid cloning message during channel msg dispatch * Add ChannelReader::lookup_module_by_channel() * Complete ics4 dispatch with callbacks * Fix compile errors from rebase * Fix CI test * Improve mock impl for capabilities * cargo fmt * Set channel version returned from on_chan_open_try() * Implement packet handler verification * Add missing check already received packets on ordered channels * Avoid cloning message during packet msg dispatch * Implement RecvPacket NoOp * cargo fmt and remove unused errors * Make on_recv_packet() Ack result optional * Don't return Result from on_recv_packet() and pass GenericAcknowledgement to on_acknowledgement_packet() * Implement packet callbacks * Allow callbacks to write logs and emit events * Fix test * Add .changelog entry * Add a comment for state rollback expectation from dispatch() * ics26_routing::handler::deliver() takes single message as input * ics26_routing::handler::deliver() returns logs as well * Remove ctx_ro * Callbacks return ModuleOutput * Return HandlerOutputBuilder from channel and packet dispatch fn * Revert "Callbacks return ModuleOutput" This reverts commit 1d430c9f86168816b55cd9185c6c632c4190118d. * Address review feedback for comments * Extract ChannelMsg::lookup_module() * Add ICS20 Denom type * Add ICS20 TracePrefix & TracePath type * Define Coin and Decimal types * Impl conversions from/to RawDenomTrace * Make better use of derive-more * Impl conversions for Coin type * Add HashedDenom and polish DenomTrace impl * Define PacketData domain type and conversions * Use Coin domain type in MsgTransfer * Fix usage of Coin type in relayer code * Always panic on decimal arith overflow * Polish generic Signer impl * Implement ICS20 signer type * Coin type with generic Denom param * Use IbcCoin in MsgTransfer and PrefixedCoin in PacketData * Minor refactoring * Fix test_util * Impl AsRef<[u8]> for GenericAcknowledgement * Ics20Context is no longer a supertrait of Ics26Context * Add ICS20 Ack type * Add ICS20 event enum placeholder * Define all ICS20 expected keepers and context * Update send_transfer() for recent context changes * Give mut ref to set_channel_escrow_address() * Define ICS20 callback functions * Fix test build * Move denom derive functions to integration tests * Add version to chan_open_try callback * Rename ChannelId::counter() to sequence() * Add AppModule error variant to ChannelError * Implement validation helpers for callbacks * Define Ics20 callback errors * Implement channel handshake callbacks * Fix clippy errors * Impl Deserialize for PacketData * Manually implement AsRef for Signer * Fix ICS20 Ack success from impl * Add more ICS20 errors * Improve ack type ctor * Add ctor for TracePrefix * Provide denom trace methods to remove prefix and check for empty trace path * Implement conversion from PrefixedCoin to IbcCoin * Add Ics20Reader trait methods to check send/receive enabled * Provided trait method get_channel_escrow_address() * Add BankReader trait for is_blocked_account() * Add FromStr bound for Ics20Context::AccountId * Use IbcCoins in Bank traits * Impl on_recv_packet() for cases where receiver chain is source * Fallible OnRecvPacket::write_fn() * Complete on_recv_packet impl * Fix test build * Fix clippy errors * Implement remaining packet callbacks for ICS20 * Make set_denom_trace() fallible * Don't derive AsRef * Complete send_transfer impl * Manual deserialize impl for Acknowledgement * Add ctor for Acknowledgement * Handle packet-data deserialize error separately * Use U256 for Denom and move bigint.rs to modules/src * Rename Denom to Amount * Cleanup * Fix trait definitions * Use source enum * Fix AccountReader trait * Validate port_id * More refactoring * Rename Signer to Address * Use Address instead of Signer where applicable * Fix send_transfer packet creation * Fix clippy warnings * Define ICS20 events * Extract relay code into separate files * Fix clippy warnings * Make HandlerOutput/Builder generic over events * Define ModuleEvent * Add AppModule variant to IbcEvent * Impl Display for Acknowledgement * Derive serde for ModuleId * Add ModuleId to ModuleEvent * Impl conversion from tuple for ModuleEventAttribute * Impl conversions from ICS20 events to ModuleEvent * Add event for transfer * Remove bech32 validation from Address * Change MsgTransfer receiver type to Address * Extract MockContext IbcStore * Improve conversion from Signer to AccountId * Add deliver method to Module trait * Implement conversions for MsgTransfer to/from Protobuf Any * Store packet result directly in send_transfer() * Make all ICS20 mods public * Make all IbsStore fields public to enable access for app modules * Implement Ics20Context for DummyTransferModule * Fix failing test * Manual Clone impl for MockContext * Fix failing test * Revert "Fix failing test" This reverts commit 40aec618a22df1c8a2592e63028cd1262957f5a1. * Fix MockContext Clone impl * Update trait definitions after merge with master * Replace ModuleOutput with ModuleOutputBuilder in callbacks * Add inout param ModuleOutputBuilder to Ics20 callback handlers * Emit ICS20 receive packet events * Module::deliver() must be able to emit IbcEvents * Allow HandlerOutputBuilder to merge HandlerOutput * Emit transfer event * Add AckStatusEvent * Emit ICS20 ack events * Emit ICS20 timeout events * Remove ABCI error code * Remove #[allow(unused)] * Add log for send transfer * MsgReceipt abstraction * Handle missing IbcEvent to AbciEvent conversions for RecvPacket and TimeoutOnClose events * Implement ModuleEvent to AbciEvent conversion * Make send_transfer() public * Allow empty TracePaths * Fix TracePath multiple prefix parse bug * Fix TracePath empty str parse bug * Improve DenomTrace FromStr * Add denom validation test * Add denom trace and serde tests * TracePath tests * Allow Denom with '/' * Fix HashedDenom FromStr impl for empty hash * Minor refactoring * Add IbcCoin tests * Add .changelog entry * Fix clippy errors * Disallow empty Signer and replace Address with it * Use ToString as trait bound for ICS20 AccountId * Change AccountId trait bound from FromStr to TryFrom * Remove Signer::new() * Delete OCap related TODO * Remove the PortKeeper * Remove Module::deliver() * Fix clippy warnings * Rename transfer module * Rename mod relay * Fix tests failing due to empty signer * Rename PORT_ID const to PORT_ID_STR * Fix escrow addr gen * Test cosmos escrow addr gen * Rename receiver account * Remove `has_denom_trace()` * Remove `BankReader::is_blocked_account()` * Remove `AccountId::ToString` bound * Remove `AccountReader` * Mint/burn into user accounts directly * Use `chunks_exact()` instead of `windows().step_by()` * Rename `DenomTrace::has_prefix()` to `trace_starts_with()` * Move trace related methods into `TracePath` * Fix add/remove prefix * Add test for add/remove trace prefix * Set denom trace only if not already set * Use truncate instead of drain in cosmos_adr028_escrow_address() * Rename DenomTrace as PrefixedDenom * Modify all `BankKeeper` methods to use `PrefixedCoin` instead of `IbcCoin` * Rename `Denom` as `BaseDenom` * Impl Into and checked_add/sub() for Amount * Add more TracePath tests * Test TracePath is_empty() * Remove IbcCoin * Remove HashedDenom and HashedCoin * Fix test compilation * Add comment for send_transfer() * Functions for determining source chain * Minor refactoring * cargo fmt * Derive serde for PacketCommitment and AcknowledgementCommitment * docstring and comment Co-authored-by: Philippe Laferriere * Merge commands `keys add` and `keys restore` into a single command. (#2251) * Merged commands 'keys add' and 'keys restore' into a single command, 'keys add'. Improved restoring a key with mnemonic by taking a file containing the mnemonic as input instead of taking the mnemonic as command line input * Updated Hermes guide with new merged 'keys add' command. And updated changelog with improvement * Fixed e2e setup script to take correct flag when adding keys * Fixed small errors in Hermes guide and improved comments for changed 'keys add' command. * Improved Hermes guide section 7.2 * Updated the Hermes guide section 7.2 to match actual features * Bump moka from 0.8.4 to 0.8.5 (#2246) Bumps [moka](https://github.com/moka-rs/moka) from 0.8.4 to 0.8.5. - [Release notes](https://github.com/moka-rs/moka/releases) - [Changelog](https://github.com/moka-rs/moka/blob/master/CHANGELOG.md) - [Commits](https://github.com/moka-rs/moka/compare/v0.8.4...v0.8.5) --- updated-dependencies: - dependency-name: moka dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Adi Seredinschi * fmt * rename LightClientContext to ReaderContext * use host hash function in escrow address * minor fix * minor fix Co-authored-by: Romain Ruetschi Co-authored-by: Soares Chen Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Sean Chen Co-authored-by: Adi Seredinschi Co-authored-by: Luca Joss <43531661+ljoss17@users.noreply.github.com> Co-authored-by: Luca Joss Co-authored-by: Ranadeep Biswas Co-authored-by: Luca Joss Co-authored-by: Shoaib Ahmed Co-authored-by: Philippe Laferriere --- .../ibc/2104-fix-commitment-computation.md | 4 +- .../ibc/2114-fix-ack-verification.md | 4 +- .../bug-fixes/ibc/2178-conn-ack-bug-fix.md | 4 +- modules/Cargo.toml | 7 +- .../relay_application_logic/send_transfer.rs | 52 --- modules/src/applications/transfer/context.rs | 36 +- .../clients/ics07_tendermint/client_def.rs | 26 +- modules/src/clients/ics11_beefy/client_def.rs | 26 +- modules/src/clients/ics13_near/client_def.rs | 24 +- modules/src/core/ics02_client/client_def.rs | 46 +-- modules/src/core/ics02_client/handler.rs | 4 +- .../ics02_client/handler/create_client.rs | 4 +- .../ics02_client/handler/update_client.rs | 4 +- .../ics02_client/handler/upgrade_client.rs | 4 +- .../core/ics02_client/msgs/create_client.rs | 2 +- .../core/ics02_client/msgs/upgrade_client.rs | 2 +- modules/src/core/ics03_connection/handler.rs | 4 +- .../ics03_connection/handler/conn_open_ack.rs | 4 +- .../handler/conn_open_confirm.rs | 4 +- .../handler/conn_open_init.rs | 4 +- .../ics03_connection/handler/conn_open_try.rs | 4 +- .../core/ics03_connection/handler/verify.rs | 12 +- modules/src/core/ics04_channel/context.rs | 13 +- modules/src/core/ics04_channel/error.rs | 8 + modules/src/core/ics04_channel/handler.rs | 6 +- .../ics04_channel/handler/acknowledgement.rs | 4 +- .../handler/chan_close_confirm.rs | 4 +- .../ics04_channel/handler/chan_close_init.rs | 4 +- .../ics04_channel/handler/chan_open_ack.rs | 4 +- .../handler/chan_open_confirm.rs | 4 +- .../ics04_channel/handler/chan_open_init.rs | 4 +- .../ics04_channel/handler/chan_open_try.rs | 4 +- .../core/ics04_channel/handler/recv_packet.rs | 4 +- .../core/ics04_channel/handler/send_packet.rs | 9 +- .../src/core/ics04_channel/handler/timeout.rs | 4 +- .../ics04_channel/handler/timeout_on_close.rs | 4 +- .../src/core/ics04_channel/handler/verify.rs | 12 +- .../handler/write_acknowledgement.rs | 4 +- modules/src/core/ics26_routing/context.rs | 5 +- modules/src/core/ics26_routing/handler.rs | 50 ++- modules/src/mock/client_def.rs | 24 +- modules/src/mock/context.rs | 28 +- modules/src/test_utils.rs | 364 +++++++++++++++++- proto-compiler/Cargo.lock | 2 +- 44 files changed, 592 insertions(+), 254 deletions(-) delete mode 100644 modules/src/applications/ics20_fungible_token_transfer/relay_application_logic/send_transfer.rs diff --git a/.changelog/v0.15.0/bug-fixes/ibc/2104-fix-commitment-computation.md b/.changelog/v0.15.0/bug-fixes/ibc/2104-fix-commitment-computation.md index 570c1b3338..da0b858a75 100644 --- a/.changelog/v0.15.0/bug-fixes/ibc/2104-fix-commitment-computation.md +++ b/.changelog/v0.15.0/bug-fixes/ibc/2104-fix-commitment-computation.md @@ -1,2 +1,2 @@ -- Fix packet commitment calculation to match IBC-Go - ([#2104](https://github.com/informalsystems/ibc-rs/issues/2104)) +- Fix packet commitment calculation to match ibc-go ([#2104](https://github.com/informalsystems/ibc- + rs/issues/2104)) diff --git a/.changelog/v0.15.0/bug-fixes/ibc/2114-fix-ack-verification.md b/.changelog/v0.15.0/bug-fixes/ibc/2114-fix-ack-verification.md index 0987d40b6f..cbe6399a16 100644 --- a/.changelog/v0.15.0/bug-fixes/ibc/2114-fix-ack-verification.md +++ b/.changelog/v0.15.0/bug-fixes/ibc/2114-fix-ack-verification.md @@ -1,2 +1,2 @@ -- Fix incorrect acknowledgement verification - ([#2114](https://github.com/informalsystems/ibc-rs/issues/2114)) +- Fix incorrect acknowledgement verification ([#2114](https://github.com/informalsystems/ibc- + rs/issues/2114)) diff --git a/.changelog/v0.15.0/bug-fixes/ibc/2178-conn-ack-bug-fix.md b/.changelog/v0.15.0/bug-fixes/ibc/2178-conn-ack-bug-fix.md index af72298e4b..054fb34968 100644 --- a/.changelog/v0.15.0/bug-fixes/ibc/2178-conn-ack-bug-fix.md +++ b/.changelog/v0.15.0/bug-fixes/ibc/2178-conn-ack-bug-fix.md @@ -1,2 +1,2 @@ -- Fix connection identifier mix-up in connection acknowledgement processing - ([#2178](https://github.com/informalsystems/ibc-rs/issues/2178)) +- fix connection id mix-up in connection acknowledgement processing + ([#2178](https://github.com/informalsystems/ibc-rs/issues/2178)) \ No newline at end of file diff --git a/modules/Cargo.toml b/modules/Cargo.toml index 024c306d94..ab11162874 100644 --- a/modules/Cargo.toml +++ b/modules/Cargo.toml @@ -21,6 +21,7 @@ std = [ "flex-error/std", "flex-error/eyre_tracer", "ibc-proto/std", + "ics23/std", "clock", "beefy-client/std", "sp-runtime/std", @@ -39,7 +40,7 @@ mocks = ["tendermint-testgen", "clock", "std", "sp-io", "sp-io/std"] [dependencies] # Proto definitions for all IBC-related interfaces, e.g., connections or channels. borsh = { version = "0.9.3", default-features = false } -ibc-proto = { version = "0.17.1", path = "../proto", default-features = false } +ibc-proto = { version = "0.18.0", path = "../proto", default-features = false } ics23 = { version = "=0.8.0-alpha", default-features = false } time = { version = "0.3", default-features = false } serde_derive = { version = "1.0.104", default-features = false } @@ -53,8 +54,8 @@ safe-regex = { version = "0.2.5", default-features = false } subtle-encoding = { version = "0.5", default-features = false } sha2 = { version = "0.10.2", default-features = false } flex-error = { version = "0.4.4", default-features = false } -num-traits = { version = "0.2.14", default-features = false } -derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] } +num-traits = { version = "0.2.15", default-features = false } +derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] } uint = { version = "0.9", default-features = false } beefy-client = { package = "beefy-generic-client", git = "https://github.com/ComposableFi/beefy-client", branch = "master", default-features = false } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false } diff --git a/modules/src/applications/ics20_fungible_token_transfer/relay_application_logic/send_transfer.rs b/modules/src/applications/ics20_fungible_token_transfer/relay_application_logic/send_transfer.rs deleted file mode 100644 index aae23629a2..0000000000 --- a/modules/src/applications/ics20_fungible_token_transfer/relay_application_logic/send_transfer.rs +++ /dev/null @@ -1,52 +0,0 @@ -use crate::applications::ics20_fungible_token_transfer::error::Error; -use crate::applications::ics20_fungible_token_transfer::msgs::transfer::MsgTransfer; -use crate::clients::host_functions::HostFunctionsProvider; -use crate::core::ics04_channel::handler::send_packet::send_packet; -use crate::core::ics04_channel::packet::Packet; -use crate::core::ics04_channel::packet::PacketResult; -use crate::core::ics26_routing::context::LightClientContext; -use crate::handler::HandlerOutput; -use crate::prelude::*; - -pub(crate) fn send_transfer( - ctx: &Ctx, - msg: MsgTransfer, -) -> Result, Error> -where - Ctx: LightClientContext, -{ - let source_channel_end = ctx - .channel_end(&(msg.source_port.clone(), msg.source_channel)) - .map_err(Error::ics04_channel)?; - - let destination_port = source_channel_end.counterparty().port_id().clone(); - let destination_channel = source_channel_end - .counterparty() - .channel_id() - .ok_or_else(|| { - Error::destination_channel_not_found(msg.source_port.clone(), msg.source_channel) - })?; - - // get the next sequence - let sequence = ctx - .get_next_sequence_send(&(msg.source_port.clone(), msg.source_channel)) - .map_err(Error::ics04_channel)?; - - //TODO: Application LOGIC. - - let packet = Packet { - sequence, - source_port: msg.source_port, - source_channel: msg.source_channel, - destination_port, - destination_channel: *destination_channel, - data: vec![0], - timeout_height: msg.timeout_height, - timeout_timestamp: msg.timeout_timestamp, - }; - - let handler_output = send_packet(ctx, packet).map_err(Error::ics04_channel)?; - - //TODO: add event/atributes and writes to the store issued by the application logic for packet sending. - Ok(handler_output) -} diff --git a/modules/src/applications/transfer/context.rs b/modules/src/applications/transfer/context.rs index 13220ca87b..ab9c1afdb8 100644 --- a/modules/src/applications/transfer/context.rs +++ b/modules/src/applications/transfer/context.rs @@ -1,4 +1,3 @@ -use sha2::{Digest, Sha256}; use subtle_encoding::hex; use super::error::Error as Ics20Error; @@ -16,7 +15,7 @@ use crate::core::ics04_channel::packet::Packet; use crate::core::ics04_channel::Version; use crate::core::ics05_port::context::PortReader; use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId}; -use crate::core::ics26_routing::context::{ModuleOutputBuilder, OnRecvPacketAck}; +use crate::core::ics26_routing::context::{ModuleOutputBuilder, OnRecvPacketAck, ReaderContext}; use crate::prelude::*; use crate::signer::Signer; @@ -26,7 +25,10 @@ pub trait Ics20Keeper: type AccountId; } -pub trait Ics20Reader: ChannelReader + PortReader { +pub trait Ics20Reader: ChannelReader + PortReader +where + Self: Sized, +{ type AccountId: TryFrom; /// get_port returns the portID for the transfer module. @@ -38,7 +40,8 @@ pub trait Ics20Reader: ChannelReader + PortReader { port_id: &PortId, channel_id: ChannelId, ) -> Result<::AccountId, Ics20Error> { - let hash = cosmos_adr028_escrow_address(port_id, channel_id); + let hash = cosmos_adr028_escrow_address(self, port_id, channel_id); + String::from_utf8(hex::encode_upper(hash)) .expect("hex encoded bytes are not valid UTF8") .parse::() @@ -61,15 +64,17 @@ pub trait Ics20Reader: ChannelReader + PortReader { } // https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-028-public-key-addresses.md -fn cosmos_adr028_escrow_address(port_id: &PortId, channel_id: ChannelId) -> Vec { +fn cosmos_adr028_escrow_address( + ctx: &dyn ChannelReader, + port_id: &PortId, + channel_id: ChannelId, +) -> Vec { let contents = format!("{}/{}", port_id, channel_id); + let mut data = VERSION.as_bytes().to_vec(); + data.extend_from_slice(&[0]); + data.extend_from_slice(contents.as_bytes()); - let mut hasher = Sha256::new(); - hasher.update(VERSION.as_bytes()); - hasher.update([0]); - hasher.update(contents.as_bytes()); - - let mut hash = hasher.finalize().to_vec(); + let mut hash = ctx.hash(data); hash.truncate(20); hash } @@ -105,6 +110,7 @@ pub trait BankKeeper { pub trait Ics20Context: Ics20Keeper::AccountId> + Ics20Reader::AccountId> + + ReaderContext { type AccountId: TryFrom; } @@ -296,6 +302,9 @@ pub fn on_timeout_packet( #[cfg(test)] pub(crate) mod test { + use std::sync::Mutex; + + use std::sync::Arc; use subtle_encoding::bech32; use crate::applications::transfer::context::cosmos_adr028_escrow_address; @@ -305,6 +314,7 @@ pub(crate) mod test { use crate::applications::transfer::PrefixedCoin; use crate::core::ics04_channel::error::Error; use crate::handler::HandlerOutputBuilder; + use crate::mock::context::MockIbcStore; use crate::prelude::*; use crate::test_utils::DummyTransferModule; @@ -322,7 +332,9 @@ pub(crate) mod test { let port_id = port_id.parse().unwrap(); let channel_id = channel_id.parse().unwrap(); let gen_address = { - let addr = cosmos_adr028_escrow_address(&port_id, channel_id); + let ibc_store = MockIbcStore::default(); + let ctx = DummyTransferModule::new(Arc::new(Mutex::new(ibc_store))); + let addr = cosmos_adr028_escrow_address(&ctx, &port_id, channel_id); bech32::encode("cosmos", addr) }; assert_eq!(gen_address, address.to_owned()) diff --git a/modules/src/clients/ics07_tendermint/client_def.rs b/modules/src/clients/ics07_tendermint/client_def.rs index 8311d361af..8f3250d57f 100644 --- a/modules/src/clients/ics07_tendermint/client_def.rs +++ b/modules/src/clients/ics07_tendermint/client_def.rs @@ -31,7 +31,7 @@ use crate::core::ics24_host::path::{ ConnectionsPath, ReceiptsPath, SeqRecvsPath, }; use crate::core::ics24_host::Path; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::downcast; use crate::prelude::*; use crate::Height; @@ -48,7 +48,7 @@ impl ClientDef for TendermintClient { fn verify_header( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -137,7 +137,7 @@ impl ClientDef for TendermintClient { fn update_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -161,7 +161,7 @@ impl ClientDef for TendermintClient { fn check_for_misbehaviour( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -242,7 +242,7 @@ impl ClientDef for TendermintClient { fn verify_client_consensus_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, client_state: &Self::ClientState, height: Height, prefix: &CommitmentPrefix, @@ -267,7 +267,7 @@ impl ClientDef for TendermintClient { fn verify_connection_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -288,7 +288,7 @@ impl ClientDef for TendermintClient { fn verify_channel_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -310,7 +310,7 @@ impl ClientDef for TendermintClient { fn verify_client_full_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, client_state: &Self::ClientState, height: Height, prefix: &CommitmentPrefix, @@ -330,7 +330,7 @@ impl ClientDef for TendermintClient { fn verify_packet_data( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, _client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -363,7 +363,7 @@ impl ClientDef for TendermintClient { fn verify_packet_acknowledgement( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, _client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -396,7 +396,7 @@ impl ClientDef for TendermintClient { fn verify_next_sequence_recv( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, _client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -428,7 +428,7 @@ impl ClientDef for TendermintClient { fn verify_packet_receipt_absence( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, _client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -512,7 +512,7 @@ fn verify_non_membership( } fn verify_delay_passed( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, connection_end: &ConnectionEnd, ) -> Result<(), Ics02Error> { diff --git a/modules/src/clients/ics11_beefy/client_def.rs b/modules/src/clients/ics11_beefy/client_def.rs index 8f1d948a1e..d734b1cf3b 100644 --- a/modules/src/clients/ics11_beefy/client_def.rs +++ b/modules/src/clients/ics11_beefy/client_def.rs @@ -29,7 +29,7 @@ use crate::core::ics23_commitment::commitment::{ use crate::core::ics24_host::identifier::ConnectionId; use crate::core::ics24_host::identifier::{ChannelId, ClientId, PortId}; use crate::core::ics24_host::Path; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::prelude::*; use crate::Height; use core::marker::PhantomData; @@ -56,7 +56,7 @@ impl ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient ClientDef for BeefyClient>( } fn verify_delay_passed( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, connection_end: &ConnectionEnd, ) -> Result<(), Error> { diff --git a/modules/src/clients/ics13_near/client_def.rs b/modules/src/clients/ics13_near/client_def.rs index b8523d6450..a59f8e93bf 100644 --- a/modules/src/clients/ics13_near/client_def.rs +++ b/modules/src/clients/ics13_near/client_def.rs @@ -10,7 +10,7 @@ use crate::core::ics23_commitment::commitment::{ CommitmentPrefix, CommitmentProofBytes, CommitmentRoot, }; use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::Height; use core::marker::PhantomData; @@ -54,7 +54,7 @@ impl ClientDef for NearClient { // rehydrate client from its own storage, then call this function fn verify_header( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -65,7 +65,7 @@ impl ClientDef for NearClient { fn update_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: ClientId, _client_state: Self::ClientState, _header: Self::Header, @@ -93,7 +93,7 @@ impl ClientDef for NearClient { fn check_for_misbehaviour( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: ClientId, _client_state: Self::ClientState, _header: Self::Header, @@ -113,7 +113,7 @@ impl ClientDef for NearClient { fn verify_client_consensus_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_state: &Self::ClientState, _height: Height, _prefix: &CommitmentPrefix, @@ -129,7 +129,7 @@ impl ClientDef for NearClient { // Consensus state will be verified in the verification functions before these are called fn verify_connection_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -144,7 +144,7 @@ impl ClientDef for NearClient { fn verify_channel_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -160,7 +160,7 @@ impl ClientDef for NearClient { fn verify_client_full_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_state: &Self::ClientState, _height: Height, _prefix: &CommitmentPrefix, @@ -174,7 +174,7 @@ impl ClientDef for NearClient { fn verify_packet_data( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -191,7 +191,7 @@ impl ClientDef for NearClient { fn verify_packet_acknowledgement( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -208,7 +208,7 @@ impl ClientDef for NearClient { fn verify_next_sequence_recv( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -224,7 +224,7 @@ impl ClientDef for NearClient { fn verify_packet_receipt_absence( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, diff --git a/modules/src/core/ics02_client/client_def.rs b/modules/src/core/ics02_client/client_def.rs index ffe6c067ec..08470e7a29 100644 --- a/modules/src/core/ics02_client/client_def.rs +++ b/modules/src/core/ics02_client/client_def.rs @@ -14,7 +14,7 @@ use crate::core::ics23_commitment::commitment::{ CommitmentPrefix, CommitmentProofBytes, CommitmentRoot, }; use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::downcast; use crate::prelude::*; use crate::Height; @@ -36,7 +36,7 @@ pub trait ClientDef: Clone { fn verify_header( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -44,7 +44,7 @@ pub trait ClientDef: Clone { fn update_state( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -58,7 +58,7 @@ pub trait ClientDef: Clone { fn check_for_misbehaviour( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -83,7 +83,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_client_consensus_state( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_state: &Self::ClientState, height: Height, prefix: &CommitmentPrefix, @@ -98,7 +98,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_connection_state( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -113,7 +113,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_channel_state( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -129,7 +129,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_client_full_state( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_state: &Self::ClientState, height: Height, prefix: &CommitmentPrefix, @@ -143,7 +143,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_packet_data( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -160,7 +160,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_packet_acknowledgement( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -177,7 +177,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_next_sequence_recv( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -193,7 +193,7 @@ pub trait ClientDef: Clone { #[allow(clippy::too_many_arguments)] fn verify_packet_receipt_absence( &self, - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_id: &ClientId, client_state: &Self::ClientState, height: Height, @@ -236,7 +236,7 @@ impl ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient ClientDef for AnyClient( msg: ClientMsg, ) -> Result, Error> where - Ctx: LightClientContext, + Ctx: ReaderContext, HostFunctions: HostFunctionsProvider, { match msg { diff --git a/modules/src/core/ics02_client/handler/create_client.rs b/modules/src/core/ics02_client/handler/create_client.rs index 169864be05..41cb2bf136 100644 --- a/modules/src/core/ics02_client/handler/create_client.rs +++ b/modules/src/core/ics02_client/handler/create_client.rs @@ -1,6 +1,6 @@ //! Protocol logic specific to processing ICS2 messages of type `MsgCreateAnyClient`. -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::prelude::*; use core::fmt::Debug; @@ -30,7 +30,7 @@ pub struct Result { } pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: MsgCreateAnyClient, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics02_client/handler/update_client.rs b/modules/src/core/ics02_client/handler/update_client.rs index b3f3a57c48..472ebc9054 100644 --- a/modules/src/core/ics02_client/handler/update_client.rs +++ b/modules/src/core/ics02_client/handler/update_client.rs @@ -12,7 +12,7 @@ use crate::core::ics02_client::handler::ClientResult; use crate::core::ics02_client::height::Height; use crate::core::ics02_client::msgs::update_client::MsgUpdateAnyClient; use crate::core::ics24_host::identifier::ClientId; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -30,7 +30,7 @@ pub struct Result { } pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: MsgUpdateAnyClient, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics02_client/handler/upgrade_client.rs b/modules/src/core/ics02_client/handler/upgrade_client.rs index 3cc7722c43..0ef3738593 100644 --- a/modules/src/core/ics02_client/handler/upgrade_client.rs +++ b/modules/src/core/ics02_client/handler/upgrade_client.rs @@ -8,7 +8,7 @@ use crate::core::ics02_client::events::Attributes; use crate::core::ics02_client::handler::ClientResult; use crate::core::ics02_client::msgs::upgrade_client::MsgUpgradeAnyClient; use crate::core::ics24_host::identifier::ClientId; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -24,7 +24,7 @@ pub struct Result { } pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: MsgUpgradeAnyClient, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics02_client/msgs/create_client.rs b/modules/src/core/ics02_client/msgs/create_client.rs index effd6bec72..c3018f4e47 100644 --- a/modules/src/core/ics02_client/msgs/create_client.rs +++ b/modules/src/core/ics02_client/msgs/create_client.rs @@ -78,7 +78,7 @@ impl TryFrom for MsgCreateAnyClient { MsgCreateAnyClient::new( AnyClientState::try_from(raw_client_state)?, consensus_state, - raw.signer.into(), + raw.signer.parse().map_err(Error::signer)?, ) } } diff --git a/modules/src/core/ics02_client/msgs/upgrade_client.rs b/modules/src/core/ics02_client/msgs/upgrade_client.rs index db7907dfed..fe5b94347c 100644 --- a/modules/src/core/ics02_client/msgs/upgrade_client.rs +++ b/modules/src/core/ics02_client/msgs/upgrade_client.rs @@ -93,7 +93,7 @@ impl TryFrom for MsgUpgradeAnyClient { consensus_state: AnyConsensusState::try_from(raw_consensus_state)?, proof_upgrade_client: proto_msg.proof_upgrade_client, proof_upgrade_consensus_state: proto_msg.proof_upgrade_consensus_state, - signer: proto_msg.signer.into(), + signer: Signer::from_str(proto_msg.signer.as_str()).map_err(Error::signer)?, }) } } diff --git a/modules/src/core/ics03_connection/handler.rs b/modules/src/core/ics03_connection/handler.rs index e3d596b3c7..154c1a3e6f 100644 --- a/modules/src/core/ics03_connection/handler.rs +++ b/modules/src/core/ics03_connection/handler.rs @@ -4,7 +4,7 @@ use crate::core::ics03_connection::connection::ConnectionEnd; use crate::core::ics03_connection::error::Error; use crate::core::ics03_connection::msgs::ConnectionMsg; use crate::core::ics24_host::identifier::ConnectionId; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::handler::HandlerOutput; use core::fmt::Debug; @@ -47,7 +47,7 @@ pub fn dispatch( msg: ConnectionMsg, ) -> Result, Error> where - Ctx: LightClientContext, + Ctx: ReaderContext, HostFunctions: HostFunctionsProvider, { match msg { diff --git a/modules/src/core/ics03_connection/handler/conn_open_ack.rs b/modules/src/core/ics03_connection/handler/conn_open_ack.rs index 1fd4fca6bd..7f1dd4a9da 100644 --- a/modules/src/core/ics03_connection/handler/conn_open_ack.rs +++ b/modules/src/core/ics03_connection/handler/conn_open_ack.rs @@ -9,13 +9,13 @@ use crate::core::ics03_connection::handler::verify::{ }; use crate::core::ics03_connection::handler::{ConnectionIdState, ConnectionResult}; use crate::core::ics03_connection::msgs::conn_open_ack::MsgConnectionOpenAck; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: MsgConnectionOpenAck, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics03_connection/handler/conn_open_confirm.rs b/modules/src/core/ics03_connection/handler/conn_open_confirm.rs index aa196d60a5..d331820a37 100644 --- a/modules/src/core/ics03_connection/handler/conn_open_confirm.rs +++ b/modules/src/core/ics03_connection/handler/conn_open_confirm.rs @@ -7,13 +7,13 @@ use crate::core::ics03_connection::events::Attributes; use crate::core::ics03_connection::handler::verify::verify_proofs; use crate::core::ics03_connection::handler::{ConnectionIdState, ConnectionResult}; use crate::core::ics03_connection::msgs::conn_open_confirm::MsgConnectionOpenConfirm; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: MsgConnectionOpenConfirm, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics03_connection/handler/conn_open_init.rs b/modules/src/core/ics03_connection/handler/conn_open_init.rs index 89b8efbb68..f2c2f386d7 100644 --- a/modules/src/core/ics03_connection/handler/conn_open_init.rs +++ b/modules/src/core/ics03_connection/handler/conn_open_init.rs @@ -6,13 +6,13 @@ use crate::core::ics03_connection::events::Attributes; use crate::core::ics03_connection::handler::{ConnectionIdState, ConnectionResult}; use crate::core::ics03_connection::msgs::conn_open_init::MsgConnectionOpenInit; use crate::core::ics24_host::identifier::ConnectionId; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: MsgConnectionOpenInit, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics03_connection/handler/conn_open_try.rs b/modules/src/core/ics03_connection/handler/conn_open_try.rs index dfc0c9301a..e5f556c780 100644 --- a/modules/src/core/ics03_connection/handler/conn_open_try.rs +++ b/modules/src/core/ics03_connection/handler/conn_open_try.rs @@ -10,13 +10,13 @@ use crate::core::ics03_connection::handler::verify::{ use crate::core::ics03_connection::handler::{ConnectionIdState, ConnectionResult}; use crate::core::ics03_connection::msgs::conn_open_try::MsgConnectionOpenTry; use crate::core::ics24_host::identifier::ConnectionId; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: MsgConnectionOpenTry, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics03_connection/handler/verify.rs b/modules/src/core/ics03_connection/handler/verify.rs index 2acc9544e3..66f9404f70 100644 --- a/modules/src/core/ics03_connection/handler/verify.rs +++ b/modules/src/core/ics03_connection/handler/verify.rs @@ -6,13 +6,13 @@ use crate::core::ics02_client::{client_def::AnyClient, client_def::ClientDef}; use crate::core::ics03_connection::connection::ConnectionEnd; use crate::core::ics03_connection::error::Error; use crate::core::ics23_commitment::commitment::CommitmentProofBytes; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::proofs::{ConsensusProof, Proofs}; use crate::Height; /// Entry point for verifying all proofs bundled in any ICS3 message. pub fn verify_proofs( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, client_state: Option, height: Height, connection_end: &ConnectionEnd, @@ -60,7 +60,7 @@ pub fn verify_proofs( /// claims to prove that an object of type connection exists on the source chain (i.e., the chain /// which created this proof). This object must match the state of `expected_conn`. pub fn verify_connection_proof( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, connection_end: &ConnectionEnd, expected_conn: &ConnectionEnd, @@ -115,7 +115,7 @@ pub fn verify_connection_proof( /// at the same revision as the current chain, with matching chain identifiers, etc) and that the /// `proof` is correct. pub fn verify_client_proof( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, connection_end: &ConnectionEnd, expected_client_state: AnyClientState, @@ -154,7 +154,7 @@ pub fn verify_client_proof( } pub fn verify_consensus_proof( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, connection_end: &ConnectionEnd, proof: &ConsensusProof, @@ -197,7 +197,7 @@ pub fn verify_consensus_proof( /// Checks that `claimed_height` is within normal bounds, i.e., fresh enough so that the chain has /// not pruned it yet, but not newer than the current (actual) height of the local chain. pub fn check_client_consensus_height( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, claimed_height: Height, ) -> Result<(), Error> { if claimed_height > ctx.host_height() { diff --git a/modules/src/core/ics04_channel/context.rs b/modules/src/core/ics04_channel/context.rs index 13f2df1ffc..0cd1aff12f 100644 --- a/modules/src/core/ics04_channel/context.rs +++ b/modules/src/core/ics04_channel/context.rs @@ -15,7 +15,7 @@ use crate::prelude::*; use crate::timestamp::Timestamp; use crate::Height; -use super::packet::{PacketResult, Sequence}; +use super::packet::{Packet, PacketResult, Sequence}; /// A context supplying all the necessary read-only dependencies for processing any `ChannelMsg`. pub trait ChannelReader { @@ -71,7 +71,7 @@ pub trait ChannelReader { self.hash(ack.into_bytes()).into() } - /// A hashing function for packet commitments + /// A Sha2_256 hashing function fn hash(&self, value: Vec) -> Vec; /// Returns the time when the client state for the given [`ClientId`] was updated with a header for the given [`Height`] @@ -137,6 +137,8 @@ pub trait ChannelKeeper { (res.port_id.clone(), res.channel_id, res.seq), res.commitment, )?; + + self.store_packet((res.port_id.clone(), res.channel_id, res.seq), res.packet)?; } PacketResult::Recv(res) => { let res = match res { @@ -199,6 +201,13 @@ pub trait ChannelKeeper { commitment: PacketCommitment, ) -> Result<(), Error>; + /// Allow implementers to optionally store packet in storage + fn store_packet( + &mut self, + key: (PortId, ChannelId, Sequence), + packet: Packet, + ) -> Result<(), Error>; + fn delete_packet_commitment(&mut self, key: (PortId, ChannelId, Sequence)) -> Result<(), Error>; diff --git a/modules/src/core/ics04_channel/error.rs b/modules/src/core/ics04_channel/error.rs index 61f77a28e3..b4473966e3 100644 --- a/modules/src/core/ics04_channel/error.rs +++ b/modules/src/core/ics04_channel/error.rs @@ -344,6 +344,14 @@ define_error! { ImplementationSpecific { reason: String } | e | { format_args!("implementation specific error: {}", e.reason) }, + + AppModule + { description: String } + | e | { + format_args!( + "application module error: {0}", + e.description) + }, } } diff --git a/modules/src/core/ics04_channel/handler.rs b/modules/src/core/ics04_channel/handler.rs index cbdba24490..bb0a789a23 100644 --- a/modules/src/core/ics04_channel/handler.rs +++ b/modules/src/core/ics04_channel/handler.rs @@ -7,7 +7,7 @@ use crate::core::ics04_channel::msgs::ChannelMsg; use crate::core::ics04_channel::{msgs::PacketMsg, packet::PacketResult}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; use crate::core::ics26_routing::context::{ - Ics26Context, LightClientContext, ModuleId, ModuleOutput, OnRecvPacketAck, Router, + Ics26Context, ModuleId, ModuleOutputBuilder, OnRecvPacketAck, ReaderContext, Router, }; use crate::handler::{HandlerOutput, HandlerOutputBuilder}; use core::fmt::Debug; @@ -64,7 +64,7 @@ pub fn channel_dispatch( msg: &ChannelMsg, ) -> Result<(HandlerOutputBuilder<()>, ChannelResult), Error> where - Ctx: LightClientContext, + Ctx: ReaderContext, HostFunctions: HostFunctionsProvider, { let output = match msg { @@ -177,7 +177,7 @@ pub fn packet_dispatch( msg: &PacketMsg, ) -> Result<(HandlerOutputBuilder<()>, PacketResult), Error> where - Ctx: LightClientContext, + Ctx: ReaderContext, HostFunctions: HostFunctionsProvider, { let output = match msg { diff --git a/modules/src/core/ics04_channel/handler/acknowledgement.rs b/modules/src/core/ics04_channel/handler/acknowledgement.rs index d316e2a703..ccdc305a55 100644 --- a/modules/src/core/ics04_channel/handler/acknowledgement.rs +++ b/modules/src/core/ics04_channel/handler/acknowledgement.rs @@ -8,7 +8,7 @@ use crate::core::ics04_channel::handler::verify::verify_packet_acknowledgement_p use crate::core::ics04_channel::msgs::acknowledgement::MsgAcknowledgement; use crate::core::ics04_channel::packet::{PacketResult, Sequence}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -23,7 +23,7 @@ pub struct AckPacketResult { } pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgAcknowledgement, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/chan_close_confirm.rs b/modules/src/core/ics04_channel/handler/chan_close_confirm.rs index 308d1e90bb..54bdcbef43 100644 --- a/modules/src/core/ics04_channel/handler/chan_close_confirm.rs +++ b/modules/src/core/ics04_channel/handler/chan_close_confirm.rs @@ -8,13 +8,13 @@ use crate::core::ics04_channel::events::Attributes; use crate::core::ics04_channel::handler::verify::verify_channel_proofs; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_close_confirm::MsgChannelCloseConfirm; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgChannelCloseConfirm, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/chan_close_init.rs b/modules/src/core/ics04_channel/handler/chan_close_init.rs index 1cfd416536..8abfd0e339 100644 --- a/modules/src/core/ics04_channel/handler/chan_close_init.rs +++ b/modules/src/core/ics04_channel/handler/chan_close_init.rs @@ -6,12 +6,12 @@ use crate::core::ics04_channel::error::Error; use crate::core::ics04_channel::events::Attributes; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_close_init::MsgChannelCloseInit; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgChannelCloseInit, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/chan_open_ack.rs b/modules/src/core/ics04_channel/handler/chan_open_ack.rs index d98316303a..7138a55a66 100644 --- a/modules/src/core/ics04_channel/handler/chan_open_ack.rs +++ b/modules/src/core/ics04_channel/handler/chan_open_ack.rs @@ -7,13 +7,13 @@ use crate::core::ics04_channel::events::Attributes; use crate::core::ics04_channel::handler::verify::verify_channel_proofs; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_open_ack::MsgChannelOpenAck; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgChannelOpenAck, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/chan_open_confirm.rs b/modules/src/core/ics04_channel/handler/chan_open_confirm.rs index 7704959d67..f605500d50 100644 --- a/modules/src/core/ics04_channel/handler/chan_open_confirm.rs +++ b/modules/src/core/ics04_channel/handler/chan_open_confirm.rs @@ -7,13 +7,13 @@ use crate::core::ics04_channel::events::Attributes; use crate::core::ics04_channel::handler::verify::verify_channel_proofs; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_open_confirm::MsgChannelOpenConfirm; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgChannelOpenConfirm, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/chan_open_init.rs b/modules/src/core/ics04_channel/handler/chan_open_init.rs index 4a1e9ecc7a..4c7af9e743 100644 --- a/modules/src/core/ics04_channel/handler/chan_open_init.rs +++ b/modules/src/core/ics04_channel/handler/chan_open_init.rs @@ -6,13 +6,13 @@ use crate::core::ics04_channel::events::Attributes; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_open_init::MsgChannelOpenInit; use crate::core::ics24_host::identifier::ChannelId; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgChannelOpenInit, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/chan_open_try.rs b/modules/src/core/ics04_channel/handler/chan_open_try.rs index 810cc4c3bc..2564486364 100644 --- a/modules/src/core/ics04_channel/handler/chan_open_try.rs +++ b/modules/src/core/ics04_channel/handler/chan_open_try.rs @@ -9,13 +9,13 @@ use crate::core::ics04_channel::handler::verify::verify_channel_proofs; use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult}; use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry; use crate::core::ics24_host::identifier::ChannelId; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub(crate) fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgChannelOpenTry, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/recv_packet.rs b/modules/src/core/ics04_channel/handler/recv_packet.rs index 8d04445504..8ac7ae0c71 100644 --- a/modules/src/core/ics04_channel/handler/recv_packet.rs +++ b/modules/src/core/ics04_channel/handler/recv_packet.rs @@ -7,7 +7,7 @@ use crate::core::ics04_channel::handler::verify::verify_packet_recv_proofs; use crate::core::ics04_channel::msgs::recv_packet::MsgRecvPacket; use crate::core::ics04_channel::packet::{PacketResult, Receipt, Sequence}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -31,7 +31,7 @@ pub enum RecvPacketResult { } pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgRecvPacket, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/send_packet.rs b/modules/src/core/ics04_channel/handler/send_packet.rs index bf0b34436d..ca484371f8 100644 --- a/modules/src/core/ics04_channel/handler/send_packet.rs +++ b/modules/src/core/ics04_channel/handler/send_packet.rs @@ -6,7 +6,7 @@ use crate::core::ics04_channel::events::SendPacket; use crate::core::ics04_channel::packet::{PacketResult, Sequence}; use crate::core::ics04_channel::{error::Error, packet::Packet}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -19,12 +19,10 @@ pub struct SendPacketResult { pub seq: Sequence, pub seq_number: Sequence, pub commitment: PacketCommitment, + pub packet: Packet, } -pub fn send_packet( - ctx: &dyn LightClientContext, - packet: Packet, -) -> HandlerResult { +pub fn send_packet(ctx: &dyn ReaderContext, packet: Packet) -> HandlerResult { let mut output = HandlerOutput::builder(); let source_channel_end = @@ -96,6 +94,7 @@ pub fn send_packet( channel_id: packet.source_channel, seq: packet.sequence, seq_number: next_seq_send.increment(), + packet: packet.clone(), commitment: ctx.packet_commitment( packet.data.clone(), packet.timeout_height, diff --git a/modules/src/core/ics04_channel/handler/timeout.rs b/modules/src/core/ics04_channel/handler/timeout.rs index cf7c08b015..5f2e7b4480 100644 --- a/modules/src/core/ics04_channel/handler/timeout.rs +++ b/modules/src/core/ics04_channel/handler/timeout.rs @@ -9,7 +9,7 @@ use crate::core::ics04_channel::handler::verify::{ use crate::core::ics04_channel::msgs::timeout::MsgTimeout; use crate::core::ics04_channel::packet::{PacketResult, Sequence}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; @@ -25,7 +25,7 @@ pub struct TimeoutPacketResult { } pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgTimeout, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/timeout_on_close.rs b/modules/src/core/ics04_channel/handler/timeout_on_close.rs index 2be21ade6c..3b194a395f 100644 --- a/modules/src/core/ics04_channel/handler/timeout_on_close.rs +++ b/modules/src/core/ics04_channel/handler/timeout_on_close.rs @@ -9,13 +9,13 @@ use crate::core::ics04_channel::handler::verify::{ use crate::core::ics04_channel::msgs::timeout_on_close::MsgTimeoutOnClose; use crate::core::ics04_channel::packet::PacketResult; use crate::core::ics04_channel::{error::Error, handler::timeout::TimeoutPacketResult}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::events::IbcEvent; use crate::handler::{HandlerOutput, HandlerResult}; use crate::prelude::*; pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, msg: &MsgTimeoutOnClose, ) -> HandlerResult { let mut output = HandlerOutput::builder(); diff --git a/modules/src/core/ics04_channel/handler/verify.rs b/modules/src/core/ics04_channel/handler/verify.rs index 086cf591c8..7e2f2e9ea9 100644 --- a/modules/src/core/ics04_channel/handler/verify.rs +++ b/modules/src/core/ics04_channel/handler/verify.rs @@ -7,14 +7,14 @@ use crate::core::ics04_channel::channel::ChannelEnd; use crate::core::ics04_channel::error::Error; use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement; use crate::core::ics04_channel::packet::{Packet, Sequence}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::prelude::*; use crate::proofs::Proofs; use crate::Height; /// Entry point for verifying all proofs bundled in any ICS4 message for channel protocols. pub fn verify_channel_proofs( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, channel_end: &ChannelEnd, connection_end: &ConnectionEnd, @@ -57,7 +57,7 @@ pub fn verify_channel_proofs( /// Entry point for verifying all proofs bundled in a ICS4 packet recv. message. pub fn verify_packet_recv_proofs( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, packet: &Packet, connection_end: &ConnectionEnd, @@ -105,7 +105,7 @@ pub fn verify_packet_recv_proofs( /// Entry point for verifying all proofs bundled in an ICS4 packet ack message. pub fn verify_packet_acknowledgement_proofs( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, packet: &Packet, acknowledgement: Acknowledgement, @@ -150,7 +150,7 @@ pub fn verify_packet_acknowledgement_proofs( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, connection_end: &ConnectionEnd, packet: Packet, @@ -191,7 +191,7 @@ pub fn verify_next_sequence_recv( } pub fn verify_packet_receipt_absence( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, height: Height, connection_end: &ConnectionEnd, packet: Packet, diff --git a/modules/src/core/ics04_channel/handler/write_acknowledgement.rs b/modules/src/core/ics04_channel/handler/write_acknowledgement.rs index e86f1f5821..299bd68f9d 100644 --- a/modules/src/core/ics04_channel/handler/write_acknowledgement.rs +++ b/modules/src/core/ics04_channel/handler/write_acknowledgement.rs @@ -4,7 +4,7 @@ use crate::core::ics04_channel::error::Error; use crate::core::ics04_channel::events::WriteAcknowledgement; use crate::core::ics04_channel::packet::{Packet, PacketResult, Sequence}; use crate::core::ics24_host::identifier::{ChannelId, PortId}; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::prelude::*; use crate::{ events::IbcEvent, @@ -20,7 +20,7 @@ pub struct WriteAckPacketResult { } pub fn process( - ctx: &dyn LightClientContext, + ctx: &dyn ReaderContext, packet: Packet, ack: Vec, ) -> HandlerResult { diff --git a/modules/src/core/ics26_routing/context.rs b/modules/src/core/ics26_routing/context.rs index aac61fb214..0582bd9673 100644 --- a/modules/src/core/ics26_routing/context.rs +++ b/modules/src/core/ics26_routing/context.rs @@ -22,7 +22,7 @@ use crate::handler::HandlerOutputBuilder; use crate::signer::Signer; /// This trait captures all the functional dependencies of needed in light client implementations -pub trait LightClientContext: ClientReader + ConnectionReader + ChannelReader {} +pub trait ReaderContext: ClientReader + ConnectionReader + ChannelReader {} /// This trait captures all the functional dependencies (i.e., context) which the ICS26 module /// requires to be able to dispatch and process IBC messages. In other words, this is the @@ -35,8 +35,7 @@ pub trait Ics26Context: + ChannelKeeper + ChannelReader + PortReader - + Ics20Context - + LightClientContext + + ReaderContext { type Router: Router; diff --git a/modules/src/core/ics26_routing/handler.rs b/modules/src/core/ics26_routing/handler.rs index b0cd942218..c2f0765b88 100644 --- a/modules/src/core/ics26_routing/handler.rs +++ b/modules/src/core/ics26_routing/handler.rs @@ -3,7 +3,6 @@ use crate::prelude::*; use ibc_proto::google::protobuf::Any; -use crate::applications::ics20_fungible_token_transfer::relay_application_logic::send_transfer::send_transfer as ics20_msg_dispatcher; use crate::core::ics02_client::handler::dispatch as ics2_msg_dispatcher; use crate::core::ics03_connection::handler::dispatch as ics3_msg_dispatcher; use crate::core::ics04_channel::handler::{ @@ -32,10 +31,7 @@ pub struct MsgReceipt { /// Mimics the DeliverTx ABCI interface, but for a single message and at a slightly lower level. /// No need for authentication info or signature checks here. /// Returns a vector of all events that got generated as a byproduct of processing `message`. -pub fn deliver( - ctx: &mut Ctx, - message: Any, -) -> Result<(Vec, Vec), Error> +pub fn deliver(ctx: &mut Ctx, message: Any) -> Result where Ctx: Ics26Context, HostFunctions: HostFunctionsProvider, @@ -44,7 +40,7 @@ where let envelope = decode(message)?; // Process the envelope, and accumulate any events that were generated. - let output = dispatch::<_, HostFunctions>(ctx, envelope)?; + let HandlerOutput { log, events, .. } = dispatch::<_, HostFunctions>(ctx, envelope)?; Ok(MsgReceipt { events, log }) } @@ -114,20 +110,6 @@ where handler_builder.with_result(()) } - Ics20Msg(msg) => { - let handler_output = ics20_msg_dispatcher::<_, HostFunctions>(ctx, msg) - .map_err(Error::ics20_fungible_token_transfer)?; - - // Apply any results to the host chain store. - ctx.store_packet_result(handler_output.result) - .map_err(Error::ics04_channel)?; - - HandlerOutput::builder() - .with_log(handler_output.log) - .with_events(handler_output.events) - .with_result(()) - } - Ics4PacketMsg(msg) => { let module_id = get_module_for_packet_msg(ctx, &msg).map_err(Error::ics04_channel)?; let (mut handler_builder, packet_result) = @@ -160,12 +142,10 @@ mod tests { use test_log::test; - use crate::applications::ics20_fungible_token_transfer::msgs::transfer::test_util::get_dummy_msg_transfer; + use crate::applications::transfer::context::test::deliver as ics20_deliver; + use crate::applications::transfer::PrefixedCoin; use crate::core::ics02_client::client_consensus::AnyConsensusState; use crate::core::ics02_client::client_state::AnyClientState; - use crate::events::IbcEvent; - use crate::test_utils::Crypto; - use crate::core::ics02_client::msgs::{ create_client::MsgCreateAnyClient, update_client::MsgUpdateAnyClient, upgrade_client::MsgUpgradeAnyClient, ClientMsg, @@ -188,8 +168,8 @@ mod tests { timeout_on_close::{test_util::get_dummy_raw_msg_timeout_on_close, MsgTimeoutOnClose}, ChannelMsg, PacketMsg, }; - use crate::core::ics23_commitment::commitment::test_util::get_dummy_merkle_proof; use crate::events::IbcEvent; + use crate::test_utils::Crypto; use crate::{ applications::transfer::msgs::transfer::test_util::get_dummy_msg_transfer, applications::transfer::msgs::transfer::MsgTransfer, @@ -236,7 +216,7 @@ mod tests { // Test parameters struct Test { name: String, - msg: Ics26Envelope, + msg: TestMsg, want_pass: bool, } let default_signer = get_dummy_account_id(); @@ -561,7 +541,23 @@ mod tests { .collect(); for test in tests { - let res = dispatch::<_, Crypto>(&mut ctx, test.msg.clone()); + let res = match test.msg.clone() { + TestMsg::Ics26(msg) => dispatch::<_, Crypto>(&mut ctx, msg).map(|_| ()), + TestMsg::Ics20(msg) => { + let transfer_module = + ctx.router_mut().get_route_mut(&transfer_module_id).unwrap(); + ics20_deliver( + transfer_module + .as_any_mut() + .downcast_mut::() + .unwrap(), + &mut HandlerOutputBuilder::new(), + msg, + ) + .map(|_| ()) + .map_err(Error::ics04_channel) + } + }; assert_eq!( test.want_pass, diff --git a/modules/src/mock/client_def.rs b/modules/src/mock/client_def.rs index 155666847e..728d5be17a 100644 --- a/modules/src/mock/client_def.rs +++ b/modules/src/mock/client_def.rs @@ -13,7 +13,7 @@ use crate::core::ics23_commitment::merkle::apply_prefix; use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; use crate::core::ics24_host::path::ClientConsensusStatePath; use crate::core::ics24_host::Path; -use crate::core::ics26_routing::context::LightClientContext; +use crate::core::ics26_routing::context::ReaderContext; use crate::mock::client_state::{MockClientState, MockConsensusState}; use crate::mock::header::MockHeader; use crate::prelude::*; @@ -30,7 +30,7 @@ impl ClientDef for MockClient { fn update_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: ClientId, client_state: Self::ClientState, header: Self::Header, @@ -50,7 +50,7 @@ impl ClientDef for MockClient { fn verify_client_consensus_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_state: &Self::ClientState, _height: Height, prefix: &CommitmentPrefix, @@ -74,7 +74,7 @@ impl ClientDef for MockClient { fn verify_connection_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -89,7 +89,7 @@ impl ClientDef for MockClient { fn verify_channel_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -105,7 +105,7 @@ impl ClientDef for MockClient { fn verify_client_full_state( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_state: &Self::ClientState, _height: Height, _prefix: &CommitmentPrefix, @@ -119,7 +119,7 @@ impl ClientDef for MockClient { fn verify_packet_data( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -136,7 +136,7 @@ impl ClientDef for MockClient { fn verify_packet_acknowledgement( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -153,7 +153,7 @@ impl ClientDef for MockClient { fn verify_next_sequence_recv( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -169,7 +169,7 @@ impl ClientDef for MockClient { fn verify_packet_receipt_absence( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: &ClientId, _client_state: &Self::ClientState, _height: Height, @@ -198,7 +198,7 @@ impl ClientDef for MockClient { fn verify_header( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: ClientId, _client_state: Self::ClientState, _header: Self::Header, @@ -216,7 +216,7 @@ impl ClientDef for MockClient { fn check_for_misbehaviour( &self, - _ctx: &dyn LightClientContext, + _ctx: &dyn ReaderContext, _client_id: ClientId, _client_state: Self::ClientState, _header: Self::Header, diff --git a/modules/src/mock/context.rs b/modules/src/mock/context.rs index eec73176b5..777aaa7c7b 100644 --- a/modules/src/mock/context.rs +++ b/modules/src/mock/context.rs @@ -37,7 +37,7 @@ use crate::core::ics05_port::error::Error; use crate::core::ics23_commitment::commitment::CommitmentPrefix; use crate::core::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; use crate::core::ics26_routing::context::{ - Ics26Context, LightClientContext, Module, ModuleId, Router, RouterBuilder, + Ics26Context, Module, ModuleId, ReaderContext, Router, RouterBuilder, }; use crate::core::ics26_routing::handler::{deliver, dispatch, MsgReceipt}; use crate::core::ics26_routing::msgs::Ics26Envelope; @@ -513,7 +513,7 @@ impl MockContext { } pub fn consensus_states(&self, client_id: &ClientId) -> Vec { - self.clients[client_id] + self.ibc_store.lock().unwrap().clients[client_id] .consensus_states .iter() .map(|(k, v)| AnyConsensusStateWithHeight { @@ -535,8 +535,8 @@ impl MockContext { &self, client_id: &ClientId, height: &Height, - ) -> &AnyConsensusState { - self.clients[client_id] + ) -> AnyConsensusState { + self.ibc_store.lock().unwrap().clients[client_id] .consensus_states .get(height) .unwrap() @@ -642,7 +642,7 @@ impl Router for MockRouter { } } -impl LightClientContext for MockContext {} +impl ReaderContext for MockContext {} impl Ics26Context for MockContext { type Router = MockRouter; @@ -954,6 +954,14 @@ impl ChannelKeeper for MockContext { .insert(key, receipt); Ok(()) } + + fn store_packet( + &mut self, + _key: (PortId, ChannelId, Sequence), + _packet: crate::core::ics04_channel::packet::Packet, + ) -> Result<(), Ics04Error> { + Ok(()) + } } impl ConnectionReader for MockContext { @@ -1033,7 +1041,7 @@ impl ClientReader for MockContext { client_id: &ClientId, height: Height, ) -> Result { - match self.clients.get(client_id) { + match self.ibc_store.lock().unwrap().clients.get(client_id) { Some(client_record) => match client_record.consensus_states.get(&height) { Some(consensus_state) => Ok(consensus_state.clone()), None => Err(Ics02Error::consensus_state_not_found( @@ -1054,7 +1062,8 @@ impl ClientReader for MockContext { client_id: &ClientId, height: Height, ) -> Result, Ics02Error> { - let client_record = self + let ibc_store = self.ibc_store.lock().unwrap(); + let client_record = ibc_store .clients .get(client_id) .ok_or_else(|| Ics02Error::client_not_found(client_id.clone()))?; @@ -1081,7 +1090,8 @@ impl ClientReader for MockContext { client_id: &ClientId, height: Height, ) -> Result, Ics02Error> { - let client_record = self + let ibc_store = self.ibc_store.lock().unwrap(); + let client_record = ibc_store .clients .get(client_id) .ok_or_else(|| Ics02Error::client_not_found(client_id.clone()))?; @@ -1242,7 +1252,7 @@ impl Ics18Context for MockContext { // Forward call to Ics26 delivery method. let mut all_events = vec![]; for msg in msgs { - let (mut events, _) = + let MsgReceipt { mut events, .. } = deliver::<_, Crypto>(self, msg).map_err(Ics18Error::transaction_failed)?; all_events.append(&mut events); } diff --git a/modules/src/test_utils.rs b/modules/src/test_utils.rs index 04fb47b3ac..23bc3df164 100644 --- a/modules/src/test_utils.rs +++ b/modules/src/test_utils.rs @@ -2,6 +2,8 @@ use std::sync::{Arc, Mutex}; use std::time::Duration; use crate::clients::host_functions::HostFunctionsProvider; +use crate::core::ics02_client::context::ClientReader; +use crate::core::ics03_connection::context::ConnectionReader; use crate::prelude::*; use sp_core::keccak_256; use sp_trie::LayoutV0; @@ -9,16 +11,24 @@ use tendermint::{block, consensus, evidence, public_key::Algorithm}; use crate::clients::ics11_beefy::error::Error as BeefyError; use crate::core::ics02_client::error::Error as Ics02Error; -use crate::core::ics04_channel::channel::{Counterparty, Order}; + +use crate::applications::transfer::context::{BankKeeper, Ics20Context, Ics20Keeper, Ics20Reader}; +use crate::applications::transfer::{error::Error as Ics20Error, PrefixedCoin}; +use crate::core::ics02_client::client_consensus::AnyConsensusState; +use crate::core::ics02_client::client_state::AnyClientState; +use crate::core::ics03_connection::connection::ConnectionEnd; +use crate::core::ics03_connection::error::Error as Ics03Error; +use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty, Order}; +use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment}; +use crate::core::ics04_channel::context::{ChannelKeeper, ChannelReader}; use crate::core::ics04_channel::error::Error; use crate::core::ics04_channel::packet::{Receipt, Sequence}; use crate::core::ics04_channel::Version; use crate::core::ics05_port::context::PortReader; use crate::core::ics05_port::error::Error as PortError; use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; -use crate::core::ics26_routing::context::{Module, ModuleId, ModuleOutputBuilder}; +use crate::core::ics26_routing::context::{Module, ModuleId, ModuleOutputBuilder, ReaderContext}; use crate::mock::context::MockIbcStore; -use crate::prelude::*; use crate::signer::Signer; use crate::timestamp::Timestamp; use crate::Height; @@ -101,7 +111,7 @@ impl HostFunctionsProvider for Crypto { .map(|val| val.to_vec()) } - fn ed25519_verify(_signature: &[u8; 64], _value: &[u8; 32], public_key: &[u8; 32]) -> bool { + fn ed25519_verify(_signature: &[u8; 64], _value: &[u8; 32], _public_key: &[u8]) -> bool { true } @@ -134,3 +144,349 @@ impl HostFunctionsProvider for Crypto { sp_io::hashing::sha2_256(data) } } + +impl Ics20Keeper for DummyTransferModule { + type AccountId = Signer; +} + +impl ChannelKeeper for DummyTransferModule { + fn store_packet_commitment( + &mut self, + key: (PortId, ChannelId, Sequence), + commitment: PacketCommitment, + ) -> Result<(), Error> { + self.ibc_store + .lock() + .unwrap() + .packet_commitment + .insert(key, commitment); + Ok(()) + } + + fn delete_packet_commitment( + &mut self, + _key: (PortId, ChannelId, Sequence), + ) -> Result<(), Error> { + unimplemented!() + } + + fn store_packet_receipt( + &mut self, + _key: (PortId, ChannelId, Sequence), + _receipt: Receipt, + ) -> Result<(), Error> { + unimplemented!() + } + + fn store_packet_acknowledgement( + &mut self, + _key: (PortId, ChannelId, Sequence), + _ack: AcknowledgementCommitment, + ) -> Result<(), Error> { + unimplemented!() + } + + fn delete_packet_acknowledgement( + &mut self, + _key: (PortId, ChannelId, Sequence), + ) -> Result<(), Error> { + unimplemented!() + } + + fn store_connection_channels( + &mut self, + _conn_id: ConnectionId, + _port_channel_id: &(PortId, ChannelId), + ) -> Result<(), Error> { + unimplemented!() + } + + fn store_channel( + &mut self, + _port_channel_id: (PortId, ChannelId), + _channel_end: &ChannelEnd, + ) -> Result<(), Error> { + unimplemented!() + } + + fn store_next_sequence_send( + &mut self, + port_channel_id: (PortId, ChannelId), + seq: Sequence, + ) -> Result<(), Error> { + self.ibc_store + .lock() + .unwrap() + .next_sequence_send + .insert(port_channel_id, seq); + Ok(()) + } + + fn store_next_sequence_recv( + &mut self, + _port_channel_id: (PortId, ChannelId), + _seq: Sequence, + ) -> Result<(), Error> { + unimplemented!() + } + + fn store_next_sequence_ack( + &mut self, + _port_channel_id: (PortId, ChannelId), + _seq: Sequence, + ) -> Result<(), Error> { + unimplemented!() + } + + fn increase_channel_counter(&mut self) { + unimplemented!() + } + + fn store_packet( + &mut self, + _key: (PortId, ChannelId, Sequence), + _packet: crate::core::ics04_channel::packet::Packet, + ) -> Result<(), Error> { + Ok(()) + } +} + +impl PortReader for DummyTransferModule { + fn lookup_module_by_port(&self, _port_id: &PortId) -> Result { + unimplemented!() + } +} + +impl BankKeeper for DummyTransferModule { + type AccountId = Signer; + + fn send_coins( + &mut self, + _from: &Self::AccountId, + _to: &Self::AccountId, + _amt: &PrefixedCoin, + ) -> Result<(), Ics20Error> { + Ok(()) + } + + fn mint_coins( + &mut self, + _account: &Self::AccountId, + _amt: &PrefixedCoin, + ) -> Result<(), Ics20Error> { + Ok(()) + } + + fn burn_coins( + &mut self, + _account: &Self::AccountId, + _amt: &PrefixedCoin, + ) -> Result<(), Ics20Error> { + Ok(()) + } +} + +impl Ics20Reader for DummyTransferModule { + type AccountId = Signer; + + fn get_port(&self) -> Result { + Ok(PortId::transfer()) + } + + fn is_send_enabled(&self) -> bool { + true + } + + fn is_receive_enabled(&self) -> bool { + true + } +} + +impl ConnectionReader for DummyTransferModule { + fn connection_end(&self, cid: &ConnectionId) -> Result { + match self.ibc_store.lock().unwrap().connections.get(cid) { + Some(connection_end) => Ok(connection_end.clone()), + None => Err(Ics03Error::connection_not_found(cid.clone())), + } + } + + fn host_oldest_height(&self) -> Height { + todo!() + } + + fn commitment_prefix(&self) -> crate::core::ics23_commitment::commitment::CommitmentPrefix { + todo!() + } + + fn connection_counter(&self) -> Result { + todo!() + } +} + +impl ClientReader for DummyTransferModule { + fn client_state(&self, client_id: &ClientId) -> Result { + match self.ibc_store.lock().unwrap().clients.get(client_id) { + Some(client_record) => client_record + .client_state + .clone() + .ok_or_else(|| Ics02Error::client_not_found(client_id.clone())), + None => Err(Ics02Error::client_not_found(client_id.clone())), + } + } + + fn host_height(&self) -> Height { + Height::zero() + } + + fn host_consensus_state(&self, _height: Height) -> Result { + unimplemented!() + } + + fn consensus_state( + &self, + client_id: &ClientId, + height: Height, + ) -> Result { + match self.ibc_store.lock().unwrap().clients.get(client_id) { + Some(client_record) => match client_record.consensus_states.get(&height) { + Some(consensus_state) => Ok(consensus_state.clone()), + None => Err(Ics02Error::consensus_state_not_found( + client_id.clone(), + height, + )), + }, + None => Err(Ics02Error::consensus_state_not_found( + client_id.clone(), + height, + )), + } + } + + fn client_type( + &self, + _client_id: &ClientId, + ) -> Result { + todo!() + } + + fn next_consensus_state( + &self, + _client_id: &ClientId, + _height: Height, + ) -> Result, Ics02Error> { + todo!() + } + + fn prev_consensus_state( + &self, + _client_id: &ClientId, + _height: Height, + ) -> Result, Ics02Error> { + todo!() + } + + fn host_timestamp(&self) -> Timestamp { + todo!() + } + + fn client_counter(&self) -> Result { + todo!() + } +} + +impl ChannelReader for DummyTransferModule { + fn channel_end(&self, pcid: &(PortId, ChannelId)) -> Result { + match self.ibc_store.lock().unwrap().channels.get(pcid) { + Some(channel_end) => Ok(channel_end.clone()), + None => Err(Error::channel_not_found(pcid.0.clone(), pcid.1)), + } + } + + fn connection_channels(&self, _cid: &ConnectionId) -> Result, Error> { + unimplemented!() + } + + fn get_next_sequence_send( + &self, + port_channel_id: &(PortId, ChannelId), + ) -> Result { + match self + .ibc_store + .lock() + .unwrap() + .next_sequence_send + .get(port_channel_id) + { + Some(sequence) => Ok(*sequence), + None => Err(Error::missing_next_send_seq(port_channel_id.clone())), + } + } + + fn get_next_sequence_recv( + &self, + _port_channel_id: &(PortId, ChannelId), + ) -> Result { + unimplemented!() + } + + fn get_next_sequence_ack( + &self, + _port_channel_id: &(PortId, ChannelId), + ) -> Result { + unimplemented!() + } + + fn get_packet_commitment( + &self, + _key: &(PortId, ChannelId, Sequence), + ) -> Result { + unimplemented!() + } + + fn get_packet_receipt(&self, _key: &(PortId, ChannelId, Sequence)) -> Result { + unimplemented!() + } + + fn get_packet_acknowledgement( + &self, + _key: &(PortId, ChannelId, Sequence), + ) -> Result { + unimplemented!() + } + + fn hash(&self, value: Vec) -> Vec { + use sha2::Digest; + + sha2::Sha256::digest(value).to_vec() + } + + fn client_update_time( + &self, + _client_id: &ClientId, + _height: Height, + ) -> Result { + unimplemented!() + } + + fn client_update_height( + &self, + _client_id: &ClientId, + _height: Height, + ) -> Result { + unimplemented!() + } + + fn channel_counter(&self) -> Result { + unimplemented!() + } + + fn max_expected_time_per_block(&self) -> Duration { + unimplemented!() + } +} + +impl Ics20Context for DummyTransferModule { + type AccountId = Signer; +} + +impl ReaderContext for DummyTransferModule {} diff --git a/proto-compiler/Cargo.lock b/proto-compiler/Cargo.lock index 759acf3513..3b69bae13d 100644 --- a/proto-compiler/Cargo.lock +++ b/proto-compiler/Cargo.lock @@ -350,7 +350,7 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "hyper" -version = "0.14.19" +version = "0.15.04" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42dc3c131584288d375f2d07f822b0cb012d8c6fb899a5b9fdb3cb7eb9b6004f" dependencies = [