2022-11-25 15:38:22 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2023-04-17 18:06:50 +00:00
|
|
|
"time"
|
2022-11-25 15:38:22 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2022-11-25 15:38:22 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Input: the currently highest slot processed and the list of blocks we want to know process
|
|
|
|
// Output: the new last new highest slot processed and an error possibly?
|
|
|
|
type ProcessFn func(
|
|
|
|
highestSlotProcessed uint64,
|
2023-01-13 18:12:18 +00:00
|
|
|
highestBlockRootProcessed libcommon.Hash,
|
2022-12-25 17:07:12 +00:00
|
|
|
blocks []*cltypes.SignedBeaconBlock) (
|
2022-12-03 02:16:26 +00:00
|
|
|
newHighestSlotProcessed uint64,
|
2023-01-13 18:12:18 +00:00
|
|
|
newHighestBlockRootProcessed libcommon.Hash,
|
2022-12-03 02:16:26 +00:00
|
|
|
err error)
|
2022-11-25 15:38:22 +00:00
|
|
|
|
|
|
|
type ForwardBeaconDownloader struct {
|
2022-12-03 02:16:26 +00:00
|
|
|
ctx context.Context
|
|
|
|
highestSlotProcessed uint64
|
2023-01-13 18:12:18 +00:00
|
|
|
highestBlockRootProcessed libcommon.Hash
|
2022-12-23 21:31:08 +00:00
|
|
|
rpc *rpc.BeaconRpcP2P
|
2022-12-03 02:16:26 +00:00
|
|
|
process ProcessFn
|
2022-11-25 15:38:22 +00:00
|
|
|
|
2023-04-30 20:24:42 +00:00
|
|
|
mu sync.Mutex
|
2022-11-25 15:38:22 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 21:31:08 +00:00
|
|
|
func NewForwardBeaconDownloader(ctx context.Context, rpc *rpc.BeaconRpcP2P) *ForwardBeaconDownloader {
|
2022-11-25 15:38:22 +00:00
|
|
|
return &ForwardBeaconDownloader{
|
2023-04-30 20:24:42 +00:00
|
|
|
ctx: ctx,
|
|
|
|
rpc: rpc,
|
2022-11-25 15:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetProcessFunction sets the function used to process segments.
|
|
|
|
func (f *ForwardBeaconDownloader) SetProcessFunction(fn ProcessFn) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
f.process = fn
|
|
|
|
}
|
|
|
|
|
2022-12-07 20:26:45 +00:00
|
|
|
// SetHighestProcessedSlot sets the highest processed slot so far.
|
|
|
|
func (f *ForwardBeaconDownloader) SetHighestProcessedSlot(highestSlotProcessed uint64) {
|
2022-11-25 15:38:22 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
f.highestSlotProcessed = highestSlotProcessed
|
|
|
|
}
|
|
|
|
|
2022-12-07 20:26:45 +00:00
|
|
|
// SetHighestProcessedRoot sets the highest processed block root so far.
|
2023-01-13 18:12:18 +00:00
|
|
|
func (f *ForwardBeaconDownloader) SetHighestProcessedRoot(root libcommon.Hash) {
|
2022-12-07 20:26:45 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
f.highestBlockRootProcessed = root
|
|
|
|
}
|
|
|
|
|
|
|
|
// HighestProcessedRoot returns the highest processed block root so far.
|
2023-01-13 18:12:18 +00:00
|
|
|
func (f *ForwardBeaconDownloader) HighestProcessedRoot() libcommon.Hash {
|
2022-12-07 20:26:45 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
return f.highestBlockRootProcessed
|
|
|
|
}
|
|
|
|
|
2022-12-03 02:16:26 +00:00
|
|
|
func (f *ForwardBeaconDownloader) RequestMore() {
|
2023-04-17 18:06:50 +00:00
|
|
|
count := uint64(4) // dont need many
|
2023-04-29 19:32:33 +00:00
|
|
|
responses, pid, err := f.rpc.SendBeaconBlocksByRangeReq(f.highestSlotProcessed+1, count)
|
2022-12-19 21:40:34 +00:00
|
|
|
if err != nil {
|
2023-04-29 19:32:33 +00:00
|
|
|
f.rpc.BanPeer(pid)
|
2023-04-17 18:06:50 +00:00
|
|
|
// Wait a bit in this case (we do not need to be super performant here).
|
|
|
|
time.Sleep(time.Second)
|
2022-12-19 21:40:34 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-25 15:38:22 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
2023-04-30 20:24:42 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
var highestBlockRootProcessed libcommon.Hash
|
2023-04-30 20:24:42 +00:00
|
|
|
var highestSlotProcessed uint64
|
|
|
|
if highestSlotProcessed, highestBlockRootProcessed, err = f.process(f.highestSlotProcessed, f.highestBlockRootProcessed, responses); err != nil {
|
|
|
|
f.rpc.BanPeer(pid)
|
|
|
|
return
|
2022-11-25 15:38:22 +00:00
|
|
|
}
|
|
|
|
f.highestSlotProcessed = highestSlotProcessed
|
2022-12-03 02:16:26 +00:00
|
|
|
f.highestBlockRootProcessed = highestBlockRootProcessed
|
2022-11-25 15:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHighestProcessedSlot retrieve the highest processed slot we accumulated.
|
|
|
|
func (f *ForwardBeaconDownloader) GetHighestProcessedSlot() uint64 {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
return f.highestSlotProcessed
|
|
|
|
}
|
2023-04-17 18:06:50 +00:00
|
|
|
|
|
|
|
func (f *ForwardBeaconDownloader) Peers() (uint64, error) {
|
|
|
|
return f.rpc.Peers()
|
|
|
|
}
|