2024-03-06 10:50:22 -06:00
|
|
|
// Copyright 2023 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package blsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/light/request"
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/light/sync"
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/lru"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// beaconBlockSync implements request.Module; it fetches the beacon blocks belonging
|
|
|
|
// to the validated and prefetch heads.
|
|
|
|
type beaconBlockSync struct {
|
2024-03-20 13:22:44 -05:00
|
|
|
recentBlocks *lru.Cache[common.Hash, *types.BeaconBlock]
|
2024-03-06 10:50:22 -06:00
|
|
|
locked map[common.Hash]request.ServerAndID
|
|
|
|
serverHeads map[request.Server]common.Hash
|
|
|
|
headTracker headTracker
|
|
|
|
|
|
|
|
lastHeadInfo types.HeadInfo
|
2024-03-20 13:22:44 -05:00
|
|
|
chainHeadFeed event.FeedOf[types.ChainHeadEvent]
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type headTracker interface {
|
|
|
|
PrefetchHead() types.HeadInfo
|
|
|
|
ValidatedHead() (types.SignedHeader, bool)
|
|
|
|
ValidatedFinality() (types.FinalityUpdate, bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBeaconBlockSync returns a new beaconBlockSync.
|
2024-03-20 13:22:44 -05:00
|
|
|
func newBeaconBlockSync(headTracker headTracker) *beaconBlockSync {
|
2024-03-06 10:50:22 -06:00
|
|
|
return &beaconBlockSync{
|
2024-03-20 13:22:44 -05:00
|
|
|
headTracker: headTracker,
|
|
|
|
recentBlocks: lru.NewCache[common.Hash, *types.BeaconBlock](10),
|
|
|
|
locked: make(map[common.Hash]request.ServerAndID),
|
|
|
|
serverHeads: make(map[request.Server]common.Hash),
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 13:22:44 -05:00
|
|
|
func (s *beaconBlockSync) SubscribeChainHead(ch chan<- types.ChainHeadEvent) event.Subscription {
|
|
|
|
return s.chainHeadFeed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
2024-03-06 10:50:22 -06:00
|
|
|
// Process implements request.Module.
|
|
|
|
func (s *beaconBlockSync) Process(requester request.Requester, events []request.Event) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch event.Type {
|
|
|
|
case request.EvResponse, request.EvFail, request.EvTimeout:
|
|
|
|
sid, req, resp := event.RequestInfo()
|
|
|
|
blockRoot := common.Hash(req.(sync.ReqBeaconBlock))
|
|
|
|
if resp != nil {
|
2024-03-20 13:22:44 -05:00
|
|
|
s.recentBlocks.Add(blockRoot, resp.(*types.BeaconBlock))
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
if s.locked[blockRoot] == sid {
|
|
|
|
delete(s.locked, blockRoot)
|
|
|
|
}
|
|
|
|
case sync.EvNewHead:
|
|
|
|
s.serverHeads[event.Server] = event.Data.(types.HeadInfo).BlockRoot
|
|
|
|
case request.EvUnregistered:
|
|
|
|
delete(s.serverHeads, event.Server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.updateEventFeed()
|
|
|
|
// request validated head block if unavailable and not yet requested
|
|
|
|
if vh, ok := s.headTracker.ValidatedHead(); ok {
|
|
|
|
s.tryRequestBlock(requester, vh.Header.Hash(), false)
|
|
|
|
}
|
|
|
|
// request prefetch head if the given server has announced it
|
|
|
|
if prefetchHead := s.headTracker.PrefetchHead().BlockRoot; prefetchHead != (common.Hash{}) {
|
|
|
|
s.tryRequestBlock(requester, prefetchHead, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *beaconBlockSync) tryRequestBlock(requester request.Requester, blockRoot common.Hash, needSameHead bool) {
|
|
|
|
if _, ok := s.recentBlocks.Get(blockRoot); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := s.locked[blockRoot]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, server := range requester.CanSendTo() {
|
|
|
|
if needSameHead && (s.serverHeads[server] != blockRoot) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
id := requester.Send(server, sync.ReqBeaconBlock(blockRoot))
|
|
|
|
s.locked[blockRoot] = request.ServerAndID{Server: server, ID: id}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 13:22:44 -05:00
|
|
|
func blockHeadInfo(block *types.BeaconBlock) types.HeadInfo {
|
2024-03-06 10:50:22 -06:00
|
|
|
if block == nil {
|
|
|
|
return types.HeadInfo{}
|
|
|
|
}
|
2024-03-20 13:22:44 -05:00
|
|
|
return types.HeadInfo{Slot: block.Slot(), BlockRoot: block.Root()}
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *beaconBlockSync) updateEventFeed() {
|
|
|
|
head, ok := s.headTracker.ValidatedHead()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
finality, ok := s.headTracker.ValidatedFinality() //TODO fetch directly if subscription does not deliver
|
|
|
|
if !ok || head.Header.Epoch() != finality.Attested.Header.Epoch() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
validatedHead := head.Header.Hash()
|
|
|
|
headBlock, ok := s.recentBlocks.Get(validatedHead)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
headInfo := blockHeadInfo(headBlock)
|
|
|
|
if headInfo == s.lastHeadInfo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.lastHeadInfo = headInfo
|
2024-03-20 13:22:44 -05:00
|
|
|
|
2024-03-06 10:50:22 -06:00
|
|
|
// new head block and finality info available; extract executable data and send event to feed
|
2024-03-20 13:22:44 -05:00
|
|
|
execBlock, err := headBlock.ExecutionPayload()
|
2024-03-06 10:50:22 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error extracting execution block from validated beacon block", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.chainHeadFeed.Send(types.ChainHeadEvent{
|
2024-03-20 13:22:44 -05:00
|
|
|
BeaconHead: head.Header,
|
|
|
|
Block: execBlock,
|
|
|
|
Finalized: finality.Finalized.PayloadHeader.BlockHash(),
|
2024-03-06 10:50:22 -06:00
|
|
|
})
|
|
|
|
}
|