-
Notifications
You must be signed in to change notification settings - Fork 105
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
Changes from all commits
387e085
846065a
65272d4
d2f0d66
63b0f07
faf5d01
c179311
d728f48
28f23dd
cce7714
48cc44f
fe1e191
d37a736
1aa5a0a
545e267
1c5b33d
647f57e
40daff1
565cd83
7414746
70d017b
585dc78
ba67bf3
f9f7458
d542799
904e97a
99c0304
8a50ced
e5c2381
29a2afe
3f6cb38
4aae19a
79e79dd
ed69a35
1c1ca20
8f95e8a
ba5957e
9673b21
e767e6e
e5120e8
6a38f79
c246f70
6892678
b76a4dd
bdec747
99f5a6d
3580c4d
d1a5b86
ad65ebd
53e4fa6
d069f97
da3d338
58a28bd
61dadc9
328d541
9d633e2
d37ee40
d4bc62b
0c14e49
0ed243a
810fa47
53c6fb8
4cdfa96
b5ea6b2
6bd6723
b892f77
a646a4e
ee71d1a
79247a2
49800bd
cdc7165
072c3f0
c42d0af
5bc2526
f465995
dd866bb
afe2b43
018c565
775e56a
3f0f027
1d67967
7b5e1e3
e8557c7
af93289
888ce4d
5213ba8
ddf7e6a
cd85cb9
03bdb5c
ef8c27a
ab191f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ contracts/beefy-state.json | |
|
||
go/ | ||
gocache/ | ||
go.work* | ||
control/target/ |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 { | ||||||
|
@@ -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) { | ||||||
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 { | ||||||
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. Should rather use 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. It's to backward to find check point(slot<beaconState.BeaconSlot<slot+8192) For the first check For the second check |
||||||
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) | ||||||
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.
Suggested change
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. |
||||||
} |
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.