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

Save validator set hash in ExtraData for non-neighboring epoch verification #47

Merged
merged 4 commits into from
May 7, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion light-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parlia-elc"
version = "0.3.0"
version = "0.2.9"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
38 changes: 34 additions & 4 deletions light-client/src/client.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions light-client/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl ClientState {
.try_into()
.map_err(Error::UnexpectedStorageRoot)?,
timestamp: header.timestamp()?,
current_validators_hash: header.current_validators_hash(),
previous_validators_hash: header.previous_validators_hash(),
current_validators_hash: header.current_epoch_validators_hash()?,
previous_validators_hash: header.previous_epoch_validators_hash(),
};

Ok((new_client_state, new_consensus_state))
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 @@ -50,8 +50,8 @@ pub enum Error {
UnexpectedValidatorsHashSize(Vec<u8>),

// Header error
MissingPreviousTrustedValidators(BlockNumber),
MissingCurrentTrustedValidators(BlockNumber),
MissingPreviousValidators(BlockNumber),
MissingCurrentValidators(BlockNumber),
OutOfTrustingPeriod(Time, Time),
HeaderFromFuture(Time, core::time::Duration, Time),
MissingTrustedHeight,
Expand Down Expand Up @@ -254,11 +254,11 @@ impl core::fmt::Display for Error {
Error::MissingValidatorInEpochBlock(e1) => {
write!(f, "MissingValidatorInEpochBlock : {:?}", e1)
}
Error::MissingPreviousTrustedValidators(e1) => {
write!(f, "MissingPreviousTrustedValidators : {:?}", e1)
Error::MissingPreviousValidators(e1) => {
write!(f, "MissingPreviousValidators : {:?}", e1)
}
Error::MissingCurrentTrustedValidators(e1) => {
write!(f, "MissingCurrentTrustedValidators : {:?}", e1)
Error::MissingCurrentValidators(e1) => {
write!(f, "MissingCurrentValidators : {:?}", e1)
}
Error::UnexpectedMixHash(e1) => {
write!(f, "UnexpectedMixHash : {:?}", e1)
Expand Down
35 changes: 19 additions & 16 deletions light-client/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct Header {
headers: ETHHeaders,
trusted_height: Height,
previous_validators: ValidatorSet,
/// validator set
/// - not a epoch block: current epoch validators (which must be in trusted cons state)
/// - non neighboring epoch header: validators in trusted cons state
/// - neighboring epoch header: validators in extra data
current_validators: ValidatorSet,
}

Expand Down Expand Up @@ -62,12 +66,17 @@ impl Header {
&self.headers.target.root
}

pub fn previous_validators_hash(&self) -> Hash {
pub fn previous_epoch_validators_hash(&self) -> Hash {
self.previous_validators.hash
}

pub fn current_validators_hash(&self) -> Hash {
self.current_validators.hash
/// In non-adjacent epochs, current_validators contains the validator set of trusted_height,
/// so you need to use the validator set of extra_data of target.
pub fn current_epoch_validators_hash(&self) -> Result<Hash, Error> {
if self.headers.target.is_epoch() {
return Ok(self.headers.target.get_validator_set()?.hash);
}
Ok(self.current_validators.hash)
}

pub fn block_hash(&self) -> &Hash {
Expand All @@ -81,11 +90,13 @@ impl Header {
) -> Result<(), Error> {
if self.is_non_neighboring_epoch() {
let n_val = self.headers.target.get_validator_set()?;
// 'current_validators' are trusted validators in non-neighboring epoch verification.
let t_val = &self.current_validators;
let (t_val, n_val) = verify_validator_set_non_neighboring_epoch(
consensus_state,
self.height(),
self.trusted_height(),
&self.current_validators,
t_val,
&n_val,
)?;

Expand Down Expand Up @@ -242,19 +253,11 @@ impl TryFrom<RawHeader> for Header {
}

if value.previous_validators.is_empty() {
return Err(Error::MissingPreviousTrustedValidators(
headers.target.number,
));
return Err(Error::MissingPreviousValidators(headers.target.number));
}

// Epoch header contains validator set
// - not a epoch block: current epoch validators (which must be in trusted cons state)
// - non neighboring epoch header: validators in trusted cons state
// - neighboring epoch header: validators in extra data
if value.current_validators.is_empty() {
return Err(Error::MissingCurrentTrustedValidators(
headers.target.number,
));
return Err(Error::MissingCurrentValidators(headers.target.number));
}

Ok(Self {
Expand Down Expand Up @@ -368,7 +371,7 @@ pub(crate) mod test {
};
let err = Header::try_from(raw).unwrap_err();
match err {
Error::MissingPreviousTrustedValidators(number) => {
Error::MissingPreviousValidators(number) => {
assert_eq!(number, h.number);
}
err => unreachable!("{:?}", err),
Expand All @@ -391,7 +394,7 @@ pub(crate) mod test {
};
let err = Header::try_from(raw).unwrap_err();
match err {
Error::MissingCurrentTrustedValidators(number) => {
Error::MissingCurrentValidators(number) => {
assert_eq!(number, h.number);
}
err => unreachable!("{:?}", err),
Expand Down
Loading