beacon/blsync: upgrade block sync to deneb

This commit is contained in:
Felix Lange 2024-03-06 18:41:13 +01:00
parent ba57045aa2
commit e3029e3c05
2 changed files with 18 additions and 15 deletions

View file

@ -31,15 +31,17 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/holiman/uint256" "github.com/holiman/uint256"
"github.com/protolambda/zrnt/eth2/beacon/capella" "github.com/protolambda/zrnt/eth2/beacon/deneb"
"github.com/protolambda/zrnt/eth2/configs" "github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/tree" "github.com/protolambda/ztyp/tree"
) )
type beaconBlockType = deneb.BeaconBlock
// beaconBlockSync implements request.Module; it fetches the beacon blocks belonging // beaconBlockSync implements request.Module; it fetches the beacon blocks belonging
// to the validated and prefetch heads. // to the validated and prefetch heads.
type beaconBlockSync struct { type beaconBlockSync struct {
recentBlocks *lru.Cache[common.Hash, *capella.BeaconBlock] recentBlocks *lru.Cache[common.Hash, *beaconBlockType]
locked map[common.Hash]request.ServerAndID locked map[common.Hash]request.ServerAndID
serverHeads map[request.Server]common.Hash serverHeads map[request.Server]common.Hash
headTracker headTracker headTracker headTracker
@ -59,7 +61,7 @@ func newBeaconBlockSync(headTracker headTracker, chainHeadFeed *event.Feed) *bea
return &beaconBlockSync{ return &beaconBlockSync{
headTracker: headTracker, headTracker: headTracker,
chainHeadFeed: chainHeadFeed, chainHeadFeed: chainHeadFeed,
recentBlocks: lru.NewCache[common.Hash, *capella.BeaconBlock](10), recentBlocks: lru.NewCache[common.Hash, *beaconBlockType](10),
locked: make(map[common.Hash]request.ServerAndID), locked: make(map[common.Hash]request.ServerAndID),
serverHeads: make(map[request.Server]common.Hash), serverHeads: make(map[request.Server]common.Hash),
} }
@ -73,7 +75,7 @@ func (s *beaconBlockSync) Process(requester request.Requester, events []request.
sid, req, resp := event.RequestInfo() sid, req, resp := event.RequestInfo()
blockRoot := common.Hash(req.(sync.ReqBeaconBlock)) blockRoot := common.Hash(req.(sync.ReqBeaconBlock))
if resp != nil { if resp != nil {
s.recentBlocks.Add(blockRoot, resp.(*capella.BeaconBlock)) s.recentBlocks.Add(blockRoot, resp.(*beaconBlockType))
} }
if s.locked[blockRoot] == sid { if s.locked[blockRoot] == sid {
delete(s.locked, blockRoot) delete(s.locked, blockRoot)
@ -112,7 +114,7 @@ func (s *beaconBlockSync) tryRequestBlock(requester request.Requester, blockRoot
} }
} }
func blockHeadInfo(block *capella.BeaconBlock) types.HeadInfo { func blockHeadInfo(block *beaconBlockType) types.HeadInfo {
if block == nil { if block == nil {
return types.HeadInfo{} return types.HeadInfo{}
} }
@ -120,12 +122,12 @@ func blockHeadInfo(block *capella.BeaconBlock) types.HeadInfo {
} }
// beaconBlockHash calculates the hash of a beacon block. // beaconBlockHash calculates the hash of a beacon block.
func beaconBlockHash(beaconBlock *capella.BeaconBlock) common.Hash { func beaconBlockHash(beaconBlock *beaconBlockType) common.Hash {
return common.Hash(beaconBlock.HashTreeRoot(configs.Mainnet, tree.GetHashFn())) return common.Hash(beaconBlock.HashTreeRoot(configs.Mainnet, tree.GetHashFn()))
} }
// getExecBlock extracts the execution block from the beacon block's payload. // getExecBlock extracts the execution block from the beacon block's payload.
func getExecBlock(beaconBlock *capella.BeaconBlock) (*ctypes.Block, error) { func getExecBlock(beaconBlock *beaconBlockType) (*ctypes.Block, error) {
payload := &beaconBlock.Body.ExecutionPayload payload := &beaconBlock.Body.ExecutionPayload
txs := make([]*ctypes.Transaction, len(payload.Transactions)) txs := make([]*ctypes.Transaction, len(payload.Transactions))
for i, opaqueTx := range payload.Transactions { for i, opaqueTx := range payload.Transactions {

View file

@ -25,6 +25,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/protolambda/zrnt/eth2/beacon/capella" "github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/deneb"
"github.com/protolambda/zrnt/eth2/configs" "github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/tree" "github.com/protolambda/ztyp/tree"
) )
@ -33,16 +34,16 @@ var (
testServer1 = "testServer1" testServer1 = "testServer1"
testServer2 = "testServer2" testServer2 = "testServer2"
testBlock1 = &capella.BeaconBlock{ testBlock1 = &beaconBlockType{
Slot: 123, Slot: 123,
Body: capella.BeaconBlockBody{ Body: deneb.BeaconBlockBody{
ExecutionPayload: capella.ExecutionPayload{BlockNumber: 456}, ExecutionPayload: deneb.ExecutionPayload{BlockNumber: 456},
}, },
} }
testBlock2 = &capella.BeaconBlock{ testBlock2 = &beaconBlockType{
Slot: 124, Slot: 124,
Body: capella.BeaconBlockBody{ Body: deneb.BeaconBlockBody{
ExecutionPayload: capella.ExecutionPayload{BlockNumber: 457}, ExecutionPayload: deneb.ExecutionPayload{BlockNumber: 457},
}, },
} }
) )
@ -64,7 +65,7 @@ func TestBlockSync(t *testing.T) {
ts.AddServer(testServer1, 1) ts.AddServer(testServer1, 1)
ts.AddServer(testServer2, 1) ts.AddServer(testServer2, 1)
expHeadBlock := func(tci int, expHead *capella.BeaconBlock) { expHeadBlock := func(tci int, expHead *beaconBlockType) {
var expNumber, headNumber uint64 var expNumber, headNumber uint64
if expHead != nil { if expHead != nil {
expNumber = uint64(expHead.Body.ExecutionPayload.BlockNumber) expNumber = uint64(expHead.Body.ExecutionPayload.BlockNumber)
@ -126,7 +127,7 @@ func TestBlockSync(t *testing.T) {
expHeadBlock(5, testBlock2) expHeadBlock(5, testBlock2)
} }
func blockHeader(block *capella.BeaconBlock) types.Header { func blockHeader(block *beaconBlockType) types.Header {
return types.Header{ return types.Header{
Slot: uint64(block.Slot), Slot: uint64(block.Slot),
ProposerIndex: uint64(block.ProposerIndex), ProposerIndex: uint64(block.ProposerIndex),