Skip to content

Commit

Permalink
Optimize gas consumption by reducing one MmrLeaf encode and
Browse files Browse the repository at this point in the history
update some test data
  • Loading branch information
en committed Nov 20, 2021
1 parent 1a5fb85 commit e35d079
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 70 deletions.
54 changes: 31 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,37 @@ impl LightClient {
}
}

let signatures_count =
signed_commitment.signatures.iter().filter(|&sig| sig.is_some()).count();
if signatures_count < (self.validator_set.len / 2) as usize {
return Err(Error::InvalidNumberOfSignatures {
expected: (self.validator_set.len / 2) as usize,
got: signatures_count,
});
}

let commitment = self.verify_commitment(signed_commitment, validator_proofs)?;

let mmr_proof = mmr::MmrLeafProof::decode(&mut &mmr_proof[..])
.map_err(|_| Error::CantDecodeMmrProof)?;
let mmr_leaf: Vec<u8> =
Decode::decode(&mut &mmr_leaf[..]).map_err(|_| Error::CantDecodeMmrLeaf)?;
let mmr_leaf_hash = Keccak256::hash(&mmr_leaf[..]);
let mmr_leaf: MmrLeaf =
Decode::decode(&mut &*mmr_leaf).map_err(|_| Error::CantDecodeMmrLeaf)?;
let mmr_leaf_hash = mmr_leaf.hash();

let mmr_proof = mmr::MmrLeafProof::decode(&mut &mmr_proof[..])
.map_err(|_| Error::CantDecodeMmrProof)?;

let commitment = self.verify_commitment(signed_commitment, validator_proofs)?;
// update the latest commitment, including mmr_root
self.latest_commitment = Some(commitment);

let result = mmr::verify_leaf_proof(commitment.payload, mmr_leaf_hash, mmr_proof)?;
if !result {
return Err(Error::InvalidMmrLeafProof);
}

// update the latest commitment, including mmr_root
self.latest_commitment = Some(commitment);

// update validator_set
if mmr_leaf.beefy_next_authority_set.id > self.validator_set.id {
self.validator_set = mmr_leaf.beefy_next_authority_set;
}

Ok(())
}

Expand All @@ -166,22 +175,22 @@ impl LightClient {
mmr_leaf: &[u8],
mmr_proof: &[u8],
) -> Result<(), Error> {
let mmr_root = self.latest_commitment.ok_or(Error::MissingLatestCommitment)?.payload;
let header = Header::decode(&mut &header[..]).map_err(|_| Error::CantDecodeHeader)?;
let header_digest = header.get_other().ok_or(Error::DigestNotFound)?;

let messages_hash = Keccak256::hash(messages);
if messages_hash != &header_digest[..] {
return Err(Error::DigestNotMatch);
}

let mmr_root = self.latest_commitment.ok_or(Error::MissingLatestCommitment)?.payload;
let mmr_proof = mmr::MmrLeafProof::decode(&mut &mmr_proof[..])
.map_err(|_| Error::CantDecodeMmrProof)?;
let mmr_leaf: Vec<u8> =
Decode::decode(&mut &mmr_leaf[..]).map_err(|_| Error::CantDecodeMmrLeaf)?;
let mmr_leaf_hash = Keccak256::hash(&mmr_leaf[..]);
let mmr_leaf: MmrLeaf =
Decode::decode(&mut &*mmr_leaf).map_err(|_| Error::CantDecodeMmrLeaf)?;
let mmr_leaf_hash = mmr_leaf.hash();
let mmr_proof = mmr::MmrLeafProof::decode(&mut &mmr_proof[..])
.map_err(|_| Error::CantDecodeMmrProof)?;

let header_digest = header.get_other().ok_or(Error::DigestNotFound)?;

let messages_hash = Keccak256::hash(messages);
// if messages_hash != &header_digest[..] {
// return Err(Error::DigestNotMatch);
// }

let header_hash = header.hash();
if header_hash != mmr_leaf.parent_number_and_hash.1 {
Expand All @@ -205,7 +214,6 @@ impl LightClient {
validator_proofs: Vec<MerkleProof<Vec<u8>>>,
) -> Result<Commitment, Error> {
let SignedCommitment { commitment, signatures } = signed_commitment;
// TODO: check length
let commitment_hash = commitment.hash();
let msg = libsecp256k1::Message::parse_slice(&commitment_hash[..])
.or(Err(Error::InvalidMessage))?;
Expand Down Expand Up @@ -387,9 +395,9 @@ mod tests {
let root_hash = hex!("aa0b510cee4270257f6362a353262253de422f069826b5af4398377a4eee03f7");
let leaf = hex!("c5010058000000e5ac4bf69913974aeb79779c77d6e22d40575a63d4bca9044b501b12916a6090010000000000000005000000304803fa5a91d9852caafe04b4b867a4ed27a07a5bee3d1507b4b187a68777a20000000000000000000000000000000000000000000000000000000000000000");
let leaf: Vec<u8> = Decode::decode(&mut &leaf[..]).unwrap();
let mmr_leaf = MmrLeaf::decode(&mut &leaf[..]).unwrap();
let mmr_leaf_hash = Keccak256::hash(&leaf[..]);
let proof = hex!("580000000000000059000000000000000c638bedc14bfdb5cfb8eb7313f311859820948868afbaa340de2a467f4eec130cd789e49d14c7068ec08e0b5680c5e01b372d28802acaeba7b63a5e1482d5147c0e395b48e5a134164c4dac0b30fc8bfd56756329824e6c70c7325769c92c1ff8");
let mmr_proof = MmrLeafProof::decode(&mut &proof[..]).unwrap();
assert_eq!(mmr::verify_leaf_proof(root_hash, mmr_leaf.hash(), mmr_proof), Ok(true));
assert_eq!(mmr::verify_leaf_proof(root_hash, mmr_leaf_hash, mmr_proof), Ok(true));
}
}
6 changes: 0 additions & 6 deletions src/mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ pub struct MmrLeaf {
pub parachain_heads: Hash,
}

impl MmrLeaf {
pub fn hash(&self) -> Hash {
Keccak256::hash(&self.encode())
}
}

/// A MMR proof data for one of the leaves.
#[derive(Debug, Clone, Default, Encode, Decode)]
pub struct MmrLeafProof {
Expand Down
75 changes: 34 additions & 41 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,67 +215,60 @@ fn verify_solochain_messages_works() {

let mut lc = new(public_keys);
let commitment = Commitment {
payload: hex!("7256c3f5a07409714e446d24fb7ea72a3f8e9f6503777aa47823fc6bad40cd96"),
payload: hex!("7fe1460305e05d0937df34aa47a251811b0f83032fd153a64ebb8812cb252ee2"),
block_number: 89,
validator_set_id: 0,
};
lc.latest_commitment = Some(commitment);
println!("light client: {:?}", lc);

// Got cross-chain messages at block 81 with commit hash 0x812570086a0f983665ea15b992352c338e89dcb83454e670ee5108e3142f302b
let messages = vec![4, 1, 0, 0, 0, 0, 0, 0, 0, 2, 16, 0, 0, 0, 0];
// Got cross-chain messages at block 81 with hash 0x63e59bf81115597c4fbd864998d610cda6bbe32c6e1319488f3eaa3c3ba5966e
let messages = hex!("040100000000000000021000000000");
let encoded_header = vec![
188, 249, 19, 41, 129, 143, 64, 158, 14, 184, 194, 196, 222, 154, 109, 232, 133, 70, 155,
213, 59, 71, 201, 74, 47, 194, 63, 178, 118, 39, 85, 237, 69, 1, 50, 209, 105, 73, 130, 76,
114, 241, 117, 30, 203, 118, 48, 18, 30, 229, 151, 145, 247, 205, 19, 208, 173, 60, 106,
208, 80, 95, 194, 91, 78, 126, 112, 130, 79, 102, 95, 70, 212, 160, 134, 113, 57, 119, 54,
84, 218, 108, 167, 9, 21, 41, 127, 84, 241, 43, 18, 142, 140, 213, 176, 48, 178, 3, 20, 6,
66, 65, 66, 69, 52, 2, 4, 0, 0, 0, 249, 44, 136, 32, 0, 0, 0, 0, 4, 66, 69, 69, 70, 132, 3,
175, 227, 250, 51, 109, 132, 249, 145, 216, 101, 22, 66, 71, 205, 70, 163, 135, 15, 63,
119, 240, 183, 32, 90, 106, 150, 151, 247, 76, 190, 171, 94, 4, 66, 65, 66, 69, 169, 3, 1,
20, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133,
88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 1, 0, 0, 0, 0, 0, 0, 0, 142,
175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201,
18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72, 1, 0, 0, 0, 0, 0, 0, 0, 144, 181, 171,
32, 92, 105, 116, 201, 234, 132, 27, 230, 136, 134, 70, 51, 220, 156, 168, 163, 87, 132,
62, 234, 207, 35, 20, 100, 153, 101, 254, 34, 1, 0, 0, 0, 0, 0, 0, 0, 48, 103, 33, 33, 29,
84, 4, 189, 157, 168, 142, 2, 4, 54, 10, 26, 154, 184, 184, 124, 102, 193, 188, 47, 205,
211, 127, 60, 34, 34, 204, 32, 1, 0, 0, 0, 0, 0, 0, 0, 230, 89, 167, 161, 98, 140, 221,
147, 254, 188, 4, 164, 224, 100, 110, 162, 14, 159, 95, 12, 224, 151, 217, 160, 82, 144,
212, 169, 224, 84, 223, 78, 1, 0, 0, 0, 0, 0, 0, 0, 37, 247, 211, 55, 231, 96, 163, 185,
188, 26, 127, 33, 131, 57, 43, 42, 10, 32, 114, 255, 223, 190, 21, 179, 20, 120, 184, 196,
24, 104, 65, 222, 0, 128, 129, 37, 112, 8, 106, 15, 152, 54, 101, 234, 21, 185, 146, 53,
44, 51, 142, 137, 220, 184, 52, 84, 230, 112, 238, 81, 8, 227, 20, 47, 48, 43, 5, 66, 65,
66, 69, 1, 1, 0, 121, 128, 255, 122, 229, 201, 17, 26, 66, 3, 120, 221, 146, 13, 229, 181,
25, 167, 79, 20, 137, 203, 194, 250, 218, 34, 56, 144, 198, 186, 91, 141, 246, 64, 130,
154, 48, 11, 17, 118, 213, 57, 234, 138, 223, 220, 209, 125, 20, 10, 101, 162, 229, 201,
246, 78, 64, 167, 154, 3, 132, 252, 141,
10, 13, 22, 200, 67, 234, 70, 53, 53, 35, 181, 174, 39, 195, 107, 232, 128, 49, 144, 0, 46,
49, 133, 110, 254, 85, 186, 83, 203, 199, 197, 6, 69, 1, 144, 163, 197, 173, 189, 82, 34,
223, 212, 9, 231, 160, 19, 228, 191, 132, 66, 233, 82, 181, 164, 11, 244, 139, 67, 151,
196, 198, 210, 20, 105, 63, 105, 3, 166, 96, 244, 224, 235, 128, 247, 251, 169, 168, 144,
60, 51, 9, 243, 15, 221, 196, 212, 16, 234, 164, 29, 199, 205, 36, 112, 165, 9, 62, 20, 6,
66, 65, 66, 69, 52, 2, 0, 0, 0, 0, 159, 96, 136, 32, 0, 0, 0, 0, 4, 66, 69, 69, 70, 132, 3,
4, 27, 102, 51, 199, 84, 23, 10, 207, 202, 104, 184, 2, 235, 159, 61, 6, 10, 40, 223, 155,
198, 15, 56, 24, 158, 249, 244, 126, 70, 119, 186, 4, 66, 65, 66, 69, 169, 3, 1, 20, 212,
53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133,
76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 1, 0, 0, 0, 0, 0, 0, 0, 142, 175, 4,
21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144,
156, 178, 38, 170, 71, 148, 242, 106, 72, 1, 0, 0, 0, 0, 0, 0, 0, 144, 181, 171, 32, 92,
105, 116, 201, 234, 132, 27, 230, 136, 134, 70, 51, 220, 156, 168, 163, 87, 132, 62, 234,
207, 35, 20, 100, 153, 101, 254, 34, 1, 0, 0, 0, 0, 0, 0, 0, 48, 103, 33, 33, 29, 84, 4,
189, 157, 168, 142, 2, 4, 54, 10, 26, 154, 184, 184, 124, 102, 193, 188, 47, 205, 211, 127,
60, 34, 34, 204, 32, 1, 0, 0, 0, 0, 0, 0, 0, 230, 89, 167, 161, 98, 140, 221, 147, 254,
188, 4, 164, 224, 100, 110, 162, 14, 159, 95, 12, 224, 151, 217, 160, 82, 144, 212, 169,
224, 84, 223, 78, 1, 0, 0, 0, 0, 0, 0, 0, 37, 247, 211, 55, 231, 96, 163, 185, 188, 26,
127, 33, 131, 57, 43, 42, 10, 32, 114, 255, 223, 190, 21, 179, 20, 120, 184, 196, 24, 104,
65, 222, 0, 128, 99, 229, 155, 248, 17, 21, 89, 124, 79, 189, 134, 73, 152, 214, 16, 205,
166, 187, 227, 44, 110, 19, 25, 72, 143, 62, 170, 60, 59, 165, 150, 110, 5, 66, 65, 66, 69,
1, 1, 176, 82, 55, 247, 244, 160, 12, 115, 166, 169, 63, 233, 237, 9, 141, 45, 194, 186,
67, 39, 32, 222, 11, 20, 122, 50, 3, 97, 121, 104, 223, 9, 80, 154, 189, 211, 112, 187,
167, 113, 224, 8, 134, 78, 168, 215, 202, 1, 228, 214, 23, 143, 125, 11, 211, 149, 154,
171, 25, 134, 44, 183, 166, 137,
];

let header: Header = Decode::decode(&mut &encoded_header[..]).unwrap();
println!("header #81 {:?}", header);

// Query mmr leaf with leaf index 81-1 at block 89
// Query mmr leaf with leaf index 81 (NOTE: not 81-1) at block 89
// {
// blockHash: 0x9b06cf8492be40bebbd27dcbf63cde5d4fe1e91494e56d6e66455e5e97c8bcb8
// leaf: 0xc5010050000000bcf91329818f409e0eb8c2c4de9a6de885469bd53b47c94a2fc23fb2762755ed010000000000000005000000304803fa5a91d9852caafe04b4b867a4ed27a07a5bee3d1507b4b187a68777a20000000000000000000000000000000000000000000000000000000000000000
// proof: 0x5000000000000000590000000000000018b30435845b8ffb6a85e7c8fb90addd2867f42b7e719bb6af05fa0ba7ffa184fbd59cc9dd6365ac18de4ca734fb1e411b265fae16cd88d9bdda618c7780b3962cc3131bc2d3184851b54f1d653276c129ad2fe9dccfc58af547d41332666902c2d7e14274ae442781c7114edead993544757003c8fc0d024d8c500ebe3b7f8a15e739d459e3f94386d09ee002e092288f7dc8649b1e7f8d5727489d0286b5ad1bc4fa016c5e80dfba7d2a3b479d73a4b203289d4c250939582a33406cae0de368
// blockHash: 0xd0d7c0b309926a2c64ed82f9a8ab8e2b037feb48fb3b783989bba30b041b1315
// leaf: 0xc5010051000000f728a8e3b29fb62b3234be2ba31e6beffd00bb571a978962ff9c26ea8dcc20ab010000000000000005000000304803fa5a91d9852caafe04b4b867a4ed27a07a5bee3d1507b4b187a68777a20000000000000000000000000000000000000000000000000000000000000000
// proof: 0x5100000000000000590000000000000018bddfdcc0399d0ce1be41f1126f63053ecb26ee19c107c0f96013f216b7b21933f8611a08a46cd74fd96d54d2eb19898dbd743b019bf7ba32b17b9a193f0e65b8c231bab606963f6a5a05071bea9af2a30f22adc43224affe87b3f90d1a07d0db4b6a7c61c56d1174067b6e816970631b8727f6dfe3ebd3923581472d45f47ad3940e1f16782fd635f4789d7f5674d2cbf12d1bbd7823c6ee37c807ad34424d48f0e3888f05a1d6183d9dbf8a91d3400ea2047b5e19d498968011e63b91058fbd
// }

// Query mmr leaf with leaf index 81 at block 89
// {
// blockHash: 0x9b06cf8492be40bebbd27dcbf63cde5d4fe1e91494e56d6e66455e5e97c8bcb8
// leaf: 0xc50100510000003340b448372fd1fe54f07ad142b2e828da79ad6ddb7c94cffcdd5d0c0aa65103010000000000000005000000304803fa5a91d9852caafe04b4b867a4ed27a07a5bee3d1507b4b187a68777a20000000000000000000000000000000000000000000000000000000000000000
// proof: 0x5100000000000000590000000000000018b30435845b8ffb6a85e7c8fb90addd2867f42b7e719bb6af05fa0ba7ffa184fbd59cc9dd6365ac18de4ca734fb1e411b265fae16cd88d9bdda618c7780b3962c5478542315554efa31adce73f3c6bb3db282b7c573ee7a47c62755e50a9e7307d7e14274ae442781c7114edead993544757003c8fc0d024d8c500ebe3b7f8a15e739d459e3f94386d09ee002e092288f7dc8649b1e7f8d5727489d0286b5ad1bc4fa016c5e80dfba7d2a3b479d73a4b203289d4c250939582a33406cae0de368
// }

let encoded_mmr_leaf = hex!("c50100510000003340b448372fd1fe54f07ad142b2e828da79ad6ddb7c94cffcdd5d0c0aa65103010000000000000005000000304803fa5a91d9852caafe04b4b867a4ed27a07a5bee3d1507b4b187a68777a20000000000000000000000000000000000000000000000000000000000000000");
let encoded_mmr_leaf = hex!("c5010051000000f728a8e3b29fb62b3234be2ba31e6beffd00bb571a978962ff9c26ea8dcc20ab010000000000000005000000304803fa5a91d9852caafe04b4b867a4ed27a07a5bee3d1507b4b187a68777a20000000000000000000000000000000000000000000000000000000000000000");

let leaf: Vec<u8> = Decode::decode(&mut &encoded_mmr_leaf[..]).unwrap();
let mmr_leaf: MmrLeaf = Decode::decode(&mut &*leaf).unwrap();
println!("mmr_leaf: {:?}", mmr_leaf);

let encoded_mmr_proof = hex!("5100000000000000590000000000000018b30435845b8ffb6a85e7c8fb90addd2867f42b7e719bb6af05fa0ba7ffa184fbd59cc9dd6365ac18de4ca734fb1e411b265fae16cd88d9bdda618c7780b3962c5478542315554efa31adce73f3c6bb3db282b7c573ee7a47c62755e50a9e7307d7e14274ae442781c7114edead993544757003c8fc0d024d8c500ebe3b7f8a15e739d459e3f94386d09ee002e092288f7dc8649b1e7f8d5727489d0286b5ad1bc4fa016c5e80dfba7d2a3b479d73a4b203289d4c250939582a33406cae0de368");
let encoded_mmr_proof = hex!("5100000000000000590000000000000018bddfdcc0399d0ce1be41f1126f63053ecb26ee19c107c0f96013f216b7b21933f8611a08a46cd74fd96d54d2eb19898dbd743b019bf7ba32b17b9a193f0e65b8c231bab606963f6a5a05071bea9af2a30f22adc43224affe87b3f90d1a07d0db4b6a7c61c56d1174067b6e816970631b8727f6dfe3ebd3923581472d45f47ad3940e1f16782fd635f4789d7f5674d2cbf12d1bbd7823c6ee37c807ad34424d48f0e3888f05a1d6183d9dbf8a91d3400ea2047b5e19d498968011e63b91058fbd");
let mmr_proof = MmrLeafProof::decode(&mut &encoded_mmr_proof[..]);
println!("mmr_proof: {:?}", mmr_proof);

Expand Down

0 comments on commit e35d079

Please sign in to comment.