github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/cmd/devp2p/nodesetcmd.go (about) 1 // Copyright 2019 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 17 package main 18 19 import ( 20 "fmt" 21 "net" 22 "time" 23 24 "github.com/kisexp/xdchain/core/forkid" 25 "github.com/kisexp/xdchain/p2p/enr" 26 "github.com/kisexp/xdchain/params" 27 "github.com/kisexp/xdchain/rlp" 28 "gopkg.in/urfave/cli.v1" 29 ) 30 31 var ( 32 nodesetCommand = cli.Command{ 33 Name: "nodeset", 34 Usage: "Node set tools", 35 Subcommands: []cli.Command{ 36 nodesetInfoCommand, 37 nodesetFilterCommand, 38 }, 39 } 40 nodesetInfoCommand = cli.Command{ 41 Name: "info", 42 Usage: "Shows statistics about a node set", 43 Action: nodesetInfo, 44 ArgsUsage: "<nodes.json>", 45 } 46 nodesetFilterCommand = cli.Command{ 47 Name: "filter", 48 Usage: "Filters a node set", 49 Action: nodesetFilter, 50 ArgsUsage: "<nodes.json> filters..", 51 52 SkipFlagParsing: true, 53 } 54 ) 55 56 func nodesetInfo(ctx *cli.Context) error { 57 if ctx.NArg() < 1 { 58 return fmt.Errorf("need nodes file as argument") 59 } 60 61 ns := loadNodesJSON(ctx.Args().First()) 62 fmt.Printf("Set contains %d nodes.\n", len(ns)) 63 return nil 64 } 65 66 func nodesetFilter(ctx *cli.Context) error { 67 if ctx.NArg() < 1 { 68 return fmt.Errorf("need nodes file as argument") 69 } 70 ns := loadNodesJSON(ctx.Args().First()) 71 filter, err := andFilter(ctx.Args().Tail()) 72 if err != nil { 73 return err 74 } 75 76 result := make(nodeSet) 77 for id, n := range ns { 78 if filter(n) { 79 result[id] = n 80 } 81 } 82 writeNodesJSON("-", result) 83 return nil 84 } 85 86 type nodeFilter func(nodeJSON) bool 87 88 type nodeFilterC struct { 89 narg int 90 fn func([]string) (nodeFilter, error) 91 } 92 93 var filterFlags = map[string]nodeFilterC{ 94 "-ip": {1, ipFilter}, 95 "-min-age": {1, minAgeFilter}, 96 "-eth-network": {1, ethFilter}, 97 "-les-server": {0, lesFilter}, 98 "-snap": {0, snapFilter}, 99 } 100 101 func parseFilters(args []string) ([]nodeFilter, error) { 102 var filters []nodeFilter 103 for len(args) > 0 { 104 fc, ok := filterFlags[args[0]] 105 if !ok { 106 return nil, fmt.Errorf("invalid filter %q", args[0]) 107 } 108 if len(args)-1 < fc.narg { 109 return nil, fmt.Errorf("filter %q wants %d arguments, have %d", args[0], fc.narg, len(args)-1) 110 } 111 filter, err := fc.fn(args[1 : 1+fc.narg]) 112 if err != nil { 113 return nil, fmt.Errorf("%s: %v", args[0], err) 114 } 115 filters = append(filters, filter) 116 args = args[1+fc.narg:] 117 } 118 return filters, nil 119 } 120 121 func andFilter(args []string) (nodeFilter, error) { 122 checks, err := parseFilters(args) 123 if err != nil { 124 return nil, err 125 } 126 f := func(n nodeJSON) bool { 127 for _, filter := range checks { 128 if !filter(n) { 129 return false 130 } 131 } 132 return true 133 } 134 return f, nil 135 } 136 137 func ipFilter(args []string) (nodeFilter, error) { 138 _, cidr, err := net.ParseCIDR(args[0]) 139 if err != nil { 140 return nil, err 141 } 142 f := func(n nodeJSON) bool { return cidr.Contains(n.N.IP()) } 143 return f, nil 144 } 145 146 func minAgeFilter(args []string) (nodeFilter, error) { 147 minage, err := time.ParseDuration(args[0]) 148 if err != nil { 149 return nil, err 150 } 151 f := func(n nodeJSON) bool { 152 age := n.LastResponse.Sub(n.FirstResponse) 153 return age >= minage 154 } 155 return f, nil 156 } 157 158 func ethFilter(args []string) (nodeFilter, error) { 159 var filter forkid.Filter 160 switch args[0] { 161 case "mainnet": 162 filter = forkid.NewStaticFilter(params.MainnetChainConfig, params.MainnetGenesisHash) 163 case "rinkeby": 164 filter = forkid.NewStaticFilter(params.RinkebyChainConfig, params.RinkebyGenesisHash) 165 case "goerli": 166 filter = forkid.NewStaticFilter(params.GoerliChainConfig, params.GoerliGenesisHash) 167 case "ropsten": 168 filter = forkid.NewStaticFilter(params.RopstenChainConfig, params.RopstenGenesisHash) 169 default: 170 return nil, fmt.Errorf("unknown network %q", args[0]) 171 } 172 173 f := func(n nodeJSON) bool { 174 var eth struct { 175 ForkID forkid.ID 176 _ []rlp.RawValue `rlp:"tail"` 177 } 178 if n.N.Load(enr.WithEntry("eth", ð)) != nil { 179 return false 180 } 181 return filter(eth.ForkID) == nil 182 } 183 return f, nil 184 } 185 186 func lesFilter(args []string) (nodeFilter, error) { 187 f := func(n nodeJSON) bool { 188 var les struct { 189 _ []rlp.RawValue `rlp:"tail"` 190 } 191 return n.N.Load(enr.WithEntry("les", &les)) == nil 192 } 193 return f, nil 194 } 195 196 func snapFilter(args []string) (nodeFilter, error) { 197 f := func(n nodeJSON) bool { 198 var snap struct { 199 _ []rlp.RawValue `rlp:"tail"` 200 } 201 return n.N.Load(enr.WithEntry("snap", &snap)) == nil 202 } 203 return f, nil 204 }