Skip to content

Commit

Permalink
Fix padding on rlp value (#585)
Browse files Browse the repository at this point in the history
* Append leading zeros if the rlp value is not 32 bytes.

* Add empty test case.

* Return better error when item cannot be decoded.

* Simplify padding.

* Added more tests

---------

Co-authored-by: Marko Petrlic <[email protected]>
  • Loading branch information
0xSasaPrsic and markopoloparadox authored May 14, 2024
1 parent c28783f commit d18f352
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 36 deletions.
136 changes: 100 additions & 36 deletions pallets/vector/src/storage_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum MessageStatusEnum {
ExecutionSucceeded,
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum StorageError {
StorageValueError,
AccountNotFound,
Expand All @@ -32,21 +32,21 @@ pub fn get_storage_value(
let trie =
TrieDBBuilder::<EIP1186Layout<keccak256::KeccakHasher>>::new(&db, &storage_root).build();

if let Some(result) = trie
.get(&key)
.map_err(|_| StorageError::StorageValueError)?
{
let r = Rlp::new(result.as_slice())
.data()
.map_err(|_| StorageError::CannotDecodeItems)?;
if r.is_empty() {
return Err(StorageError::CannotDecodeItems);
}

Ok(H256::from_slice(r))
} else {
Err(StorageError::StorageValueError)
let Ok(Some(trie_value)) = trie.get(&key) else {
return Err(StorageError::StorageValueError);
};

let Ok(rlp_storage_value) = Rlp::new(trie_value.as_slice()).data() else {
return Err(StorageError::CannotDecodeItems);
};

if rlp_storage_value.is_empty() {
return Err(StorageError::CannotDecodeItems);
}

let storage_value = rlp_to_h256(rlp_storage_value)?;

Ok(storage_value)
}

/// get_storage_root returns storage root based on the provided proof.
Expand All @@ -60,45 +60,84 @@ pub fn get_storage_root(
let trie =
TrieDBBuilder::<EIP1186Layout<keccak256::KeccakHasher>>::new(&db, &state_root).build();

if let Some(result) = trie
.get(key.as_slice())
.map_err(|_| StorageError::StorageValueError)?
{
let byte_slice = result.as_slice();
let r = Rlp::new(byte_slice);
let Ok(Some(trie_value)) = trie.get(key.as_slice()) else {
return Err(StorageError::StorageValueError);
};

let item_count = r
.item_count()
.map_err(|_| StorageError::StorageValueError)?;
let r = Rlp::new(trie_value.as_slice());

if item_count != 4 {
return Err(StorageError::AccountNotFound);
}
let Ok(item_count) = r.item_count() else {
return Err(StorageError::StorageValueError);
};

let item = r
.at(2)
.map_err(|_| StorageError::StorageValueError)?
.data()
.map_err(|_| StorageError::StorageValueError)?;
if item_count != 4 {
return Err(StorageError::AccountNotFound);
}

let Ok(item) = r.at(2).and_then(|e| e.data()) else {
return Err(StorageError::StorageValueError);
};

let storage_root = rlp_to_h256(item)?;

Ok(storage_root)
}

let storage_root = H256::from_slice(item);
fn rlp_to_h256(value: &[u8]) -> Result<H256, StorageError> {
const H256_LENGTH: usize = 32;

Ok(storage_root)
} else {
Err(StorageError::StorageValueError)
if value.len() > H256_LENGTH {
return Err(StorageError::CannotDecodeItems);
}

// 0s are prepended if value.len() is less than 32.
let mut slot_value = [0u8; H256_LENGTH];
let offset = H256_LENGTH - value.len();
for (i, v) in value.iter().enumerate() {
slot_value[i + offset] = *v;
}

Ok(H256::from(slot_value))
}

#[cfg(test)]
mod test {
use super::*;
use ark_std::vec;
use avail_core::data_proof::{AddressedMessage, Message};
use frame_support::assert_err;

use hex_literal::hex;
use primitive_types::{H160, H256};
use sp_io::hashing::keccak_256;

#[test]
fn rlp_to_h256_fails_with_len_over_32() {
let faulty = [0u8; 33];
assert_err!(rlp_to_h256(&faulty), StorageError::CannotDecodeItems);
}

#[test]
fn rlp_to_h256_works_with_len_32() {
let rlp = [1u8; 32];
let expected = H256::from(rlp.clone());

let actual = rlp_to_h256(&rlp).unwrap();
assert_eq!(actual, expected);
}

#[test]
fn rlp_to_h256_works_with_len_under_32() {
let rlp: [u8; 31] = [1u8; 31];

let mut expected = [1u8; 32];
expected[0] = 0;
let expected = H256::from(expected);

let actual = rlp_to_h256(&rlp).unwrap();
assert_eq!(actual, expected);
}

#[test]
fn test_account_proof() {
let key = H160::from_slice(hex!("426bde66abd85741be832b824ea65a3aad70113e").as_slice());
Expand Down Expand Up @@ -194,4 +233,29 @@ mod test {
let encoded = m.abi_encode();
assert_eq!(expected_encoded_message, encoded);
}

#[test]
fn test_storage_with_padded_value() {
let expected_value = H256(hex!(
"00eee07ead3b0877b420f4f13c67d4449fa051db6a6b877de1265def8f1f3f99"
));

let trimmed_value = hex!("eee07ead3b0877b420f4f13c67d4449fa051db6a6b877de1265def8f1f3f99");
let padded_value_resutl = rlp_to_h256(trimmed_value.as_slice());
assert_eq!(expected_value, padded_value_resutl.unwrap());

let exact_value = hex!("00eee07ead3b0877b420f4f13c67d4449fa051db6a6b877de1265def8f1f3f99");
let padded_exact_value = rlp_to_h256(exact_value.as_slice());
assert_eq!(expected_value, padded_exact_value.unwrap());

let empty: &[u8] = &[];
let empty_padded = rlp_to_h256(empty);
assert_eq!(H256::zero(), empty_padded.unwrap());

let invalid_value =
hex!("0000eee07ead3b0877b420f4f13c67d4449fa051db6a6b877de1265def8f1f3f99");
let error = rlp_to_h256(invalid_value.as_slice());

assert_err!(error, StorageError::CannotDecodeItems);
}
}
91 changes: 91 additions & 0 deletions pallets/vector/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
RotateVerificationKey, SourceChainFrozen, StepVerificationKey, SyncCommitteePoseidons, Updater,
ValidProof, WhitelistedDomains,
};
use avail_core::data_proof::Message::FungibleToken;
use avail_core::data_proof::{tx_uid, AddressedMessage, Message};

use frame_support::{
Expand Down Expand Up @@ -75,6 +76,29 @@ fn get_valid_account_proof() -> ValidProof {
])
}

fn get_valid_trimmed_account_proof() -> ValidProof {
BoundedVec::truncate_from(vec![
BoundedVec::truncate_from(hex!("f90211a03f67cc4693a599249b84bfeff3a5d12e853bf772ae066a77221736d723559d1aa06d2b1cc23f415ff8ad546eee0b17411ad19b57701b85f96e27e6ba5724f9c69ca02e682d0fcb1bc2e30cb30db360871870f20766f38878970c52077474b4bb302ea0f1fd41727a86c80436ab4338b4a1f189f02591b7faf7487ee424c71e80bd77a7a0195c17c74a608f2848526a9ef4b81d1eab7f1d9906e74f706bb2d99cd557e9f9a0fc31be6f33b1f4655add8ca1f92fc6ac823db5b0e5d384545f639c199f9b1412a0bc16f1b8369608a15f4e425fb72d5d919b625016ea2765efd937b81b2fb95f7ca0300ab398a0037c592cf9a6d21c5d1bbe480b73f88c8b1c6f67796cf6af06d15ca0ce1ac3af168abe59b356c182c4823f869f26658f0e40d140ada95c804b066f70a0f6db7ed6b431338769f86f050543d6dfef570b52ab2ac677be95e789b9fa6cdfa036a2de51d9b52bcfe611a9b6e60e6fb66da89e7f199dcdb94ad3c8ff231d991aa042846d291fc4e1028d3c984ff8c0dc0602183b9ac72e8f1a7e47655acd4d5b8aa0b6875189e1e5fce8d06ecb5de139af5cddc9bf6cb945a165fc68b7311e2b692ea076ed2532d8a35f6a813e66585380f1368288e64512b2f970edd5905855493feaa06f26c65a9131dcd09e52ceceec1c91dfe04ec2e055ede6f6ceb24c1d297d46a4a0ce8e3c86b6d650cffb18b64325117513fb823c8872fbdcb3979435ba37cba25e80").to_vec()),
BoundedVec::truncate_from(hex!("f90211a0bd2e3689d300a30100a2329f8402e73dd245a1c669bb160aab470a284abbe982a0b25da6bb9e7831fbd9e48fbf4874de13785c60e60f7fd2425da0a89d0f790578a06d8564eb4a5870a4b99321423ef15918e64293489c05fe87780a547f723c7d16a0d6ab8cfdeb467341f0d1f2576f8e574cf6535bbc29b589e7676f6be1561820d6a006f5e3dc5ccb84eb9c7ab28b62118a33218829543f69f0ed3f88984a8cd3de82a0caa12557af64ae8c5b84a63d0f7300a33a7536c85ad39b87bd12ff0c7b4c1530a06cfcee7f8123c7914f4920bcd21d1fdde4a5f912b4d2c03224bec6e0f32af487a0ee943defe13e21b3ffcd1d8e70c324dd796df825edc27d27f342add5cfe0bf21a00825681a49714a705942453e89ac474023716f7e94b72ea8212f991d4e3ea8e2a0a793299ac09099832c69cbf8abeb8fd270fc3bd156063c49a39134d44905c047a08eaa7057951e3b83488d09f5fcaabcf4f3bb20974049d1a4a3531d0540ea5ab2a02edfe21f3ab3a99cf37dbc28d22ca51c4e047294f45cf444277ffba450638e18a0330a8cb44a696e8d530641831f5be9e7cefbdee0720c6cb7ecf65d5062a67f14a0c7a27629e9d3d0fdbcdad8098b318eec1b124c0bfce2e72d5d793359c5985ea8a0e5f686ba4b1a9505ae8b9585fe7859f25a03c771b3de08b73395acb3fa0737a2a0c2f391630cbed30ed21eb8f19dc978836795fbeb659f3cb33ca984986c65859980").to_vec()),
BoundedVec::truncate_from(hex!("f90211a0829b73c40aeb9b1818e1d57df6712a27b9b4f5d816a8ba8d52e42cdaa2fcacf4a03c0715f962f5276d48a5c4c95fd0a54d1d5d871da0d20f2fb762b6e979b419b7a073632e830540ce138a96622e236bfc58b342bedf5e921d672161268f5c83a59fa0b2ec8caef8c00274470c3e6bac0c45706a469cdf1ca10e004a9d1be102c50c15a0046d3775da9dc52e63b78e83d6dcf9277ef70102ac606ef687d3a30e45346129a096589fc3f3f0abbc11086f898cd53fc72c959672f165f709e74f97ab5dc4139ea088223af20347c73f74356d580408cd64ccc287154366eebef139af7c9d62cc88a0fd2827ebaa6d88887250f9cbc5a1ae99fe48f51b91018d21566a6854b9d30ac1a05a3b49162aff1d7c980dbd11346252b25960d08be8c22735d0c8ef87c86b5c69a0b04137dd339bbb8ab46a460265b151e05df96712f6136520d444795526ac4b19a03cf079da86b918c6dec36865ef5ab76ae35d72eaaa08554c28a2ae49cd202795a004af2ee7dc2d6c6c88856c5fedc0faa350c8288a534e042e3f8fd36ac398157ba0df1eeb3bdf8aac42e0c4d49ea9b3b510346e46163905363095880fd73726aa2aa0d860a0fd0adb23e1cdeac0cc47d6294b5c7b7d6c1de655b5ea539c8754086f4ba015a8f23891525fa3306e310f5657c149f98eb8328842bdcd583a764376fcba42a00547529cef7d460c500e265f08746ed224c6726ddc8d70774eebd5b5f5d05a5880").to_vec()),
BoundedVec::truncate_from(hex!("f90211a0ff81c45f032f6270c46b2a2f392732bf4aa912d39e2780eeae4a7e7750e1efb5a08853f8503dd089ef324fa24d7177ecb430eb7db5ec27282eed0b264227e6265da09729f356f3fff7282a81928e6b09647b0e23f918f7cd01536d63dbc72aa11de2a0c26dca08e22987f703f384be3b896c269399479729dc49ccaa7bb2982951c3a4a060000c0147c14224541082b7ca67933e78c4778a291b248519f2f2f3e5b05b3ca03e2bd8e111d7396e9aef5dd91c12644fbebc5a99fe70e7b304d592875c2bf11da08fae631f08b84bb3fdcf8bdac2746f832bb0128fa790e73ca33aa64030bd4f24a01d839e77cba33e769bb66a535a48789267b50bd4f6bd0298941c26079c8d944ca0d89366cf55e4971e308a048cd2db663c8e13ba47c352c8b3e97015e0c232154ca06c58e7b529df2fb48df4dce3ef05758c3ba08bdf9f1f3c0e4dc46711c32a0bb8a0692107b01385afeaa5fd411a5f6e5d32ecffaef38bd8b2dd074a1877bd8fcab6a0d0dc3314dcb9670af94030a20de7e8e85882d2db439afe8d2cb1b144cabb353ba019a4d351e59fbd39222bed12d17384b0c3c9714b83533fe4d67a1a49f7540857a0c6c465750481dffa71d31b8511cdc58398267fd7113bf9e357e4a467971f3a1aa0ccbdfab5a0eeb421fa54b9a52afbad7acbd410eb64476e2d3336801dbedf759ba021b46858e99b0a1742f85b6a7c98d60079f60d6b416f77e3375fab51abf2bf5780").to_vec()),
BoundedVec::truncate_from(hex!("f90211a0072c336b149a863d4660864b22c148294be18b2d02ec70c3bb8cc5c796baf2eea01426486970928305b02b630c2dd2dda9aae5da8695d8687ee8d5e852dd8ca32ca07ad83ef3d645503b9afcf48de32fb677beef8b637aa120e6624acea518eb5b3fa0233f6fd48deb7f3ad124f6ecb24a07191085d0de94b0a86b78d4fc5557c87acca073c15c299a6e94c6295b71105d0ffe0f1f5a6df434cdafb9d408123ef4240fafa0a55aa4ce263fd8b1f321efcfd5d2cc39dec241b476ff92dbb7711282c2005d30a068ed51b36d5643ca995f36234bad2e623183e65c64d6c57677a6d94c4b3aabe9a029a319fc16340bb5889952869c0184a1fc1d0427aa488dd597909671c9b41b6ca02c96d570cb2c6069fe6326009edd6ef43384946bbf1e1db53bcb3a7ea13a2438a0f444d13d4b89d3e7107f37adf304b3a7bc9efb750c2f7fb881996b1f2b18912da0bc7654e778f69cc2eb09823b102f8176f2f093df6e8d533ef416b9c462faf196a066c8c7b398b35e45e6ac3b17055f064f6f39233bdf89a169ea349d2d3495bad9a03c0165cff666b0a9bf88e1faf4382096166d12fb90f641061288121c612ff442a024cddecf9aa07dfae93a6030b953f4a5e0406f3714d801762ee55c4b6132d35aa065dcf4be29a72d2fd844e5e9ed101b74f456e1782f9432fe1aa508f47de4f49ea0bcc85c28e9f209b9b419e66f87bce37fbb42ba8416880935275c377d2fcebcc180").to_vec()),
BoundedVec::truncate_from(hex!("f90191a0deb916373640a76bb6056aa37e9d548908c92fb5d3ea1fc69a1c99ddeb40eb24a029b4cdce0d4f7eed71cf6f6d21f4d27ef510b3e36c7c67587e84a08b8b288de380a0cd92c24f26cec2802437f2f8e56cdd35e47ed3edc4c806540740f5bf83f1c5ffa0aadf083c80930dee9f09d295708b24f31d2f41f50e1442e32ecbf03eb5b4a707a0f5335f3280d3be255686e95e46a353a1562a8008919a0051a46fb3fcce7c53caa0539a7d628c0e0295ffe5dc4bc8a737d797e03726f4fc4088a5c34f477e43c576a0d000e75b89a65aedb850451d369e3ac5bf4d7ff0d71ec5e837417c0eaf074135a0a994cad72a8a641c62979d6f94ea0087a14770876869de76ff0aba4c51b7cfc480a00a3095615f351a63f22e4b2e3c79f36ae423d091524b6603a3960d9a5e050f62a06e2c53738e77dd7b3c06aada2fcd6035a8435f8edbd40e0e9856439024502a2fa0dfd0cb17d8ab569541cb008b992e89031eb8bb903d1abeef7fec629ac2a7602780a0ca1bd867039f590f970b921ef487f051a6bb54e1d480dd9529899d02de4a46a08080").to_vec()),
BoundedVec::truncate_from(hex!("f87180808080a038a7a4270f22ff1603ceaeeb23b33d88250139e9bd96e0bc5cd44b575f3cd256a07564257a73fdfa4e290ad21fe914294174996ac87095b5730370371f5ea133e980808080a0d51dbe737bf6d8c5b89bcf7724074067d2a4986c4180a5016a8bebfbc92f56e1808080808080").to_vec()),
BoundedVec::truncate_from(hex!("f8669d38b7b6c4749ee47ec3483ea3325831ccd2fbcbbbcf7cb559ed13d35a14b846f8440280a0aa7680864299b147a9a6d669c27e061f3e95c09fa2e26398d644ba66f9b976e9a0fc50d62823735da871a4b45630e8f4a5aef99c18855869762b05d4f7fac4a859").to_vec()),
])
}

fn get_valid_trimmed_storage_proof() -> ValidProof {
BoundedVec::truncate_from(vec![
BoundedVec::truncate_from(hex!("f90211a00b32abb8354c7486f39ed2ac1152624ef001bffb0ce0eb7c31ae063df789f994a080bb0ddb0ad0557859ba0c2ef1e2d67e4461b379043ec91ecebbefab53d569b3a06734318e802e53ba9f909ab03ede43bbf05c0aa1dee2affea86e533c49aaefdba072a71576a3efe0a39495f056c24ed4f0bcd11355d850e737887a456d7fb0bca9a0c379788ccd4ceb4a915d30c03258c61725cf57132f00c4653db0214a39cd04d0a01d5aa540d5728ae4900a82aca7f240ce98538d196cd2dc92a70c3f7dfee8c5aaa07e2e97a62072a02c650aec15bc75f36a8dde008e1f6bd2592129ba6c41493f43a091fbeea8e97fd76afdea2d1a92b7f3140b5165923403b9917f527e74359dcabea08dbd10c6ed681079481c92ac3687049653d391c8269c25d83538081fa8a8bc76a05e0f867c10c0fe18ed170d68546b82a1171326368e7b7405a20b252b15498e1ea056b131482acba838f00027ca6ea75079d1b1485894a19ec4512ba6b1887e8148a0fb123da682ae4a2214681e7793801c3e5167591d97b072e851f5f9112c79f6bfa0a15859022edd732924fc5192e0fd93c758f463e97d0f765a0b3ff3c0cd5d3a60a0cbf4df8376c85940b02f98c851db0182b25302f5658e43871e9c6511f3e88a25a020f58bc2c7cafb6460830aaaa5c41a9567ede0b636a20fa04593004fe43ecf7fa0059c2b1df5bcf7ae85f61f51182275c3b6bff4fe482c714e7c3f50516a933b3780").to_vec()),
BoundedVec::truncate_from(hex!("f90211a09900b767b77271c9af3fc54de094d91dc7b08650aa756ada2800ab79148eefbfa008fb501eeef4ce577c036c664f9d9275309d155c8aa7e96572819f389405ce5da05a9206d832ec4df61192e2ff2c5eb9efebe77e29a54a3dd4bd7fe6212110c18fa0e318d4044bde6cb11dd358ad9dbee4d763ec5ca946192ba924049bf15b23329ea06b60d1fbd395bcccdcb6f22f0fd19e6233043710743a1bf030fcc5921d10ac12a04073234ea2baa8e1eda5ee3abefb70d575786de6565267d90c659e0917010e19a0ebfe792b17dcdcbc456259f76d9af665eb34ef5fa464f51959886136543ea8d6a055ca87cc8ffa31c826b991fc30419bcd1067194eab1b1fb2023688656006782fa07db930f6a48aa842be5608d2dd099b7fed997058763b33e6419062e65cf92353a07be0780df1edede41c7795e9747d20173e12c3c4c06521edf6b4354a871ac0f5a01fb962cc28068fa0cb59e4ee2912e7890e301efadd34d389c80404f80c87c486a0597f19d7d15226fa7410817e8be23adc3cadfa01550f28b62b094d7fda48a7fba06a4b5abfae8b92f163863f644cc3f0c1cc2f75cbbe31ac80c7f8ad71f277b8f5a05d1213906174d263996d60b4d136e3d24387226166abcca5798f46c8bd1a2214a08258fd3c37c7ef5649c3f1591540aca89dfd39345a17d98775da9208e8d8a19ca0000fb606926d0ece17aa61bea6e4c7e09c14d85564cc0de1c9ae98d41d6985e780").to_vec()),
BoundedVec::truncate_from(hex!("f90211a0a69b09b1d904eae2844423e60a7728a496cf60270d2ff529f0abc2cbe4075841a0ef9af64741ecf8128fd1c05b534fa0bb426166fc4e9d100984737e4ef57e9cd7a0c45c73ebdb8cbe7b7e77444ccce5c00cf929bc8df356e256827236a220ae29cca0c82d4ab55c79e2d8d2574a8908b524e792f34983e16af94890efe2150da5c53da0cebc2cd4801f683f47aac87df85315adf0d02ef6a19cdf5d745c22272e301f6fa06b1a741e95c3de95cdaf0222fe25363cdef6c598843baab805bcc4f9cc64b586a0974986a98df15aafb10a24cc3c6a5ba74670e654248b7b810b98e8133a9e4701a0b6f0487e9876f634ff7552fb5f8cce474e7c04d8891ff0c68ff5f24f91f1bfdba0d68ac2c2d956a69c6f1f9cf9fb6f495a0cff75350b3cca2dad9abf9d96687703a035a8bc714ff33c5e42d616cc21b52bb502ef9d60a13921b956222e05ccd94f5fa06cc749e55d9f9f2ec594f772a7d0edca8b0f57665f8cc2f46a555287c9f1eb83a02556521a54a5461d46304ef99e99f930d0b7f56dec88e92eb0a2adb55de521d1a0a0ed415c1982bc87cd8a5feab63ddb4b571d045df9ea34d0812e7251ed03e010a02d63e8a665b4b94817d9d4d5dd1c775b7efee4ddf844a2d566a433d1097debf9a095f5e1fc74683bf4fad753379429f79e254cf5b003b27291caa3636bd7426ccea0946e1f6fa2dbca1c556607be864e1de46ba306603c9db678efa8e62dab0e972a80").to_vec()),
BoundedVec::truncate_from(hex!("f8b180a0b0c8dee83045cb27e112f3c82c39ee3a068b5032538250bd1d073420324b582280a02d15211aaa6878b99a22d16da29fbf2809b4eba2201f82bc3761dbc9b4a21bd9808080a0f10a1a9b00eb477bf41ffd20f38fb221b79ffdfb8ec849317a2cbd95970ef194a06f9cb181aabf086945247d8766a1803188f78339dcb6a94130cdd198370a31448080808080a00c14e855667d3df310ab2c2b80a309bb10959b6fb7d9185afd1ed155e7bb0eae8080").to_vec()),
BoundedVec::truncate_from(hex!("f8419f20842d8360009d31c89c8f6f564e011d20cfd5d16d9caff797600c3ae19913a09feee07ead3b0877b420f4f13c67d4449fa051db6a6b877de1265def8f1f3f99").to_vec()),
])
}

fn get_invalid_account_proof() -> ValidProof {
BoundedVec::truncate_from(vec![
BoundedVec::truncate_from(hex!("f90211a050da92c339db0b71cd6a8ac7893a6b8689ec5a3a46a0231b3ee2bd1baee75e1da045a3d973eb74a02b762d8b1ba683f39bca3965806276c8ceffe2d2ebc6cce233a0e88ad29ca98fa08f59f2a7f0110d63505d99a173628643290df869c4d1fa312ba00bb4cc9dc0b1de6ae0d80424b1fa992efb400a07a0e84615c91762fe734b2d0ca0a07e495d39bf2b779405790c6c7e7eb1cc3c803a88db36d1ec600fb0e555b5bba09a1c776e89c8be75d0a9ea022c05fd2ff095869d549e74a8fff7f2fb2deaf738a073b874e49e77dfd9312d7b1afd1ac10e02021a1ba2ab7c97ecaeaa0e26a34027a07e3424405c13aa33a2eb9ec6d8640aa1f67fdd8c8e9e4276334515b1cf1df65ca0246b93b2e3cc625a5e75b40165c6cb95ae8ffb9406563d34092d6359c7616aeea04d2fd8fdb1ab7d8f8fc6079400396fec828914230fade3794f13dc5ae7f6bbb8a04811b9efbfa8d495c5be91be78372b4a29140bd1e092e793db50ed9c495a6d54a02e1b3a417e8341dc8e1ade6ca527778192d33c7c827cfa63a366d007f2884e24a0845f4f33a4993d85766a14222cde1d124bd0f15523d239572883258a7bbcccd9a0ed2021cc2206fcfd9f80d592890b1b4eb615fae4f11d4e4a66d54a6767908901a07d46bf6e9dc9599eb7ca036aa976ef9cc63f02e9097252799f5d3a8792c49620a00b58d1d2cc72401c7cb978d34e15f74038ac63355e415d53b894179b8938dbb780").to_vec()),
Expand Down Expand Up @@ -311,6 +335,73 @@ fn test_execute_fungible_token_via_storage() {
});
}

#[test]
fn test_execute_fungible_token_via_storage_with_trimmed_storage_value() {
new_test_ext().execute_with(|| {
let balance_before = Balances::balance(&Bridge::account_id());
Broadcasters::<Test>::set(
2,
H256(hex!(
"1369a4c9391cf90d393b40faead521b0f7019dc5000000000000000000000000"
)),
);

let slot = 4965568;
ExecutionStateRoots::<Test>::set(
slot,
H256(hex!(
"5e3fe0dd03c7ce3f89524cfa65545232bbf52645b52ac0a3939f766540a6ed69"
)),
);

let account_proof = get_valid_trimmed_account_proof();
let storage_proof = get_valid_trimmed_storage_proof();

let message = AddressedMessage {
message: FungibleToken {
asset_id: H256::zero(),
amount: 10_000_000_000_000_000,
},
from: H256(hex!(
"8d31529525f23b14767d4dde78567ca083d3d56f000000000000000000000000"
)),
to: H256(hex!(
"1a985fdff5f6eee4afce1dc0f367ab925cdca57e7e8585329830fc3ce6ef4e7a"
)),
origin_domain: 2,
destination_domain: 1,
id: 5469,
};

let message_encoded = message.clone().abi_encode();
let message_root = H256(keccak_256(message_encoded.as_slice()));

// amount in message 1000000000000000000
let result = Bridge::execute(
RuntimeOrigin::signed(TEST_SENDER_ACCOUNT),
slot,
message,
account_proof,
storage_proof,
);

let expected_message_root: H256 = H256(hex!(
"00eee07ead3b0877b420f4f13c67d4449fa051db6a6b877de1265def8f1f3f99"
));
let balance_left = Balances::balance(&Bridge::account_id());
assert_ok!(result);
assert_eq!(
balance_before.saturating_sub(10_000_000_000_000_000u128),
balance_left
);
assert_eq!(expected_message_root, message_root);
assert_eq!(
MessageStatus::<Test>::get(message_root),
MessageStatusEnum::ExecutionSucceeded
);
});
}

#[test]
fn test_execute_message_with_frozen_chain() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit d18f352

Please sign in to comment.