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

Sync execution update on demand #1154

Merged
merged 91 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
387e085
Update for Dencun
yrong Sep 21, 2023
846065a
build geth&lodestar from source
yrong Sep 25, 2023
65272d4
Fix makeTrie
yrong Sep 25, 2023
d2f0d66
Merge branch 'main' into ron/dencun
yrong Nov 21, 2023
63b0f07
Update go ethereum to v1.13.5
yrong Nov 21, 2023
faf5d01
Update packages & Speedup e2e setup
yrong Nov 21, 2023
c179311
Update lodestar to v1.12.0
yrong Nov 21, 2023
d728f48
Remove unused
yrong Nov 21, 2023
28f23dd
Remove unrelated changes
yrong Nov 21, 2023
cce7714
Ignore build geth when binary exist
yrong Nov 21, 2023
48cc44f
Cleanup for deprecated ethashproof
yrong Nov 22, 2023
fe1e191
Update with VersionedExecutionPayloadHeader
yrong Nov 22, 2023
d37a736
Fix breaking tests
yrong Nov 22, 2023
1aa5a0a
Merge branch 'main' into ron/dencun
yrong Dec 1, 2023
545e267
Update node package
yrong Dec 1, 2023
1c5b33d
Update git modules
yrong Dec 1, 2023
647f57e
Sync packages with nix
yrong Dec 1, 2023
40daff1
Merge branch 'main' of https://github.com/Snowfork/snowbridge into ro…
yrong Dec 4, 2023
565cd83
Update sdk
yrong Dec 4, 2023
7414746
Update contract binding
yrong Dec 5, 2023
70d017b
More cleanup
yrong Dec 5, 2023
585dc78
For deneb(1)
yrong Dec 5, 2023
ba67bf3
For deneb(2)
yrong Dec 5, 2023
f9f7458
Update sdk
yrong Dec 5, 2023
d542799
For deneb(3)
yrong Dec 5, 2023
904e97a
Update sdk
yrong Dec 5, 2023
99c0304
For deneb(4)
yrong Dec 5, 2023
8a50ced
Enable deneb for local setup
yrong Dec 5, 2023
e5c2381
Fix tests
yrong Dec 5, 2023
29a2afe
Fix BeaconStateDeneb & more tests
yrong Dec 6, 2023
3f6cb38
Merge branch 'main' into ron/dencun
yrong Dec 6, 2023
4aae19a
Generate test fixture for Deneb and more tests accordingly
yrong Dec 7, 2023
79e79dd
Merge branch 'main' into ron/dencun
yrong Dec 7, 2023
ed69a35
Fix parse empty field
yrong Dec 7, 2023
1c1ca20
Setup for switchover test
yrong Dec 7, 2023
8f95e8a
Test workflow specific for deneb
yrong Dec 7, 2023
ba5957e
Ignore change detect building polkadot binary
yrong Dec 11, 2023
9673b21
Encode with error handling
yrong Dec 11, 2023
e767e6e
Fix format
yrong Dec 11, 2023
e5120e8
Merge branch 'main' into ron/dencun
yrong Dec 13, 2023
6a38f79
Update lodestar
yrong Dec 14, 2023
c246f70
Improve scripts setting up nodes for production
yrong Dec 14, 2023
6892678
Merge branch 'main' into ron/dencun
yrong Dec 27, 2023
b76a4dd
Sync execution update on demand
yrong Dec 31, 2023
bdec747
Merge branch 'main' into ron/dencun
yrong Jan 11, 2024
99f5a6d
Fix import
yrong Jan 11, 2024
3580c4d
Reorganize deneb primitives
yrong Jan 11, 2024
d1a5b86
Update go mod
yrong Jan 12, 2024
ad65ebd
Fix format
yrong Jan 12, 2024
53e4fa6
Remove obsolete
yrong Jan 12, 2024
d069f97
Fix import
yrong Jan 12, 2024
da3d338
Merge branch 'main' into ron/dencun
yrong Jan 12, 2024
58a28bd
Merge branch 'main' into ron/dencun
yrong Jan 17, 2024
61dadc9
Fix the deneb ForkVersion
yrong Jan 17, 2024
328d541
Upgrade geth and lodestar
yrong Jan 17, 2024
9d633e2
Merge branch 'ron/dencun' into ron/execution-update-on-demand
yrong Jan 18, 2024
d37ee40
Download geth release to replace the nix default
yrong Jan 18, 2024
d4bc62b
Naming consistently
yrong Jan 18, 2024
0c14e49
Update lodestar with the mock hack
yrong Jan 18, 2024
0ed243a
Fix generate mainnet fixture
yrong Jan 18, 2024
810fa47
Merge branch 'ron/dencun' into ron/execution-update-on-demand
yrong Jan 18, 2024
53c6fb8
Remove fixture unused
yrong Jan 18, 2024
4cdfa96
Backward compatible
yrong Jan 18, 2024
b5ea6b2
Check execution_branch
yrong Jan 18, 2024
6bd6723
Remove build geth
yrong Jan 18, 2024
b892f77
Remove cross check
yrong Jan 18, 2024
a646a4e
sync all lagging periods
yrong Jan 18, 2024
ee71d1a
Clean up
yrong Jan 18, 2024
79247a2
More refactoring
yrong Jan 18, 2024
49800bd
Cleanup scripts
yrong Jan 18, 2024
cdc7165
Update sdk
yrong Jan 18, 2024
072c3f0
Merge branch 'main' into ron/execution-update-on-demand
yrong Jan 19, 2024
c42d0af
Ignore go.work
yrong Jan 19, 2024
5bc2526
Rename function
yrong Jan 19, 2024
f465995
Merge branch 'main' into ron/execution-update-on-demand
yrong Jan 24, 2024
dd866bb
Fix test
yrong Jan 24, 2024
afe2b43
Fix populate checkpoint
yrong Jan 24, 2024
018c565
Fix smoke test
yrong Jan 24, 2024
775e56a
Merge branch 'main' into ron/execution-update-on-demand
yrong Jan 28, 2024
3f0f027
Merge branch 'main' into ron/execution-update-on-demand
yrong Jan 30, 2024
1d67967
Merge branch 'main' into ron/execution-update-on-demand
yrong Feb 6, 2024
7b5e1e3
Update lodestar & polkadot-sdk
yrong Feb 6, 2024
e8557c7
Merge branch 'main' into ron/execution-update-on-demand
yrong Mar 12, 2024
af93289
Check execution already exist before submit
yrong Mar 12, 2024
888ce4d
Update relayer/cmd/generate_beacon_data.go
yrong Mar 12, 2024
5213ba8
Update relayer/relays/beacon/header/header.go
yrong Mar 12, 2024
ddf7e6a
Rename to OptionalExecutionHeader
yrong Mar 12, 2024
cd85cb9
Rename import alias
yrong Mar 12, 2024
03bdb5c
Improve logs
yrong Mar 12, 2024
ef8c27a
Fetch checkpoint on chain from history
yrong Mar 12, 2024
ab191f9
Remove LastExecutionHeaderState & Remove execution header from update
yrong Mar 13, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ contracts/beefy-state.json

go/
gocache/
go.work*
control/target/
3 changes: 2 additions & 1 deletion relayer/chain/ethereum/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package ethereum

import (
"fmt"
"math/big"

etypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/snowfork/go-substrate-rpc-client/v4/scale"
types "github.com/snowfork/go-substrate-rpc-client/v4/types"
"math/big"
)

type HeaderID struct {
Expand Down
113 changes: 88 additions & 25 deletions relayer/chain/parachain/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,31 +266,6 @@ func (wr *ParachainWriter) GetLastBasicChannelNonceByAddress(address common.Addr
return uint64(nonce), nil
}

func (wr *ParachainWriter) GetLastExecutionHeaderState() (state.ExecutionHeader, error) {
key, err := types.CreateStorageKey(wr.conn.Metadata(), "EthereumBeaconClient", "LatestExecutionState", nil, nil)
if err != nil {
return state.ExecutionHeader{}, fmt.Errorf("create storage key for LatestExecutionHeaderState: %w", err)
}

var storageState struct {
BeaconBlockRoot types.H256
BeaconSlot types.U64
BlockHash types.H256
BlockNumber types.U64
}
_, err = wr.conn.API().RPC.State.GetStorageLatest(key, &storageState)
if err != nil {
return state.ExecutionHeader{}, fmt.Errorf("get storage for LatestExecutionHeaderState (err): %w", err)
}

return state.ExecutionHeader{
BeaconBlockRoot: common.Hash(storageState.BeaconBlockRoot),
BeaconSlot: uint64(storageState.BeaconSlot),
BlockHash: common.Hash(storageState.BlockHash),
BlockNumber: uint64(storageState.BlockNumber),
}, nil
}

func (wr *ParachainWriter) GetLastFinalizedHeaderState() (state.FinalizedHeader, error) {
finalizedState, err := wr.GetFinalizedStateByStorageKey("LatestFinalizedBlockRoot")
if err != nil {
Expand Down Expand Up @@ -385,3 +360,91 @@ func (wr *ParachainWriter) getNumberFromParachain(pallet, storage string) (uint6

return uint64(number), nil
}

func (wr *ParachainWriter) GetCompactExecutionHeaderStateByBlockHash(blockHash types.H256) (state.CompactExecutionHeaderState, error) {
var headerState state.CompactExecutionHeaderState
key, err := types.CreateStorageKey(wr.conn.Metadata(), "EthereumBeaconClient", "ExecutionHeaders", blockHash[:], nil)
if err != nil {
return headerState, fmt.Errorf("create storage key for ExecutionHeaders: %w", err)
}

var compactExecutionHeader scale.CompactExecutionHeader
_, err = wr.conn.API().RPC.State.GetStorageLatest(key, &compactExecutionHeader)
if err != nil {
return headerState, fmt.Errorf("get storage for ExecutionHeaders (err): %w", err)
}
headerState = state.CompactExecutionHeaderState{
ParentHash: common.Hash(compactExecutionHeader.ParentHash),
BlockNumber: uint64(compactExecutionHeader.BlockNumber.Int64()),
StateRoot: common.Hash(compactExecutionHeader.StateRoot),
ReceiptsRoot: common.Hash(compactExecutionHeader.ReceiptsRoot),
}
return headerState, nil
}

func (wr *ParachainWriter) GetLastFinalizedStateIndex() (types.U32, error) {
var index types.U32
key, err := types.CreateStorageKey(wr.conn.Metadata(), "EthereumBeaconClient", "FinalizedBeaconStateIndex", nil, nil)
if err != nil {
return index, fmt.Errorf("create storage key for FinalizedBeaconStateIndex: %w", err)
}

_, err = wr.conn.API().RPC.State.GetStorageLatest(key, &index)
if err != nil {
return index, fmt.Errorf("get storage for FinalizedBeaconStateIndex (err): %w", err)
}

return index, nil
}

func (wr *ParachainWriter) GetFinalizedBeaconRootByIndex(index uint32) (types.H256, error) {
var beaconRoot types.H256
encodedIndex, err := types.EncodeToBytes(types.NewU32(index))
if err != nil {
return beaconRoot, fmt.Errorf("get finalized beacon root encode index error: %w", err)
}
key, err := types.CreateStorageKey(wr.conn.Metadata(), "EthereumBeaconClient", "FinalizedBeaconStateMapping", encodedIndex, nil)
if err != nil {
return beaconRoot, fmt.Errorf("create storage key for FinalizedBeaconStateMapping: %w", err)
}

_, err = wr.conn.API().RPC.State.GetStorageLatest(key, &beaconRoot)
if err != nil {
return beaconRoot, fmt.Errorf("get storage for FinalizedBeaconStateMapping (err): %w", err)
}

return beaconRoot, nil
}

func (wr *ParachainWriter) FindCheckPointBackward(slot uint64) (state.FinalizedHeader, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (wr *ParachainWriter) FindCheckPointBackward(slot uint64) (state.FinalizedHeader, error) {
func (wr *ParachainWriter) FindCheckPoint(slot uint64) (state.FinalizedHeader, error) {

var beaconState state.FinalizedHeader
lastIndex, err := wr.GetLastFinalizedStateIndex()
if err != nil {
return beaconState, fmt.Errorf("GetLastFinalizedStateIndex error: %w", err)
}
startIndex := uint32(lastIndex)
endIndex := uint32(0)
if lastIndex > 256 {
endIndex = endIndex - 256
}
for index := startIndex; index >= endIndex; index-- {
beaconRoot, err := wr.GetFinalizedBeaconRootByIndex(index)
if err != nil {
return beaconState, fmt.Errorf("GetFinalizedBeaconRootByIndex %d, error: %w", index, err)
}
beaconState, err = wr.GetFinalizedHeaderStateByBlockRoot(beaconRoot)
if err != nil {
return beaconState, fmt.Errorf("GetFinalizedHeaderStateByBlockRoot %s, error: %w", beaconRoot.Hex(), err)
}
if beaconState.BeaconSlot < slot {
break
}
if beaconState.BeaconSlot > slot && beaconState.BeaconSlot < slot+8192 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should rather use SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD here. Please also add code comments explaining this line and the one above, it's not clear to me what these 2 checks check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's to backward to find check point(slot<beaconState.BeaconSlot<slot+8192)

For the first check beaconState.BeaconSlot < slot which means there is no valid checkpoint on chain so just exit the loop.

For the second check if beaconState.BeaconSlot > slot && beaconState.BeaconSlot < slot+8192 which means we find that so exit the loop.

break
}
}
if beaconState.BeaconSlot > slot && beaconState.BeaconSlot < slot+8192 {
return beaconState, nil
}
return beaconState, fmt.Errorf("Can't find checkpoint on chain for slot %d", slot)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return beaconState, fmt.Errorf("Can't find checkpoint on chain for slot %d", slot)
return beaconState, fmt.Errorf("find checkpoint on chain for slot %d", slot)

The error message is appended continuously, like here https://github.com/Snowfork/snowbridge/pull/1154/files#diff-92cc554d5b7218e7576839ca25a3b71ae0bbfa8200552beaf703a95a1a0da2a3R292, so simplify and lowercase the error message.

}
8 changes: 4 additions & 4 deletions relayer/cmd/generate_beacon_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func generateBeaconCheckpointCmd() *cobra.Command {
}

cmd.Flags().String("url", "http://127.0.0.1:9596", "Beacon URL")
cmd.Flags().Bool("export_json", false, "Export Json")
cmd.Flags().Bool("export-json", false, "Export Json")

return cmd
}
Expand Down Expand Up @@ -115,9 +115,9 @@ func generateBeaconCheckpoint(cmd *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("get initial sync: %w", err)
}
exportJson, err := cmd.Flags().GetBool("export_json")
exportJson, err := cmd.Flags().GetBool("export-json")
if err != nil {
return err
return fmt.Errorf("get export-json flag: %w", err)
}
if exportJson {
initialSync := checkPointScale.ToJSON()
Expand Down Expand Up @@ -282,7 +282,7 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
BlockRootsTree: finalizedUpdateAfterMessage.BlockRootsTree,
Slot: uint64(finalizedUpdateAfterMessage.Payload.FinalizedHeader.Slot),
}
headerUpdateScale, err := s.GetNextHeaderUpdateBySlotWithCheckpoint(beaconBlockSlot, &checkPoint)
headerUpdateScale, err := s.GetHeaderUpdateBySlotWithCheckpoint(beaconBlockSlot, &checkPoint)
if err != nil {
return fmt.Errorf("get header update: %w", err)
}
Expand Down
8 changes: 0 additions & 8 deletions relayer/relays/beacon/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ func (b *BeaconCache) SetLastSyncedFinalizedState(finalizedHeaderRoot common.Has
}
}

func (b *BeaconCache) SetLastSyncedExecutionSlot(slot uint64) {
b.mu.Lock()
defer b.mu.Unlock()
if slot > b.LastSyncedExecutionSlot {
b.LastSyncedExecutionSlot = slot
}
}

func (b *BeaconCache) SetInitialCheckpointSlot(slot uint64) {
b.mu.Lock()
defer b.mu.Unlock()
Expand Down
Loading