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/trie"
"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/ztyp/tree"
)
type beaconBlockType = deneb.BeaconBlock
// beaconBlockSync implements request.Module; it fetches the beacon blocks belonging
// to the validated and prefetch heads.
type beaconBlockSync struct {
recentBlocks *lru.Cache[common.Hash, *capella.BeaconBlock]
recentBlocks *lru.Cache[common.Hash, *beaconBlockType]
locked map[common.Hash]request.ServerAndID
serverHeads map[request.Server]common.Hash
headTracker headTracker
@ -59,7 +61,7 @@ func newBeaconBlockSync(headTracker headTracker, chainHeadFeed *event.Feed) *bea
return &beaconBlockSync{
headTracker: headTracker,
chainHeadFeed: chainHeadFeed,
recentBlocks: lru.NewCache[common.Hash, *capella.BeaconBlock](10),
recentBlocks: lru.NewCache[common.Hash, *beaconBlockType](10),
locked: make(map[common.Hash]request.ServerAndID),
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()
blockRoot := common.Hash(req.(sync.ReqBeaconBlock))
if resp != nil {
s.recentBlocks.Add(blockRoot, resp.(*capella.BeaconBlock))
s.recentBlocks.Add(blockRoot, resp.(*beaconBlockType))
}
if s.locked[blockRoot] == sid {
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 {
return types.HeadInfo{}
}
@ -120,12 +122,12 @@ func blockHeadInfo(block *capella.BeaconBlock) types.HeadInfo {
}
// 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()))
}
// 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
txs := make([]*ctypes.Transaction, len(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/event"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/deneb"
"github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/tree"
)
@ -33,16 +34,16 @@ var (
testServer1 = "testServer1"
testServer2 = "testServer2"
testBlock1 = &capella.BeaconBlock{
testBlock1 = &beaconBlockType{
Slot: 123,
Body: capella.BeaconBlockBody{
ExecutionPayload: capella.ExecutionPayload{BlockNumber: 456},
Body: deneb.BeaconBlockBody{
ExecutionPayload: deneb.ExecutionPayload{BlockNumber: 456},
},
}
testBlock2 = &capella.BeaconBlock{
testBlock2 = &beaconBlockType{
Slot: 124,
Body: capella.BeaconBlockBody{
ExecutionPayload: capella.ExecutionPayload{BlockNumber: 457},
Body: deneb.BeaconBlockBody{
ExecutionPayload: deneb.ExecutionPayload{BlockNumber: 457},
},
}
)
@ -64,7 +65,7 @@ func TestBlockSync(t *testing.T) {
ts.AddServer(testServer1, 1)
ts.AddServer(testServer2, 1)
expHeadBlock := func(tci int, expHead *capella.BeaconBlock) {
expHeadBlock := func(tci int, expHead *beaconBlockType) {
var expNumber, headNumber uint64
if expHead != nil {
expNumber = uint64(expHead.Body.ExecutionPayload.BlockNumber)
@ -126,7 +127,7 @@ func TestBlockSync(t *testing.T) {
expHeadBlock(5, testBlock2)
}
func blockHeader(block *capella.BeaconBlock) types.Header {
func blockHeader(block *beaconBlockType) types.Header {
return types.Header{
Slot: uint64(block.Slot),
ProposerIndex: uint64(block.ProposerIndex),