2024-03-06 19:50:22 +03: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"
|
2024-04-17 18:07:28 +03:00
|
|
|
"github.com/ethereum/go-ethereum/beacon/params"
|
2024-03-06 19:50:22 +03:00
|
|
|
"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 21:22:44 +03:00
|
|
|
recentBlocks *lru.Cache[common.Hash, *types.BeaconBlock]
|
2024-03-06 19:50:22 +03:00
|
|
|
locked map[common.Hash]request.ServerAndID
|
|
|
|
serverHeads map[request.Server]common.Hash
|
|
|
|
headTracker headTracker
|
|
|
|
|
|
|
|
lastHeadInfo types.HeadInfo
|
2024-03-20 21:22:44 +03:00
|
|
|
chainHeadFeed event.FeedOf[types.ChainHeadEvent]
|
2024-03-06 19:50:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type headTracker interface {
|
|
|
|
PrefetchHead() types.HeadInfo
|
2024-04-23 14:31:32 +03:00
|
|
|
ValidatedOptimistic() (types.OptimisticUpdate, bool)
|
2024-03-06 19:50:22 +03:00
|
|
|
ValidatedFinality() (types.FinalityUpdate, bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBeaconBlockSync returns a new beaconBlockSync.
|
2024-03-20 21:22:44 +03:00
|
|
|
func newBeaconBlockSync(headTracker headTracker) *beaconBlockSync {
|
2024-03-06 19:50:22 +03:00
|
|
|
return &beaconBlockSync{
|
2024-03-20 21:22:44 +03: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 19:50:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 21:22:44 +03:00
|
|
|
func (s *beaconBlockSync) SubscribeChainHead(ch chan<- types.ChainHeadEvent) event.Subscription {
|
|
|
|
return s.chainHeadFeed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
2024-03-06 19:50:22 +03: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))
|
2024-04-23 14:31:32 +03:00
|
|
|
log.Debug("Beacon block event", "type", event.Type.Name, "hash", blockRoot)
|
2024-03-06 19:50:22 +03:00
|
|
|
if resp != nil {
|
2024-03-20 21:22:44 +03:00
|
|
|
s.recentBlocks.Add(blockRoot, resp.(*types.BeaconBlock))
|
2024-03-06 19:50:22 +03: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
|
2024-04-23 14:31:32 +03:00
|
|
|
if vh, ok := s.headTracker.ValidatedOptimistic(); ok {
|
|
|
|
s.tryRequestBlock(requester, vh.Attested.Hash(), false)
|
2024-03-06 19:50:22 +03:00
|
|
|
}
|
|
|
|
// 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 21:22:44 +03:00
|
|
|
func blockHeadInfo(block *types.BeaconBlock) types.HeadInfo {
|
2024-03-06 19:50:22 +03:00
|
|
|
if block == nil {
|
|
|
|
return types.HeadInfo{}
|
|
|
|
}
|
2024-03-20 21:22:44 +03:00
|
|
|
return types.HeadInfo{Slot: block.Slot(), BlockRoot: block.Root()}
|
2024-03-06 19:50:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *beaconBlockSync) updateEventFeed() {
|
2024-04-23 14:31:32 +03:00
|
|
|
optimistic, ok := s.headTracker.ValidatedOptimistic()
|
2024-03-06 19:50:22 +03:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2024-04-17 18:07:28 +03:00
|
|
|
|
2024-04-23 14:31:32 +03:00
|
|
|
validatedHead := optimistic.Attested.Hash()
|
2024-03-06 19:50:22 +03:00
|
|
|
headBlock, ok := s.recentBlocks.Get(validatedHead)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2024-04-17 18:07:28 +03:00
|
|
|
|
|
|
|
var finalizedHash common.Hash
|
|
|
|
if finality, ok := s.headTracker.ValidatedFinality(); ok {
|
2024-04-23 14:31:32 +03:00
|
|
|
he := optimistic.Attested.Epoch()
|
2024-04-17 18:07:28 +03:00
|
|
|
fe := finality.Attested.Header.Epoch()
|
|
|
|
switch {
|
|
|
|
case he == fe:
|
|
|
|
finalizedHash = finality.Finalized.PayloadHeader.BlockHash()
|
|
|
|
case he < fe:
|
|
|
|
return
|
|
|
|
case he == fe+1:
|
2024-04-23 14:31:32 +03:00
|
|
|
parent, ok := s.recentBlocks.Get(optimistic.Attested.ParentRoot)
|
2024-04-17 18:07:28 +03:00
|
|
|
if !ok || parent.Slot()/params.EpochLength == fe {
|
|
|
|
return // head is at first slot of next epoch, wait for finality update
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 19:50:22 +03:00
|
|
|
headInfo := blockHeadInfo(headBlock)
|
|
|
|
if headInfo == s.lastHeadInfo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.lastHeadInfo = headInfo
|
2024-03-20 21:22:44 +03:00
|
|
|
|
2024-03-06 19:50:22 +03:00
|
|
|
// new head block and finality info available; extract executable data and send event to feed
|
2024-03-20 21:22:44 +03:00
|
|
|
execBlock, err := headBlock.ExecutionPayload()
|
2024-03-06 19:50:22 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error extracting execution block from validated beacon block", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.chainHeadFeed.Send(types.ChainHeadEvent{
|
2024-04-23 14:31:32 +03:00
|
|
|
BeaconHead: optimistic.Attested.Header,
|
2024-03-20 21:22:44 +03:00
|
|
|
Block: execBlock,
|
2024-04-17 18:07:28 +03:00
|
|
|
Finalized: finalizedHash,
|
2024-03-06 19:50:22 +03:00
|
|
|
})
|
|
|
|
}
|