2020-09-22 03:17:39 -05: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 (
|
2023-05-24 05:21:29 -05:00
|
|
|
"errors"
|
2020-09-22 03:17:39 -05:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2020-09-23 08:18:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/devp2p/internal/ethtest"
|
2020-09-22 03:17:39 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2023-12-20 10:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-09-22 03:17:39 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/rlpx"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2022-06-27 11:22:36 -05:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-09-22 03:17:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-27 11:22:36 -05:00
|
|
|
rlpxCommand = &cli.Command{
|
2020-09-22 03:17:39 -05:00
|
|
|
Name: "rlpx",
|
|
|
|
Usage: "RLPx Commands",
|
2022-06-27 11:22:36 -05:00
|
|
|
Subcommands: []*cli.Command{
|
2020-09-22 03:17:39 -05:00
|
|
|
rlpxPingCommand,
|
2020-09-23 08:18:17 -05:00
|
|
|
rlpxEthTestCommand,
|
2022-02-04 08:24:32 -06:00
|
|
|
rlpxSnapTestCommand,
|
2020-09-22 03:17:39 -05:00
|
|
|
},
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
rlpxPingCommand = &cli.Command{
|
2020-09-23 08:18:17 -05:00
|
|
|
Name: "ping",
|
|
|
|
Usage: "ping <node>",
|
|
|
|
Action: rlpxPing,
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
rlpxEthTestCommand = &cli.Command{
|
2020-09-23 08:18:17 -05:00
|
|
|
Name: "eth-test",
|
2023-12-20 10:23:48 -06:00
|
|
|
Usage: "Runs eth protocol tests against a node",
|
|
|
|
ArgsUsage: "<node>",
|
2020-09-23 08:18:17 -05:00
|
|
|
Action: rlpxEthTest,
|
2020-11-04 08:02:58 -06:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
testPatternFlag,
|
|
|
|
testTAPFlag,
|
2023-12-20 10:23:48 -06:00
|
|
|
testChainDirFlag,
|
|
|
|
testNodeFlag,
|
|
|
|
testNodeJWTFlag,
|
|
|
|
testNodeEngineFlag,
|
2020-11-04 08:02:58 -06:00
|
|
|
},
|
2020-09-22 03:17:39 -05:00
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
rlpxSnapTestCommand = &cli.Command{
|
2022-02-04 08:24:32 -06:00
|
|
|
Name: "snap-test",
|
2023-12-20 10:23:48 -06:00
|
|
|
Usage: "Runs snap protocol tests against a node",
|
|
|
|
ArgsUsage: "",
|
2022-02-04 08:24:32 -06:00
|
|
|
Action: rlpxSnapTest,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
testPatternFlag,
|
|
|
|
testTAPFlag,
|
2023-12-20 10:23:48 -06:00
|
|
|
testChainDirFlag,
|
|
|
|
testNodeFlag,
|
|
|
|
testNodeJWTFlag,
|
|
|
|
testNodeEngineFlag,
|
2022-02-04 08:24:32 -06:00
|
|
|
},
|
|
|
|
}
|
2020-09-22 03:17:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func rlpxPing(ctx *cli.Context) error {
|
|
|
|
n := getNodeArg(ctx)
|
2024-06-05 12:31:04 -05:00
|
|
|
tcpEndpoint, ok := n.TCPEndpoint()
|
|
|
|
if !ok {
|
2024-07-24 17:32:58 -05:00
|
|
|
return errors.New("node has no TCP endpoint")
|
2024-06-05 12:31:04 -05:00
|
|
|
}
|
|
|
|
fd, err := net.Dial("tcp", tcpEndpoint.String())
|
2020-09-22 03:17:39 -05:00
|
|
|
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 08:18:17 -05:00
|
|
|
var h ethtest.Hello
|
2020-09-22 03:17:39 -05: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 {
|
2023-05-24 05:21:29 -05:00
|
|
|
return errors.New("invalid disconnect message")
|
2020-09-22 03:17:39 -05:00
|
|
|
}
|
|
|
|
return fmt.Errorf("received disconnect message: %v", msg[0])
|
|
|
|
default:
|
2024-06-11 08:27:35 -05:00
|
|
|
return fmt.Errorf("invalid message code %d, expected handshake (code zero) or disconnect (code one)", code)
|
2020-09-22 03:17:39 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-04 08:02:58 -06:00
|
|
|
// rlpxEthTest runs the eth protocol test suite.
|
2020-09-23 08:18:17 -05:00
|
|
|
func rlpxEthTest(ctx *cli.Context) error {
|
2023-12-20 10:23:48 -06:00
|
|
|
p := cliTestParams(ctx)
|
|
|
|
suite, err := ethtest.NewSuite(p.node, p.chainDir, p.engineAPI, p.jwt)
|
2021-02-25 11:36:01 -06:00
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
2022-08-02 07:48:55 -05:00
|
|
|
return runTests(ctx, suite.EthTests())
|
2020-09-22 03:17:39 -05:00
|
|
|
}
|
2022-02-04 08:24:32 -06:00
|
|
|
|
|
|
|
// rlpxSnapTest runs the snap protocol test suite.
|
|
|
|
func rlpxSnapTest(ctx *cli.Context) error {
|
2023-12-20 10:23:48 -06:00
|
|
|
p := cliTestParams(ctx)
|
|
|
|
suite, err := ethtest.NewSuite(p.node, p.chainDir, p.engineAPI, p.jwt)
|
2022-02-04 08:24:32 -06:00
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
return runTests(ctx, suite.SnapTests())
|
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
|
|
|
|
type testParams struct {
|
|
|
|
node *enode.Node
|
|
|
|
engineAPI string
|
|
|
|
jwt string
|
|
|
|
chainDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func cliTestParams(ctx *cli.Context) *testParams {
|
|
|
|
nodeStr := ctx.String(testNodeFlag.Name)
|
|
|
|
if nodeStr == "" {
|
|
|
|
exit(fmt.Errorf("missing -%s", testNodeFlag.Name))
|
|
|
|
}
|
|
|
|
node, err := parseNode(nodeStr)
|
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
p := testParams{
|
|
|
|
node: node,
|
|
|
|
engineAPI: ctx.String(testNodeEngineFlag.Name),
|
|
|
|
jwt: ctx.String(testNodeJWTFlag.Name),
|
|
|
|
chainDir: ctx.String(testChainDirFlag.Name),
|
|
|
|
}
|
|
|
|
if p.engineAPI == "" {
|
|
|
|
exit(fmt.Errorf("missing -%s", testNodeEngineFlag.Name))
|
|
|
|
}
|
|
|
|
if p.jwt == "" {
|
|
|
|
exit(fmt.Errorf("missing -%s", testNodeJWTFlag.Name))
|
|
|
|
}
|
|
|
|
if p.chainDir == "" {
|
|
|
|
exit(fmt.Errorf("missing -%s", testChainDirFlag.Name))
|
|
|
|
}
|
|
|
|
return &p
|
|
|
|
}
|