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

BEP466 #80

Open
wants to merge 10 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
13 changes: 11 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ jobs:
command: test
args: --release --features=std --manifest-path light-client/Cargo.toml
- uses: actions-rs/cargo@v1
name: unit-test-dev-test
name: unit-test-dev-test-min
with:
command: test
args: --release --features=dev --manifest-path light-client/Cargo.toml --lib test::dev_test
args: --release --features=dev --manifest-path light-client/Cargo.toml --lib test::dev_test_min
env:
MINIMUM_TIMESTAMP_SUPPORTED: 1731495592
MINIMUM_HEIGHT_SUPPORTED: 100
- uses: actions-rs/cargo@v1
name: unit-test-dev-test-pascal
with:
command: test
args: --release --features=dev --manifest-path light-client/Cargo.toml --lib test::dev_test_pascal
env:
MINIMUM_TIMESTAMP_SUPPORTED: 1
MINIMUM_HEIGHT_SUPPORTED: 1
PASCAL_TIMESTAMP: 1
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ This is available in dev feature only.

```sh
BSC_BLOCKS_PER_EPOCH=20 cargo build --features=dev
```
```

### Build Parameters

Parameters can be specified to check for acceptable headers at build time.

| Name | Description |
| --- |-------------------------------------------------------------------------------------------------------------------------------------------------|
| `MINIMUM_TIMESTAMP_SUPPORTED` | Timestamp of the lowest header this light client will accept |
| `MINIMUM_HEIGHT_SUPPORTED` | Height of the lowest header this light client will accept |
| `PASCAL_TIMESTAMP` | Timestamp of the first Pascal Hardfork header, used to check the header structure after Pascal Hardfork; if 0 is specified, no check is made. |
7 changes: 5 additions & 2 deletions light-client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ fn main() {
std::env::var("MINIMUM_TIMESTAMP_SUPPORTED").unwrap_or_else(|_| "0".to_string());
let minimum_height_supported =
std::env::var("MINIMUM_HEIGHT_SUPPORTED").unwrap_or_else(|_| "0".to_string());
let pascal_timestamp =
std::env::var("PASCAL_TIMESTAMP").unwrap_or_else(|_| "0".to_string());
writeln!(
file,
"pub const MINIMUM_TIMESTAMP_SUPPORTED: u64 = {};\npub const MINIMUM_HEIGHT_SUPPORTED: u64 = {};",
"pub const MINIMUM_TIMESTAMP_SUPPORTED: u64 = {};\npub const MINIMUM_HEIGHT_SUPPORTED: u64 = {};\npub const PASCAL_TIMESTAMP: u64 = {};",
minimum_time_stamp_supported,
minimum_height_supported
minimum_height_supported,
pascal_timestamp
)
.unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions light-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ mod test {
fn test_error_create_client() {
let client_state = hex!("0a272f6962632e6c69676874636c69656e74732e7061726c69612e76312e436c69656e745374617465124d08381214151f3951fa218cac426edfe078fa9e5c6dcea5001a2000000000000000000000000000000000000000000000000000000000000000002205109b9ea90f2a040880a305320410c0843d").to_vec();
let consensus_state = hex!("0a2a2f6962632e6c69676874636c69656e74732e7061726c69612e76312e436f6e73656e7375735374617465126c0a2056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b42110de82d5a8061a209c59cf0b5717cb6e2bd8620b7f3481605c8abcd45636bdf45c86db06338f0c5e22207a1dede35f5c835fecdc768324928cd0d9d9161e8529e1ba1e60451f3a9d088a").to_vec();
let client = ParliaLightClient::default();
let client = ParliaLightClient;
let mock_consensus_state = BTreeMap::new();
let ctx = MockClientReader {
client_state: None,
Expand Down Expand Up @@ -1156,7 +1156,7 @@ mod test {
}

#[cfg(feature = "dev")]
mod dev_test {
mod dev_test_min {
use crate::client::test::MockClientReader;
use crate::client::ParliaLightClient;
use crate::client_state::ClientState;
Expand Down
2 changes: 1 addition & 1 deletion light-client/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ mod test {
}
}
#[cfg(feature = "dev")]
mod dev_test {
mod dev_test_min {
use crate::client_state::ClientState;
use crate::consensus_state::ConsensusState;
use crate::errors::Error;
Expand Down
4 changes: 4 additions & 0 deletions light-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum Error {
UnexpectedUntrustedValidatorsHashInEpoch(Height, Height, Hash, Hash),
UnexpectedCurrentValidatorsHashInEpoch(Height, Height, Hash, Hash),
UnexpectedUntrustedValidators(BlockNumber, BlockNumber),
MissingRequestsHash(BlockNumber),

// Vote attestation
UnexpectedTooManyHeadersToFinalize(BlockNumber, usize),
Expand Down Expand Up @@ -384,6 +385,9 @@ impl core::fmt::Display for Error {
Error::UnexpectedRevisionHeight(e1) => {
write!(f, "UnexpectedRevisionHeight : {}", e1)
}
Error::MissingRequestsHash(e1) => {
write!(f, "MissingRequestsHash : {}", e1)
}
}
}
}
Expand Down
128 changes: 60 additions & 68 deletions light-client/src/header/eth_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use parlia_ibc_proto::ibc::lightclients::parlia::v1::EthHeader as RawETHHeader;

use crate::errors::Error;
use crate::header::epoch::Epoch;

use crate::header::hardfork::PASCAL_TIMESTAMP;
use crate::header::vote_attestation::VoteAttestation;
use crate::misc::{Address, BlockNumber, ChainId, Hash, RlpIterator, Validators};

Expand Down Expand Up @@ -58,6 +58,7 @@ pub struct ETHHeader {
pub blob_gas_used: Option<u64>,
pub excess_blob_gas: Option<u64>,
pub parent_beacon_root: Option<Vec<u8>>,
pub requests_hash: Option<Vec<u8>>,

// calculated by RawETHHeader
pub hash: Hash,
Expand Down Expand Up @@ -133,6 +134,11 @@ impl ETHHeader {
stream.append_empty_data();
}
stream.append(parent_beacon_root);

// https://github.com/bnb-chain/bsc/blob/e2f2111a85fecabb4782099338aca21bf58bde09/core/types/block.go#L776
if let Some(value) = &self.requests_hash {
stream.append(value);
}
}
}
stream.finalize_unbounded_list();
Expand Down Expand Up @@ -378,6 +384,7 @@ impl TryFrom<RawETHHeader> for ETHHeader {
let blob_gas_used: Option<u64> = rlp.try_next_as_val().map(Some).unwrap_or(None);
let excess_blob_gas: Option<u64> = rlp.try_next_as_val().map(Some).unwrap_or(None);
let parent_beacon_root: Option<Vec<u8>> = rlp.try_next_as_val().map(Some).unwrap_or(None);
let requests_hash: Option<Vec<u8>> = rlp.try_next_as_val().map(Some).unwrap_or(None);

// Check that the extra-data contains the vanity, validators and signature
let extra_size = extra_data.len();
Expand Down Expand Up @@ -415,72 +422,7 @@ impl TryFrom<RawETHHeader> for ETHHeader {
return Err(Error::UnexpectedNonce(number));
}

// create block hash
let mut stream = RlpStream::new();
stream.begin_unbounded_list();
stream.append(&parent_hash);
stream.append(&uncle_hash);
stream.append(&coinbase);
stream.append(&root.to_vec());
stream.append(&tx_hash);
stream.append(&receipt_hash);
stream.append(&bloom);
stream.append(&difficulty);
stream.append(&number);
stream.append(&gas_limit);
stream.append(&gas_used);
stream.append(&timestamp);
stream.append(&extra_data);
stream.append(&mix_digest);
stream.append(&nonce);

if base_fee_per_gas.is_some()
|| withdrawals_hash.is_some()
|| blob_gas_used.is_some()
|| excess_blob_gas.is_some()
|| parent_beacon_root.is_some()
{
if let Some(v) = base_fee_per_gas {
stream.append(&v);
} else {
stream.append_empty_data();
}
}
if withdrawals_hash.is_some()
|| blob_gas_used.is_some()
|| excess_blob_gas.is_some()
|| parent_beacon_root.is_some()
{
if let Some(v) = &withdrawals_hash {
stream.append(v);
} else {
stream.append_empty_data();
}
}
if blob_gas_used.is_some() || excess_blob_gas.is_some() || parent_beacon_root.is_some() {
if let Some(v) = blob_gas_used {
stream.append(&v);
} else {
stream.append_empty_data();
}
}
if excess_blob_gas.is_some() || parent_beacon_root.is_some() {
if let Some(v) = excess_blob_gas {
stream.append(&v);
} else {
stream.append_empty_data();
}
}
if parent_beacon_root.is_some() {
if let Some(v) = &parent_beacon_root {
stream.append(v);
} else {
stream.append_empty_data();
}
}
stream.finalize_unbounded_list();
let buffer_vec: Vec<u8> = stream.out().to_vec();
let hash: Hash = keccak_256(&buffer_vec);
let hash: Hash = keccak_256(value.header.as_slice());

let epoch = if number % BLOCKS_PER_EPOCH == 0 {
let (validators, turn_length) = get_validator_bytes_and_turn_length(&extra_data)?;
Expand All @@ -489,6 +431,11 @@ impl TryFrom<RawETHHeader> for ETHHeader {
None
};

#[allow(clippy::absurd_extreme_comparisons)]
if PASCAL_TIMESTAMP > 0 && timestamp >= PASCAL_TIMESTAMP && requests_hash.is_none() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When PASCAL_TIMESTAMP is 0, it's interpreted as not being Pascal, rather than indicating it's already activated.
It would be helpful to have explanatory comments near where users specify this value, to prevent misunderstandings since this behavior differs from flag management in geth and bsc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add the comment into README.
e0cc787

return Err(Error::MissingRequestsHash(number));
}

Ok(Self {
parent_hash,
uncle_hash,
Expand All @@ -510,6 +457,7 @@ impl TryFrom<RawETHHeader> for ETHHeader {
withdrawals_hash,
blob_gas_used,
parent_beacon_root,
requests_hash,
hash,
epoch,
})
Expand All @@ -527,9 +475,11 @@ pub(crate) mod test {
use rlp::RlpStream;
use rstest::*;

use crate::fixture::{localnet, Network};
use crate::fixture::{decode_header, localnet, Network};
use crate::header::epoch::Epoch;

use alloc::boxed::Box;
use hex_literal::hex;
use parlia_ibc_proto::ibc::lightclients::parlia::v1::EthHeader as RawETHHeader;

fn to_raw(header: &ETHHeader) -> RawETHHeader {
Expand Down Expand Up @@ -871,4 +821,46 @@ pub(crate) mod test {
err => unreachable!("{:?}", err),
}
}
#[test]
fn test_success_bep466_header() {
let header = hex!("f90370a04a99d244666a287d9aaa1a81aa5bba573f156865369023eaa53a4ba8bb303ad1a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794e04db2de85453e0936b441c339a26d10cfa71b50a0d0a25a7c6b93d5d2e8f7e2075d2886fa62840f31c127b880b7cd503e2d364163a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282071b8402625a008084678f4827b90111d883010503846765746888676f312e32332e35856c696e75780000002f5b9772f8ae0fb860959e5c417ecd8a5e5ddabd85485cf2cc4433f26beea076d77bbc6f461e4129881b8772bdae5fdd6ca927b571662ac5750d4abeca4f44a4406ab3254e0d98e6ee92b5b6396122853b45db2d18d24fb79e8397e253ca10a2a03b3b18e5961173b5f848820719a0e5ef3de482ecc3de5aea0efb17457d7edc5b1a39fc97c29cc5780b4665c9ca2082071aa04a99d244666a287d9aaa1a81aa5bba573f156865369023eaa53a4ba8bb303ad180aba9a203cbc9ac6e2eabbc44b15f7c526ec5f9d570a0addc005d5958d8415f760794e65762057ff9956dce68034d30cca6d9cc2ac3eb35f699d47c74931c470a01a0000000000000000000000000000000000000000000000000000000000000000088000000000000000080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4218080a00000000000000000000000000000000000000000000000000000000000000000a0e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855").to_vec();
let header = decode_header(header);
let chain_id = localnet().network();

let prev_epoch = hex!("f90484a0cdbf04705a1f6ed4989217c1e89f4c0ab22b3122df2f48c09e2fef3d0aa4b5b4a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794b2e42bc54d19116d2348ac83461e2e0915d508ada08b3fad7b45691957d1bf905d2601a14e12d48d1da89205d22b4eb582af803e3da0629579638c8423e2836b6ad04eed7a7dcda123a3f4b6d2ab488121fac9df6c10a03cd1ebc99cd975182c58de47be968c97658cff4c465e20654185f408a851403cb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028207088402625a008229a884678f47eeb90223d883010503846765746888676f312e32332e35856c696e75780000002f5b9772048fdaaa7e6631e438625ca25c857a3727ea28e565b5ad484c80ff8ed9a2aff68923f36e8975c377abd0c62a8a66ac0d2519b3eb4c14951312006c9e8b3829dc68cab6bcf0a7876ea32e7a748c697d01345145485561305b24958ec28bac0db09ee3e6cfc1769fd72b493a6c44118598abb600bec65e66aafd23acd46e0d1e0bda9d8101dbbdbf369fd9a13701eafb76870cb220843b8c6476824bfa15ad21d1bf47e3df7d8d99b105a24ebca95a84b035e6af22880b9eaf1d6a4a233920a57ecf09f8a6b89d7f5ca3cfe6484fe04db2de85453e0936b441c339a26d10cfa71b50b611e87c256a23edc8b7e55558abe1a7ff94262bacb53d600e657270ee6af9172c5a31c24498a162147dd7e1bfdcef9107f8ae0fb860a70e55ed7260c28c69880ca12370872c2059f0540ca88c1cb7a6ba772ed6e7f62a5e11c31e117ea483a4c6b46cf213681092f5273512ad15e030f52c3485838801a5ae99ece187028adbf6f543b34bd4a48199dc9b58ea47b16d06049a370a65f848820706a0ab18ec6cce429c9918cb4f354ffda5c0119871589de4829c9871ee7eddbce0ed820707a0cdbf04705a1f6ed4989217c1e89f4c0ab22b3122df2f48c09e2fef3d0aa4b5b48099019414f8ca7f80176785458468355225878402b80bbe64de4de083cbd909163b1c8dd0564e07516e6bbd0ec64381847f8f3d94d75992fd04243b538ba9348d01a0000000000000000000000000000000000000000000000000000000000000000088000000000000000080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4218080a00000000000000000000000000000000000000000000000000000000000000000a0e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855").to_vec();
let prev_epoch = decode_header(prev_epoch);

header
.verify_seal(&prev_epoch.epoch.unwrap(), &chain_id)
.unwrap();
assert!(&header.requests_hash.is_some());
assert_eq!(32, header.requests_hash.unwrap().len());
}

#[cfg(feature = "dev")]
mod dev_test_pascal {
use crate::errors::Error;
use crate::fixture::{decode_header, localnet};
use crate::header::eth_header::ETHHeader;
use hex_literal::hex;
use parlia_ibc_proto::ibc::lightclients::parlia::v1::EthHeader;

#[test]
fn test_error_missing_request_hash() {
// number = 401
let raw_header = localnet().epoch_header_plus_1_rlp();
let raw_header = EthHeader { header: raw_header };
let result = ETHHeader::try_from(raw_header).unwrap_err();
match result {
Error::MissingRequestsHash(_) => {}
_ => unreachable!(),
}
}

#[test]
fn test_success_after_bep466() {
let header = hex!("f90370a04a99d244666a287d9aaa1a81aa5bba573f156865369023eaa53a4ba8bb303ad1a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794e04db2de85453e0936b441c339a26d10cfa71b50a0d0a25a7c6b93d5d2e8f7e2075d2886fa62840f31c127b880b7cd503e2d364163a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282071b8402625a008084678f4827b90111d883010503846765746888676f312e32332e35856c696e75780000002f5b9772f8ae0fb860959e5c417ecd8a5e5ddabd85485cf2cc4433f26beea076d77bbc6f461e4129881b8772bdae5fdd6ca927b571662ac5750d4abeca4f44a4406ab3254e0d98e6ee92b5b6396122853b45db2d18d24fb79e8397e253ca10a2a03b3b18e5961173b5f848820719a0e5ef3de482ecc3de5aea0efb17457d7edc5b1a39fc97c29cc5780b4665c9ca2082071aa04a99d244666a287d9aaa1a81aa5bba573f156865369023eaa53a4ba8bb303ad180aba9a203cbc9ac6e2eabbc44b15f7c526ec5f9d570a0addc005d5958d8415f760794e65762057ff9956dce68034d30cca6d9cc2ac3eb35f699d47c74931c470a01a0000000000000000000000000000000000000000000000000000000000000000088000000000000000080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4218080a00000000000000000000000000000000000000000000000000000000000000000a0e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855").to_vec();
decode_header(header);
}
}
}
1 change: 1 addition & 0 deletions light-client/src/header/hardfork.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub const MINIMUM_TIMESTAMP_SUPPORTED: u64 = 0;
pub const MINIMUM_HEIGHT_SUPPORTED: u64 = 0;
pub const PASCAL_TIMESTAMP: u64 = 0;
1 change: 1 addition & 0 deletions light-client/src/header/vote_attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ mod test {
blob_gas_used: None,
excess_blob_gas: None,
parent_beacon_root: None,
requests_hash: None,
hash: [0u8; 32],
epoch: None,
};
Expand Down
Loading