From 8c0b126898e793243d34491a9389913f185919a2 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Wed, 26 Jun 2024 13:16:59 +0900 Subject: [PATCH] improve error messages Signed-off-by: Jun Kimura --- crates/ibc/Cargo.toml | 2 ++ crates/ibc/src/client_state.rs | 63 ++++++++++++++++++++++++++++++---- crates/ibc/src/errors.rs | 23 +++++++------ 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/crates/ibc/Cargo.toml b/crates/ibc/Cargo.toml index 64bea25..a1dfaf3 100644 --- a/crates/ibc/Cargo.toml +++ b/crates/ibc/Cargo.toml @@ -13,6 +13,7 @@ bytes = { version = "1.2.1", default-features = false } rlp = { version = "0.5.2", default-features = false } tiny-keccak = { version = "2.0.2", default-features = false } ssz-rs = { git = "https://github.com/bluele/ssz_rs", branch = "serde-no-std", default-features = false, features = ["serde"] } +hex = { version = "0.4.3", default-features = false } ethereum-ibc-proto = { path = "../../proto", default-features = false } ethereum-consensus = { git = "https://github.com/datachainlab/ethereum-light-client-rs", rev = "186c5b1d9935f15428ca8b209ad99ed20eaf81c6", default-features = false } @@ -20,3 +21,4 @@ ethereum-light-client-verifier = { git = "https://github.com/datachainlab/ethere [dev-dependencies] time = { version = "0.3", default-features = false, features = ["macros", "parsing"] } +hex-literal = "0.4.1" diff --git a/crates/ibc/src/client_state.rs b/crates/ibc/src/client_state.rs index 812b96e..daaf3af 100644 --- a/crates/ibc/src/client_state.rs +++ b/crates/ibc/src/client_state.rs @@ -117,11 +117,25 @@ impl Result<(), Error> { - match self.execution_verifier.verify( - state_root, - address.0.as_slice(), - account_update.account_proof.clone(), - )? { + match self + .execution_verifier + .verify( + state_root, + address.0.as_slice(), + account_update.account_proof.clone(), + ) + .map_err(|e| { + Error::MPTVerificationError( + e, + state_root, + hex::encode(address.0), + account_update + .account_proof + .iter() + .map(hex::encode) + .collect(), + ) + })? { Some(account) => { let storage_root = H256( Rlp::new(&account) @@ -136,10 +150,25 @@ impl Err(Error::AccountNotFound(state_root, address.clone())), + None => Err(Error::AccountNotFound( + state_root, + hex::encode(address.0), + account_update + .account_proof + .iter() + .map(hex::encode) + .collect(), + )), } } @@ -879,8 +908,30 @@ fn trim_left_zero(value: &[u8]) -> &[u8] { #[cfg(test)] mod tests { use super::*; + use hex_literal::hex; use time::{macros::datetime, OffsetDateTime}; + #[test] + fn test_verify_account_storage() { + let client_state = ClientState::<0, 0>::default(); + let account_proof = decode_eip1184_rlp_proof( + hex!("f901fff90191a05844e303fa8db3fa31c729db25d9b593367f853b4cbcb1a91fc85eda11e16617a09bb111cd80eee4c6ae6af0d01422ae82fccfa80d0267c4c8d525bc7f2b6233afa0323230228b1ba9b7eb88084b6d1ed9b75813a2da2d5ff0df9067335f5f55444ca0bfca1461a76f96944aa00afff03dc8de770275fbbe360f6ee03b0fe0ce902fd8a04c7579812e09de2b1aa746b0a047d357e898e9d634ac185d7e9d25b3d2336ab3808080a0c7de43d788c5228ebde29b62cb0f9b9eb10c0cb9b1078d6a51f768e0cdf296d6a0b8ad2523a3d1fdf33b627f598622775508297710e3623de115f2174c7f5727dfa023910890abfb016861bb7916cb555add80e552f118c0f1b93ec7d26798976f1da077153f3a45bebfb8b6709bd52e71d0993e9ecfd4e425204e258e5e5ac775ee73a01b42efb18b5af3defc59ba21f68965c5a28c716e109df937d216a2041fee4770a06b4b8f8ad0ae7588581c191bf177d5020fcc0f9152123cd26b3acf4e3469744280a0b4ec201ec80c64cefbe351f2febea48eb21c0d65d3e1c868178ece65e3d63ff480f869a0346090ccaa6fa9fa12360268a84aaba21af051a53bfdc84493350c840f61b79eb846f8440180a0d70e9391a3dd508a60195d2a5e12fb2f7e49582f9ce2c12477299377ccfadaada073092abb9be4a3fa206fd43699af07ff9d4278c27693f013fceb7780f3654c09").to_vec() + ).unwrap(); + let res = client_state.verify_account_storage( + H256(hex!( + "48b7747ba1094684d9197bbaa5dcb134587d23e493fb53a29e400c50e50f5147" + )), + &Address(hex!("ff77D90D6aA12db33d3Ba50A34fB25401f6e4c4F")), + &AccountUpdateInfo { + account_proof, + account_storage_root: H256(hex!( + "d70e9391a3dd508a60195d2a5e12fb2f7e49582f9ce2c12477299377ccfadaad" + )), + }, + ); + assert!(res.is_ok(), "{:?}", res); + } + #[test] fn test_trusting_period_validation() { { diff --git a/crates/ibc/src/errors.rs b/crates/ibc/src/errors.rs index 10c9af9..60fe567 100644 --- a/crates/ibc/src/errors.rs +++ b/crates/ibc/src/errors.rs @@ -5,7 +5,7 @@ use ethereum_consensus::{ beacon::{BeaconBlockHeader, Slot}, bls::PublicKey, sync_protocol::SyncCommitteePeriod, - types::{Address, H256, U64}, + types::{H256, U64}, }; use ibc::{ core::{ics02_client::error::ClientError, ics24_host::error::ValidationError, ContextError}, @@ -19,6 +19,13 @@ pub enum Error { InvalidRawConsensusState { reason: String }, /// verification error: {0} VerificationError(ethereum_light_client_verifier::errors::Error), + /// mpt verification error: {0} state_root={1} address={2} account_proof={3:?} + MPTVerificationError( + ethereum_light_client_verifier::errors::Error, + H256, + String, + Vec, + ), /// consensus state doesn't have next sync committee NoNextSyncCommitteeInConsensusState, /// invalid current sync committee keys: expected={0:?} actual={1:?} @@ -29,10 +36,10 @@ pub enum Error { InvalidProofFormatError(String), /// rlp decode error: {0} RLPDecodeError(rlp::DecoderError), - /// account not found: state_root={0:?} address={1:?} - AccountNotFound(H256, Address), - /// account storage root mismatch: expected={0:?} actual={1:?} - AccountStorageRootMismatch(H256, H256), + /// account not found: state_root={0} address={1} account_proof={2:?} + AccountNotFound(H256, String, Vec), + /// account storage root mismatch: expected={0} actual={1} state_root={2} address={3} account_proof={4:?} + AccountStorageRootMismatch(H256, H256, H256, String, Vec), /// invalid account storage root: {0:?} InvalidAccountStorageRoot(Vec), /// future period error: store={0} update={1} @@ -122,12 +129,6 @@ impl From for ContextError { } } -impl From for Error { - fn from(value: ethereum_light_client_verifier::errors::Error) -> Self { - Error::VerificationError(value) - } -} - impl From for Error { fn from(value: rlp::DecoderError) -> Self { Error::RLPDecodeError(value)