2020-09-22 11:17:39 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2020-09-23 16:18:17 +03:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/devp2p/internal/ethtest"
|
2020-09-22 11:17:39 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-03-19 17:15:39 +03:00
|
|
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
2020-09-22 11:17:39 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/rlpx"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2022-06-27 19:22:36 +03:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-09-22 11:17:39 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-27 19:22:36 +03:00
|
|
|
rlpxCommand = &cli.Command{
|
2020-09-22 11:17:39 +03:00
|
|
|
Name: "rlpx",
|
|
|
|
Usage: "RLPx Commands",
|
2022-06-27 19:22:36 +03:00
|
|
|
Subcommands: []*cli.Command{
|
2020-09-22 11:17:39 +03:00
|
|
|
rlpxPingCommand,
|
2020-09-23 16:18:17 +03:00
|
|
|
rlpxEthTestCommand,
|
2022-02-04 17:24:32 +03:00
|
|
|
rlpxSnapTestCommand,
|
2020-09-22 11:17:39 +03:00
|
|
|
},
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
rlpxPingCommand = &cli.Command{
|
2020-09-23 16:18:17 +03:00
|
|
|
Name: "ping",
|
|
|
|
Usage: "ping <node>",
|
|
|
|
Action: rlpxPing,
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
rlpxEthTestCommand = &cli.Command{
|
2020-09-23 16:18:17 +03:00
|
|
|
Name: "eth-test",
|
|
|
|
Usage: "Runs tests against a node",
|
2020-11-04 17:02:58 +03:00
|
|
|
ArgsUsage: "<node> <chain.rlp> <genesis.json>",
|
2020-09-23 16:18:17 +03:00
|
|
|
Action: rlpxEthTest,
|
2020-11-04 17:02:58 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
testPatternFlag,
|
|
|
|
testTAPFlag,
|
|
|
|
},
|
2020-09-22 11:17:39 +03:00
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
rlpxSnapTestCommand = &cli.Command{
|
2022-02-04 17:24:32 +03:00
|
|
|
Name: "snap-test",
|
|
|
|
Usage: "Runs tests against a node",
|
|
|
|
ArgsUsage: "<node> <chain.rlp> <genesis.json>",
|
|
|
|
Action: rlpxSnapTest,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
testPatternFlag,
|
|
|
|
testTAPFlag,
|
|
|
|
},
|
|
|
|
}
|
2020-09-22 11:17:39 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func rlpxPing(ctx *cli.Context) error {
|
|
|
|
n := getNodeArg(ctx)
|
|
|
|
fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn := rlpx.NewConn(fd, n.Pubkey())
|
|
|
|
ourKey, _ := crypto.GenerateKey()
|
|
|
|
_, err = conn.Handshake(ourKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
code, data, _, err := conn.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch code {
|
|
|
|
case 0:
|
2020-09-23 16:18:17 +03:00
|
|
|
var h ethtest.Hello
|
2020-09-22 11:17:39 +03:00
|
|
|
if err := rlp.DecodeBytes(data, &h); err != nil {
|
|
|
|
return fmt.Errorf("invalid handshake: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("%+v\n", h)
|
|
|
|
case 1:
|
|
|
|
var msg []p2p.DiscReason
|
|
|
|
if rlp.DecodeBytes(data, &msg); len(msg) == 0 {
|
|
|
|
return fmt.Errorf("invalid disconnect message")
|
|
|
|
}
|
|
|
|
return fmt.Errorf("received disconnect message: %v", msg[0])
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-04 17:02:58 +03:00
|
|
|
// rlpxEthTest runs the eth protocol test suite.
|
2020-09-23 16:18:17 +03:00
|
|
|
func rlpxEthTest(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 3 {
|
|
|
|
exit("missing path to chain.rlp as command-line argument")
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2))
|
2021-02-25 20:36:01 +03:00
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
2021-03-19 17:15:39 +03:00
|
|
|
// check if given node supports eth66, and if so, run eth66 protocol tests as well
|
|
|
|
is66Failed, _ := utesting.Run(utesting.Test{Name: "Is_66", Fn: suite.Is_66})
|
|
|
|
if is66Failed {
|
|
|
|
return runTests(ctx, suite.EthTests())
|
|
|
|
}
|
|
|
|
return runTests(ctx, suite.AllEthTests())
|
2020-09-22 11:17:39 +03:00
|
|
|
}
|
2022-02-04 17:24:32 +03:00
|
|
|
|
|
|
|
// rlpxSnapTest runs the snap protocol test suite.
|
|
|
|
func rlpxSnapTest(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 3 {
|
|
|
|
exit("missing path to chain.rlp as command-line argument")
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2))
|
2022-02-04 17:24:32 +03:00
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
return runTests(ctx, suite.SnapTests())
|
|
|
|
}
|