diff options
author | Felix Lange <fjl@twurst.com> | 2016-11-09 10:09:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-09 10:09:05 +0800 |
commit | 355f4b0c15fadec683877b5de1361bd678fee28e (patch) | |
tree | ef1c3a915af7f07c049c08318fe450b2ecf46654 /cmd | |
parent | 8b1df1a259fe6dc4c15e391e9c0762c9621d9d72 (diff) | |
parent | bbb5e5d56a51d9430aec934c76a0bd02f1f2427d (diff) | |
download | dexon-355f4b0c15fadec683877b5de1361bd678fee28e.tar.gz dexon-355f4b0c15fadec683877b5de1361bd678fee28e.tar.zst dexon-355f4b0c15fadec683877b5de1361bd678fee28e.zip |
Merge pull request #3232 from zsfelfoldi/light-topic3
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/bootnode/main.go | 13 | ||||
-rw-r--r-- | cmd/geth/chaincmd.go | 2 | ||||
-rw-r--r-- | cmd/geth/main.go | 5 | ||||
-rw-r--r-- | cmd/geth/usage.go | 4 | ||||
-rw-r--r-- | cmd/utils/flags.go | 65 | ||||
-rw-r--r-- | cmd/utils/version.go | 2 | ||||
-rw-r--r-- | cmd/v5test/main.go | 101 |
7 files changed, 181 insertions, 11 deletions
diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index 40d3cdc17..abecac3d8 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -27,6 +27,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/p2p/discover" + "github.com/ethereum/go-ethereum/p2p/discv5" "github.com/ethereum/go-ethereum/p2p/nat" ) @@ -38,6 +39,7 @@ func main() { nodeKeyFile = flag.String("nodekey", "", "private key filename") nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") + runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode") nodeKey *ecdsa.PrivateKey err error @@ -79,8 +81,15 @@ func main() { os.Exit(0) } - if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil { - utils.Fatalf("%v", err) + if *runv5 { + if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil { + utils.Fatalf("%v", err) + } + } else { + if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil { + utils.Fatalf("%v", err) + } } + select {} } diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index c1bbbd8dc..7a9cf4ac2 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -180,7 +180,7 @@ func exportChain(ctx *cli.Context) error { func removeDB(ctx *cli.Context) error { stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) - dbdir := stack.ResolvePath("chaindata") + dbdir := stack.ResolvePath(utils.ChainDbName(ctx)) if !common.FileExist(dbdir) { fmt.Println(dbdir, "does not exist") return nil diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 551705208..557bf57fa 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -118,8 +118,10 @@ participating. utils.KeyStoreDirFlag, utils.OlympicFlag, utils.FastSyncFlag, + utils.LightModeFlag, + utils.LightServFlag, + utils.LightPeersFlag, utils.LightKDFFlag, - utils.CacheFlag, utils.TrieCacheGenFlag, utils.JSpathFlag, utils.ListenPortFlag, @@ -136,6 +138,7 @@ participating. utils.NATFlag, utils.NatspecEnabledFlag, utils.NoDiscoverFlag, + utils.DiscoveryV5Flag, utils.NodeKeyFileFlag, utils.NodeKeyHexFlag, utils.RPCEnabledFlag, diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index 4c4e27630..e4abf6b30 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -72,6 +72,9 @@ var AppHelpFlagGroups = []flagGroup{ utils.DevModeFlag, utils.IdentityFlag, utils.FastSyncFlag, + utils.LightModeFlag, + utils.LightServFlag, + utils.LightPeersFlag, utils.LightKDFFlag, }, }, @@ -119,6 +122,7 @@ var AppHelpFlagGroups = []flagGroup{ utils.MaxPendingPeersFlag, utils.NATFlag, utils.NoDiscoverFlag, + utils.DiscoveryV5Flag, utils.NodeKeyFileFlag, utils.NodeKeyHexFlag, }, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index edb0873f6..626c2615d 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -39,6 +39,8 @@ import ( "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/les" + "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/metrics" @@ -145,6 +147,20 @@ var ( Name: "fast", Usage: "Enable fast syncing through state downloads", } + LightModeFlag = cli.BoolFlag{ + Name: "light", + Usage: "Enable light client mode", + } + LightServFlag = cli.IntFlag{ + Name: "lightserv", + Usage: "Maximum percentage of time allowed for serving LES requests (0-90)", + Value: 0, + } + LightPeersFlag = cli.IntFlag{ + Name: "lightpeers", + Usage: "Maximum number of LES client peers", + Value: 20, + } LightKDFFlag = cli.BoolFlag{ Name: "lightkdf", Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength", @@ -348,6 +364,10 @@ var ( Name: "nodiscover", Usage: "Disables the peer discovery mechanism (manual peer addition)", } + DiscoveryV5Flag = cli.BoolFlag{ + Name: "v5disc", + Usage: "Enables the experimental RLPx V5 (Topic Discovery) mechanism", + } WhisperEnabledFlag = cli.BoolFlag{ Name: "shh", Usage: "Enable Whisper", @@ -491,6 +511,10 @@ func MakeListenAddress(ctx *cli.Context) string { return fmt.Sprintf(":%d", ctx.GlobalInt(ListenPortFlag.Name)) } +func MakeListenAddressV5(ctx *cli.Context) string { + return fmt.Sprintf(":%d", ctx.GlobalInt(ListenPortFlag.Name)+1) +} + // MakeNAT creates a port mapper from set command line flags. func MakeNAT(ctx *cli.Context) nat.Interface { natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name)) @@ -621,9 +645,11 @@ func MakeNode(ctx *cli.Context, name, gitCommit string) *node.Node { Name: name, Version: vsn, UserIdent: makeNodeUserIdent(ctx), - NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name), + NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name) || ctx.GlobalBool(LightModeFlag.Name), + DiscoveryV5: ctx.GlobalBool(DiscoveryV5Flag.Name) || ctx.GlobalBool(LightModeFlag.Name) || ctx.GlobalInt(LightServFlag.Name) > 0, BootstrapNodes: MakeBootstrapNodes(ctx), ListenAddr: MakeListenAddress(ctx), + ListenAddrV5: MakeListenAddressV5(ctx), NAT: MakeNAT(ctx), MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name), @@ -680,6 +706,10 @@ func RegisterEthService(ctx *cli.Context, stack *node.Node, extra []byte) { Etherbase: MakeEtherbase(stack.AccountManager(), ctx), ChainConfig: MakeChainConfig(ctx, stack), FastSync: ctx.GlobalBool(FastSyncFlag.Name), + LightMode: ctx.GlobalBool(LightModeFlag.Name), + LightServ: ctx.GlobalInt(LightServFlag.Name), + LightPeers: ctx.GlobalInt(LightPeersFlag.Name), + MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), DatabaseCache: ctx.GlobalInt(CacheFlag.Name), DatabaseHandles: MakeDatabaseHandles(), NetworkId: ctx.GlobalInt(NetworkIdFlag.Name), @@ -714,6 +744,7 @@ func RegisterEthService(ctx *cli.Context, stack *node.Node, extra []byte) { } ethConf.Genesis = core.TestNetGenesisBlock() state.StartingNonce = 1048576 // (2**20) + light.StartingNonce = 1048576 // (2**20) case ctx.GlobalBool(DevModeFlag.Name): ethConf.Genesis = core.OlympicGenesisBlock() @@ -727,10 +758,23 @@ func RegisterEthService(ctx *cli.Context, stack *node.Node, extra []byte) { state.MaxTrieCacheGen = uint16(gen) } - if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - return eth.New(ctx, ethConf) - }); err != nil { - Fatalf("Failed to register the Ethereum service: %v", err) + if ethConf.LightMode { + if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { + return les.New(ctx, ethConf) + }); err != nil { + Fatalf("Failed to register the Ethereum light node service: %v", err) + } + } else { + if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { + fullNode, err := eth.New(ctx, ethConf) + if fullNode != nil && ethConf.LightServ > 0 { + ls, _ := les.NewLesServer(fullNode, ethConf) + fullNode.AddLesServer(ls) + } + return fullNode, err + }); err != nil { + Fatalf("Failed to register the Ethereum full node service: %v", err) + } } } @@ -830,14 +874,23 @@ func MakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainConfi return config } +func ChainDbName(ctx *cli.Context) string { + if ctx.GlobalBool(LightModeFlag.Name) { + return "lightchaindata" + } else { + return "chaindata" + } +} + // MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails. func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database { var ( cache = ctx.GlobalInt(CacheFlag.Name) handles = MakeDatabaseHandles() + name = ChainDbName(ctx) ) - chainDb, err := stack.OpenDatabase("chaindata", cache, handles) + chainDb, err := stack.OpenDatabase(name, cache, handles) if err != nil { Fatalf("Could not open database: %v", err) } diff --git a/cmd/utils/version.go b/cmd/utils/version.go index 03633d694..b057f4293 100644 --- a/cmd/utils/version.go +++ b/cmd/utils/version.go @@ -1,4 +1,4 @@ -// Copyright 2014 The go-ethereum Authors +// Copyright 2016 The go-ethereum Authors // This file is part of go-ethereum. // // go-ethereum is free software: you can redistribute it and/or modify diff --git a/cmd/v5test/main.go b/cmd/v5test/main.go new file mode 100644 index 000000000..1daff56f8 --- /dev/null +++ b/cmd/v5test/main.go @@ -0,0 +1,101 @@ +// Copyright 2016 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/>. + +// bootnode runs a bootstrap node for the Ethereum Discovery Protocol. +package main + +import ( + "flag" + "fmt" + "math/rand" + "strconv" + "time" + + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/p2p/discv5" + "github.com/ethereum/go-ethereum/p2p/nat" +) + +func main() { + var ( + listenPort = flag.Int("addr", 31000, "beginning of listening port range") + natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") + count = flag.Int("count", 1, "number of v5 topic discovery test nodes (adds default bootnodes to form a test network)") + regtopic = flag.String("reg", "", "topic to register on the network") + looktopic = flag.String("search", "", "topic to search on the network") + ) + flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)") + flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern") + glog.SetToStderr(true) + flag.Parse() + + natm, err := nat.Parse(*natdesc) + if err != nil { + utils.Fatalf("-nat: %v", err) + } + + for i := 0; i < *count; i++ { + listenAddr := ":" + strconv.Itoa(*listenPort+i) + + nodeKey, err := crypto.GenerateKey() + if err != nil { + utils.Fatalf("could not generate key: %v", err) + } + + if net, err := discv5.ListenUDP(nodeKey, listenAddr, natm, ""); err != nil { + utils.Fatalf("%v", err) + } else { + if err := net.SetFallbackNodes(discv5.BootNodes); err != nil { + utils.Fatalf("%v", err) + } + go func() { + if *looktopic == "" { + for i := 0; i < 20; i++ { + time.Sleep(time.Millisecond * time.Duration(2000+rand.Intn(2001))) + net.BucketFill() + } + } + switch { + case *regtopic != "": + // register topic + fmt.Println("Starting topic register") + stop := make(chan struct{}) + net.RegisterTopic(discv5.Topic(*regtopic), stop) + case *looktopic != "": + // search topic + fmt.Println("Starting topic search") + stop := make(chan struct{}) + found := make(chan string, 100) + go net.SearchTopic(discv5.Topic(*looktopic), stop, found) + for s := range found { + fmt.Println(time.Now(), s) + } + default: + // just keep doing lookups + for { + time.Sleep(time.Millisecond * time.Duration(40000+rand.Intn(40001))) + net.BucketFill() + } + } + }() + } + fmt.Printf("Started test node #%d with public key %v\n", i, discv5.PubkeyID(&nodeKey.PublicKey)) + } + + select {} +} |