Fix 'add/delnode' type switch evaluation in server
* The cases for the 'addnode' command were previously stacked on top the new cases for the 'node' command. The intended behavior was to create a fall through and handle both commands. However, trying to use this syntax with a type switch caused the first case to be ignored. * addnode' specific functions and structs in the server have been removed. Instead, the 'add' and 'del' subcommands are now proxied to the matching 'node' cmd functions.
This commit is contained in:
parent
99ac1f5667
commit
ab2ed710cb
2 changed files with 3 additions and 42 deletions
|
@ -364,11 +364,11 @@ func handleAddNode(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (in
|
||||||
var err error
|
var err error
|
||||||
switch c.SubCmd {
|
switch c.SubCmd {
|
||||||
case "add":
|
case "add":
|
||||||
err = s.server.AddAddr(addr, true)
|
err = s.server.ConnectNode(addr, true)
|
||||||
case "remove":
|
case "remove":
|
||||||
err = s.server.RemoveAddr(addr)
|
err = s.server.RemoveNodeByAddr(addr)
|
||||||
case "onetry":
|
case "onetry":
|
||||||
err = s.server.AddAddr(addr, false)
|
err = s.server.ConnectNode(addr, false)
|
||||||
default:
|
default:
|
||||||
return nil, &btcjson.RPCError{
|
return nil, &btcjson.RPCError{
|
||||||
Code: btcjson.ErrRPCInvalidParameter,
|
Code: btcjson.ErrRPCInvalidParameter,
|
||||||
|
|
39
server.go
39
server.go
|
@ -404,17 +404,6 @@ type getPeerInfoMsg struct {
|
||||||
reply chan []*btcjson.GetPeerInfoResult
|
reply chan []*btcjson.GetPeerInfoResult
|
||||||
}
|
}
|
||||||
|
|
||||||
type addNodeMsg struct {
|
|
||||||
addr string
|
|
||||||
permanent bool
|
|
||||||
reply chan error
|
|
||||||
}
|
|
||||||
|
|
||||||
type delNodeMsg struct {
|
|
||||||
cmp func(*peer) bool
|
|
||||||
reply chan error
|
|
||||||
}
|
|
||||||
|
|
||||||
type getAddedNodesMsg struct {
|
type getAddedNodesMsg struct {
|
||||||
reply chan []*peer
|
reply chan []*peer
|
||||||
}
|
}
|
||||||
|
@ -490,7 +479,6 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
||||||
})
|
})
|
||||||
msg.reply <- infos
|
msg.reply <- infos
|
||||||
|
|
||||||
case addNodeMsg:
|
|
||||||
case connectNodeMsg:
|
case connectNodeMsg:
|
||||||
// XXX(oga) duplicate oneshots?
|
// XXX(oga) duplicate oneshots?
|
||||||
for e := state.persistentPeers.Front(); e != nil; e = e.Next() {
|
for e := state.persistentPeers.Front(); e != nil; e = e.Next() {
|
||||||
|
@ -512,8 +500,6 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
||||||
} else {
|
} else {
|
||||||
msg.reply <- errors.New("failed to add peer")
|
msg.reply <- errors.New("failed to add peer")
|
||||||
}
|
}
|
||||||
|
|
||||||
case delNodeMsg:
|
|
||||||
case removeNodeMsg:
|
case removeNodeMsg:
|
||||||
found := disconnectPeer(state.persistentPeers, msg.cmp, func(p *peer) {
|
found := disconnectPeer(state.persistentPeers, msg.cmp, func(p *peer) {
|
||||||
// Keep group counts ok since we remove from
|
// Keep group counts ok since we remove from
|
||||||
|
@ -526,7 +512,6 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
||||||
} else {
|
} else {
|
||||||
msg.reply <- errors.New("peer not found")
|
msg.reply <- errors.New("peer not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request a list of the persistent (added) peers.
|
// Request a list of the persistent (added) peers.
|
||||||
case getAddedNodesMsg:
|
case getAddedNodesMsg:
|
||||||
// Respond with a slice of the relavent peers.
|
// Respond with a slice of the relavent peers.
|
||||||
|
@ -880,30 +865,6 @@ func (s *server) PeerInfo() []*btcjson.GetPeerInfoResult {
|
||||||
return <-replyChan
|
return <-replyChan
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAddr adds `addr' as a new outbound peer. If permanent is true then the
|
|
||||||
// peer will be persistent and reconnect if the connection is lost.
|
|
||||||
// It is an error to call this with an already existing peer.
|
|
||||||
func (s *server) AddAddr(addr string, permanent bool) error {
|
|
||||||
replyChan := make(chan error)
|
|
||||||
|
|
||||||
s.query <- addNodeMsg{addr: addr, permanent: permanent, reply: replyChan}
|
|
||||||
|
|
||||||
return <-replyChan
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveAddr removes `addr' from the list of persistent peers if present.
|
|
||||||
// An error will be returned if the peer was not found.
|
|
||||||
func (s *server) RemoveAddr(addr string) error {
|
|
||||||
replyChan := make(chan error)
|
|
||||||
|
|
||||||
s.query <- delNodeMsg{
|
|
||||||
cmp: func(p *peer) bool { return p.addr == addr },
|
|
||||||
reply: replyChan,
|
|
||||||
}
|
|
||||||
|
|
||||||
return <-replyChan
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisconnectNodeByAddr disconnects a peer by target address. Both outbound and
|
// DisconnectNodeByAddr disconnects a peer by target address. Both outbound and
|
||||||
// inbound nodes will be searched for the target node. An error message will
|
// inbound nodes will be searched for the target node. An error message will
|
||||||
// be returned if the peer was not found.
|
// be returned if the peer was not found.
|
||||||
|
|
Loading…
Reference in a new issue