Commit efdab230 authored by Aviv Eyal's avatar Aviv Eyal Committed by Steven Allen
Browse files

Use pointers for methods to avoid copying

parent 8d6313d1
...@@ -33,7 +33,7 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol { ...@@ -33,7 +33,7 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol {
} }
// remote peer requests handler // remote peer requests handler
func (e EchoProtocol) onEchoRequest(s inet.Stream) { func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// get request data // get request data
data := &p2p.EchoRequest{} data := &p2p.EchoRequest{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s)) decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
...@@ -86,7 +86,7 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) { ...@@ -86,7 +86,7 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
} }
// remote echo response handler // remote echo response handler
func (e EchoProtocol) onEchoResponse(s inet.Stream) { func (e *EchoProtocol) onEchoResponse(s inet.Stream) {
data := &p2p.EchoResponse{} data := &p2p.EchoResponse{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s)) decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data) err := decoder.Decode(data)
...@@ -120,7 +120,7 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) { ...@@ -120,7 +120,7 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) {
e.done <- true e.done <- true
} }
func (e EchoProtocol) Echo(host host.Host) bool { func (e *EchoProtocol) Echo(host host.Host) bool {
log.Printf("%s: Sending echo to: %s....", e.node.ID(), host.ID()) log.Printf("%s: Sending echo to: %s....", e.node.ID(), host.ID())
// create message data // create message data
......
...@@ -24,7 +24,7 @@ func NewNode(host host.Host, done chan bool) *Node { ...@@ -24,7 +24,7 @@ func NewNode(host host.Host, done chan bool) *Node {
return node return node
} }
func (n Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool { func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool {
// store a temp ref to sig and remove it from data // store a temp ref to sig and remove it from data
sign := data.Sign sign := data.Sign
...@@ -49,7 +49,7 @@ func (n Node) authenticateMessage(message proto.Message, data *p2p.MessageData) ...@@ -49,7 +49,7 @@ func (n Node) authenticateMessage(message proto.Message, data *p2p.MessageData)
return n.verifyData(bin, []byte(sign), peerId, []byte(data.NodePubKey)) return n.verifyData(bin, []byte(sign), peerId, []byte(data.NodePubKey))
} }
func (n Node) signProtoMessage(message proto.Message) ([]byte, error) { func (n *Node) signProtoMessage(message proto.Message) ([]byte, error) {
data, err := proto.Marshal(message) data, err := proto.Marshal(message)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -57,16 +57,16 @@ func (n Node) signProtoMessage(message proto.Message) ([]byte, error) { ...@@ -57,16 +57,16 @@ func (n Node) signProtoMessage(message proto.Message) ([]byte, error) {
return n.signData(data) return n.signData(data)
} }
func (n Node) signData(data []byte) ([]byte, error) { func (n *Node) signData(data []byte) ([]byte, error) {
key := n.Peerstore().PrivKey(n.ID()) key := n.Peerstore().PrivKey(n.ID())
res, err := key.Sign(data) res, err := key.Sign(data)
return res, err return res, err
} }
// precondition: we have info about the signer peer in the local peer store // precondition: we have info about the signer peer in the local peer store
func (n Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyData []byte) bool { func (n *Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyData []byte) bool {
// todo: restore pub key from message and use it // todo: restore pub key from message and not from the local peer store and use it
key := n.Peerstore().PubKey(peerId) key := n.Peerstore().PubKey(peerId)
//todo: fix this //todo: fix this
...@@ -79,7 +79,7 @@ func (n Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyDa ...@@ -79,7 +79,7 @@ func (n Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyDa
res, err := key.Verify(data, signature) res, err := key.Verify(data, signature)
if err != nil { if err != nil {
log.Println("Error authenticating data") log.Println ("Error authenticating data")
return false return false
} }
......
...@@ -10,8 +10,8 @@ message MessageData { ...@@ -10,8 +10,8 @@ message MessageData {
string id = 3; // allows requesters to use request data when processing a response string id = 3; // allows requesters to use request data when processing a response
bool gossip = 4; // true to have receiver peer gossip the message to neighbors bool gossip = 4; // true to have receiver peer gossip the message to neighbors
string nodeId = 5; // id of node that created the message (not the peer that may have sent it). =base58(mh(sha256(nodePubKey))) string nodeId = 5; // id of node that created the message (not the peer that may have sent it). =base58(mh(sha256(nodePubKey)))
bytes nodePubKey = 6; // node's Secp256k1 public key bytes (32bytes) bytes nodePubKey = 6; // Authoring node Secp256k1 public key (32bytes)
string sign = 7; // signature of message data + method specific data by message authoring node string sign = 7; // signature of message data + method specific data by message authoring node
} }
//// ping protocol //// ping protocol
......
...@@ -33,7 +33,7 @@ func NewPingProtocol(node *Node, done chan bool) *PingProtocol { ...@@ -33,7 +33,7 @@ func NewPingProtocol(node *Node, done chan bool) *PingProtocol {
} }
// remote peer requests handler // remote peer requests handler
func (p PingProtocol) onPingRequest(s inet.Stream) { func (p *PingProtocol) onPingRequest(s inet.Stream) {
// get request data // get request data
data := &p2p.PingRequest{} data := &p2p.PingRequest{}
...@@ -86,7 +86,7 @@ func (p PingProtocol) onPingRequest(s inet.Stream) { ...@@ -86,7 +86,7 @@ func (p PingProtocol) onPingRequest(s inet.Stream) {
} }
// remote ping response handler // remote ping response handler
func (p PingProtocol) onPingResponse(s inet.Stream) { func (p *PingProtocol) onPingResponse(s inet.Stream) {
data := &p2p.PingResponse{} data := &p2p.PingResponse{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s)) decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data) err := decoder.Decode(data)
...@@ -117,7 +117,7 @@ func (p PingProtocol) onPingResponse(s inet.Stream) { ...@@ -117,7 +117,7 @@ func (p PingProtocol) onPingResponse(s inet.Stream) {
p.done <- true p.done <- true
} }
func (p PingProtocol) Ping(host host.Host) bool { func (p *PingProtocol) Ping(host host.Host) bool {
log.Printf("%s: Sending ping to: %s....", p.node.ID(), host.ID()) log.Printf("%s: Sending ping to: %s....", p.node.ID(), host.ID())
// create message data // create message data
......
...@@ -37,7 +37,7 @@ func NewMessageData(node *Node, messageId string, gossip bool) *p2p.MessageData ...@@ -37,7 +37,7 @@ func NewMessageData(node *Node, messageId string, gossip bool) *p2p.MessageData
nodePubKey, err := node.Peerstore().PubKey(node.ID()).Bytes() nodePubKey, err := node.Peerstore().PubKey(node.ID()).Bytes()
if err != nil { if err != nil {
panic("Failed to get public key for sender node from peer store.") panic("Failed to get public key for sender from local peer store.")
} }
return &p2p.MessageData{ClientVersion: clientVersion, return &p2p.MessageData{ClientVersion: clientVersion,
......
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