2019-05-27 13:51:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-15 16:10:07 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-03-25 15:40:30 +00:00
|
|
|
"errors"
|
2019-05-27 13:51:49 +00:00
|
|
|
"fmt"
|
2020-03-04 22:25:40 +00:00
|
|
|
"io"
|
2020-03-15 16:10:07 +00:00
|
|
|
"io/ioutil"
|
2019-05-27 13:51:49 +00:00
|
|
|
"os"
|
2020-03-15 16:10:07 +00:00
|
|
|
"os/signal"
|
2019-05-27 13:51:49 +00:00
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/cmd/utils"
|
2020-06-15 14:39:07 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/console/prompt"
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/crypto"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/eth"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/internal/debug"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/p2p"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/p2p/enode"
|
2020-03-04 22:25:40 +00:00
|
|
|
"github.com/mattn/go-colorable"
|
|
|
|
"github.com/mattn/go-isatty"
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Git SHA1 commit hash of the release (set via linker flags)
|
|
|
|
gitCommit = ""
|
|
|
|
gitDate = ""
|
|
|
|
// The app that holds all commands and flags.
|
2020-04-04 07:18:10 +00:00
|
|
|
app = utils.NewApp(gitCommit, gitDate, "Ethereum Tester")
|
2019-05-27 13:51:49 +00:00
|
|
|
// flags that configure the node
|
2020-03-27 13:12:26 +00:00
|
|
|
VerbosityFlag = cli.IntFlag{
|
|
|
|
Name: "verbosity",
|
|
|
|
Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
|
|
|
|
Value: 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = []cli.Flag{VerbosityFlag}
|
2019-05-27 13:51:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-03-25 15:40:30 +00:00
|
|
|
|
2020-07-05 06:18:21 +00:00
|
|
|
// Initialize the CLI app and start TurboGeth
|
2019-05-27 13:51:49 +00:00
|
|
|
app.Action = tester
|
|
|
|
app.HideVersion = true // we have a command to print the version
|
2020-07-05 06:18:21 +00:00
|
|
|
app.Copyright = "Copyright 2020 The turbo-geth Authors"
|
2019-05-27 13:51:49 +00:00
|
|
|
app.Commands = []cli.Command{}
|
|
|
|
sort.Sort(cli.CommandsByName(app.Commands))
|
|
|
|
|
|
|
|
app.Flags = append(app.Flags, flags...)
|
|
|
|
|
|
|
|
app.Before = func(ctx *cli.Context) error {
|
2020-04-04 07:18:10 +00:00
|
|
|
setupLogger(ctx)
|
2019-05-27 13:51:49 +00:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2020-02-21 15:25:11 +00:00
|
|
|
if err := debug.Setup(ctx); err != nil {
|
2019-05-27 13:51:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
app.After = func(ctx *cli.Context) error {
|
|
|
|
debug.Exit()
|
2020-06-15 14:39:07 +00:00
|
|
|
prompt.Stdin.Close() // Resets terminal mode.
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-15 16:10:07 +00:00
|
|
|
|
|
|
|
app.Commands = []cli.Command{
|
|
|
|
{
|
2020-03-25 15:40:30 +00:00
|
|
|
Action: utils.MigrateFlags(genesisCmd),
|
|
|
|
Name: "genesis",
|
|
|
|
Usage: "Produce genesis.json file for geth",
|
|
|
|
},
|
2020-03-15 16:10:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 16:10:07 +00:00
|
|
|
func rootContext() context.Context {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
|
|
|
defer signal.Stop(ch)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
log.Info("Got interrupt, shutting down...")
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:12:26 +00:00
|
|
|
func setupLogger(cliCtx *cli.Context) {
|
2020-03-04 22:25:40 +00:00
|
|
|
var (
|
|
|
|
ostream log.Handler
|
|
|
|
glogger *log.GlogHandler
|
|
|
|
)
|
|
|
|
|
|
|
|
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
|
|
|
output := io.Writer(os.Stderr)
|
|
|
|
if usecolor {
|
|
|
|
output = colorable.NewColorableStderr()
|
|
|
|
}
|
|
|
|
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
|
|
|
|
glogger = log.NewGlogHandler(ostream)
|
|
|
|
log.Root().SetHandler(glogger)
|
2020-03-27 13:12:26 +00:00
|
|
|
glogger.Verbosity(log.Lvl(cliCtx.GlobalInt(VerbosityFlag.Name)))
|
2020-03-25 15:40:30 +00:00
|
|
|
}
|
2020-03-04 22:25:40 +00:00
|
|
|
|
2020-03-25 15:40:30 +00:00
|
|
|
func tester(cliCtx *cli.Context) error {
|
|
|
|
ctx := rootContext()
|
|
|
|
nodeToConnect, err := getTargetAddr(cliCtx)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
2020-03-25 15:40:30 +00:00
|
|
|
return err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-03-25 15:40:30 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
tp := NewTesterProtocol()
|
2020-04-06 07:53:52 +00:00
|
|
|
|
|
|
|
//fmt.Printf("%s %s\n", ctx.Args()[0], ctx.Args()[1])
|
2019-05-27 13:51:49 +00:00
|
|
|
//tp.blockFeeder, err = NewBlockAccessor(ctx.Args()[0]/*, ctx.Args()[1]*/)
|
2020-07-15 12:26:59 +00:00
|
|
|
blockGen, err := NewBlockGenerator(ctx, "blocks", 50000)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to create block generator: %v", err))
|
|
|
|
}
|
2020-03-15 16:10:07 +00:00
|
|
|
defer blockGen.Close()
|
2019-05-27 13:51:49 +00:00
|
|
|
tp.blockFeeder = blockGen
|
2020-07-15 12:26:59 +00:00
|
|
|
tp.forkBase = 49998
|
2020-03-04 22:25:40 +00:00
|
|
|
tp.forkHeight = 5
|
2020-03-15 16:10:07 +00:00
|
|
|
tp.forkFeeder, err = NewForkGenerator(ctx, blockGen, "forkblocks", tp.forkBase, tp.forkHeight)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to create fork generator: %v", err))
|
|
|
|
}
|
2020-03-15 16:10:07 +00:00
|
|
|
defer tp.forkFeeder.Close()
|
2020-03-26 21:52:05 +00:00
|
|
|
tp.protocolVersion = uint32(eth.ProtocolVersions[0])
|
2019-05-27 13:51:49 +00:00
|
|
|
tp.networkId = 1 // Mainnet
|
2020-04-06 07:53:52 +00:00
|
|
|
tp.genesisBlockHash = tp.forkFeeder.Genesis().Hash()
|
|
|
|
|
2020-07-15 06:15:48 +00:00
|
|
|
server := makeP2PServer(ctx, tp, []string{eth.ProtocolName, eth.DebugName})
|
2020-03-25 15:40:30 +00:00
|
|
|
// Add protocol
|
|
|
|
if err := server.Start(); err != nil {
|
2020-03-26 21:52:05 +00:00
|
|
|
panic(fmt.Errorf("could not start server: %w", err))
|
2020-03-25 15:40:30 +00:00
|
|
|
}
|
|
|
|
server.AddPeer(nodeToConnect)
|
|
|
|
|
2020-03-27 13:12:26 +00:00
|
|
|
<-ctx.Done()
|
2020-03-25 15:40:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genesisCmd(cliCtx *cli.Context) error {
|
|
|
|
res, err := json.Marshal(genesis())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprintf(os.Stdout, string(res))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeP2PServer(ctx context.Context, tp *TesterProtocol, protocols []string) *p2p.Server {
|
2019-05-27 13:51:49 +00:00
|
|
|
serverKey, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to generate server key: %v", err))
|
|
|
|
}
|
2020-03-25 15:40:30 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
p2pConfig := p2p.Config{}
|
|
|
|
p2pConfig.PrivateKey = serverKey
|
|
|
|
p2pConfig.Name = "geth tester"
|
|
|
|
p2pConfig.Logger = log.New()
|
2020-03-04 22:25:40 +00:00
|
|
|
p2pConfig.MaxPeers = 1
|
2020-03-25 15:40:30 +00:00
|
|
|
p2pConfig.Protocols = []p2p.Protocol{}
|
|
|
|
pMap := map[string]p2p.Protocol{
|
|
|
|
eth.ProtocolName: {
|
2019-05-27 13:51:49 +00:00
|
|
|
Name: eth.ProtocolName,
|
2020-03-26 21:52:05 +00:00
|
|
|
Version: eth.ProtocolVersions[0],
|
|
|
|
Length: eth.ProtocolLengths[eth.ProtocolVersions[0]],
|
2020-03-25 15:40:30 +00:00
|
|
|
Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
|
|
|
|
return tp.protocolRun(ctx, peer, rw)
|
|
|
|
},
|
2019-05-27 13:51:49 +00:00
|
|
|
},
|
2020-03-25 15:40:30 +00:00
|
|
|
eth.DebugName: {
|
2020-03-15 16:10:07 +00:00
|
|
|
Name: eth.DebugName,
|
|
|
|
Version: eth.DebugVersions[0],
|
|
|
|
Length: eth.DebugLengths[eth.DebugVersions[0]],
|
2020-03-25 15:40:30 +00:00
|
|
|
Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
|
|
|
|
return tp.debugProtocolRun(ctx, peer, rw)
|
|
|
|
},
|
|
|
|
},
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-03-25 15:40:30 +00:00
|
|
|
|
|
|
|
for _, protocolName := range protocols {
|
|
|
|
p2pConfig.Protocols = append(p2pConfig.Protocols, pMap[protocolName])
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-03-25 15:40:30 +00:00
|
|
|
return &p2p.Server{Config: p2pConfig}
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2020-03-25 15:40:30 +00:00
|
|
|
func getTargetAddr(cliCtx *cli.Context) (*enode.Node, error) {
|
|
|
|
var enodeAddress string
|
|
|
|
if len(cliCtx.Args()) < 1 {
|
|
|
|
addr, err := ioutil.ReadFile(p2p.EnodeAddressFileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
enodeAddress = string(addr)
|
|
|
|
} else {
|
|
|
|
enodeAddress = cliCtx.Args()[0]
|
|
|
|
}
|
|
|
|
if enodeAddress == "" {
|
|
|
|
return nil, errors.New("Usage: tester <enode>\n")
|
|
|
|
}
|
|
|
|
nodeToConnect, err := enode.ParseV4(enodeAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse the node info: %w", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Parsed node: %s, IP: %s\n", nodeToConnect, nodeToConnect.IP())
|
|
|
|
|
|
|
|
return nodeToConnect, nil
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|