2019-10-29 10:08:57 -05:00
|
|
|
// Copyright 2019 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 (
|
2021-04-19 07:54:38 -05:00
|
|
|
"errors"
|
2019-10-29 10:08:57 -05:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-19 07:54:38 -05:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-10-29 10:08:57 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2022-06-27 11:22:36 -05:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-10-29 10:08:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-27 11:22:36 -05:00
|
|
|
nodesetCommand = &cli.Command{
|
2019-10-29 10:08:57 -05:00
|
|
|
Name: "nodeset",
|
|
|
|
Usage: "Node set tools",
|
2022-06-27 11:22:36 -05:00
|
|
|
Subcommands: []*cli.Command{
|
2019-10-29 10:08:57 -05:00
|
|
|
nodesetInfoCommand,
|
|
|
|
nodesetFilterCommand,
|
|
|
|
},
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
nodesetInfoCommand = &cli.Command{
|
2019-10-29 10:08:57 -05:00
|
|
|
Name: "info",
|
|
|
|
Usage: "Shows statistics about a node set",
|
|
|
|
Action: nodesetInfo,
|
|
|
|
ArgsUsage: "<nodes.json>",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
nodesetFilterCommand = &cli.Command{
|
2019-10-29 10:08:57 -05:00
|
|
|
Name: "filter",
|
|
|
|
Usage: "Filters a node set",
|
|
|
|
Action: nodesetFilter,
|
|
|
|
ArgsUsage: "<nodes.json> filters..",
|
|
|
|
|
|
|
|
SkipFlagParsing: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func nodesetInfo(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 1 {
|
|
|
|
return fmt.Errorf("need nodes file as argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
ns := loadNodesJSON(ctx.Args().First())
|
|
|
|
fmt.Printf("Set contains %d nodes.\n", len(ns))
|
2021-04-19 07:54:38 -05:00
|
|
|
showAttributeCounts(ns)
|
2019-10-29 10:08:57 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:54:38 -05:00
|
|
|
// showAttributeCounts prints the distribution of ENR attributes in a node set.
|
|
|
|
func showAttributeCounts(ns nodeSet) {
|
|
|
|
attrcount := make(map[string]int)
|
|
|
|
var attrlist []interface{}
|
|
|
|
for _, n := range ns {
|
|
|
|
r := n.N.Record()
|
|
|
|
attrlist = r.AppendElements(attrlist[:0])[1:]
|
|
|
|
for i := 0; i < len(attrlist); i += 2 {
|
|
|
|
key := attrlist[i].(string)
|
|
|
|
attrcount[key]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
var maxlength int
|
|
|
|
for key := range attrcount {
|
|
|
|
keys = append(keys, key)
|
|
|
|
if len(key) > maxlength {
|
|
|
|
maxlength = len(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
fmt.Println("ENR attribute counts:")
|
|
|
|
for _, key := range keys {
|
|
|
|
fmt.Printf("%s%s: %d\n", strings.Repeat(" ", maxlength-len(key)+1), key, attrcount[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:08:57 -05:00
|
|
|
func nodesetFilter(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 1 {
|
|
|
|
return fmt.Errorf("need nodes file as argument")
|
|
|
|
}
|
2021-04-19 07:54:38 -05:00
|
|
|
// Parse -limit.
|
|
|
|
limit, err := parseFilterLimit(ctx.Args().Tail())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Parse the filters.
|
2019-10-29 10:08:57 -05:00
|
|
|
filter, err := andFilter(ctx.Args().Tail())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:54:38 -05:00
|
|
|
// Load nodes and apply filters.
|
|
|
|
ns := loadNodesJSON(ctx.Args().First())
|
2019-10-29 10:08:57 -05:00
|
|
|
result := make(nodeSet)
|
|
|
|
for id, n := range ns {
|
|
|
|
if filter(n) {
|
|
|
|
result[id] = n
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 07:54:38 -05:00
|
|
|
if limit >= 0 {
|
|
|
|
result = result.topN(limit)
|
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
writeNodesJSON("-", result)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeFilter func(nodeJSON) bool
|
|
|
|
|
|
|
|
type nodeFilterC struct {
|
|
|
|
narg int
|
|
|
|
fn func([]string) (nodeFilter, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterFlags = map[string]nodeFilterC{
|
2021-04-19 07:54:38 -05:00
|
|
|
"-limit": {1, trueFilter}, // needed to skip over -limit
|
2019-10-29 10:08:57 -05:00
|
|
|
"-ip": {1, ipFilter},
|
|
|
|
"-min-age": {1, minAgeFilter},
|
|
|
|
"-eth-network": {1, ethFilter},
|
|
|
|
"-les-server": {0, lesFilter},
|
2020-12-03 06:16:20 -06:00
|
|
|
"-snap": {0, snapFilter},
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:54:38 -05:00
|
|
|
// parseFilters parses nodeFilters from args.
|
2019-10-29 10:08:57 -05:00
|
|
|
func parseFilters(args []string) ([]nodeFilter, error) {
|
|
|
|
var filters []nodeFilter
|
|
|
|
for len(args) > 0 {
|
|
|
|
fc, ok := filterFlags[args[0]]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid filter %q", args[0])
|
|
|
|
}
|
2020-12-03 06:16:20 -06:00
|
|
|
if len(args)-1 < fc.narg {
|
|
|
|
return nil, fmt.Errorf("filter %q wants %d arguments, have %d", args[0], fc.narg, len(args)-1)
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
2020-12-03 06:16:20 -06:00
|
|
|
filter, err := fc.fn(args[1 : 1+fc.narg])
|
2019-10-29 10:08:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %v", args[0], err)
|
|
|
|
}
|
|
|
|
filters = append(filters, filter)
|
2020-12-03 06:16:20 -06:00
|
|
|
args = args[1+fc.narg:]
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
return filters, nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:54:38 -05:00
|
|
|
// parseFilterLimit parses the -limit option in args. It returns -1 if there is no limit.
|
|
|
|
func parseFilterLimit(args []string) (int, error) {
|
|
|
|
limit := -1
|
|
|
|
for i, arg := range args {
|
|
|
|
if arg == "-limit" {
|
|
|
|
if i == len(args)-1 {
|
|
|
|
return -1, errors.New("-limit requires an argument")
|
|
|
|
}
|
|
|
|
n, err := strconv.Atoi(args[i+1])
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("invalid -limit %q", args[i+1])
|
|
|
|
}
|
|
|
|
limit = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return limit, nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 03:16:34 -05:00
|
|
|
// andFilter parses node filters in args and returns a single filter that requires all
|
2021-04-19 07:54:38 -05:00
|
|
|
// of them to match.
|
2019-10-29 10:08:57 -05:00
|
|
|
func andFilter(args []string) (nodeFilter, error) {
|
|
|
|
checks, err := parseFilters(args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := func(n nodeJSON) bool {
|
|
|
|
for _, filter := range checks {
|
|
|
|
if !filter(n) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:54:38 -05:00
|
|
|
func trueFilter(args []string) (nodeFilter, error) {
|
|
|
|
return func(n nodeJSON) bool { return true }, nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:08:57 -05:00
|
|
|
func ipFilter(args []string) (nodeFilter, error) {
|
|
|
|
_, cidr, err := net.ParseCIDR(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := func(n nodeJSON) bool { return cidr.Contains(n.N.IP()) }
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func minAgeFilter(args []string) (nodeFilter, error) {
|
|
|
|
minage, err := time.ParseDuration(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := func(n nodeJSON) bool {
|
|
|
|
age := n.LastResponse.Sub(n.FirstResponse)
|
|
|
|
return age >= minage
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ethFilter(args []string) (nodeFilter, error) {
|
2019-10-31 03:38:14 -05:00
|
|
|
var filter forkid.Filter
|
2019-10-29 10:08:57 -05:00
|
|
|
switch args[0] {
|
|
|
|
case "mainnet":
|
|
|
|
filter = forkid.NewStaticFilter(params.MainnetChainConfig, params.MainnetGenesisHash)
|
|
|
|
case "rinkeby":
|
|
|
|
filter = forkid.NewStaticFilter(params.RinkebyChainConfig, params.RinkebyGenesisHash)
|
|
|
|
case "goerli":
|
|
|
|
filter = forkid.NewStaticFilter(params.GoerliChainConfig, params.GoerliGenesisHash)
|
2021-11-08 05:06:01 -06:00
|
|
|
case "sepolia":
|
|
|
|
filter = forkid.NewStaticFilter(params.SepoliaChainConfig, params.SepoliaGenesisHash)
|
2019-10-29 10:08:57 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown network %q", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
f := func(n nodeJSON) bool {
|
|
|
|
var eth struct {
|
|
|
|
ForkID forkid.ID
|
2021-03-24 06:32:39 -05:00
|
|
|
Tail []rlp.RawValue `rlp:"tail"`
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
if n.N.Load(enr.WithEntry("eth", ð)) != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return filter(eth.ForkID) == nil
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lesFilter(args []string) (nodeFilter, error) {
|
|
|
|
f := func(n nodeJSON) bool {
|
|
|
|
var les struct {
|
2021-03-24 06:32:39 -05:00
|
|
|
Tail []rlp.RawValue `rlp:"tail"`
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
return n.N.Load(enr.WithEntry("les", &les)) == nil
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
2020-12-03 06:16:20 -06:00
|
|
|
|
|
|
|
func snapFilter(args []string) (nodeFilter, error) {
|
|
|
|
f := func(n nodeJSON) bool {
|
|
|
|
var snap struct {
|
2021-03-24 06:32:39 -05:00
|
|
|
Tail []rlp.RawValue `rlp:"tail"`
|
2020-12-03 06:16:20 -06:00
|
|
|
}
|
|
|
|
return n.N.Load(enr.WithEntry("snap", &snap)) == nil
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|