Refactor peer message sending into single interface method .Send() (#40)

* Adds Send method to Peer interface and removes redundant methods

* Fix imports

* Bumps version
This commit is contained in:
Steven Jack 2018-03-04 13:47:56 +00:00 committed by Anthony De Meulemeester
parent e09c870f7f
commit 42195b1af4
6 changed files with 30 additions and 101 deletions

View file

@ -1 +1 @@
0.21.0
0.23.0

View file

@ -11,13 +11,8 @@ type Peer interface {
id() uint32
addr() util.Endpoint
disconnect()
Send(*Message) error
version() *payload.Version
callVersion(*Message) error
callGetaddr(*Message) error
callVerack(*Message) error
callGetdata(*Message) error
callGetblocks(*Message) error
callGetheaders(*Message) error
}
// LocalPeer is the simplest kind of peer, mapped to a server in the
@ -35,36 +30,23 @@ func NewLocalPeer(s *Server) *LocalPeer {
return &LocalPeer{endpoint: e, s: s}
}
func (p *LocalPeer) Send(msg *Message) error {
switch msg.commandType() {
case cmdVersion:
version := msg.Payload.(*payload.Version)
return p.s.handleVersionCmd(version, p)
case cmdGetAddr:
return p.s.handleGetaddrCmd(msg, p)
default:
return nil
}
}
// Version implements the Peer interface.
func (p *LocalPeer) version() *payload.Version {
return p.pVersion
}
func (p *LocalPeer) callVersion(msg *Message) error {
version := msg.Payload.(*payload.Version)
return p.s.handleVersionCmd(version, p)
}
func (p *LocalPeer) callVerack(msg *Message) error {
return nil
}
func (p *LocalPeer) callGetaddr(msg *Message) error {
return p.s.handleGetaddrCmd(msg, p)
}
func (p *LocalPeer) callGetblocks(msg *Message) error {
return nil
}
func (p *LocalPeer) callGetheaders(msg *Message) error {
return nil
}
func (p *LocalPeer) callGetdata(msg *Message) error {
return nil
}
func (p *LocalPeer) id() uint32 { return p.nonce }
func (p *LocalPeer) addr() util.Endpoint { return p.endpoint }
func (p *LocalPeer) disconnect() {}

View file

@ -209,7 +209,7 @@ func (s *Server) handlePeerConnected(p Peer) error {
// TODO: get the blockheight of this server once core implemented this.
payload := payload.NewVersion(s.id, s.port, s.userAgent, s.bc.HeaderHeight(), s.relay)
msg := newMessage(s.net, cmdVersion, payload)
return p.callVersion(msg)
return p.Send(msg)
}
func (s *Server) handleVersionCmd(version *payload.Version, p Peer) error {
@ -220,7 +220,9 @@ func (s *Server) handleVersionCmd(version *payload.Version, p Peer) error {
return fmt.Errorf("port mismatch: %d and %d", version.Port, p.addr().Port)
}
return p.callVerack(newMessage(s.net, cmdVerack, nil))
return p.Send(
newMessage(s.net, cmdVerack, nil),
)
}
func (s *Server) handleGetaddrCmd(msg *Message, p Peer) error {
@ -242,7 +244,7 @@ func (s *Server) handleInvCmd(inv *payload.Inventory, p Peer) error {
payload := payload.NewInventory(inv.Type, inv.Hashes)
resp := newMessage(s.net, cmdGetData, payload)
return p.callGetdata(resp)
return p.Send(resp)
}
// handleBlockCmd processes the received block.
@ -297,7 +299,7 @@ func (s *Server) askMoreHeaders(p Peer) error {
payload := payload.NewGetBlocks(start, util.Uint256{})
msg := newMessage(s.net, cmdGetHeaders, payload)
return p.callGetheaders(msg)
return p.Send(msg)
}
// check if the addr is already connected to the server.
@ -318,7 +320,7 @@ func (s *Server) startProtocol(p Peer) {
}
for {
getaddrMsg := newMessage(s.net, cmdGetAddr, nil)
p.callGetaddr(getaddrMsg)
p.Send(getaddrMsg)
time.Sleep(30 * time.Second)
}
@ -351,10 +353,10 @@ type StartOpts struct {
func logo() string {
return `
_ ____________ __________
_ ____________ __________
/ | / / ____/ __ \ / ____/ __ \
/ |/ / __/ / / / /_____/ / __/ / / /
/ /| / /___/ /_/ /_____/ /_/ / /_/ /
/_/ |_/_____/\____/ \____/\____/
/ /| / /___/ /_/ /_____/ /_/ / /_/ /
/_/ |_/_____/\____/ \____/\____/
`
}

View file

@ -181,7 +181,10 @@ func NewTCPPeer(conn net.Conn, s *Server) *TCPPeer {
}
}
func (p *TCPPeer) callVersion(msg *Message) error {
// Send needed to implement the network.Peer interface
// and provide the functionality to send a message to
// the current peer.
func (p *TCPPeer) Send(msg *Message) error {
t := sendTuple{
msg: msg,
err: make(chan error),
@ -206,64 +209,6 @@ func (p *TCPPeer) addr() util.Endpoint {
return p.endpoint
}
// callGetaddr will send the "getaddr" command to the remote.
func (p *TCPPeer) callGetaddr(msg *Message) error {
t := sendTuple{
msg: msg,
err: make(chan error),
}
p.send <- t
return <-t.err
}
// callGetblocks will send the "getblocks" command to the remote.
func (p *TCPPeer) callGetblocks(msg *Message) error {
t := sendTuple{
msg: msg,
err: make(chan error),
}
p.send <- t
return <-t.err
}
// callGetheaders will send the "getheaders" command to the remote.
func (p *TCPPeer) callGetheaders(msg *Message) error {
t := sendTuple{
msg: msg,
err: make(chan error),
}
p.send <- t
return <-t.err
}
func (p *TCPPeer) callVerack(msg *Message) error {
t := sendTuple{
msg: msg,
err: make(chan error),
}
p.send <- t
return <-t.err
}
func (p *TCPPeer) callGetdata(msg *Message) error {
t := sendTuple{
msg: msg,
err: make(chan error),
}
p.send <- t
return <-t.err
}
// disconnect disconnects the peer, cleaning up all its resources.
// 3 goroutines needs to be cleanup (writeLoop, handleConnection and handleMessage)
func (p *TCPPeer) disconnect() {

View file

@ -1,6 +1,6 @@
package wallet
import "github.com/anthdm/neo-go/pkg/util"
import "github.com/CityOfZion/neo-go/pkg/util"
// Account represents a NEO account. It holds the private and public key
// along with some metadata.

View file

@ -3,7 +3,7 @@ package wallet
import (
"math/big"
"github.com/anthdm/neo-go/pkg/util"
"github.com/CityOfZion/neo-go/pkg/util"
)
// TransferOutput respresents the output of a transaction.