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

Sync gas price #144

Open
wants to merge 40 commits into
base: snowbridge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ad01996
gas price feature
claravanstaden May 1, 2024
e9735cd
readme
claravanstaden May 1, 2024
b75111d
adds execution header to update
claravanstaden May 2, 2024
7060b73
Adds gas provider to outbound queue
claravanstaden May 2, 2024
ac86a8b
adds impl
claravanstaden May 2, 2024
ad2682b
progress
claravanstaden May 2, 2024
aad2f35
remove crate
claravanstaden May 2, 2024
b80eefe
remove upstream workflow file
claravanstaden May 2, 2024
c898b6d
fix optional dep
claravanstaden May 2, 2024
1cfc972
unused variable
claravanstaden May 3, 2024
1ab639c
remove option
claravanstaden May 4, 2024
2ec70ec
Merge branch 'snowbridge' into sync-gas-price
yrong Jun 11, 2024
89b44a6
Fix compile
yrong Jun 11, 2024
ab62865
Update json fixture
yrong Jun 11, 2024
649263a
Fix compile & more refactoring
yrong Jun 11, 2024
803ff50
Rename event
yrong Jun 11, 2024
5bd8871
More fix
yrong Jun 11, 2024
bc5c70f
Fix format
yrong Jun 11, 2024
e12c785
Merge branch 'snowbridge' into sync-gas-price
yrong Jun 11, 2024
fe27347
Use twap price
yrong Jun 11, 2024
ec203e6
Update test fixture
yrong Jun 12, 2024
6ab08ba
Improve ci workflow
yrong Jun 12, 2024
8001239
Use a simple EMA instead
yrong Jun 12, 2024
2870845
Fix format
yrong Jun 12, 2024
bcf1bb9
Fix test
yrong Jun 12, 2024
98e420f
Fix test
yrong Jun 12, 2024
4d396ee
Format
yrong Jun 12, 2024
a43de4d
Fix format
yrong Jun 12, 2024
f27eada
Fix ci
yrong Jun 12, 2024
bc7ee16
Fix taplo format
yrong Jun 12, 2024
1b62893
Update template and test fixtures
yrong Jun 12, 2024
b34fbf2
Initialize with DefaultFeePerGas
yrong Jun 13, 2024
61115de
Fix tests
yrong Jun 13, 2024
66ebf79
Make ScalingFactor dynamically adjusted
yrong Jun 17, 2024
427b8de
Update price only forward
yrong Jun 17, 2024
9745788
Update algo with docs
yrong Jun 23, 2024
7c41ad4
Fix breaking tests
yrong Jun 24, 2024
efc272a
Merge branch 'snowbridge' into sync-gas-price
yrong Jun 26, 2024
c498edf
More tests
yrong Jun 26, 2024
46733eb
Improve gas price implementation (#156)
vgeddes Jul 10, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/parachain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
- name: clippy
run: cargo clippy --all-features -- -D warnings
- name: format
working-directory: bridges/snowbridge
run: cargo +nightly fmt --all -- --check
- name: install taplo
run: |
Expand Down
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ members = [
"bridges/relays/utils",
"bridges/snowbridge/pallets/ethereum-client",
"bridges/snowbridge/pallets/ethereum-client/fixtures",
"bridges/snowbridge/pallets/gas-price",
"bridges/snowbridge/pallets/inbound-queue",
"bridges/snowbridge/pallets/inbound-queue/fixtures",
"bridges/snowbridge/pallets/outbound-queue",
Expand Down
25 changes: 23 additions & 2 deletions bridges/snowbridge/Cargo.lock

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

1 change: 1 addition & 0 deletions bridges/snowbridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ resolver = "2"
members = [
"pallets/ethereum-client",
"pallets/ethereum-client/fixtures",
"pallets/gas-price",
"pallets/inbound-queue",
"pallets/inbound-queue/fixtures",
"pallets/outbound-queue",
Expand Down
4 changes: 4 additions & 0 deletions bridges/snowbridge/pallets/ethereum-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sp-io = { path = "../../../../substrate/primitives/io", default-features = false
snowbridge-core = { path = "../../primitives/core", default-features = false }
snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = false }
snowbridge-pallet-ethereum-client-fixtures = { path = "fixtures", default-features = false, optional = true }
snowbridge-pallet-gas-price = { path = "../gas-price", default-features = false }
primitives = { package = "snowbridge-beacon-primitives", path = "../../primitives/beacon", default-features = false }
static_assertions = { version = "1.1.0", default-features = false }
pallet-migrations = { path = "../../../../substrate/frame/migrations", default-features = false }
Expand Down Expand Up @@ -66,6 +67,7 @@ std = [
"snowbridge-core/std",
"snowbridge-ethereum/std",
"snowbridge-pallet-ethereum-client-fixtures/std",
"snowbridge-pallet-gas-price/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
Expand All @@ -80,11 +82,13 @@ runtime-benchmarks = [
"pallet-migrations/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"snowbridge-pallet-ethereum-client-fixtures/runtime-benchmarks",
"snowbridge-pallet-gas-price/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-migrations/try-runtime",
"sp-runtime/try-runtime",
"snowbridge-pallet-gas-price/try-runtime",
]
52 changes: 51 additions & 1 deletion bridges/snowbridge/pallets/ethereum-client/fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,31 @@ pub fn make_sync_committee_update() -> Box<Update> {
hex!("5340ad5877c72dca689ca04bc8fedb78d67a4801d99887937edd8ccd29f87e82").into(),
hex!("f2b3cb56753939a728ccad399a434ca490f018f2f331529ec0d8b2d59c509271").into(),
],
execution_header: VersionedExecutionPayloadHeader::Deneb(deneb::ExecutionPayloadHeader {
parent_hash: hex!("8092290aa21b7751576440f77edd02a94058429ce50e63a92d620951fb25eda2").into(),
fee_recipient: hex!("0000000000000000000000000000000000000000").into(),
state_root: hex!("96a83e9ddf745346fafcb0b03d57314623df669ed543c110662b21302a0fae8b").into(),
receipts_root: hex!("dccdfceea05036f7b61dcdabadc937945d31e68a8d3dfd4dc85684457988c284").into(),
logs_bloom: hex!("00000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000080000000400000000000000000000004000000000080000000000000000000000000000000000010100000000000000000000000000000000020000000000000000000000000000000000080000000000000000000000000000040004000000000000002002002000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000080000000000000000000000000000000000100000000000000000200000200000010").into(),
prev_randao: hex!("62e309d4f5119d1f5c783abc20fc1a549efbab546d8d0b25ff1cfd58be524e67").into(),
block_number: 393,
gas_limit: 54492273,
gas_used: 199644,
timestamp: 1710552813,
extra_data: hex!("d983010d0b846765746888676f312e32312e368664617277696e").into(),
base_fee_per_gas: U256::from(7u64),
block_hash: hex!("6a9810efb9581d30c1a5c9074f27c68ea779a8c1ae31c213241df16225f4e131").into(),
transactions_root: hex!("2cfa6ed7327e8807c7973516c5c32a68ef2459e586e8067e113d081c3bd8c07d").into(),
withdrawals_root: hex!("792930bbd5baac43bcc798ee49aa8185ef76bb3b44ba62b91d86ae569e4bb535").into(),
blob_gas_used: 0,
excess_blob_gas: 0,
}),
execution_branch: vec![
hex!("a6833fa629f3286b6916c6e50b8bf089fc9126bee6f64d0413b4e59c1265834d").into(),
hex!("b46f0c01805fe212e15907981b757e6c496b0cb06664224655613dcec82505bb").into(),
hex!("db56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71").into(),
hex!("d3af7c05c516726be7505239e0b9c7cb53d24abce6b91cdb3b3995f0164a75da").into(),
],
})
}

Expand Down Expand Up @@ -1166,7 +1191,32 @@ pub fn make_finalized_header_update() -> Box<Update> {
hex!("bcc666ad0ad9f9725cbd682bc95589d35b1b53b2a615f1e6e8dd5e086336becf").into(),
hex!("3069b547a08f703a1715016e926cbd64e71f93f64fb68d98d8c8f1ab745c46e5").into(),
hex!("c2de7e1097239404e17b263cfa0473533cc41e903cb03440d633bc5c27314cb4").into(),
]
],
execution_header: VersionedExecutionPayloadHeader::Deneb(deneb::ExecutionPayloadHeader {
parent_hash: hex!("8092290aa21b7751576440f77edd02a94058429ce50e63a92d620951fb25eda2").into(),
fee_recipient: hex!("0000000000000000000000000000000000000000").into(),
state_root: hex!("96a83e9ddf745346fafcb0b03d57314623df669ed543c110662b21302a0fae8b").into(),
receipts_root: hex!("dccdfceea05036f7b61dcdabadc937945d31e68a8d3dfd4dc85684457988c284").into(),
logs_bloom: hex!("00000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000080000000400000000000000000000004000000000080000000000000000000000000000000000010100000000000000000000000000000000020000000000000000000000000000000000080000000000000000000000000000040004000000000000002002002000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000080000000000000000000000000000000000100000000000000000200000200000010").into(),
prev_randao: hex!("62e309d4f5119d1f5c783abc20fc1a549efbab546d8d0b25ff1cfd58be524e67").into(),
block_number: 393,
gas_limit: 54492273,
gas_used: 199644,
timestamp: 1710552813,
extra_data: hex!("d983010d0b846765746888676f312e32312e368664617277696e").into(),
base_fee_per_gas: U256::from(7u64),
block_hash: hex!("6a9810efb9581d30c1a5c9074f27c68ea779a8c1ae31c213241df16225f4e131").into(),
transactions_root: hex!("2cfa6ed7327e8807c7973516c5c32a68ef2459e586e8067e113d081c3bd8c07d").into(),
withdrawals_root: hex!("792930bbd5baac43bcc798ee49aa8185ef76bb3b44ba62b91d86ae569e4bb535").into(),
blob_gas_used: 0,
excess_blob_gas: 0,
}),
execution_branch: vec![
hex!("a6833fa629f3286b6916c6e50b8bf089fc9126bee6f64d0413b4e59c1265834d").into(),
hex!("b46f0c01805fe212e15907981b757e6c496b0cb06664224655613dcec82505bb").into(),
hex!("db56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71").into(),
hex!("d3af7c05c516726be7505239e0b9c7cb53d24abce6b91cdb3b3995f0164a75da").into(),
],
})
}

Expand Down
30 changes: 27 additions & 3 deletions bridges/snowbridge/pallets/ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ use primitives::{
fast_aggregate_verify, verify_merkle_branch, verify_receipt_proof, BeaconHeader, BlsError,
CompactBeaconState, ForkData, ForkVersion, ForkVersions, PublicKeyPrepared, SigningData,
};
use snowbridge_core::{BasicOperatingMode, RingBufferMap};
use snowbridge_core::{BasicOperatingMode, GasPriceProvider, RingBufferMap};
use sp_core::H256;
use sp_std::prelude::*;
pub use weights::WeightInfo;

use functions::{
compute_epoch, compute_period, decompress_sync_committee_bits, sync_committee_sum,
};
use types::{CheckpointUpdate, FinalizedBeaconStateBuffer, SyncCommitteePrepared, Update};

pub use pallet::*;
use types::{CheckpointUpdate, FinalizedBeaconStateBuffer, SyncCommitteePrepared, Update};

pub use config::SLOTS_PER_HISTORICAL_ROOT;

Expand Down Expand Up @@ -83,6 +82,7 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
#[pallet::constant]
type ForkVersions: Get<ForkVersions>;
type GasPrice: GasPriceProvider;
type WeightInfo: WeightInfo;
}

Expand Down Expand Up @@ -115,6 +115,7 @@ pub mod pallet {
InvalidExecutionHeaderProof,
InvalidAncestryMerkleProof,
InvalidBlockRootsRootMerkleProof,
ExpectedExecutionHeader,
/// The gap between the finalized headers is larger than the sync committee period,
/// rendering execution headers unprovable using ancestry proofs (blocks root size is
/// the same as the sync committee period slots).
Expand Down Expand Up @@ -403,6 +404,24 @@ pub mod pallet {
);
}

// Execution payload header corresponding to `beacon.body_root` (from Capella onward)
// https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/light-client/sync-protocol.md#modified-lightclientheader
let execution_header_root: H256 = update
.execution_header
.hash_tree_root()
.map_err(|_| Error::<T>::BlockBodyHashTreeRootFailed)?;
ensure!(update.execution_branch.len() > 0, Error::<T>::InvalidExecutionHeaderProof);
ensure!(
verify_merkle_branch(
execution_header_root,
&update.execution_branch.clone(),
config::EXECUTION_HEADER_SUBTREE_INDEX,
config::EXECUTION_HEADER_DEPTH,
update.finalized_header.body_root
),
Error::<T>::InvalidExecutionHeaderProof
);

// Verify sync committee aggregate signature.
let sync_committee = if signature_period == store_period {
<CurrentSyncCommittee<T>>::get()
Expand Down Expand Up @@ -470,6 +489,11 @@ pub mod pallet {
Self::store_finalized_header(update.finalized_header, update.block_roots_root)?;
}

T::GasPrice::update(
update.execution_header.base_fee_per_gas(),
update.finalized_header.slot,
);

Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions bridges/snowbridge/pallets/ethereum-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ frame_support::construct_runtime!(
System: frame_system,
EthereumBeaconClient: crate,
Migrator: pallet_migrations,
GasPrice: snowbridge_pallet_gas_price::{Pallet, Call, Storage, Event<T>},
}
);

Expand All @@ -74,6 +75,11 @@ impl frame_system::Config for Test {
type MultiBlockMigrator = Migrator;
}

impl snowbridge_pallet_gas_price::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

parameter_types! {
pub const ChainForkVersions: ForkVersions = ForkVersions {
genesis: Fork {
Expand Down Expand Up @@ -103,6 +109,7 @@ impl ethereum_beacon_client::Config for Test {
type RuntimeEvent = RuntimeEvent;
type ForkVersions = ChainForkVersions;
type WeightInfo = ();
type GasPrice = GasPrice;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@
"0x3069b547a08f703a1715016e926cbd64e71f93f64fb68d98d8c8f1ab745c46e5",
"0xc2de7e1097239404e17b263cfa0473533cc41e903cb03440d633bc5c27314cb4"
],
"execution_header": null,
"execution_branch": null
"execution_header": {
"Deneb": {
"parent_hash": "0x8092290aa21b7751576440f77edd02a94058429ce50e63a92d620951fb25eda2",
"fee_recipient": "0x0000000000000000000000000000000000000000",
"state_root": "0x96a83e9ddf745346fafcb0b03d57314623df669ed543c110662b21302a0fae8b",
"receipts_root": "0xdccdfceea05036f7b61dcdabadc937945d31e68a8d3dfd4dc85684457988c284",
"logs_bloom": "0x00000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000080000000400000000000000000000004000000000080000000000000000000000000000000000010100000000000000000000000000000000020000000000000000000000000000000000080000000000000000000000000000040004000000000000002002002000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000080000000000000000000000000000000000100000000000000000200000200000010",
"prev_randao": "0x62e309d4f5119d1f5c783abc20fc1a549efbab546d8d0b25ff1cfd58be524e67",
"block_number": 393,
"gas_limit": 54492273,
"gas_used": 199644,
"timestamp": 1710552813,
"extra_data": "0xd983010d0b846765746888676f312e32312e368664617277696e",
"base_fee_per_gas": 7,
"block_hash": "0x6a9810efb9581d30c1a5c9074f27c68ea779a8c1ae31c213241df16225f4e131",
"transactions_root": "0x2cfa6ed7327e8807c7973516c5c32a68ef2459e586e8067e113d081c3bd8c07d",
"withdrawals_root": "0x792930bbd5baac43bcc798ee49aa8185ef76bb3b44ba62b91d86ae569e4bb535",
"blob_gas_used": 0,
"excess_blob_gas": 0
}
},
"execution_branch": [
"0xa6833fa629f3286b6916c6e50b8bf089fc9126bee6f64d0413b4e59c1265834d",
"0xb46f0c01805fe212e15907981b757e6c496b0cb06664224655613dcec82505bb",
"0xdb56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71",
"0xd3af7c05c516726be7505239e0b9c7cb53d24abce6b91cdb3b3995f0164a75da"
]
}
Loading
Loading