rlpxcmd.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "net"
  20. "github.com/ethereum/go-ethereum/cmd/devp2p/internal/ethtest"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/p2p"
  23. "github.com/ethereum/go-ethereum/p2p/rlpx"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. "github.com/urfave/cli/v2"
  26. )
  27. var (
  28. rlpxCommand = &cli.Command{
  29. Name: "rlpx",
  30. Usage: "RLPx Commands",
  31. Subcommands: []*cli.Command{
  32. rlpxPingCommand,
  33. rlpxEthTestCommand,
  34. rlpxSnapTestCommand,
  35. },
  36. }
  37. rlpxPingCommand = &cli.Command{
  38. Name: "ping",
  39. Usage: "ping <node>",
  40. Action: rlpxPing,
  41. }
  42. rlpxEthTestCommand = &cli.Command{
  43. Name: "eth-test",
  44. Usage: "Runs tests against a node",
  45. ArgsUsage: "<node> <chain.rlp> <genesis.json>",
  46. Action: rlpxEthTest,
  47. Flags: []cli.Flag{
  48. testPatternFlag,
  49. testTAPFlag,
  50. },
  51. }
  52. rlpxSnapTestCommand = &cli.Command{
  53. Name: "snap-test",
  54. Usage: "Runs tests against a node",
  55. ArgsUsage: "<node> <chain.rlp> <genesis.json>",
  56. Action: rlpxSnapTest,
  57. Flags: []cli.Flag{
  58. testPatternFlag,
  59. testTAPFlag,
  60. },
  61. }
  62. )
  63. func rlpxPing(ctx *cli.Context) error {
  64. n := getNodeArg(ctx)
  65. fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP()))
  66. if err != nil {
  67. return err
  68. }
  69. conn := rlpx.NewConn(fd, n.Pubkey())
  70. ourKey, _ := crypto.GenerateKey()
  71. _, err = conn.Handshake(ourKey)
  72. if err != nil {
  73. return err
  74. }
  75. code, data, _, err := conn.Read()
  76. if err != nil {
  77. return err
  78. }
  79. switch code {
  80. case 0:
  81. var h ethtest.Hello
  82. if err := rlp.DecodeBytes(data, &h); err != nil {
  83. return fmt.Errorf("invalid handshake: %v", err)
  84. }
  85. fmt.Printf("%+v\n", h)
  86. case 1:
  87. var msg []p2p.DiscReason
  88. if rlp.DecodeBytes(data, &msg); len(msg) == 0 {
  89. return fmt.Errorf("invalid disconnect message")
  90. }
  91. return fmt.Errorf("received disconnect message: %v", msg[0])
  92. default:
  93. return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code)
  94. }
  95. return nil
  96. }
  97. // rlpxEthTest runs the eth protocol test suite.
  98. func rlpxEthTest(ctx *cli.Context) error {
  99. if ctx.NArg() < 3 {
  100. exit("missing path to chain.rlp as command-line argument")
  101. }
  102. suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2))
  103. if err != nil {
  104. exit(err)
  105. }
  106. return runTests(ctx, suite.EthTests())
  107. }
  108. // rlpxSnapTest runs the snap protocol test suite.
  109. func rlpxSnapTest(ctx *cli.Context) error {
  110. if ctx.NArg() < 3 {
  111. exit("missing path to chain.rlp as command-line argument")
  112. }
  113. suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2))
  114. if err != nil {
  115. exit(err)
  116. }
  117. return runTests(ctx, suite.SnapTests())
  118. }