-
Notifications
You must be signed in to change notification settings - Fork 107
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
Limit finalized header gap #1156
Merged
Merged
Changes from 26 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
024159d
construct finalized update
42b62c8
extract interfaces for tests
e3e1247
mock data and tests
8ccbc4f
adds db store
9ba62db
Merge branch 'main' into limit-finalized-headers-gap
9e1582c
completes db store
a5d88bb
test works
98a713d
cleanup and fixes
106e9ce
cleanup and fixes
b66be24
tests
a237b1b
relayer progress
04a5213
finish tests
ebeb5e3
formatting
dd46c65
move the interim finalized header check
c0b3f11
fix ci
4d9ee30
fix ci
82e37e9
adds working dir
bd632f5
go mod download
82ffe26
version
9271dfc
wrap go version
e9d254d
update
e15e4bd
add gopath to bin
2c994cc
install sszgen
d118781
missing go install
46e2ebe
remove build
5431c57
skip utility tests
8673521
Merge branch 'main' into limit-finalized-headers-gap
ebf100a
remove unused file
0da2b16
remove test line
b5334dc
rename method
de118ca
refactor method
9397587
Fix compile error and breaking tests
yrong c428bff
clean up imports
9669aab
Merge remote-tracking branch 'origin/limit-finalized-headers-gap' int…
862c6dc
fix duplicate import
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: relayer | ||
|
||
on: | ||
push: | ||
paths: | ||
- "relayer/**" | ||
branches: | ||
- main | ||
pull_request: | ||
paths: | ||
- "relayer/**" | ||
|
||
jobs: | ||
build: | ||
runs-on: snowbridge-runner | ||
timeout-minutes: 15 | ||
steps: | ||
- uses: actions/checkout@v1 | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: setup go | ||
uses: actions/checkout@v4 | ||
with: | ||
go-version: '^1.20.1' | ||
|
||
- name: check go version | ||
run: go version | ||
|
||
- name: install dependencies | ||
working-directory: relayer | ||
run: go mod download | ||
|
||
- name: Add gopath to bin | ||
run: echo "$HOME/go/bin" >> $GITHUB_PATH | ||
|
||
- name: test | ||
working-directory: relayer | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,30 @@ import ( | |
"fmt" | ||
"sync" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/snowfork/go-substrate-rpc-client/v4/rpc/author" | ||
"github.com/snowfork/go-substrate-rpc-client/v4/types" | ||
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale" | ||
"github.com/snowfork/snowbridge/relayer/relays/beacon/state" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
log "github.com/sirupsen/logrus" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type ChainWriter interface { | ||
BatchCall(ctx context.Context, extrinsic string, calls []interface{}) error | ||
WriteToParachainAndRateLimit(ctx context.Context, extrinsicName string, payload ...interface{}) error | ||
WriteToParachainAndWatch(ctx context.Context, extrinsicName string, payload ...interface{}) error | ||
GetLastFinalizedHeaderState() (state.FinalizedHeader, error) | ||
GetFinalizedStateByStorageKey(key string) (scale.BeaconState, error) | ||
GetLastBasicChannelBlockNumber() (uint64, error) | ||
GetLastBasicChannelNonceByAddress(address common.Address) (uint64, error) | ||
GetFinalizedHeaderStateByBlockRoot(blockRoot types.H256) (state.FinalizedHeader, error) | ||
GetCompactExecutionHeaderStateByBlockHash(blockHash types.H256) (state.CompactExecutionHeaderState, error) | ||
GetLastFinalizedStateIndex() (types.U32, error) | ||
GetFinalizedBeaconRootByIndex(index uint32) (types.H256, error) | ||
} | ||
|
||
type ParachainWriter struct { | ||
conn *Connection | ||
nonce uint32 | ||
|
@@ -133,105 +148,6 @@ func (wr *ParachainWriter) WriteToParachainAndWatch(ctx context.Context, extrins | |
} | ||
} | ||
|
||
func (wr *ParachainWriter) writeToParachain(ctx context.Context, extrinsicName string, payload ...interface{}) (*author.ExtrinsicStatusSubscription, error) { | ||
extI, err := wr.prepExtrinstic(ctx, extrinsicName, payload...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sub, err := wr.conn.API().RPC.Author.SubmitAndWatchExtrinsic(*extI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return sub, nil | ||
} | ||
|
||
func (wr *ParachainWriter) queryAccountNonce() (uint32, error) { | ||
key, err := types.CreateStorageKey(wr.conn.Metadata(), "System", "Account", wr.conn.Keypair().PublicKey, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var accountInfo types.AccountInfo | ||
ok, err := wr.conn.API().RPC.State.GetStorageLatest(key, &accountInfo) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if !ok { | ||
return 0, fmt.Errorf("no account info found for %s", wr.conn.Keypair().URI) | ||
} | ||
|
||
return uint32(accountInfo.Nonce), nil | ||
} | ||
|
||
func (wr *ParachainWriter) prepExtrinstic(ctx context.Context, extrinsicName string, payload ...interface{}) (*types.Extrinsic, error) { | ||
meta, err := wr.conn.API().RPC.State.GetMetadataLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c, err := types.NewCall(meta, extrinsicName, payload...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
latestHash, err := wr.conn.API().RPC.Chain.GetFinalizedHead() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
latestBlock, err := wr.conn.API().RPC.Chain.GetBlock(latestHash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ext := types.NewExtrinsic(c) | ||
era := NewMortalEra(uint64(latestBlock.Block.Header.Number)) | ||
|
||
genesisHash, err := wr.conn.API().RPC.Chain.GetBlockHash(0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rv, err := wr.conn.API().RPC.State.GetRuntimeVersionLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
o := types.SignatureOptions{ | ||
BlockHash: latestHash, | ||
Era: era, | ||
GenesisHash: genesisHash, | ||
Nonce: types.NewUCompactFromUInt(uint64(wr.nonce)), | ||
SpecVersion: rv.SpecVersion, | ||
Tip: types.NewUCompactFromUInt(0), | ||
TransactionVersion: rv.TransactionVersion, | ||
} | ||
|
||
extI := ext | ||
|
||
err = extI.Sign(*wr.conn.Keypair(), o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &extI, nil | ||
} | ||
|
||
func (wr *ParachainWriter) prepCall(extrinsicName string, payload ...interface{}) (*types.Call, error) { | ||
meta, err := wr.conn.API().RPC.State.GetMetadataLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c, err := types.NewCall(meta, extrinsicName, payload...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &c, nil | ||
} | ||
|
||
func (wr *ParachainWriter) GetLastBasicChannelBlockNumber() (uint64, error) { | ||
return wr.getNumberFromParachain("EthereumInboundQueue", "LatestVerifiedBlockNumber") | ||
} | ||
|
@@ -331,6 +247,105 @@ func (wr *ParachainWriter) GetFinalizedHeaderStateByBlockRoot(blockRoot types.H2 | |
}, nil | ||
} | ||
|
||
func (wr *ParachainWriter) writeToParachain(ctx context.Context, extrinsicName string, payload ...interface{}) (*author.ExtrinsicStatusSubscription, error) { | ||
extI, err := wr.prepExtrinstic(ctx, extrinsicName, payload...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sub, err := wr.conn.API().RPC.Author.SubmitAndWatchExtrinsic(*extI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return sub, nil | ||
} | ||
|
||
func (wr *ParachainWriter) queryAccountNonce() (uint32, error) { | ||
key, err := types.CreateStorageKey(wr.conn.Metadata(), "System", "Account", wr.conn.Keypair().PublicKey, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var accountInfo types.AccountInfo | ||
ok, err := wr.conn.API().RPC.State.GetStorageLatest(key, &accountInfo) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if !ok { | ||
return 0, fmt.Errorf("no account info found for %s", wr.conn.Keypair().URI) | ||
} | ||
|
||
return uint32(accountInfo.Nonce), nil | ||
} | ||
|
||
func (wr *ParachainWriter) prepExtrinstic(ctx context.Context, extrinsicName string, payload ...interface{}) (*types.Extrinsic, error) { | ||
meta, err := wr.conn.API().RPC.State.GetMetadataLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c, err := types.NewCall(meta, extrinsicName, payload...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
latestHash, err := wr.conn.API().RPC.Chain.GetFinalizedHead() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
latestBlock, err := wr.conn.API().RPC.Chain.GetBlock(latestHash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ext := types.NewExtrinsic(c) | ||
era := NewMortalEra(uint64(latestBlock.Block.Header.Number)) | ||
|
||
genesisHash, err := wr.conn.API().RPC.Chain.GetBlockHash(0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rv, err := wr.conn.API().RPC.State.GetRuntimeVersionLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
o := types.SignatureOptions{ | ||
BlockHash: latestHash, | ||
Era: era, | ||
GenesisHash: genesisHash, | ||
Nonce: types.NewUCompactFromUInt(uint64(wr.nonce)), | ||
SpecVersion: rv.SpecVersion, | ||
Tip: types.NewUCompactFromUInt(0), | ||
TransactionVersion: rv.TransactionVersion, | ||
} | ||
|
||
extI := ext | ||
|
||
err = extI.Sign(*wr.conn.Keypair(), o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &extI, nil | ||
} | ||
|
||
func (wr *ParachainWriter) prepCall(extrinsicName string, payload ...interface{}) (*types.Call, error) { | ||
meta, err := wr.conn.API().RPC.State.GetMetadataLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c, err := types.NewCall(meta, extrinsicName, payload...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &c, nil | ||
} | ||
|
||
Comment on lines
+249
to
+347
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just reordered the private methods. |
||
func (wr *ParachainWriter) getHashFromParachain(pallet, storage string) (common.Hash, error) { | ||
key, err := types.CreateStorageKey(wr.conn.Metadata(), pallet, storage, nil, nil) | ||
if err != nil { | ||
|
@@ -415,36 +430,3 @@ func (wr *ParachainWriter) GetFinalizedBeaconRootByIndex(index uint32) (types.H2 | |
|
||
return beaconRoot, nil | ||
} | ||
|
||
func (wr *ParachainWriter) FindCheckPointBackward(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 { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract interfaces that we can mock out in tests.