Use pointers for methods to avoid copying

This commit is contained in:
Aviv Eyal 2017-11-29 10:16:16 +02:00 committed by Steven Allen
parent 8d6313d195
commit efdab23082
5 changed files with 15 additions and 15 deletions

View File

@ -33,7 +33,7 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol {
}
// remote peer requests handler
func (e EchoProtocol) onEchoRequest(s inet.Stream) {
func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// get request data
data := &p2p.EchoRequest{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
@ -86,7 +86,7 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
}
// remote echo response handler
func (e EchoProtocol) onEchoResponse(s inet.Stream) {
func (e *EchoProtocol) onEchoResponse(s inet.Stream) {
data := &p2p.EchoResponse{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data)
@ -120,7 +120,7 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) {
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())
// create message data

View File

@ -24,7 +24,7 @@ func NewNode(host host.Host, done chan bool) *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
sign := data.Sign
@ -49,7 +49,7 @@ func (n Node) authenticateMessage(message proto.Message, data *p2p.MessageData)
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)
if err != nil {
return nil, err
@ -57,16 +57,16 @@ func (n Node) signProtoMessage(message proto.Message) ([]byte, error) {
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())
res, err := key.Sign(data)
return res, err
}
// 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)
//todo: fix this
@ -79,7 +79,7 @@ func (n Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyDa
res, err := key.Verify(data, signature)
if err != nil {
log.Println("Error authenticating data")
log.Println ("Error authenticating data")
return false
}

View File

@ -10,7 +10,7 @@ message MessageData {
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
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
}

View File

@ -33,7 +33,7 @@ func NewPingProtocol(node *Node, done chan bool) *PingProtocol {
}
// remote peer requests handler
func (p PingProtocol) onPingRequest(s inet.Stream) {
func (p *PingProtocol) onPingRequest(s inet.Stream) {
// get request data
data := &p2p.PingRequest{}
@ -86,7 +86,7 @@ func (p PingProtocol) onPingRequest(s inet.Stream) {
}
// remote ping response handler
func (p PingProtocol) onPingResponse(s inet.Stream) {
func (p *PingProtocol) onPingResponse(s inet.Stream) {
data := &p2p.PingResponse{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data)
@ -117,7 +117,7 @@ func (p PingProtocol) onPingResponse(s inet.Stream) {
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())
// create message data

View File

@ -37,7 +37,7 @@ func NewMessageData(node *Node, messageId string, gossip bool) *p2p.MessageData
nodePubKey, err := node.Peerstore().PubKey(node.ID()).Bytes()
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,