Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
Merge branch 'bsc-light-client' of github.com:darwinia-network/darwin…
Browse files Browse the repository at this point in the history
…ia-common into bsc-light-client
  • Loading branch information
xiaoch05 committed Jan 5, 2022
2 parents b65f30f + 20bdb23 commit 70712d3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 53 deletions.
4 changes: 2 additions & 2 deletions frame/bridge/bsc/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const VERIFIED_HEADERS_BYTES: &'static str = "0x1c017203a062da40a61596fff8df385e
const SUBMIT_HEADER_BYTES: &'static str = "0x3d88dc9a505b0ed2d1d69601b33da491e8820dc2e278b3c25247e16784abea161dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347c89c669357d161d57b0b255c94ea96e17999991972e58f33063d686a2a9f858795b0cc7bdb43f83fb475f29712142409cfdb46dd23be7c3fcf9d400078755c02a163c26c0382d1b6aaa5ab3e05b003a2489059aa7c0cc1be44dfe4c94c680036d7cd041ede16dccadfaa24537f3ccef224fb0f5c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000020100000000000000000000000000000000000200002000000000000000000000000000000000000000000000000000000000000000000000000000010000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000001000000000000000000200000000000000000000000000000000000000000000000000000000000000403691000000000080c3c901000000000000000000000000000000000000000000000000000000000695000000000000000000000000000000000000000000000000000000000000adbbbd60000000008501d883010007846765746888676f312e31352e35856c696e75780000001600553df952160a93183da2e06577d3fe37b5bf756ed849473fdc858565ab68f31929960215f00cdfa8703a60e88a8c75b2334c5cb29156a0f24827504fe996b6840f2b000000000000000000000000000000000000000000000000000000000000000000200000000000000000";

benchmarks! {
verify_and_update_authority_set_signed {
relay_finalized_epoch_header {
let genesis_header = BSCHeader::decode(&mut hex2bytes_unchecked(GENSIS_HEADER_BYTES).as_slice()).unwrap();
let header = Vec::<BSCHeader>::decode(&mut hex2bytes_unchecked(VERIFIED_HEADERS_BYTES).as_slice()).unwrap();
let initial_authority_set =
Expand Down Expand Up @@ -70,7 +70,7 @@ benchmarks! {
(0u32..initial_authority_set.len() as u32).collect::<Vec<u32>>(),
);
let caller: T::AccountId = T::AccountId::decode(&mut &[0; 32][..]).unwrap_or_default();
assert_ok!(<BscBridge<T>>::verify_and_update_authority_set_signed(
assert_ok!(<BscBridge<T>>::relay_finalized_epoch_header(
RawOrigin::Signed(caller.clone()).into(),
update_headers
));
Expand Down
10 changes: 5 additions & 5 deletions frame/bridge/bsc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Verify signed relayed headers and finalize authority set
#[pallet::weight(<T as Config>::WeightInfo::verify_and_update_authority_set_signed())]
pub fn verify_and_update_authority_set_signed(
#[pallet::weight(<T as Config>::WeightInfo::relay_finalized_epoch_header())]
pub fn relay_finalized_epoch_header(
origin: OriginFor<T>,
headers: Vec<BSCHeader>,
proof: Vec<BSCHeader>,
) -> DispatchResultWithPostInfo {
let submitter = frame_system::ensure_signed(origin)?;

match Self::verify_and_update_authority_set(&headers) {
match Self::verify_and_update_authority_set_and_checkpoint(&proof) {
Ok(new_authority_set) => {
T::OnHeadersSubmitted::on_valid_authority_finalized(
submitter,
Expand Down Expand Up @@ -415,7 +415,7 @@ pub mod pallet {
}

/// Verify unsigned relayed headers and finalize authority set
pub fn verify_and_update_authority_set(
pub fn verify_and_update_authority_set_and_checkpoint(
headers: &[BSCHeader],
) -> Result<Vec<Address>, DispatchError> {
// get finalized authority set from storage
Expand Down
1 change: 0 additions & 1 deletion frame/bridge/bsc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ frame_support::parameter_types! {
pub const EpochInStorage: u64 = 128;
}
impl Config for Test {
type Event = ();
type WeightInfo = ();
type BSCConfiguration = Configuration;
type OnHeadersSubmitted = ();
Expand Down
47 changes: 5 additions & 42 deletions frame/bridge/bsc/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// --- paritytech ---
use frame_support::{assert_noop, assert_ok};
use sp_runtime::DispatchError;
// --- darwinia-network ---
use crate::mock::*;
use bsc_primitives::BSCHeader;
Expand Down Expand Up @@ -79,19 +78,7 @@ fn extract_authorities_should_work() {
}

#[test]
fn verify_and_update_authority_set_unsigned_should_fail() {
ExtBuilder::default().build().execute_with(|| {
let header = BSCHeader::default();

assert_noop!(
BSC::verify_and_update_authority_set_unsigned(Origin::root(), vec![header]),
DispatchError::BadOrigin
);
});
}

#[test]
fn verify_and_update_authority_set_signed_should_fail() {
fn relay_finalized_epoch_header_should_fail() {
ExtBuilder::default().build().execute_with(|| {
let header = serde_json::from_str::<BSCHeader>(
r#"{
Expand All @@ -116,14 +103,14 @@ fn verify_and_update_authority_set_signed_should_fail() {
).unwrap();

assert_noop!(
BSC::verify_and_update_authority_set_signed(Origin::signed(1), vec![header]),
BSC::relay_finalized_epoch_header(Origin::signed(1), vec![header]),
BSCError::InvalidHeadersSize
);
});
}

#[test]
fn verify_and_update_authority_set_signed_should_work() {
fn relay_finalized_epoch_header_should_work() {
ExtBuilder::default().build().execute_with(|| {
let headers_7706000_to_7706010 = [
r#"{
Expand Down Expand Up @@ -337,7 +324,7 @@ fn verify_and_update_authority_set_signed_should_work() {
}"#,
].iter().map(|json| serde_json::from_str(json).unwrap()).collect::<Vec<BSCHeader>>();

assert_ok!(BSC::verify_and_update_authority_set_signed(
assert_ok!(BSC::relay_finalized_epoch_header(
Origin::signed(1),
headers_7706000_to_7706010,
));
Expand Down Expand Up @@ -481,33 +468,9 @@ fn verify_and_update_authority_set_signed_should_work() {
}"#,
].iter().map(|json| serde_json::from_str(json).unwrap()).collect::<Vec<BSCHeader>>();

assert_ok!(BSC::verify_and_update_authority_set_signed(
assert_ok!(BSC::relay_finalized_epoch_header(
Origin::signed(1),
testnet_headers_9516600_to_9516606,
));

let h9516608 = serde_json::from_str::<BSCHeader>(r#"{
"difficulty": "0x2",
"extraData": "0xd883010007846765746888676f312e31352e35856c696e75780000001600553df952160a93183da2e06577d3fe37b5bf756ed849473fdc858565ab68f31929960215f00cdfa8703a60e88a8c75b2334c5cb29156a0f24827504fe996b6840f2b00",
"gasLimit": "0x1c9c380",
"gasUsed": "0x9506",
"hash": "0x65b2ed201fe91456ba941b48f0adb6e3cd7777ab5b7135154e40cad34d603db4",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000002010000000000000000000000000000000000020000200000000000000000000000000000000000000000000000000000000000000000000000000001000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000100000000000000000",
"miner": "0xc89c669357d161d57b0b255c94ea96e179999919",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"number": "0x913640",
"parentHash": "0x3d88dc9a505b0ed2d1d69601b33da491e8820dc2e278b3c25247e16784abea16",
"receiptsRoot": "0x7c0cc1be44dfe4c94c680036d7cd041ede16dccadfaa24537f3ccef224fb0f5c",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x36d",
"stateRoot": "0x72e58f33063d686a2a9f858795b0cc7bdb43f83fb475f29712142409cfdb46dd",
"timestamp": "0x60bdbbad",
"totalDifficulty": "0x1211b59",
"transactionsRoot": "0x23be7c3fcf9d400078755c02a163c26c0382d1b6aaa5ab3e05b003a2489059aa",
"uncles": []
}"#).unwrap();

assert_ok!(BSC::verify_header(&h9516608));
})
}
6 changes: 3 additions & 3 deletions frame/bridge/bsc/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use sp_std::marker::PhantomData;

/// Weight functions needed for darwinia_bridge_bsc.
pub trait WeightInfo {
fn verify_and_update_authority_set_signed() -> Weight;
fn relay_finalized_epoch_header() -> Weight;
}

/// Weights for darwinia_bridge_bsc using the Substrate node and recommended hardware.
Expand All @@ -63,7 +63,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: BSC FinalizedCheckpoint (r:1 w:1)
// Storage: BSC Authorities (r:1 w:1)
// Storage: BSC AuthoritiesOfRound (r:0 w:1)
fn verify_and_update_authority_set_signed() -> Weight {
fn relay_finalized_epoch_header() -> Weight {
(1_626_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
Expand All @@ -76,7 +76,7 @@ impl WeightInfo for () {
// Storage: BSC FinalizedCheckpoint (r:1 w:1)
// Storage: BSC Authorities (r:1 w:1)
// Storage: BSC AuthoritiesOfRound (r:0 w:1)
fn verify_and_update_authority_set_signed() -> Weight {
fn relay_finalized_epoch_header() -> Weight {
(1_626_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
Expand Down

0 comments on commit 70712d3

Please sign in to comment.