Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd, eth: implement full-sync tester #26035

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ var (
utils.TxPoolGlobalQueueFlag,
utils.TxPoolLifetimeFlag,
utils.SyncModeFlag,
utils.SyncTargetFlag,
utils.ExitWhenSyncedFlag,
utils.GCModeFlag,
utils.SnapshotFlag,
Expand Down
37 changes: 37 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package utils

import (
"bytes"
"context"
"crypto/ecdsa"
"errors"
Expand All @@ -36,10 +37,12 @@ import (
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
Expand Down Expand Up @@ -68,6 +71,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/p2p/netutil"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
pcsclite "github.com/gballet/go-libpcsclite"
gopsutil "github.com/shirou/gopsutil/mem"
Expand Down Expand Up @@ -664,11 +668,18 @@ var (
Category: flags.LoggingCategory,
}

// MISC settings
IgnoreLegacyReceiptsFlag = &cli.BoolFlag{
Name: "ignore-legacy-receipts",
Usage: "Geth will start up even if there are legacy receipts in freezer",
Category: flags.MiscCategory,
}
SyncTargetFlag = &cli.PathFlag{
Name: "synctarget",
Usage: `File for containing the hex-encoded block-rlp as sync target(dev feature)`,
TakesFile: true,
Category: flags.MiscCategory,
}

// RPC settings
IPCDisabledFlag = &cli.BoolFlag{
Expand Down Expand Up @@ -1874,6 +1885,25 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.EthDiscoveryURLs = SplitAndTrim(urls)
}
}
if ctx.IsSet(SyncTargetFlag.Name) {
path := ctx.Path(SyncTargetFlag.Name)
if path == "" {
Fatalf("Failed to resolve file path")
}
blob, err := os.ReadFile(path)
if err != nil {
Fatalf("Failed to read block file: %v", err)
}
rlpBlob, err := hexutil.Decode(string(bytes.TrimRight(blob, "\r\n")))
if err != nil {
Fatalf("Failed to decode block blob: %v", err)
}
var block types.Block
if err := rlp.DecodeBytes(rlpBlob, &block); err != nil {
Fatalf("Failed to decode block: %v", err)
}
cfg.SyncTarget = &block
}
// Override any default configs for hard coded networks.
switch {
case ctx.Bool(MainnetFlag.Name):
Expand Down Expand Up @@ -2027,6 +2057,13 @@ func RegisterEthService(stack *node.Node, cfg *ethconfig.Config) (ethapi.Backend
Fatalf("Failed to register the Engine API service: %v", err)
}
stack.RegisterAPIs(tracers.APIs(backend.APIBackend))

// Register the auxiliary full-sync tester service in case the sync
// target is configured.
if cfg.SyncTarget != nil && cfg.SyncMode == downloader.FullSync {
ethcatalyst.RegisterFullSyncTester(stack, backend, cfg.SyncTarget)
log.Info("Registered full-sync tester", "number", cfg.SyncTarget.NumberU64(), "hash", cfg.SyncTarget.Hash())
}
return backend.APIBackend, backend
}

Expand Down
100 changes: 100 additions & 0 deletions eth/catalyst/tester.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2022 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 catalyst

import (
"sync"
"time"

"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
)

// FullSyncTester is an auxiliary service that allows Geth to perform full sync
// alone without consensus-layer attached. Users must specify a valid block as
// the sync target. This tester can be applied to different networks, no matter
// it's pre-merge or post-merge, but only for full-sync.
type FullSyncTester struct {
api *ConsensusAPI
block *types.Block
closed chan struct{}
wg sync.WaitGroup
}

// RegisterFullSyncTester registers the full-sync tester service into the node
// stack for launching and stopping the service controlled by node.
func RegisterFullSyncTester(stack *node.Node, backend *eth.Ethereum, block *types.Block) (*FullSyncTester, error) {
cl := &FullSyncTester{
api: NewConsensusAPI(backend),
block: block,
closed: make(chan struct{}),
}
stack.RegisterLifecycle(cl)
return cl, nil
}

// Start launches the full-sync tester by spinning up a background thread
// for keeping firing NewPayload-UpdateForkChoice combos with the provided
// target block, it may or may not trigger the beacon sync depends on if
// there are protocol peers connected.
func (tester *FullSyncTester) Start() error {
tester.wg.Add(1)
go func() {
defer tester.wg.Done()

ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()

for {
select {
case <-ticker.C:
// Don't bother downloader in case it's already syncing.
if tester.api.eth.Downloader().Synchronising() {
continue
}
Comment on lines +69 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, despite this check here, I see this in the logs :

INFO [10-24|11:49:17.672] Syncing beacon headers                   downloaded=512 left=15,816,369 eta=1375h56m34.037s
INFO [10-24|11:49:22.324] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|11:49:25.352] Looking for peers                        peercount=1 tried=30 static=0
INFO [10-24|11:49:25.700] Syncing beacon headers                   downloaded=51712 left=15,765,169 eta=14h15m32.286s
INFO [10-24|11:49:27.324] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|11:49:32.323] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|11:49:33.746] Syncing beacon headers                   downloaded=110,592 left=15,706,289 eta=6h57m35.607s

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, it's snap sync? This thing shouldn't work in snap sync mode at all...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No ... well, I told it to do syncmode=full. As far as I know, it has not switched over to snap (but it started from an already initiated genesis)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so there is an issue that "reverse-header-sync" is not considered as SYNCING. We should fix it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, syncmode=full. While it has peers, and actively downloading headers from them, it still spits out this message:

WARN [10-24|12:46:03.514] Post-merge network, but no beacon client seen. Please launch one to follow the chain! 
INFO [10-24|12:46:03.520] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:08.520] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:08.881] Looking for peers                        peercount=0 tried=25 static=0
INFO [10-24|12:46:13.521] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:18.452] Syncing beacon headers                   downloaded=512 left=15,816,369 eta=385h33m11.723s
INFO [10-24|12:46:18.520] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:18.920] Looking for peers                        peercount=1 tried=30 static=0
INFO [10-24|12:46:23.520] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:26.458] Syncing beacon headers                   downloaded=32768 left=15,784,113 eta=7h4m59.726s
INFO [10-24|12:46:28.520] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:28.962] Looking for peers                        peercount=2 tried=30 static=0
INFO [10-24|12:46:33.524] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:34.550] Syncing beacon headers                   downloaded=69120 left=15,747,761 eta=3h51m44.555s
INFO [10-24|12:46:38.523] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:39.255] Looking for peers                        peercount=2 tried=37 static=0
INFO [10-24|12:46:42.582] Syncing beacon headers                   downloaded=124,416 left=15,692,465 eta=2h25m10.633s
INFO [10-24|12:46:43.520] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:48.519] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:50.218] Looking for peers                        peercount=2 tried=40 static=0
INFO [10-24|12:46:50.585] Syncing beacon headers                   downloaded=179,712 left=15,637,169 eta=1h51m45.556s
INFO [10-24|12:46:53.520] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:58.521] Forkchoice requested sync to new head    number=15,816,882 hash=23cd51..dff02b
INFO [10-24|12:46:58.652] Syncing beacon headers                   downloaded=235,008 left=15,581,873 eta=1h34m4.527s

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's because the status of downloader is still NON-SYNCING but actually it is. So tester keeps firing events non-stop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

// Short circuit in case the target block is already stored
// locally.
if tester.api.eth.BlockChain().HasBlock(tester.block.Hash(), tester.block.NumberU64()) {
log.Info("Full-sync target reached", "number", tester.block.NumberU64(), "hash", tester.block.Hash())
return
}
// Shoot out consensus events in order to trigger syncing.
data := beacon.BlockToExecutableData(tester.block)
tester.api.NewPayloadV1(*data)
tester.api.ForkchoiceUpdatedV1(beacon.ForkchoiceStateV1{
HeadBlockHash: tester.block.Hash(),
SafeBlockHash: tester.block.Hash(),
FinalizedBlockHash: tester.block.Hash(),
}, nil)
case <-tester.closed:
return
}
}
}()
return nil
}

// Stop stops the full-sync tester to stop all background activities.
// This function can only be called for one time.
func (tester *FullSyncTester) Stop() error {
close(tester.closed)
tester.wg.Wait()
return nil
}
5 changes: 5 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/gasprice"
"github.com/ethereum/go-ethereum/ethdb"
Expand Down Expand Up @@ -211,6 +212,10 @@ type Config struct {

// OverrideTerminalTotalDifficultyPassed (TODO: remove after the fork)
OverrideTerminalTotalDifficultyPassed *bool `toml:",omitempty"`

// SyncTarget defines the target block of sync. It's only used for
// development purposes.
SyncTarget *types.Block
}

// CreateConsensusEngine creates a consensus engine for the given chain configuration.
Expand Down
7 changes: 7 additions & 0 deletions eth/ethconfig/gen_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.