cmd, eth, p2p: fix review issues enumerated by Felix

This commit is contained in:
Péter Szilágyi 2015-04-30 16:15:29 +03:00
parent 1528dbc171
commit 701591b403
8 changed files with 25 additions and 34 deletions

View File

@ -25,7 +25,7 @@ func (js *jsre) adminBindings() {
js.re.Set("admin", struct{}{}) js.re.Set("admin", struct{}{})
t, _ := js.re.Get("admin") t, _ := js.re.Get("admin")
admin := t.Object() admin := t.Object()
admin.Set("trustPeer", js.trustPeer) admin.Set("addPeer", js.addPeer)
admin.Set("startRPC", js.startRPC) admin.Set("startRPC", js.startRPC)
admin.Set("stopRPC", js.stopRPC) admin.Set("stopRPC", js.stopRPC)
admin.Set("nodeInfo", js.nodeInfo) admin.Set("nodeInfo", js.nodeInfo)
@ -243,13 +243,13 @@ func (js *jsre) stopRPC(call otto.FunctionCall) otto.Value {
return otto.FalseValue() return otto.FalseValue()
} }
func (js *jsre) trustPeer(call otto.FunctionCall) otto.Value { func (js *jsre) addPeer(call otto.FunctionCall) otto.Value {
nodeURL, err := call.Argument(0).ToString() nodeURL, err := call.Argument(0).ToString()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return otto.FalseValue() return otto.FalseValue()
} }
err = js.ethereum.TrustPeer(nodeURL) err = js.ethereum.AddPeer(nodeURL)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return otto.FalseValue() return otto.FalseValue()

View File

@ -232,7 +232,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.IdentityFlag, utils.IdentityFlag,
utils.UnlockedAccountFlag, utils.UnlockedAccountFlag,
utils.PasswordFileFlag, utils.PasswordFileFlag,
utils.BootNodesFlag, utils.BootnodesFlag,
utils.DataDirFlag, utils.DataDirFlag,
utils.BlockchainVersionFlag, utils.BlockchainVersionFlag,
utils.JSpathFlag, utils.JSpathFlag,

View File

@ -69,7 +69,7 @@ func init() {
assetPathFlag, assetPathFlag,
rpcCorsFlag, rpcCorsFlag,
utils.BootNodesFlag, utils.BootnodesFlag,
utils.DataDirFlag, utils.DataDirFlag,
utils.ListenPortFlag, utils.ListenPortFlag,
utils.LogFileFlag, utils.LogFileFlag,

View File

@ -104,7 +104,7 @@ func (ui *UiLib) Connect(button qml.Object) {
} }
func (ui *UiLib) ConnectToPeer(nodeURL string) { func (ui *UiLib) ConnectToPeer(nodeURL string) {
if err := ui.eth.TrustPeer(nodeURL); err != nil { if err := ui.eth.AddPeer(nodeURL); err != nil {
guilogger.Infoln("TrustPeer error: " + err.Error()) guilogger.Infoln("TrustPeer error: " + err.Error())
} }
} }

View File

@ -202,7 +202,7 @@ var (
Usage: "Network listening port", Usage: "Network listening port",
Value: 30303, Value: 30303,
} }
BootNodesFlag = cli.StringFlag{ BootnodesFlag = cli.StringFlag{
Name: "bootnodes", Name: "bootnodes",
Usage: "Space-separated enode URLs for p2p discovery bootstrap", Usage: "Space-separated enode URLs for p2p discovery bootstrap",
Value: "", Value: "",
@ -292,7 +292,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
NodeKey: GetNodeKey(ctx), NodeKey: GetNodeKey(ctx),
Shh: ctx.GlobalBool(WhisperEnabledFlag.Name), Shh: ctx.GlobalBool(WhisperEnabledFlag.Name),
Dial: true, Dial: true,
BootNodes: ctx.GlobalString(BootNodesFlag.Name), BootNodes: ctx.GlobalString(BootnodesFlag.Name),
} }
} }

View File

@ -108,7 +108,6 @@ func (cfg *Config) parseTrustedNodes() []*discover.Node {
// Short circuit if no trusted node config is present // Short circuit if no trusted node config is present
path := filepath.Join(cfg.DataDir, trustedNodes) path := filepath.Join(cfg.DataDir, trustedNodes)
if _, err := os.Stat(path); err != nil { if _, err := os.Stat(path); err != nil {
fmt.Println("nodes", nil)
return nil return nil
} }
// Load the trusted nodes from the config file // Load the trusted nodes from the config file
@ -122,7 +121,6 @@ func (cfg *Config) parseTrustedNodes() []*discover.Node {
glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err) glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err)
return nil return nil
} }
fmt.Println("nodes", nodelist)
// Interpret the list as a discovery node array // Interpret the list as a discovery node array
var nodes []*discover.Node var nodes []*discover.Node
for _, url := range nodelist { for _, url := range nodelist {
@ -486,13 +484,15 @@ func (s *Ethereum) StartForTest() {
s.txPool.Start() s.txPool.Start()
} }
// TrustPeer injects a new node into the list of privileged nodes. // AddPeer connects to the given node and maintains the connection until the
func (self *Ethereum) TrustPeer(nodeURL string) error { // server is shut down. If the connection fails for any reason, the server will
// attempt to reconnect the peer.
func (self *Ethereum) AddPeer(nodeURL string) error {
n, err := discover.ParseNode(nodeURL) n, err := discover.ParseNode(nodeURL)
if err != nil { if err != nil {
return fmt.Errorf("invalid node URL: %v", err) return fmt.Errorf("invalid node URL: %v", err)
} }
self.net.TrustPeer(n) self.net.AddPeer(n)
return nil return nil
} }

View File

@ -100,10 +100,9 @@ type Server struct {
ourHandshake *protoHandshake ourHandshake *protoHandshake
lock sync.RWMutex // protects running and peers lock sync.RWMutex // protects running, peers and the trust fields
running bool running bool
peers map[discover.NodeID]*Peer peers map[discover.NodeID]*Peer
trusts map[discover.NodeID]*discover.Node // Map of currently trusted remote nodes trusts map[discover.NodeID]*discover.Node // Map of currently trusted remote nodes
trustDial chan *discover.Node // Dial request channel reserved for the trusted nodes trustDial chan *discover.Node // Dial request channel reserved for the trusted nodes
@ -138,8 +137,10 @@ func (srv *Server) PeerCount() int {
return n return n
} }
// TrustPeer inserts a node into the list of privileged nodes. // AddPeer connects to the given node and maintains the connection until the
func (srv *Server) TrustPeer(node *discover.Node) { // server is shut down. If the connection fails for any reason, the server will
// attempt to reconnect the peer.
func (srv *Server) AddPeer(node *discover.Node) {
srv.lock.Lock() srv.lock.Lock()
defer srv.lock.Unlock() defer srv.lock.Unlock()
@ -246,7 +247,7 @@ func (srv *Server) Start() (err error) {
glog.V(logger.Warn).Infoln("I will be kind-of useless, neither dialing nor listening.") glog.V(logger.Warn).Infoln("I will be kind-of useless, neither dialing nor listening.")
} }
// maintain the trusted peers // maintain the trusted peers
go srv.trustLoop() go srv.trustedNodesLoop()
srv.running = true srv.running = true
return nil return nil
@ -341,16 +342,13 @@ func (srv *Server) listenLoop() {
} }
} }
// trustLoop is responsible for periodically checking that trusted connections // trustedNodesLoop is responsible for periodically checking that trusted
// are actually live, and requests dialing if not. // connections are actually live, and requests dialing if not.
func (srv *Server) trustLoop() { func (srv *Server) trustedNodesLoop() {
// Create a ticker for verifying trusted connections
tick := time.Tick(trustedPeerCheckInterval) tick := time.Tick(trustedPeerCheckInterval)
for { for {
select { select {
case <-srv.quit: case <-srv.quit:
// Termination requested, simple return
return return
case <-tick: case <-tick:
@ -369,10 +367,7 @@ func (srv *Server) trustLoop() {
glog.V(logger.Debug).Infof("Dialing trusted peer %v", node) glog.V(logger.Debug).Infof("Dialing trusted peer %v", node)
select { select {
case srv.trustDial <- node: case srv.trustDial <- node:
// Ok, dialing
case <-srv.quit: case <-srv.quit:
// Terminating, return
return return
} }
} }
@ -547,16 +542,12 @@ func (srv *Server) checkPeer(id discover.NodeID) (bool, DiscReason) {
switch { switch {
case !srv.running: case !srv.running:
return false, DiscQuitting return false, DiscQuitting
case !trusted && len(srv.peers) >= srv.MaxPeers: case !trusted && len(srv.peers) >= srv.MaxPeers:
return false, DiscTooManyPeers return false, DiscTooManyPeers
case srv.peers[id] != nil: case srv.peers[id] != nil:
return false, DiscAlreadyConnected return false, DiscAlreadyConnected
case id == srv.ntab.Self().ID: case id == srv.ntab.Self().ID:
return false, DiscSelf return false, DiscSelf
default: default:
return true, 0 return true, 0
} }

View File

@ -260,7 +260,7 @@ func TestServerTrustedPeers(t *testing.T) {
trusted := &discover.Node{ trusted := &discover.Node{
ID: discover.PubkeyID(&key.PublicKey), ID: discover.PubkeyID(&key.PublicKey),
} }
server.TrustPeer(trusted) server.AddPeer(trusted)
conn, err := dialer.Dial("tcp", server.ListenAddr) conn, err := dialer.Dial("tcp", server.ListenAddr)
if err != nil { if err != nil {