Skip to content

Commit

Permalink
Merge pull request #17 from datachainlab/improve-error-messages
Browse files Browse the repository at this point in the history
Improve error messages

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Jun 26, 2024
2 parents 53f91b0 + 8c0b126 commit bc530c9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
2 changes: 2 additions & 0 deletions crates/ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ 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 }
ethereum-light-client-verifier = { git = "https://github.com/datachainlab/ethereum-light-client-rs", rev = "186c5b1d9935f15428ca8b209ad99ed20eaf81c6", default-features = false }

[dev-dependencies]
time = { version = "0.3", default-features = false, features = ["macros", "parsing"] }
hex-literal = "0.4.1"
63 changes: 57 additions & 6 deletions crates/ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,25 @@ impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize
address: &Address,
account_update: &AccountUpdateInfo,
) -> 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)
Expand All @@ -136,10 +150,25 @@ impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize
Err(Error::AccountStorageRootMismatch(
account_update.account_storage_root,
storage_root,
state_root,
hex::encode(address.0),
account_update
.account_proof
.iter()
.map(hex::encode)
.collect(),
))
}
}
None => 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(),
)),
}
}

Expand Down Expand Up @@ -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() {
{
Expand Down
23 changes: 12 additions & 11 deletions crates/ibc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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<String>,
),
/// consensus state doesn't have next sync committee
NoNextSyncCommitteeInConsensusState,
/// invalid current sync committee keys: expected={0:?} actual={1:?}
Expand All @@ -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<String>),
/// account storage root mismatch: expected={0} actual={1} state_root={2} address={3} account_proof={4:?}
AccountStorageRootMismatch(H256, H256, H256, String, Vec<String>),
/// invalid account storage root: {0:?}
InvalidAccountStorageRoot(Vec<u8>),
/// future period error: store={0} update={1}
Expand Down Expand Up @@ -122,12 +129,6 @@ impl From<Error> for ContextError {
}
}

impl From<ethereum_light_client_verifier::errors::Error> for Error {
fn from(value: ethereum_light_client_verifier::errors::Error) -> Self {
Error::VerificationError(value)
}
}

impl From<rlp::DecoderError> for Error {
fn from(value: rlp::DecoderError) -> Self {
Error::RLPDecodeError(value)
Expand Down

0 comments on commit bc530c9

Please sign in to comment.