Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check malicious vote #43

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions light-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,11 +1034,6 @@ mod test {

#[test]
fn test_error_submit_misbehavior() {
let ctx = MockClientReader {
client_state: Some(ClientState::default()),
consensus_state: BTreeMap::new(),
};

let client = ParliaLightClient::default();
let client_id = ClientId::new(client.client_type().as_str(), 1).unwrap();

Expand All @@ -1047,6 +1042,23 @@ mod test {
let any2= hex!("7c8f289cda6dcbc5edbb26400e1306c2de06f52a9a583dab99300138011ade1b0ab4060ab106f9032ea0c3ca2cd851054700e22f8313521399aacfcc225541cde7dfeca894f9b57d7c22a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948fdaaa7e6631e438625ca25c857a3727ea28e565a0d269e8b47a345f3a77343f46943f78480a96ff82a8466963fb4a29c0e2b66091a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002820c1c8402625a0080846699bcdab90111d98301040b846765746889676f312e32312e3132856c696e75780000ffcc693ef8ae03b86086dcdfb683f4965de8062063dd843775eeb55a1271b317a6d65ef7aadabb1e10a9327c9face4c69c331b914f3b24aeb4010b30d4c4a7007c67d7de812e9c7cca598c9b3b1e94679e90aeb94b790b19dce6bee8662126eecdb3b539efee6468b7f848820c1aa049430f5e612a0b37a9bab1b1c5003f02f3586a4e2ea9e84d8de511f879f30561820c1ba0c3ca2cd851054700e22f8313521399aacfcc225541cde7dfeca894f9b57d7c22803135a7c114e967442e0bc0996b3b083aae7638e142b3a145b8d966267fecb84141fcf58023341e1aeaf333bfe4d91349db198c157947780f46b1d476a892c25900a0000000000000000000000000000000000000000000000000000000000000000088000000000000000080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b42180800ab4060ab106f9032ea02c07474042f3b317665fe4a233454a1ec921770d6429dd3eb0a23e812429d595a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794a7876ea32e7a748c697d01345145485561305b24a0d269e8b47a345f3a77343f46943f78480a96ff82a8466963fb4a29c0e2b66091a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002820c1d8402625a0080846699bcddb90111d98301040b846765746889676f312e32312e3132856c696e75780000ffcc693ef8ae03b860a410689613510c2087d762d49d42acb83f84b136e417e0567c8fbd693fd25cb36f794ae231b79101b94d700457f511120b7b2c8c0294c33b2d9728fa5d0a81a9c02901e28c3df1c840e7a4f7f6b5f150c3d7276b84a5f05d57a582ac6659692ff848820c1ba0c3ca2cd851054700e22f8313521399aacfcc225541cde7dfeca894f9b57d7c22820c1ca02c07474042f3b317665fe4a233454a1ec921770d6429dd3eb0a23e812429d59580dfca46ba06e40ca0191deacc05e2185acbfb04ca3ce23355f2b3d1be19667d096f33e5174260e4e5be857a49307dcf824f1b7e353c3be3e7bb4202c5bf4ae32b00a0000000000000000000000000000000000000000000000000000000000000000088000000000000000080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b42180800ab4060ab106f9032ea0c0e82c0cda470def9ae9b03f55755b9cf0af015ae255bbb90a7969296918514aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948fdaaa7e6631e438625ca25c857a3727ea28e565a0d269e8b47a345f3a77343f46943f78480a96ff82a8466963fb4a29c0e2b66091a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002820c1e8402625a0080846699bce0b90111d98301040b846765746889676f312e32312e3132856c696e75780000ffcc693ef8ae03b860856d586a1618c0da1e906937b0a4c2596598a2d34e5cbf62063fc52ca9e5078fc962443b3a53166fabf69548b8d885ec08260a0d01293a3e61ba14d82e976f477f6ab2db2a8ae7c641cca0077c1770f472e7306ab43f8fecf762a95bc45ca1a5f848820c1ca02c07474042f3b317665fe4a233454a1ec921770d6429dd3eb0a23e812429d595820c1da0c0e82c0cda470def9ae9b03f55755b9cf0af015ae255bbb90a7969296918514a802a8efd919b1fe8bd679c8db7976de46350cfe3c2bdf67ec67e95c2f1c02909736bbb1bcdd798058072adc2e3cea6a16d519d1630e121539b8ccd77b1a7f84a1c00a0000000000000000000000000000000000000000000000000000000000000000088000000000000000080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b42180801203109b181a9506f90312f901f1a094e7dcd05ddcc3923085b451330f3aa5ce5a628d6685506d99cb09b3aef0e11ea0d8a8f0aaba741477e7d0c15b6a1759b3d842e346b35afd14219ecdd18188718ea0b116ef7733a93eed23f018027c116e60436a228a9f9173bb9b0c40eb71216da6a0bd63cc1cadd9ef7ffd41bb496484bff7c6ddbc3b29cf1a8b82d1f8f12bc6347680a0a74e307420bf9966d5db9a83ce48edc32c7a8e43328c6373de6875cefa04ad7aa0e502f113ade32bee3770b5a0117fa799c8812e5ab5a239e3878660c59c3537fda00577e3e2c4649c5a23cbdabe0bbfed7cdf6e85c136d84d58127cdec86264ad6ea070c0f30031f40c8017dbfc2ef008e6a3aae2e3105a654e0c5439b6104752882ea08f81903ec8515875682785142e1f92bdeaf65fccd5d0cf78b1ff2905a07e5883a03052420ba2d24a04d3f830584d3dbd6907b6d82bab84ddd806d03470e2c9d51ca06fb1a1498c2c8f93944a4f672ff4e982480ad181c835c0d8078159c517c7977aa013a426820f7b7249edc97cc5c002e653ac84b437f3ac12ac940c3d4b09e09827a09fbc54eac488b27315b09a1afa8d12f168e4c4cb5aea2d9a6ab5e7266da2f7e8a077c5e5cd5bd518bc509ee5e71790f1e42e492e23875b097e565cff8e809e7c8aa0d4182165b298d7b52b0f2064d19ab8bdad2b3955fa5b3d85c00673beb97e124480f8b18080a0dc77b6ae50b675036e77b31973c79ec60c28c0d2c57b03ad99c2acfff2f0cd4e80a03f47312ca98ac1f6ae9db9752c2a33529723b64e654d87f7847cee0382edfb55a05e0f116451aaa1baab3f3abff2793c8318050eeed6bf62d464d343a11d86eb2880808080808080a0abbb1987d09a71106f586030d1ab913bae0008e2a7dec0d08f2d60cd30fb2ac8a096c706907bfc6472dd88315cb8e21ee6f60a661cd8050065e2ba387023ee96858080f869a020b1e2b1f9852058ee0aaadca3c963f77f6483a1a51c644d79386bcada360583b846f8440180a0e39304f0ec064a98e4b0a96432dfb0a9e4c7fd0f26a6bbcf9c75bff68c51a7a9a0b3d632130dcb5cb583b47ec0623e59ca3703e6e2564f144272b597f3e3511ba822448fdaaa7e6631e438625ca25c857a3727ea28e56593428ee663799df81ea82bc8445a7d93c891ef324b5f4438eb766bcf75fc405fb79d3f618fcdd17f107b374368ef512f2244a7876ea32e7a748c697d01345145485561305b24903201f874819815e1a3f183c4addc814b71ec0e573e07f79ee9082926d82dd1711d6c45a9cc8841916d8563c2f80baa2a448fdaaa7e6631e438625ca25c857a3727ea28e56593428ee663799df81ea82bc8445a7d93c891ef324b5f4438eb766bcf75fc405fb79d3f618fcdd17f107b374368ef512f2a44d9a13701eafb76870cb220843b8c6476824bfa15a82968379b116362f75bdb7cc4be8ca0ceea7c0f2e74be7c8f289cda6dcbc5edbb26400e1306c2de06f52a9a583dab9930013801").to_vec();
any.extend(any2);
let any: Any = any.try_into().unwrap();
let misbehavior = Misbehaviour::try_from(any.clone()).unwrap();
let mut mock_consensus_state = BTreeMap::new();
mock_consensus_state.insert(
misbehavior.header_1.trusted_height(),
ConsensusState {
current_validators_hash: misbehavior.header_1.current_epoch_validators_hash(),
previous_validators_hash: misbehavior.header_1.previous_epoch_validators_hash(),
..Default::default()
},
);
let ctx = MockClientReader {
client_state: Some(ClientState {
chain_id: ChainId::new(9999),
..Default::default()
}),
consensus_state: mock_consensus_state,
};
// check if misbehavior
let err = client
.update_client(&ctx, client_id.clone(), any)
Expand Down
3 changes: 3 additions & 0 deletions light-client/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ impl ClientState {
) -> Result<ClientState, Error> {
self.check_header(now, h1_trusted_cs, &misbehaviour.header_1)?;
self.check_header(now, h2_trusted_cs, &misbehaviour.header_2)?;

misbehaviour.verify()?;

Ok(self.clone().freeze())
}

Expand Down
12 changes: 6 additions & 6 deletions light-client/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn verify(raw_root: &Hash, proof: &[Vec<u8>], key: &[u8]) -> Result<Option<Vec<u

fn trim_left_zero(value: &[u8]) -> &[u8] {
let mut pos = 0;
for v in value.iter() {
for v in value {
if *v != 0 {
break;
}
Expand Down Expand Up @@ -378,11 +378,11 @@ mod test {

#[test]
fn test_trim_left_zero() {
assert_eq!(trim_left_zero(&[1, 2, 3, 4]), &vec![1, 2, 3, 4]);
assert_eq!(trim_left_zero(&[1, 2, 3, 0]), &vec![1, 2, 3, 0]);
assert_eq!(trim_left_zero(&[0, 2, 3, 0]), &vec![2, 3, 0]);
assert_eq!(trim_left_zero(&[0, 0, 3, 0]), &vec![3, 0]);
assert_eq!(trim_left_zero(&[0, 0, 0, 4]), &vec![4]);
assert_eq!(trim_left_zero(&[1, 2, 3, 4]), [1, 2, 3, 4]);
assert_eq!(trim_left_zero(&[1, 2, 3, 0]), [1, 2, 3, 0]);
assert_eq!(trim_left_zero(&[0, 2, 3, 0]), [2, 3, 0]);
assert_eq!(trim_left_zero(&[0, 0, 3, 0]), [3, 0]);
assert_eq!(trim_left_zero(&[0, 0, 0, 4]), [4]);
assert!(trim_left_zero(&[0, 0, 0, 0]).is_empty());
assert!(trim_left_zero(&[]).is_empty());
}
Expand Down
12 changes: 6 additions & 6 deletions light-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ pub enum Error {
MissingHeader1,
MissingHeader2,
UnexpectedClientId(String),
UnexpectedDifferentHeight(Height, Height),
UnexpectedSameBlockHash(Height),
UnexpectedSameBlockHash(Height, Height, Hash),
UnexpectedHonestVote(Height, Height),

TrieError(BoxedTrieError, Hash, Vec<Vec<u8>>, Vec<u8>),

Expand Down Expand Up @@ -205,11 +205,11 @@ impl core::fmt::Display for Error {
Error::MissingHeader1 => write!(f, "MissingHeader1"),
Error::MissingHeader2 => write!(f, "MissingHeader2"),
Error::UnexpectedClientId(e1) => write!(f, "UnexpectedClientId : {}", e1),
Error::UnexpectedDifferentHeight(e1, e2) => {
write!(f, "UnexpectedDifferentHeight : {} {}", e1, e2)
Error::UnexpectedSameBlockHash(e1, e2, e3) => {
write!(f, "UnexpectedSameBlockHash : {} {} {:?}", e1, e2, e3)
}
Error::UnexpectedSameBlockHash(e1) => {
write!(f, "UnexpectedSameBlockHash : {}", e1)
Error::UnexpectedHonestVote(e1, e2) => {
write!(f, "UnexpectedHonestVote : {} {}", e1, e2)
}
Error::UnknownMisbehaviourType(e1) => write!(f, "UnknownMisbehaviourType : {}", e1),
Error::UnexpectedStateValue(e1, e2, e3, e4, e5) => {
Expand Down
13 changes: 12 additions & 1 deletion light-client/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::header::epoch::{EitherEpoch, Epoch, TrustedEpoch, UntrustedEpoch};
use crate::header::eth_header::{validate_turn_length, ETHHeader};

use crate::header::eth_headers::ETHHeaders;
use crate::misc::{new_height, new_timestamp, ChainId, Hash};
use crate::header::vote_attestation::VoteData;
use crate::misc::{new_height, new_timestamp, BlockNumber, ChainId, Hash};

use super::errors::Error;

Expand Down Expand Up @@ -75,6 +76,16 @@ impl Header {
&self.headers.target.hash
}

pub fn votes(&self) -> Vec<(BlockNumber, VoteData)> {
let mut votes = vec![];
for h in self.headers.all.iter() {
if let Ok(vote) = h.get_vote_attestation() {
votes.push((h.number, vote.data));
}
}
votes
}

pub fn verify(
&self,
chain_id: &ChainId,
Expand Down
Loading
Loading