Commit 49da4298 authored by Zsolt Felfoldi's avatar Zsolt Felfoldi Committed by Felix Lange

p2p/discv5: added new topic discovery package

parent 7db7109a
...@@ -27,6 +27,7 @@ import ( ...@@ -27,6 +27,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/p2p/nat"
) )
...@@ -38,6 +39,7 @@ func main() { ...@@ -38,6 +39,7 @@ func main() {
nodeKeyFile = flag.String("nodekey", "", "private key filename") nodeKeyFile = flag.String("nodekey", "", "private key filename")
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") 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 nodeKey *ecdsa.PrivateKey
err error err error
...@@ -79,8 +81,15 @@ func main() { ...@@ -79,8 +81,15 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil { if *runv5 {
utils.Fatalf("%v", err) 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 {} select {}
} }
...@@ -41,7 +41,6 @@ import ( ...@@ -41,7 +41,6 @@ import (
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/discover"
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
...@@ -120,11 +119,9 @@ participating. ...@@ -120,11 +119,9 @@ participating.
utils.OlympicFlag, utils.OlympicFlag,
utils.FastSyncFlag, utils.FastSyncFlag,
utils.LightModeFlag, utils.LightModeFlag,
utils.NoDefSrvFlag,
utils.LightServFlag, utils.LightServFlag,
utils.LightPeersFlag, utils.LightPeersFlag,
utils.LightKDFFlag, utils.LightKDFFlag,
utils.CacheFlag,
utils.TrieCacheGenFlag, utils.TrieCacheGenFlag,
utils.JSpathFlag, utils.JSpathFlag,
utils.ListenPortFlag, utils.ListenPortFlag,
...@@ -141,6 +138,7 @@ participating. ...@@ -141,6 +138,7 @@ participating.
utils.NATFlag, utils.NATFlag,
utils.NatspecEnabledFlag, utils.NatspecEnabledFlag,
utils.NoDiscoverFlag, utils.NoDiscoverFlag,
utils.DiscoveryV5Flag,
utils.NodeKeyFileFlag, utils.NodeKeyFileFlag,
utils.NodeKeyHexFlag, utils.NodeKeyHexFlag,
utils.RPCEnabledFlag, utils.RPCEnabledFlag,
...@@ -285,31 +283,6 @@ func startNode(ctx *cli.Context, stack *node.Node) { ...@@ -285,31 +283,6 @@ func startNode(ctx *cli.Context, stack *node.Node) {
// Start up the node itself // Start up the node itself
utils.StartNode(stack) utils.StartNode(stack)
if ctx.GlobalBool(utils.LightModeFlag.Name) && !ctx.GlobalBool(utils.NoDefSrvFlag.Name) {
// add default light server; test phase only
addPeer := func(url string) {
node, err := discover.ParseNode(url)
if err == nil {
stack.Server().AddPeer(node)
}
}
if ctx.GlobalBool(utils.TestNetFlag.Name) {
// TestNet (John Gerryts @phonikg)
addPeer("enode://d72af45ba9b60851a8077a4eb07700484b585e5f2e55024e0c93b7ec7d114f2e3fa3c8f3a3358f89da00a609f5a062415deb857ada863b8cdad02b0b0bc90da3@50.112.52.169:30301")
} else {
if ctx.GlobalBool(utils.OpposeDAOFork.Name) {
// Classic (Azure)
addPeer("enode://fc3d7b57e5d317946bf421411632ec98d5ffcbf94548cd7bc10088e4fef176670f8ec70280d301a9d0b22fe498203f62b323da15b3acc18b02a1fee2a06b7d3f@40.118.3.223:30305")
} else {
// MainNet (Azure)
addPeer("enode://feaf206a308a669a789be45f4dadcb351246051727f12415ad69e44f8080daf0569c10fe1d9944d245dd1f3e1c89cedda8ce03d7e3d5ed8975a35cad4b4f7ec1@40.118.3.223:30303")
// MainNet (John Gerryts @phonikg)
addPeer("enode://02b80f0d47c7c157c069d0584067a284cdf188b9267666234b872e70d936a803ad20ea27f78ef1fd6425ae4b7108907e1875adbca96b038004114ac4d1e529a3@50.112.52.169:30300")
}
}
}
// Unlock any account specifically requested // Unlock any account specifically requested
accman := stack.AccountManager() accman := stack.AccountManager()
passwords := utils.MakePasswordList(ctx) passwords := utils.MakePasswordList(ctx)
......
...@@ -73,7 +73,6 @@ var AppHelpFlagGroups = []flagGroup{ ...@@ -73,7 +73,6 @@ var AppHelpFlagGroups = []flagGroup{
utils.IdentityFlag, utils.IdentityFlag,
utils.FastSyncFlag, utils.FastSyncFlag,
utils.LightModeFlag, utils.LightModeFlag,
utils.NoDefSrvFlag,
utils.LightServFlag, utils.LightServFlag,
utils.LightPeersFlag, utils.LightPeersFlag,
utils.LightKDFFlag, utils.LightKDFFlag,
...@@ -123,6 +122,7 @@ var AppHelpFlagGroups = []flagGroup{ ...@@ -123,6 +122,7 @@ var AppHelpFlagGroups = []flagGroup{
utils.MaxPendingPeersFlag, utils.MaxPendingPeersFlag,
utils.NATFlag, utils.NATFlag,
utils.NoDiscoverFlag, utils.NoDiscoverFlag,
utils.DiscoveryV5Flag,
utils.NodeKeyFileFlag, utils.NodeKeyFileFlag,
utils.NodeKeyHexFlag, utils.NodeKeyHexFlag,
}, },
......
...@@ -151,10 +151,6 @@ var ( ...@@ -151,10 +151,6 @@ var (
Name: "light", Name: "light",
Usage: "Enable light client mode", Usage: "Enable light client mode",
} }
NoDefSrvFlag = cli.BoolFlag{
Name: "nodefsrv",
Usage: "Don't add default LES server (only for test version)",
}
LightServFlag = cli.IntFlag{ LightServFlag = cli.IntFlag{
Name: "lightserv", Name: "lightserv",
Usage: "Maximum percentage of time allowed for serving LES requests (0-90)", Usage: "Maximum percentage of time allowed for serving LES requests (0-90)",
...@@ -368,6 +364,10 @@ var ( ...@@ -368,6 +364,10 @@ var (
Name: "nodiscover", Name: "nodiscover",
Usage: "Disables the peer discovery mechanism (manual peer addition)", 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{ WhisperEnabledFlag = cli.BoolFlag{
Name: "shh", Name: "shh",
Usage: "Enable Whisper", Usage: "Enable Whisper",
...@@ -511,6 +511,10 @@ func MakeListenAddress(ctx *cli.Context) string { ...@@ -511,6 +511,10 @@ func MakeListenAddress(ctx *cli.Context) string {
return fmt.Sprintf(":%d", ctx.GlobalInt(ListenPortFlag.Name)) 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. // MakeNAT creates a port mapper from set command line flags.
func MakeNAT(ctx *cli.Context) nat.Interface { func MakeNAT(ctx *cli.Context) nat.Interface {
natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name)) natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
...@@ -641,9 +645,11 @@ func MakeNode(ctx *cli.Context, name, gitCommit string) *node.Node { ...@@ -641,9 +645,11 @@ func MakeNode(ctx *cli.Context, name, gitCommit string) *node.Node {
Name: name, Name: name,
Version: vsn, Version: vsn,
UserIdent: makeNodeUserIdent(ctx), 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), BootstrapNodes: MakeBootstrapNodes(ctx),
ListenAddr: MakeListenAddress(ctx), ListenAddr: MakeListenAddress(ctx),
ListenAddrV5: MakeListenAddressV5(ctx),
NAT: MakeNAT(ctx), NAT: MakeNAT(ctx),
MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name), MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name),
...@@ -701,7 +707,6 @@ func RegisterEthService(ctx *cli.Context, stack *node.Node, extra []byte) { ...@@ -701,7 +707,6 @@ func RegisterEthService(ctx *cli.Context, stack *node.Node, extra []byte) {
ChainConfig: MakeChainConfig(ctx, stack), ChainConfig: MakeChainConfig(ctx, stack),
FastSync: ctx.GlobalBool(FastSyncFlag.Name), FastSync: ctx.GlobalBool(FastSyncFlag.Name),
LightMode: ctx.GlobalBool(LightModeFlag.Name), LightMode: ctx.GlobalBool(LightModeFlag.Name),
NoDefSrv: ctx.GlobalBool(NoDefSrvFlag.Name),
LightServ: ctx.GlobalInt(LightServFlag.Name), LightServ: ctx.GlobalInt(LightServFlag.Name),
LightPeers: ctx.GlobalInt(LightPeersFlag.Name), LightPeers: ctx.GlobalInt(LightPeersFlag.Name),
MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
......
// Copyright 2015 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 {}
}
...@@ -70,7 +70,6 @@ type Config struct { ...@@ -70,7 +70,6 @@ type Config struct {
Genesis string // Genesis JSON to seed the chain database with Genesis string // Genesis JSON to seed the chain database with
FastSync bool // Enables the state download based fast synchronisation algorithm FastSync bool // Enables the state download based fast synchronisation algorithm
LightMode bool // Running in light client mode LightMode bool // Running in light client mode
NoDefSrv bool // No default LES server
LightServ int // Maximum percentage of time allowed for serving LES requests LightServ int // Maximum percentage of time allowed for serving LES requests
LightPeers int // Maximum number of LES client peers LightPeers int // Maximum number of LES client peers
MaxPeers int // Maximum number of global peers MaxPeers int // Maximum number of global peers
...@@ -106,7 +105,7 @@ type Config struct { ...@@ -106,7 +105,7 @@ type Config struct {
} }
type LesServer interface { type LesServer interface {
Start() Start(srvr *p2p.Server)
Stop() Stop()
Protocols() []p2p.Protocol Protocols() []p2p.Protocol
} }
...@@ -434,7 +433,7 @@ func (s *Ethereum) Start(srvr *p2p.Server) error { ...@@ -434,7 +433,7 @@ func (s *Ethereum) Start(srvr *p2p.Server) error {
} }
s.protocolManager.Start() s.protocolManager.Start()
if s.lesServer != nil { if s.lesServer != nil {
s.lesServer.Start() s.lesServer.Start(srvr)
} }
return nil return nil
} }
......
...@@ -196,7 +196,7 @@ func (s *LightEthereum) Protocols() []p2p.Protocol { ...@@ -196,7 +196,7 @@ func (s *LightEthereum) Protocols() []p2p.Protocol {
// Ethereum protocol implementation. // Ethereum protocol implementation.
func (s *LightEthereum) Start(srvr *p2p.Server) error { func (s *LightEthereum) Start(srvr *p2p.Server) error {
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId) s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId)
s.protocolManager.Start() s.protocolManager.Start(srvr)
return nil return nil
} }
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"sync" "sync"
"time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
...@@ -36,6 +37,7 @@ import ( ...@@ -36,6 +37,7 @@ import (
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
...@@ -99,6 +101,10 @@ type ProtocolManager struct { ...@@ -99,6 +101,10 @@ type ProtocolManager struct {
odr *LesOdr odr *LesOdr
server *LesServer server *LesServer
topicDisc *discv5.Network
lesTopic discv5.Topic
p2pServer *p2p.Server
downloader *downloader.Downloader downloader *downloader.Downloader
fetcher *lightFetcher fetcher *lightFetcher
peers *peerSet peers *peerSet
...@@ -229,11 +235,52 @@ func (pm *ProtocolManager) removePeer(id string) { ...@@ -229,11 +235,52 @@ func (pm *ProtocolManager) removePeer(id string) {
} }
} }
func (pm *ProtocolManager) Start() { func (pm *ProtocolManager) findServers() {
if pm.p2pServer == nil {
return
}
enodes := make(chan string, 100)
stop := make(chan struct{})
go pm.topicDisc.SearchTopic(pm.lesTopic, stop, enodes)
go func() {
added := make(map[string]bool)
for {
select {
case enode := <-enodes:
if !added[enode] {
fmt.Println("Found LES server:", enode)
added[enode] = true
if node, err := discover.ParseNode(enode); err == nil {
pm.p2pServer.AddPeer(node)
}
}
case <-stop:
return
}
}
}()
time.Sleep(time.Second * 20)
close(stop)
}
func (pm *ProtocolManager) Start(srvr *p2p.Server) {
pm.p2pServer = srvr
if srvr != nil {
pm.topicDisc = srvr.DiscV5
}
pm.lesTopic = discv5.Topic("LES@" + common.Bytes2Hex(pm.blockchain.Genesis().Hash().Bytes()[0:8]))
if pm.lightSync { if pm.lightSync {
// start sync handler // start sync handler
go pm.findServers()
go pm.syncer() go pm.syncer()
} else { } else {
if pm.topicDisc != nil {
go func() {
fmt.Println("Starting topic register")
pm.topicDisc.RegisterTopic(pm.lesTopic, pm.quitSync)
fmt.Println("Stopped topic register")
}()
}
go func() { go func() {
for range pm.newPeerCh { for range pm.newPeerCh {
} }
......
...@@ -148,7 +148,7 @@ func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *cor ...@@ -148,7 +148,7 @@ func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *cor
srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000) srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000)
srv.fcCostStats = newCostStats(nil) srv.fcCostStats = newCostStats(nil)
} }
pm.Start() pm.Start(nil)
return pm, db, odr, nil return pm, db, odr, nil
} }
......
...@@ -66,8 +66,9 @@ func (s *LesServer) Protocols() []p2p.Protocol { ...@@ -66,8 +66,9 @@ func (s *LesServer) Protocols() []p2p.Protocol {
return s.protocolManager.SubProtocols return s.protocolManager.SubProtocols
} }
func (s *LesServer) Start() { func (s *LesServer) Start(srvr *p2p.Server) {
s.protocolManager.Start() s.protocolManager.Start(srvr)
} }
func (s *LesServer) Stop() { func (s *LesServer) Stop() {
......
...@@ -106,8 +106,8 @@ func NewLightChain(odr OdrBackend, config *core.ChainConfig, pow pow.PoW, mux *e ...@@ -106,8 +106,8 @@ func NewLightChain(odr OdrBackend, config *core.ChainConfig, pow pow.PoW, mux *e
// add trusted CHT // add trusted CHT
if config.DAOForkSupport { if config.DAOForkSupport {
WriteTrustedCht(bc.chainDb, TrustedCht{ WriteTrustedCht(bc.chainDb, TrustedCht{
Number: 564, Number: 612,
Root: common.HexToHash("ee31f7fc21f627dc2b8d3ed8fed5b74dbc393d146a67249a656e163148e39016"), Root: common.HexToHash("8c87a93e0ee531e2aca1b4460e4c201a60c19ffec4f5979262bf14ceeeff8471"),
}) })
} else { } else {
WriteTrustedCht(bc.chainDb, TrustedCht{ WriteTrustedCht(bc.chainDb, TrustedCht{
...@@ -120,8 +120,8 @@ func NewLightChain(odr OdrBackend, config *core.ChainConfig, pow pow.PoW, mux *e ...@@ -120,8 +120,8 @@ func NewLightChain(odr OdrBackend, config *core.ChainConfig, pow pow.PoW, mux *e
if bc.genesisBlock.Hash() == (common.Hash{12, 215, 134, 162, 66, 93, 22, 241, 82, 198, 88, 49, 108, 66, 62, 108, 225, 24, 30, 21, 195, 41, 88, 38, 215, 201, 144, 76, 186, 156, 227, 3}) { if bc.genesisBlock.Hash() == (common.Hash{12, 215, 134, 162, 66, 93, 22, 241, 82, 198, 88, 49, 108, 66, 62, 108, 225, 24, 30, 21, 195, 41, 88, 38, 215, 201, 144, 76, 186, 156, 227, 3}) {
// add trusted CHT for testnet // add trusted CHT for testnet
WriteTrustedCht(bc.chainDb, TrustedCht{ WriteTrustedCht(bc.chainDb, TrustedCht{
Number: 319, Number: 436,
Root: common.HexToHash("43b679ff9b4918b0b19e6256f20e35877365ec3e20b38e3b2a02cef5606176dc"), Root: common.HexToHash("97a12df5d04d72bde4b4b840e1018e4f08aee34b7d0bf2c5dbfc052b86fe7439"),
}) })
glog.V(logger.Info).Infoln("Added trusted CHT for testnet") glog.V(logger.Info).Infoln("Added trusted CHT for testnet")
} else { } else {
......
...@@ -95,12 +95,16 @@ type Config struct { ...@@ -95,12 +95,16 @@ type Config struct {
// or not. Disabling is usually useful for protocol debugging (manual topology). // or not. Disabling is usually useful for protocol debugging (manual topology).
NoDiscovery bool NoDiscovery bool
DiscoveryV5 bool
// Bootstrap nodes used to establish connectivity with the rest of the network. // Bootstrap nodes used to establish connectivity with the rest of the network.
BootstrapNodes []*discover.Node BootstrapNodes []*discover.Node
// Network interface address on which the node should listen for inbound peers. // Network interface address on which the node should listen for inbound peers.
ListenAddr string ListenAddr string
ListenAddrV5 string
// If set to a non-nil value, the given NAT port mapper is used to make the // If set to a non-nil value, the given NAT port mapper is used to make the
// listening port available to the Internet. // listening port available to the Internet.
NAT nat.Interface NAT nat.Interface
......
...@@ -157,11 +157,13 @@ func (n *Node) Start() error { ...@@ -157,11 +157,13 @@ func (n *Node) Start() error {
PrivateKey: n.config.NodeKey(), PrivateKey: n.config.NodeKey(),
Name: n.config.NodeName(), Name: n.config.NodeName(),
Discovery: !n.config.NoDiscovery, Discovery: !n.config.NoDiscovery,
DiscoveryV5: n.config.DiscoveryV5,
BootstrapNodes: n.config.BootstrapNodes, BootstrapNodes: n.config.BootstrapNodes,
StaticNodes: n.config.StaticNodes(), StaticNodes: n.config.StaticNodes(),
TrustedNodes: n.config.TrusterNodes(), TrustedNodes: n.config.TrusterNodes(),
NodeDatabase: n.config.NodeDB(), NodeDatabase: n.config.NodeDB(),
ListenAddr: n.config.ListenAddr, ListenAddr: n.config.ListenAddr,
ListenAddrV5: n.config.ListenAddrV5,
NAT: n.config.NAT, NAT: n.config.NAT,
Dialer: n.config.Dialer, Dialer: n.config.Dialer,
NoDial: n.config.NoDial, NoDial: n.config.NoDial,
......
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package discv5
import (
//"github.com/btcsuite/btcd/btcec"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
)
func S256() *secp256k1.BitCurve {
return secp256k1.S256()
}
// This version should be used for NaCl compilation
/*func S256() *btcec.KoblitzCurve {
return S256()
}*/
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
// Code generated by "stringer -type nodeEvent"; DO NOT EDIT
package discv5
import "fmt"
const (
_nodeEvent_name_0 = "invalidEventpingPacketpongPacketfindnodePacketneighborsPacketfindnodeHashPackettopicRegisterPackettopicQueryPackettopicNodesPacket"
_nodeEvent_name_1 = "pongTimeoutpingTimeoutneighboursTimeout"
)
var (
_nodeEvent_index_0 = [...]uint8{0, 12, 22, 32, 46, 61, 79, 98, 114, 130}
_nodeEvent_index_1 = [...]uint8{0, 11, 22, 39}
)
func (i nodeEvent) String() string {
switch {
case 0 <= i && i <= 8:
return _nodeEvent_name_0[_nodeEvent_index_0[i]:_nodeEvent_index_0[i+1]]
case 265 <= i && i <= 267:
i -= 265
return _nodeEvent_name_1[_nodeEvent_index_1[i]:_nodeEvent_index_1[i+1]]
default:
return fmt.Sprintf("nodeEvent(%d)", i)
}
}
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains the NTP time drift detection via the SNTP protocol:
// https://tools.ietf.org/html/rfc4330
package discv5
import (
"fmt"
"net"
"sort"
"strings"
"time"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
)
const (
ntpPool = "pool.ntp.org" // ntpPool is the NTP server to query for the current time
ntpChecks = 3 // Number of measurements to do against the NTP server
)
// durationSlice attaches the methods of sort.Interface to []time.Duration,
// sorting in increasing order.
type durationSlice []time.Duration
func (s durationSlice) Len() int { return len(s) }
func (s durationSlice) Less(i, j int) bool { return s[i] < s[j] }
func (s durationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// checkClockDrift queries an NTP server for clock drifts and warns the user if
// one large enough is detected.
func checkClockDrift() {
drift, err := sntpDrift(ntpChecks)
if err != nil {
return
}
if drift < -driftThreshold || drift > driftThreshold {
warning := fmt.Sprintf("System clock seems off by %v, which can prevent network connectivity", drift)
howtofix := fmt.Sprintf("Please enable network time synchronisation in system settings")
separator := strings.Repeat("-", len(warning))
glog.V(logger.Warn).Info(separator)
glog.V(logger.Warn).Info(warning)
glog.V(logger.Warn).Info(howtofix)
glog.V(logger.Warn).Info(separator)
} else {
glog.V(logger.Debug).Infof("Sanity NTP check reported %v drift, all ok", drift)
}
}
// sntpDrift does a naive time resolution against an NTP server and returns the
// measured drift. This method uses the simple version of NTP. It's not precise
// but should be fine for these purposes.
//
// Note, it executes two extra measurements compared to the number of requested
// ones to be able to discard the two extremes as outliers.
func sntpDrift(measurements int) (time.Duration, error) {
// Resolve the address of the NTP server
addr, err := net.ResolveUDPAddr("udp", ntpPool+":123")
if err != nil {
return 0, err
}
// Construct the time request (empty package with only 2 fields set):
// Bits 3-5: Protocol version, 3
// Bits 6-8: Mode of operation, client, 3
request := make([]byte, 48)
request[0] = 3<<3 | 3
// Execute each of the measurements
drifts := []time.Duration{}
for i := 0; i < measurements+2; i++ {
// Dial the NTP server and send the time retrieval request
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return 0, err
}
defer conn.Close()
sent := time.Now()
if _, err = conn.Write(request); err != nil {
return 0, err
}
// Retrieve the reply and calculate the elapsed time
conn.SetDeadline(time.Now().Add(5 * time.Second))
reply := make([]byte, 48)
if _, err = conn.Read(reply); err != nil {
return 0, err
}
elapsed := time.Since(sent)
// Reconstruct the time from the reply data
sec := uint64(reply[43]) | uint64(reply[42])<<8 | uint64(reply[41])<<16 | uint64(reply[40])<<24
frac := uint64(reply[47]) | uint64(reply[46])<<8 | uint64(reply[45])<<16 | uint64(reply[44])<<24
nanosec := sec*1e9 + (frac*1e9)>>32
t := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nanosec)).Local()
// Calculate the drift based on an assumed answer time of RRT/2
drifts = append(drifts, sent.Sub(t)+elapsed/2)
}
// Calculate average drif (drop two extremities to avoid outliers)
sort.Sort(durationSlice(drifts))
drift := time.Duration(0)
for i := 1; i < len(drifts)-1; i++ {
drift += drifts[i]
}
return drift / time.Duration(measurements), nil
}
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package discv5
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"testing"
)
func getnacl() (string, error) {
switch runtime.GOARCH {
case "amd64":
_, err := exec.LookPath("sel_ldr_x86_64")
return "amd64p32", err
case "i386":
_, err := exec.LookPath("sel_ldr_i386")
return "i386", err
default:
return "", errors.New("nacl is not supported on " + runtime.GOARCH)
}
}
// runWithPlaygroundTime executes the caller
// in the NaCl sandbox with faketime enabled.
//
// This function must be called from a Test* function
// and the caller must skip the actual test when isHost is true.
func runWithPlaygroundTime(t *testing.T) (isHost bool) {
if runtime.GOOS == "nacl" {
return false
}
// Get the caller.
callerPC, _, _, ok := runtime.Caller(1)
if !ok {
panic("can't get caller")
}
callerFunc := runtime.FuncForPC(callerPC)
if callerFunc == nil {
panic("can't get caller")
}
callerName := callerFunc.Name()[strings.LastIndexByte(callerFunc.Name(), '.')+1:]
if !strings.HasPrefix(callerName, "Test") {
panic("must be called from witin a Test* function")
}
testPattern := "^" + callerName + "$"
// Unfortunately runtime.faketime (playground time mode) only works on NaCl. The NaCl
// SDK must be installed and linked into PATH for this to work.
arch, err := getnacl()
if err != nil {
t.Skip(err)
}
// Compile and run the calling test using NaCl.
// The extra tag ensures that the TestMain function in sim_main_test.go is used.
cmd := exec.Command("go", "test", "-v", "-tags", "faketime_simulation", "-timeout", "100h", "-run", testPattern, ".")
cmd.Env = append([]string{"GOOS=nacl", "GOARCH=" + arch}, os.Environ()...)
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
go skipPlaygroundOutputHeaders(os.Stdout, stdout)
go skipPlaygroundOutputHeaders(os.Stderr, stderr)
if err := cmd.Run(); err != nil {
t.Error(err)
}
// Ensure that the test function doesn't run in the (non-NaCl) host process.
return true
}
func skipPlaygroundOutputHeaders(out io.Writer, in io.Reader) {
// Additional output can be printed without the headers
// before the NaCl binary starts running (e.g. compiler error messages).
bufin := bufio.NewReader(in)
output, err := bufin.ReadBytes(0)
output = bytes.TrimSuffix(output, []byte{0})
if len(output) > 0 {
out.Write(output)
}
if err != nil {
return
}
bufin.UnreadByte()
// Playback header: 0 0 P B <8-byte time> <4-byte data length>
head := make([]byte, 4+8+4)
for {
if _, err := io.ReadFull(bufin, head); err != nil {
if err != io.EOF {
fmt.Fprintln(out, "read error:", err)
}
return
}
if !bytes.HasPrefix(head, []byte{0x00, 0x00, 'P', 'B'}) {
fmt.Fprintf(out, "expected playback header, got %q\n", head)
io.Copy(out, bufin)
return
}
// Copy data until next header.
size := binary.BigEndian.Uint32(head[12:])
io.CopyN(out, bufin, int64(size))
}
}
This diff is collapsed.
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build go1.4,nacl,faketime_simulation
package discv5
import (
"os"
"runtime"
"testing"
"unsafe"
)
// Enable fake time mode in the runtime, like on the go playground.
// There is a slight chance that this won't work because some go code
// might have executed before the variable is set.
//go:linkname faketime runtime.faketime
var faketime = 1
func TestMain(m *testing.M) {
// We need to use unsafe somehow in order to get access to go:linkname.
_ = unsafe.Sizeof(0)
// Run the actual test. runWithPlaygroundTime ensures that the only test
// that runs is the one calling it.
runtime.GOMAXPROCS(8)
os.Exit(m.Run())
}
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package discv5 implements the RLPx v5 Topic Discovery Protocol.
//
// The Topic Discovery protocol provides a way to find RLPx nodes that
// can be connected to. It uses a Kademlia-like protocol to maintain a
// distributed database of the IDs and endpoints of all listening
// nodes.
package discv5
import (
"crypto/rand"
"encoding/binary"
"net"
"sort"
"github.com/ethereum/go-ethereum/common"
)
const (
alpha = 3 // Kademlia concurrency factor
bucketSize = 16 // Kademlia bucket size
hashBits = len(common.Hash{}) * 8
nBuckets = hashBits + 1 // Number of buckets
maxBondingPingPongs = 16
maxFindnodeFailures = 5
)
type Table struct {
count int // number of nodes
buckets [nBuckets]*bucket // index of known nodes by distance
nodeAddedHook func(*Node) // for testing
self *Node // metadata of the local node
}
// bucket contains nodes, ordered by their last activity. the entry
// that was most recently active is the first element in entries.
type bucket struct {
entries []*Node
replacements []*Node
}
func newTable(ourID NodeID, ourAddr *net.UDPAddr) *Table {
self := NewNode(ourID, ourAddr.IP, uint16(ourAddr.Port), uint16(ourAddr.Port))
tab := &Table{self: self}
for i := range tab.buckets {
tab.buckets[i] = new(bucket)
}
return tab
}
func (tab *Table) chooseBucketFillTarget() common.Hash {
bucketCount := nBuckets
for bucketCount > 0 && len(tab.buckets[nBuckets-bucketCount].entries) == 0 {
bucketCount--
}
var bucket int
for {
// select a target hash that could go into a certain randomly selected bucket
// buckets are chosen with an even chance out of the existing ones that contain
// less that bucketSize entries, plus a potential new one beyond these
bucket = nBuckets - 1 - int(randUint(uint32(bucketCount+1)))
if bucket == bucketCount || len(tab.buckets[bucket].entries) < bucketSize {
break
}
}
// calculate target that has the desired log distance from our own address hash
target := tab.self.sha.Bytes()
prefix := binary.BigEndian.Uint64(target[0:8])
shift := uint(nBuckets - 1 - bucket)
if bucket != bucketCount {
shift++
}
var b [8]byte
rand.Read(b[:])
rnd := binary.BigEndian.Uint64(b[:])
rndMask := (^uint64(0)) >> shift
addrMask := ^rndMask
xorMask := uint64(0)
if bucket != bucketCount {
xorMask = rndMask + 1
}
prefix = (prefix&addrMask ^ xorMask) | (rnd & rndMask)
binary.BigEndian.PutUint64(target[0:8], prefix)
rand.Read(target[8:])
return common.BytesToHash(target)
}
// readRandomNodes fills the given slice with random nodes from the
// table. It will not write the same node more than once. The nodes in
// the slice are copies and can be modified by the caller.
func (tab *Table) readRandomNodes(buf []*Node) (n int) {
// TODO: tree-based buckets would help here
// Find all non-empty buckets and get a fresh slice of their entries.
var buckets [][]*Node
for _, b := range tab.buckets {
if len(b.entries) > 0 {
buckets = append(buckets, b.entries[:])
}
}
if len(buckets) == 0 {
return 0
}
// Shuffle the buckets.
for i := uint32(len(buckets)) - 1; i > 0; i-- {
j := randUint(i)
buckets[i], buckets[j] = buckets[j], buckets[i]
}
// Move head of each bucket into buf, removing buckets that become empty.
var i, j int
for ; i < len(buf); i, j = i+1, (j+1)%len(buckets) {
b := buckets[j]
buf[i] = &(*b[0])
buckets[j] = b[1:]
if len(b) == 1 {
buckets = append(buckets[:j], buckets[j+1:]...)
}
if len(buckets) == 0 {
break
}
}
return i + 1
}
func randUint(max uint32) uint32 {
if max < 2 {
return 0
}
var b [4]byte
rand.Read(b[:])
return binary.BigEndian.Uint32(b[:]) % max
}
func randUint64n(max uint64) uint64 {
if max < 2 {
return 0
}
var b [8]byte
rand.Read(b[:])
return binary.BigEndian.Uint64(b[:]) % max
}
// closest returns the n nodes in the table that are closest to the
// given id. The caller must hold tab.mutex.
func (tab *Table) closest(target common.Hash, nresults int) *nodesByDistance {
// This is a very wasteful way to find the closest nodes but
// obviously correct. I believe that tree-based buckets would make
// this easier to implement efficiently.
close := &nodesByDistance{target: target}
for _, b := range tab.buckets {
for _, n := range b.entries {
close.push(n, nresults)
}
}
return close
}
// add attempts to add the given node its corresponding bucket. If the
// bucket has space available, adding the node succeeds immediately.
// Otherwise, the node is added to the replacement cache for the bucket.
func (tab *Table) add(n *Node) (contested *Node) {
b := tab.buckets[logdist(tab.self.sha, n.sha)]
switch {
case b.bump(n):
// n exists in b.
return nil
case len(b.entries) < bucketSize:
// b has space available.
b.addFront(n)
tab.count++
if tab.nodeAddedHook != nil {
tab.nodeAddedHook(n)
}
return nil
default:
// b has no space left, add to replacement cache
// and revalidate the last entry.
// TODO: drop previous node
b.replacements = append(b.replacements, n)
if len(b.replacements) > bucketSize {
copy(b.replacements, b.replacements[1:])
b.replacements = b.replacements[:len(b.replacements)-1]
}
return b.entries[len(b.entries)-1]
}
}
// stuff adds nodes the table to the end of their corresponding bucket
// if the bucket is not full.
func (tab *Table) stuff(nodes []*Node) {
outer:
for _, n := range nodes {
if n.ID == tab.self.ID {
continue // don't add self
}
bucket := tab.buckets[logdist(tab.self.sha, n.sha)]
for i := range bucket.entries {
if bucket.entries[i].ID == n.ID {
continue outer // already in bucket
}
}
if len(bucket.entries) < bucketSize {
bucket.entries = append(bucket.entries, n)
tab.count++
if tab.nodeAddedHook != nil {
tab.nodeAddedHook(n)
}
}
}
}
// delete removes an entry from the node table (used to evacuate
// failed/non-bonded discovery peers).
func (tab *Table) delete(node *Node) {
bucket := tab.buckets[logdist(tab.self.sha, node.sha)]
for i := range bucket.entries {
if bucket.entries[i].ID == node.ID {
bucket.entries = append(bucket.entries[:i], bucket.entries[i+1:]...)
tab.count--
return
}
}
}
func (tab *Table) deleteReplace(node *Node) {
b := tab.buckets[logdist(tab.self.sha, node.sha)]
i := 0
for i < len(b.entries) {
if b.entries[i].ID == node.ID {
b.entries = append(b.entries[:i], b.entries[i+1:]...)
tab.count--
} else {
i++
}
}
// refill from replacement cache
// TODO: maybe use random index
if len(b.entries) < bucketSize && len(b.replacements) > 0 {
ri := len(b.replacements) - 1
b.addFront(b.replacements[ri])
tab.count++
b.replacements[ri] = nil
b.replacements = b.replacements[:ri]
}
}
func (b *bucket) addFront(n *Node) {
b.entries = append(b.entries, nil)
copy(b.entries[1:], b.entries)
b.entries[0] = n
}
func (b *bucket) bump(n *Node) bool {
for i := range b.entries {
if b.entries[i].ID == n.ID {
// move it to the front
copy(b.entries[1:], b.entries[:i])
b.entries[0] = n
return true
}
}
return false
}
// nodesByDistance is a list of nodes, ordered by
// distance to target.
type nodesByDistance struct {
entries []*Node
target common.Hash
}
// push adds the given node to the list, keeping the total size below maxElems.
func (h *nodesByDistance) push(n *Node, maxElems int) {
ix := sort.Search(len(h.entries), func(i int) bool {
return distcmp(h.target, h.entries[i].sha, n.sha) > 0
})
if len(h.entries) < maxElems {
h.entries = append(h.entries, n)
}
if ix == len(h.entries) {
// farther away than all nodes we already have.
// if there was room for it, the node is now the last element.
} else {
// slide existing entries down to make room
// this will overwrite the entry we just appended.
copy(h.entries[ix+1:], h.entries[ix:])
h.entries[ix] = n
}
}
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package discv5
import (
"crypto/ecdsa"
"fmt"
"math/rand"
"net"
"reflect"
"testing"
"testing/quick"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
type nullTransport struct{}
func (nullTransport) sendPing(remote *Node, remoteAddr *net.UDPAddr) []byte { return []byte{1} }
func (nullTransport) sendPong(remote *Node, pingHash []byte) {}
func (nullTransport) sendFindnode(remote *Node, target NodeID) {}
func (nullTransport) sendNeighbours(remote *Node, nodes []*Node) {}
func (nullTransport) localAddr() *net.UDPAddr { return new(net.UDPAddr) }
func (nullTransport) Close() {}
// func TestTable_pingReplace(t *testing.T) {
// doit := func(newNodeIsResponding, lastInBucketIsResponding bool) {
// transport := newPingRecorder()
// tab, _ := newTable(transport, NodeID{}, &net.UDPAddr{})
// defer tab.Close()
// pingSender := NewNode(MustHexID("a502af0f59b2aab7746995408c79e9ca312d2793cc997e44fc55eda62f0150bbb8c59a6f9269ba3a081518b62699ee807c7c19c20125ddfccca872608af9e370"), net.IP{}, 99, 99)
//
// // fill up the sender's bucket.
// last := fillBucket(tab, 253)
//
// // this call to bond should replace the last node
// // in its bucket if the node is not responding.
// transport.responding[last.ID] = lastInBucketIsResponding
// transport.responding[pingSender.ID] = newNodeIsResponding
// tab.bond(true, pingSender.ID, &net.UDPAddr{}, 0)
//
// // first ping goes to sender (bonding pingback)
// if !transport.pinged[pingSender.ID] {
// t.Error("table did not ping back sender")
// }
// if newNodeIsResponding {
// // second ping goes to oldest node in bucket
// // to see whether it is still alive.
// if !transport.pinged[last.ID] {
// t.Error("table did not ping last node in bucket")
// }
// }
//
// tab.mutex.Lock()
// defer tab.mutex.Unlock()
// if l := len(tab.buckets[253].entries); l != bucketSize {
// t.Errorf("wrong bucket size after bond: got %d, want %d", l, bucketSize)
// }
//
// if lastInBucketIsResponding || !newNodeIsResponding {
// if !contains(tab.buckets[253].entries, last.ID) {
// t.Error("last entry was removed")
// }
// if contains(tab.buckets[253].entries, pingSender.ID) {
// t.Error("new entry was added")
// }
// } else {
// if contains(tab.buckets[253].entries, last.ID) {
// t.Error("last entry was not removed")
// }
// if !contains(tab.buckets[253].entries, pingSender.ID) {
// t.Error("new entry was not added")
// }
// }
// }
//
// doit(true, true)
// doit(false, true)
// doit(true, false)
// doit(false, false)
// }
func TestBucket_bumpNoDuplicates(t *testing.T) {
t.Parallel()
cfg := &quick.Config{
MaxCount: 1000,
Rand: rand.New(rand.NewSource(time.Now().Unix())),
Values: func(args []reflect.Value, rand *rand.Rand) {
// generate a random list of nodes. this will be the content of the bucket.
n := rand.Intn(bucketSize-1) + 1
nodes := make([]*Node, n)
for i := range nodes {
nodes[i] = nodeAtDistance(common.Hash{}, 200)
}
args[0] = reflect.ValueOf(nodes)
// generate random bump positions.
bumps := make([]int, rand.Intn(100))
for i := range bumps {
bumps[i] = rand.Intn(len(nodes))
}
args[1] = reflect.ValueOf(bumps)
},
}
prop := func(nodes []*Node, bumps []int) (ok bool) {
b := &bucket{entries: make([]*Node, len(nodes))}
copy(b.entries, nodes)
for i, pos := range bumps {
b.bump(b.entries[pos])
if hasDuplicates(b.entries) {
t.Logf("bucket has duplicates after %d/%d bumps:", i+1, len(bumps))
for _, n := range b.entries {
t.Logf(" %p", n)
}
return false
}
}
return true
}
if err := quick.Check(prop, cfg); err != nil {
t.Error(err)
}
}
// fillBucket inserts nodes into the given bucket until
// it is full. The node's IDs dont correspond to their
// hashes.
func fillBucket(tab *Table, ld int) (last *Node) {
b := tab.buckets[ld]
for len(b.entries) < bucketSize {
b.entries = append(b.entries, nodeAtDistance(tab.self.sha, ld))
}
return b.entries[bucketSize-1]
}
// nodeAtDistance creates a node for which logdist(base, n.sha) == ld.
// The node's ID does not correspond to n.sha.
func nodeAtDistance(base common.Hash, ld int) (n *Node) {
n = new(Node)
n.sha = hashAtDistance(base, ld)
copy(n.ID[:], n.sha[:]) // ensure the node still has a unique ID
return n
}
type pingRecorder struct{ responding, pinged map[NodeID]bool }
func newPingRecorder() *pingRecorder {
return &pingRecorder{make(map[NodeID]bool), make(map[NodeID]bool)}
}
func (t *pingRecorder) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) {
panic("findnode called on pingRecorder")
}
func (t *pingRecorder) close() {}
func (t *pingRecorder) waitping(from NodeID) error {
return nil // remote always pings
}
func (t *pingRecorder) ping(toid NodeID, toaddr *net.UDPAddr) error {
t.pinged[toid] = true
if t.responding[toid] {
return nil
} else {
return errTimeout
}
}
func TestTable_closest(t *testing.T) {
t.Parallel()
test := func(test *closeTest) bool {
// for any node table, Target and N
tab := newTable(test.Self, &net.UDPAddr{})
tab.stuff(test.All)
// check that doClosest(Target, N) returns nodes
result := tab.closest(test.Target, test.N).entries
if hasDuplicates(result) {
t.Errorf("result contains duplicates")
return false
}
if !sortedByDistanceTo(test.Target, result) {
t.Errorf("result is not sorted by distance to target")
return false
}
// check that the number of results is min(N, tablen)
wantN := test.N
if tab.count < test.N {
wantN = tab.count
}
if len(result) != wantN {
t.Errorf("wrong number of nodes: got %d, want %d", len(result), wantN)
return false
} else if len(result) == 0 {
return true // no need to check distance
}
// check that the result nodes have minimum distance to target.
for _, b := range tab.buckets {
for _, n := range b.entries {
if contains(result, n.ID) {
continue // don't run the check below for nodes in result
}
farthestResult := result[len(result)-1].sha
if distcmp(test.Target, n.sha, farthestResult) < 0 {
t.Errorf("table contains node that is closer to target but it's not in result")
t.Logf(" Target: %v", test.Target)
t.Logf(" Farthest Result: %v", farthestResult)
t.Logf(" ID: %v", n.ID)
return false
}
}
}
return true
}
if err := quick.Check(test, quickcfg()); err != nil {
t.Error(err)
}
}
func TestTable_ReadRandomNodesGetAll(t *testing.T) {
cfg := &quick.Config{
MaxCount: 200,
Rand: rand.New(rand.NewSource(time.Now().Unix())),
Values: func(args []reflect.Value, rand *rand.Rand) {
args[0] = reflect.ValueOf(make([]*Node, rand.Intn(1000)))
},
}
test := func(buf []*Node) bool {
tab := newTable(NodeID{}, &net.UDPAddr{})
for i := 0; i < len(buf); i++ {
ld := cfg.Rand.Intn(len(tab.buckets))
tab.stuff([]*Node{nodeAtDistance(tab.self.sha, ld)})
}
gotN := tab.readRandomNodes(buf)
if gotN != tab.count {
t.Errorf("wrong number of nodes, got %d, want %d", gotN, tab.count)
return false
}
if hasDuplicates(buf[:gotN]) {
t.Errorf("result contains duplicates")
return false
}
return true
}
if err := quick.Check(test, cfg); err != nil {
t.Error(err)
}
}
type closeTest struct {
Self NodeID
Target common.Hash
All []*Node
N int
}
func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value {
t := &closeTest{
Self: gen(NodeID{}, rand).(NodeID),
Target: gen(common.Hash{}, rand).(common.Hash),
N: rand.Intn(bucketSize),
}
for _, id := range gen([]NodeID{}, rand).([]NodeID) {
t.All = append(t.All, &Node{ID: id})
}
return reflect.ValueOf(t)
}
func hasDuplicates(slice []*Node) bool {
seen := make(map[NodeID]bool)
for i, e := range slice {
if e == nil {
panic(fmt.Sprintf("nil *Node at %d", i))
}
if seen[e.ID] {
return true
}
seen[e.ID] = true
}
return false
}
func sortedByDistanceTo(distbase common.Hash, slice []*Node) bool {
var last common.Hash
for i, e := range slice {
if i > 0 && distcmp(distbase, e.sha, last) < 0 {
return false
}
last = e.sha
}
return true
}
func contains(ns []*Node, id NodeID) bool {
for _, n := range ns {
if n.ID == id {
return true
}
}
return false
}
// gen wraps quick.Value so it's easier to use.
// it generates a random value of the given value's type.
func gen(typ interface{}, rand *rand.Rand) interface{} {
v, ok := quick.Value(reflect.TypeOf(typ), rand)
if !ok {
panic(fmt.Sprintf("couldn't generate random value of type %T", typ))
}
return v.Interface()
}
func newkey() *ecdsa.PrivateKey {
key, err := crypto.GenerateKey()
if err != nil {
panic("couldn't generate key: " + err.Error())
}
return key
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment