Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Calculate finalized header time on genesis config import (paritytech#768
Browse files Browse the repository at this point in the history
)

* Calculate finalized header time on genesis config import

* Finishes up import time check fix.

* fmt

* Revert switching back to polkadot launch.

* Update parachain/pallets/ethereum-beacon-client/src/benchmarking/data.rs

Co-authored-by: David Dunn <26876072+doubledup@users.noreply.github.com>

Co-authored-by: claravanstaden <Cats 4 life!>
Co-authored-by: David Dunn <26876072+doubledup@users.noreply.github.com>
  • Loading branch information
claravanstaden and doubledup authored Jan 26, 2023
1 parent 2ed1155 commit 5353de6
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 32 deletions.
26 changes: 17 additions & 9 deletions core/packages/test/scripts/deploy-polkadot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source scripts/set-env.sh

generate_chain_spec() {
echo "Generating chain specification"
"$parachain_bin" build-spec --chain "$parachain_runtime" --disable-default-bootnode > "$output_dir/spec.json"
"$parachain_bin" build-spec --chain "$parachain_runtime" --disable-default-bootnode > "$output_dir/spec.json"

initial_beacon_block=""
while [ -z "$initial_beacon_block" ] || [ "$initial_beacon_block" == "0x0000000000000000000000000000000000000000000000000000000000000000" ]
do
Expand All @@ -18,26 +18,34 @@ generate_chain_spec() {

echo "Found initial finalized block: $initial_beacon_block"
bootstrap_header=""
slot=0
while [ -z "$bootstrap_header" ] || [ "$bootstrap_header" == "" ] || [ "$bootstrap_header" == "null" ]
do
echo "Waiting for beacon to get initial bootstrap..."
bootstrap_header=$(curl -s "$beacon_endpoint_http/eth/v1/beacon/light_client/bootstrap/$initial_beacon_block" \
| jq -r '.data.header')
bootstrap_data=$(curl -s "$beacon_endpoint_http/eth/v1/beacon/light_client/bootstrap/$initial_beacon_block")
bootstrap_header=$(jq -r '.data.header' <<< "$bootstrap_data")
slot=$(jq -r '.data.header.slot' <<< "$bootstrap_data")
sleep 3
done

curl -s "$beacon_endpoint_http/eth/v1/beacon/light_client/bootstrap/$initial_beacon_block" \
| node scripts/helpers/transformInitialBeaconSync.js > "$output_dir/initialBeaconSync_tmp.json"
curl -s "$beacon_endpoint_http/eth/v1/beacon/light_client/bootstrap/$initial_beacon_block" > "$output_dir/initialBeaconSync_tmp.json"

genesisData=$(curl -s "$beacon_endpoint_http/eth/v1/beacon/genesis")
validatorsRoot=$(jq -r '.data.genesis_validators_root' <<< "$genesisData")
genesisTime=$(jq -r '.data.genesis_time' <<< "$genesisData")

validatorsRoot=$(curl -s "$beacon_endpoint_http/eth/v1/beacon/genesis" \
| jq -r '.data.genesis_validators_root')
importTime="$((genesisTime + (seconds_per_slot * slot)))"

jq \
--arg validatorsRoot "$validatorsRoot" \
--arg importTime "$importTime" \
' .validators_root = $validatorsRoot
| .import_time = $importTime
' \
"$output_dir/initialBeaconSync_tmp.json" \
> "$output_dir/initialBeaconSync.json"
> "$output_dir/initialBeaconSync_tmp2.json"

cat "$output_dir/initialBeaconSync_tmp2.json" | node scripts/helpers/transformInitialBeaconSync.js | sponge "$output_dir/initialBeaconSync.json"

cat "$output_dir/spec.json" | node scripts/helpers/mutateSpec.js "$output_dir/contracts.json" "$output_dir/initialBeaconSync.json" | sponge "$output_dir/spec.json"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function transformBeaconForParachain(input) {
let output = input["data"];
output["header"]["slot"] = parseInt(output["header"]["slot"]);
output["header"]["proposer_index"] = parseInt(output["header"]["proposer_index"]);
output["validators_root"] = "";
output["import_time"] = parseInt(input["import_time"]);
output["validators_root"] = input["validators_root"];
return output;
}

Expand Down
3 changes: 3 additions & 0 deletions core/packages/test/scripts/set-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ lodestar_version="${LODESTAR_VER:-1.3.0}"
contract_dir="$core_dir/packages/contracts"
relay_dir="$root_dir/relayer"
relay_bin="$relay_dir/build/snowbridge-relay"
[ "$parachain_runtime" = "snowbase" ] &&
seconds_per_slot=6 ||
seconds_per_slot=12
export output_dir=/tmp/snowbridge
export output_bin_dir="$output_dir/bin"
ethereum_data_dir="$output_dir/geth"
Expand Down
3 changes: 2 additions & 1 deletion parachain/pallets/ethereum-beacon-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ std = [
"snowbridge-beacon-primitives/std",
"milagro_bls/std",
"ssz-rs/std",
"byte-slice-cast/std"
"byte-slice-cast/std",
"pallet-timestamp/std"
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
12 changes: 10 additions & 2 deletions parachain/pallets/ethereum-beacon-client/src/benchmarking/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ use snowbridge_beacon_primitives::{
};
use sp_core::U256;
use sp_std::vec;
use std::time::{SystemTime, UNIX_EPOCH};

pub fn initial_sync<SyncCommitteeSize: Get<u32>, ProofSize: Get<u32>>(
) -> InitialSync<SyncCommitteeSize, ProofSize> {
let time_now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs();

if config::IS_MINIMAL {
return InitialSync{
header: BeaconHeader{
Expand Down Expand Up @@ -64,7 +70,8 @@ pub fn initial_sync<SyncCommitteeSize: Get<u32>, ProofSize: Get<u32>>(
hex!("d33a17a3903ceac967c0afc2be32962dd69f5836e7674b4c30b2c68116720b2c").into(),
hex!("0d0607530d6ffd3dfffafee157c34db1430cd7a1f29dea854769cf5c45aed99d").into()
].try_into().expect("too many branch proof items"),
validators_root: hex!("270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69").into()
validators_root: hex!("270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69").into(),
import_time: time_now + 97200, // now + 27 hour sync committee period
};
}
return InitialSync{
Expand Down Expand Up @@ -599,7 +606,8 @@ pub fn initial_sync<SyncCommitteeSize: Get<u32>, ProofSize: Get<u32>>(
hex!("b073d53f925033e981d9069da106e2a158989a3bd734651a874f95cdb63e203b").into(),
hex!("b132c9711ec41fb5b14de2c9d06da61cd09f57da54ca5556e70824e4787a1e84").into()
].try_into().expect("too many branch proof items"),
validators_root: hex!("043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb").into()
validators_root: hex!("043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb").into(),
import_time: time_now + 97200,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub const SLOTS_PER_EPOCH: u64 = 32;
pub const SECONDS_PER_SLOT: u64 = 12;
pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: u64 = 256;
pub const SYNC_COMMITTEE_SIZE: usize = 512;
pub const IS_MINIMAL: bool = false;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub const SLOTS_PER_EPOCH: u64 = 4;
pub const SECONDS_PER_SLOT: u64 = 6;
pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: u64 = 8;
pub const SYNC_COMMITTEE_SIZE: usize = 32;
pub const IS_MINIMAL: bool = true;
12 changes: 10 additions & 2 deletions parachain/pallets/ethereum-beacon-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,17 @@ pub mod pallet {
.into();

Self::store_sync_committee(period, initial_sync.current_sync_committee);
Self::store_finalized_header(block_root, initial_sync.header);
Self::store_validators_root(initial_sync.validators_root);

let last_finalized_header = FinalizedHeaderState {
beacon_block_root: block_root,
beacon_slot: initial_sync.header.slot,
import_time: initial_sync.import_time,
};

<FinalizedBeaconHeaders<T>>::insert(block_root, initial_sync.header);
<LatestFinalizedHeaderState<T>>::set(last_finalized_header);

Ok(())
}

Expand Down Expand Up @@ -417,7 +425,7 @@ pub mod pallet {

log::info!(
target: "ethereum-beacon-client",
"💫 Checking weak subjectivity period. Current time is :{:?} Weak subjectvitity period check: {:?}.",
"💫 Checking weak subjectivity period. Current time is :{:?} Weak subjectivity period check: {:?}.",
time,
weak_subjectivity_period_check
);
Expand Down
104 changes: 89 additions & 15 deletions parachain/pallets/ethereum-beacon-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,22 @@ mod beacon_tests {
true => hex!("ffff").to_vec(),
false => hex!("bffffffff7f1ffdfcfeffeffbfdffffbfffffdffffefefffdffff7f7ffff77fffdf7bffff5f7fedfffdfb6ddff7bf7").to_vec(),
};
let provided = match config::IS_MINIMAL {
true => 2,
false => 47,
};
let expected = match config::IS_MINIMAL {
true => 4,
false => 64,
};

let sync_committee_bits = merkleization::get_sync_committee_bits::<
mock_minimal::MaxSyncCommitteeSize,
>(bits.try_into().expect("invalid sync committee bits"));

assert_err!(
sync_committee_bits,
MerkleizationError::ExpectedFurtherInput { provided: 47, expected: 64 }
MerkleizationError::ExpectedFurtherInput { provided, expected }
);
}

Expand All @@ -339,14 +347,22 @@ mod beacon_tests {
true => hex!("bffffffff7f1ffdfcfeffeffbfdffffbfffffdffffefefff").to_vec(),
false => hex!("bffffffff7f1ffdfcfeffeffbfdffffbfffffdffffefefffdffff7f7ffff77fffdf7bff77ffdf7fffafffffff77fefffeff7effffffff5f7fedfffdfb6ddff7bf7bffffffff7f1ffdfcfeffeffbfdffffbfffffdffffefefffdffff7f7ffff77fffdf7bff77ffdf7fffafffffff77fefffeff7effffffff5f7fedfffdfb6ddff7bf7").to_vec(),
};
let provided = match config::IS_MINIMAL {
true => 24,
false => 130,
};
let expected = match config::IS_MINIMAL {
true => 4,
false => 64,
};

let sync_committee_bits = merkleization::get_sync_committee_bits::<
mock_minimal::MaxSyncCommitteeSize,
>(bits.try_into().expect("invalid sync committee bits"));

assert_err!(
sync_committee_bits,
MerkleizationError::AdditionalInput { provided: 130, expected: 64 }
MerkleizationError::AdditionalInput { provided, expected }
);
}

Expand Down Expand Up @@ -660,13 +676,14 @@ mod beacon_tests {
#[cfg(feature = "minimal")]
mod beacon_minimal_tests {
use crate::{
merkleization, merkleization::MerkleizationError, mock::*, ssz::SSZBeaconBlockBody, Error,
ExecutionHeaders, FinalizedBeaconHeaders, FinalizedHeaderState, LatestFinalizedHeaderState,
SyncCommittees, ValidatorsRoot,
config, merkleization, merkleization::MerkleizationError, mock::*, ssz::SSZBeaconBlockBody,
Error, ExecutionHeaders, FinalizedBeaconHeaders, FinalizedHeaderState,
LatestFinalizedHeaderState, SyncCommittees, ValidatorsRoot,
};
use frame_support::{assert_err, assert_ok};
use hex_literal::hex;
use sp_core::H256;
use std::time::{SystemTime, UNIX_EPOCH};

#[test]
fn it_syncs_from_an_initial_checkpoint() {
Expand Down Expand Up @@ -716,15 +733,29 @@ mod beacon_minimal_tests {
#[test]
fn it_processes_a_finalized_header_update() {
let update = get_finalized_header_update::<mock_minimal::Test>();

let current_sync_committee =
get_initial_sync::<mock_minimal::Test>().current_sync_committee;
let initial_sync = get_initial_sync::<mock_minimal::Test>();
let current_sync_committee = initial_sync.current_sync_committee;

let current_period = mock_minimal::EthereumBeaconClient::compute_current_sync_period(
update.attested_header.slot,
);

let time_now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs();

let slot = initial_sync.header.slot;
let import_time = time_now + (slot * config::SECONDS_PER_SLOT); // Goerli genesis time + finalized header update time
let mock_pallet_time = import_time + 3600; // plus one hour

new_tester::<mock_minimal::Test>().execute_with(|| {
mock_minimal::Timestamp::set_timestamp(mock_pallet_time * 1000); // needs to be in milliseconds
LatestFinalizedHeaderState::<mock_minimal::Test>::set(FinalizedHeaderState {
beacon_block_root: Default::default(),
import_time,
beacon_slot: slot,
});
SyncCommittees::<mock_minimal::Test>::insert(current_period, current_sync_committee);
ValidatorsRoot::<mock_minimal::Test>::set(get_validators_root::<mock_minimal::Test>());

Expand Down Expand Up @@ -848,11 +879,11 @@ mod beacon_minimal_tests {
#[cfg(not(feature = "minimal"))]
mod beacon_mainnet_tests {
use crate::{
merkleization, merkleization::MerkleizationError, mock::*, ssz::SSZBeaconBlockBody,
ExecutionHeaders, FinalizedBeaconHeaders, FinalizedHeaderState, LatestFinalizedHeaderState,
SyncCommittees, ValidatorsRoot,
config, merkleization, merkleization::MerkleizationError, mock::*, ssz::SSZBeaconBlockBody,
Error, ExecutionHeaders, FinalizedBeaconHeaders, FinalizedHeaderState,
LatestFinalizedHeaderState, SyncCommittees, ValidatorsRoot,
};
use frame_support::assert_ok;
use frame_support::{assert_err, assert_ok};
use hex_literal::hex;
use sp_core::H256;

Expand Down Expand Up @@ -904,16 +935,25 @@ mod beacon_mainnet_tests {
#[test]
fn it_processes_a_finalized_header_update() {
let update = get_finalized_header_update::<mock_mainnet::Test>();

let current_sync_committee =
get_initial_sync::<mock_mainnet::Test>().current_sync_committee;
let initial_sync = get_initial_sync::<mock_mainnet::Test>();
let current_sync_committee = initial_sync.current_sync_committee;

let current_period = mock_mainnet::EthereumBeaconClient::compute_current_sync_period(
update.attested_header.slot,
);

let slot = initial_sync.header.slot;
let import_time = 1616508000u64 + (slot * config::SECONDS_PER_SLOT); // Goerli genesis time + finalized header update time
let mock_pallet_time = import_time + 3600; // plus one hour

new_tester::<mock_mainnet::Test>().execute_with(|| {
mock_mainnet::Timestamp::set_timestamp(mock_pallet_time * 1000); // needs to be in milliseconds
SyncCommittees::<mock_mainnet::Test>::insert(current_period, current_sync_committee);
LatestFinalizedHeaderState::<mock_mainnet::Test>::set(FinalizedHeaderState {
beacon_block_root: Default::default(),
import_time,
beacon_slot: slot,
});
ValidatorsRoot::<mock_mainnet::Test>::set(get_validators_root::<mock_mainnet::Test>());

assert_ok!(mock_mainnet::EthereumBeaconClient::import_finalized_header(
Expand All @@ -930,6 +970,40 @@ mod beacon_mainnet_tests {
});
}

#[test]
fn it_errors_when_weak_subjectivity_period_exceeded_for_a_finalized_header_update() {
let update = get_finalized_header_update::<mock_mainnet::Test>();
let initial_sync = get_initial_sync::<mock_mainnet::Test>();
let current_sync_committee = initial_sync.current_sync_committee;

let current_period = mock_mainnet::EthereumBeaconClient::compute_current_sync_period(
update.attested_header.slot,
);

let slot = initial_sync.header.slot;
let import_time = 1616508000u64 + (slot * config::SECONDS_PER_SLOT);
let mock_pallet_time = import_time + 100800; // plus 28 hours

new_tester::<mock_mainnet::Test>().execute_with(|| {
mock_mainnet::Timestamp::set_timestamp(mock_pallet_time * 1000); // needs to be in milliseconds
SyncCommittees::<mock_mainnet::Test>::insert(current_period, current_sync_committee);
LatestFinalizedHeaderState::<mock_mainnet::Test>::set(FinalizedHeaderState {
beacon_block_root: Default::default(),
import_time,
beacon_slot: slot,
});
ValidatorsRoot::<mock_mainnet::Test>::set(get_validators_root::<mock_mainnet::Test>());

assert_err!(
mock_mainnet::EthereumBeaconClient::import_finalized_header(
mock_mainnet::RuntimeOrigin::signed(1),
update.clone()
),
Error::<mock_mainnet::Test>::BridgeBlocked
);
});
}

#[test]
fn it_processes_a_header_update() {
let update = get_header_update::<mock_mainnet::Test>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,6 @@
"0xb073d53f925033e981d9069da106e2a158989a3bd734651a874f95cdb63e203b",
"0xb132c9711ec41fb5b14de2c9d06da61cd09f57da54ca5556e70824e4787a1e84"
],
"validators_root": "0x043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb"
"validators_root": "0x043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb",
"import_time": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@
"0xd33a17a3903ceac967c0afc2be32962dd69f5836e7674b4c30b2c68116720b2c",
"0x0d0607530d6ffd3dfffafee157c34db1430cd7a1f29dea854769cf5c45aed99d"
],
"validators_root": "0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69"
"validators_root": "0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69",
"import_time": 0
}
2 changes: 2 additions & 0 deletions parachain/primitives/beacon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl<SyncCommitteeSize: Get<u32>, ProofSize: Get<u32>> Default
current_sync_committee: Default::default(),
current_sync_committee_branch: Default::default(),
validators_root: Default::default(),
import_time: Default::default(),
}
}
}
Expand Down Expand Up @@ -151,6 +152,7 @@ pub struct InitialSync<SyncCommitteeSize: Get<u32>, ProofSize: Get<u32>> {
pub current_sync_committee: SyncCommittee<SyncCommitteeSize>,
pub current_sync_committee_branch: BoundedVec<H256, ProofSize>,
pub validators_root: Root,
pub import_time: u64,
}

#[derive(
Expand Down

0 comments on commit 5353de6

Please sign in to comment.