Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
4b231df9
Commit
4b231df9
authored
Nov 28, 2017
by
Aviv Eyal
Committed by
Steven Allen
Feb 21, 2018
Browse files
log.Fatal() -> log.Println()
parent
bf7f80c3
Changes
6
Show whitespace changes
Inline
Side-by-side
examples/multipro/echo.go
View file @
4b231df9
...
...
@@ -39,7 +39,7 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
decoder
:=
protobufCodec
.
Multicodec
(
nil
)
.
Decoder
(
bufio
.
NewReader
(
s
))
err
:=
decoder
.
Decode
(
data
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Println
(
err
)
return
}
...
...
@@ -48,10 +48,10 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
valid
:=
e
.
node
.
authenticateMessage
(
data
,
data
.
MessageData
)
if
!
valid
{
log
.
Fatal
(
"Failed to authenticate message"
)
log
.
Println
(
"Failed to authenticate message"
)
return
}
else
{
log
.
Print
(
"Authenticated request content was generated by author node :-)"
)
log
.
Print
ln
(
"Authenticated request content was generated by author node :-)"
)
}
log
.
Printf
(
"%s: Sending echo response to %s. Message id: %s..."
,
s
.
Conn
()
.
LocalPeer
(),
s
.
Conn
()
.
RemotePeer
(),
data
.
MessageData
.
Id
)
...
...
@@ -65,7 +65,7 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
// sign the data
signature
,
err
:=
e
.
node
.
signProtoMessage
(
resp
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign response"
)
log
.
Println
(
"failed to sign response"
)
return
}
...
...
@@ -74,7 +74,7 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
s
,
respErr
:=
e
.
node
.
NewStream
(
context
.
Background
(),
s
.
Conn
()
.
RemotePeer
(),
echoResponse
)
if
respErr
!=
nil
{
log
.
Fatal
(
respErr
)
log
.
Println
(
respErr
)
return
}
...
...
@@ -98,10 +98,10 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) {
valid
:=
e
.
node
.
authenticateMessage
(
data
,
data
.
MessageData
)
if
!
valid
{
log
.
Fatal
(
"Failed to authenticate message"
)
log
.
Println
(
"Failed to authenticate message"
)
return
}
else
{
log
.
Print
(
"Authenticated response content generated by claimed node :-)"
)
log
.
Print
ln
(
"Authenticated response content generated by claimed node :-)"
)
}
// locate request data and remove it if found
...
...
@@ -110,7 +110,7 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) {
// remove request from map as we have processed it here
delete
(
e
.
requests
,
data
.
MessageData
.
Id
)
}
else
{
log
.
Print
(
"Failed to locate request data boject for response"
)
log
.
Print
ln
(
"Failed to locate request data boject for response"
)
return
}
...
...
@@ -130,7 +130,7 @@ func (e EchoProtocol) Echo(host host.Host) bool {
signature
,
err
:=
e
.
node
.
signProtoMessage
(
req
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign message"
)
log
.
Println
(
"failed to sign message"
)
return
false
}
...
...
@@ -139,7 +139,7 @@ func (e EchoProtocol) Echo(host host.Host) bool {
s
,
err
:=
e
.
node
.
NewStream
(
context
.
Background
(),
host
.
ID
(),
echoRequest
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Println
(
err
)
return
false
}
...
...
examples/multipro/node.go
View file @
4b231df9
...
...
@@ -33,7 +33,7 @@ func (n Node) authenticateMessage(message proto.Message, data *p2p.MessageData)
// marshall data without the sig to binary format
bin
,
err
:=
proto
.
Marshal
(
message
)
if
err
!=
nil
{
log
.
Fatal
(
err
,
"failed to marshal pb message"
)
log
.
Println
(
err
,
"failed to marshal pb message"
)
return
false
}
...
...
@@ -42,7 +42,7 @@ func (n Node) authenticateMessage(message proto.Message, data *p2p.MessageData)
peerId
,
err
:=
peer
.
IDB58Decode
(
data
.
NodeId
)
if
err
!=
nil
{
log
.
Fatal
(
err
,
"Failed to decode node id from base58"
)
log
.
Println
(
err
,
"Failed to decode node id from base58"
)
return
false
}
...
...
@@ -73,13 +73,13 @@ func (n Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyDa
//key, err := key.UnmarshalPublicKey(pubKeyData)
if
key
==
nil
{
log
.
Fatal
(
"Failed to find public key for %s in local peer store."
,
peerId
.
String
())
log
.
Println
(
"Failed to find public key for %s in local peer store."
,
peerId
.
String
())
return
false
}
res
,
err
:=
key
.
Verify
(
data
,
signature
)
if
err
!=
nil
{
log
.
Fatal
(
"Error authenticating data"
)
log
.
Println
(
"Error authenticating data"
)
return
false
}
...
...
examples/multipro/pb/p2p.pb.go
View file @
4b231df9
...
...
@@ -39,7 +39,7 @@ type MessageData struct {
Id
string
`protobuf:"bytes,3,opt,name=id" json:"id,omitempty"`
Gossip
bool
`protobuf:"varint,4,opt,name=gossip" json:"gossip,omitempty"`
NodeId
string
`protobuf:"bytes,5,opt,name=nodeId" json:"nodeId,omitempty"`
NodePubKey
string
`protobuf:"bytes,6,opt,name=nodePubKey" json:"nodePubKey,omitempty"`
NodePubKey
[]
byte
`protobuf:"bytes,6,opt,name=nodePubKey
,proto3
" json:"nodePubKey,omitempty"`
Sign
string
`protobuf:"bytes,7,opt,name=sign" json:"sign,omitempty"`
}
...
...
@@ -83,11 +83,11 @@ func (m *MessageData) GetNodeId() string {
return
""
}
func
(
m
*
MessageData
)
GetNodePubKey
()
string
{
func
(
m
*
MessageData
)
GetNodePubKey
()
[]
byte
{
if
m
!=
nil
{
return
m
.
NodePubKey
}
return
""
return
nil
}
func
(
m
*
MessageData
)
GetSign
()
string
{
...
...
@@ -211,21 +211,21 @@ func init() { proto.RegisterFile("p2p.proto", fileDescriptor0) }
var
fileDescriptor0
=
[]
byte
{
// 261 bytes of a gzipped FileDescriptorProto
0x1f
,
0x8b
,
0x08
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x02
,
0xff
,
0xbc
,
0x8f
,
0x
3
1
,
0x4
f
,
0x
fb
,
0x30
,
0x10
,
0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
0x
71
,
0x
a
9
,
0x
87
,
0x
b4
,
0x
e7
,
0x
d
6
,
0xb
5
,
0x
f4
,
0xd
2
,
0x
aa
,
0x
8b
,
0x
b8
,
0x
3b
,
0x
24
,
0x
bc
,
0x0
3
,
0x
d8
,
0x
4f
,
0x
ab
,
0xd
d
,
0x
f
a
,
0x
95
,
0x
b
e
,
0x
55
,
0x
9
2
,
0x
8
9
,
0x6
2
,
0x
a1
,
0x4
7
,
0x
0
4
,
0x
11
,
0x
66
,
0x
6c
,
0x
3a
,
0xa
b
,
0x
e6
,
0x
f1
,
0x
2a
,
0x
ce
,
0x
39
,
0x
81
,
0x
5c
,
0x
19
,
0x
d
b
,
0x
69
,
0xf
a
,
0x
da
,
0x
11
,
0x0
7
,
0x
7c
,
0x0
4
,
0x
d9
,
0x
1f
,
0x
ad
,
0x
a2
,
0x
84
,
0x
ac
,
0x
6e
,
0x
ca
,
0x
13
,
0x
f7
,
0x
72
,
0x
e4
,
0x
ad
,
0x
c7
,
0xe
b
,
0xa
8
,
0x6
0
,
0x3
e
,
0xc
4
,
0x
28
,
0x9
7
,
0xe
a
,
0x
bf
,
0x
98
,
0x
7f
,
0xc
0
,
0x
e2
,
0x
50
,
0x
c3
,
0x
de
,
0x
59
,
0x
a6
,
0xb
3
,
0x
f5
,
0x
1
0
,
0x
c8
,
0x
e7
,
0xe
6
,
0x
d3
,
0xf
d
,
0x
83
,
0x
ce
,
0x
a1
,
0x
e6
,
0x
bc
,
0x
3a
,
0x
eb
,
0x
24
,
0x
fe
,
0xf
0
,
0xf
0
,
0x1
b
,
0x00
,
0x00
,
0xff
,
0xff
,
0x
96
,
0x
51
,
0x
39
,
0x
88
,
0x88
,
0x02
,
0x00
,
0x00
,
}
examples/multipro/pb/p2p.proto
View file @
4b231df9
...
...
@@ -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)))
string
nodePubKey
=
6
;
// node's Secp256k1 public key bytes (32bytes)
bytes
nodePubKey
=
6
;
// node's Secp256k1 public key bytes (32bytes)
string
sign
=
7
;
// signature of message data + method specific data by message authoring node
}
...
...
examples/multipro/ping.go
View file @
4b231df9
...
...
@@ -40,7 +40,7 @@ func (p PingProtocol) onPingRequest(s inet.Stream) {
decoder
:=
protobufCodec
.
Multicodec
(
nil
)
.
Decoder
(
bufio
.
NewReader
(
s
))
err
:=
decoder
.
Decode
(
data
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Println
(
err
)
return
}
...
...
@@ -49,10 +49,10 @@ func (p PingProtocol) onPingRequest(s inet.Stream) {
valid
:=
p
.
node
.
authenticateMessage
(
data
,
data
.
MessageData
)
if
!
valid
{
log
.
Fatal
(
"Failed to authenticate message"
)
log
.
Println
(
"Failed to authenticate message"
)
return
}
else
{
log
.
Print
(
"Authenticated request content was generated by claimed node :-)"
)
log
.
Print
ln
(
"Authenticated request content was generated by claimed node :-)"
)
}
// generate response message
...
...
@@ -64,7 +64,7 @@ func (p PingProtocol) onPingRequest(s inet.Stream) {
// sign the data
signature
,
err
:=
p
.
node
.
signProtoMessage
(
resp
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign response"
)
log
.
Println
(
"failed to sign response"
)
return
}
...
...
@@ -74,7 +74,7 @@ func (p PingProtocol) onPingRequest(s inet.Stream) {
// send the response
s
,
respErr
:=
p
.
node
.
NewStream
(
context
.
Background
(),
s
.
Conn
()
.
RemotePeer
(),
pingResponse
)
if
respErr
!=
nil
{
log
.
Fatal
(
respErr
)
log
.
Println
(
respErr
)
return
}
...
...
@@ -97,10 +97,10 @@ func (p PingProtocol) onPingResponse(s inet.Stream) {
valid
:=
p
.
node
.
authenticateMessage
(
data
,
data
.
MessageData
)
if
!
valid
{
log
.
Fatal
(
"Failed to authenticate message"
)
log
.
Println
(
"Failed to authenticate message"
)
return
}
else
{
log
.
Print
(
"Authenticated response content generated by claimed node :-)"
)
log
.
Print
ln
(
"Authenticated response content generated by claimed node :-)"
)
}
// locate request data and remove it if found
...
...
@@ -109,7 +109,7 @@ func (p PingProtocol) onPingResponse(s inet.Stream) {
// remove request from map as we have processed it here
delete
(
p
.
requests
,
data
.
MessageData
.
Id
)
}
else
{
log
.
Print
(
"Failed to locate request data boject for response"
)
log
.
Print
ln
(
"Failed to locate request data boject for response"
)
return
}
...
...
@@ -127,7 +127,7 @@ func (p PingProtocol) Ping(host host.Host) bool {
// sign the data
signature
,
err
:=
p
.
node
.
signProtoMessage
(
req
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign pb data"
)
log
.
Println
(
"failed to sign pb data"
)
return
false
}
...
...
@@ -136,7 +136,7 @@ func (p PingProtocol) Ping(host host.Host) bool {
s
,
err
:=
p
.
node
.
NewStream
(
context
.
Background
(),
host
.
ID
(),
pingRequest
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Println
(
err
)
return
false
}
...
...
examples/multipro/protocol.go
View file @
4b231df9
...
...
@@ -22,7 +22,7 @@ func sendProtoMessage(data proto.Message, s inet.Stream) bool {
enc
:=
protobufCodec
.
Multicodec
(
nil
)
.
Encoder
(
writer
)
err
:=
enc
.
Encode
(
data
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Println
(
err
)
return
false
}
writer
.
Flush
()
...
...
@@ -42,7 +42,7 @@ func NewMessageData(node *Node, messageId string, gossip bool) *p2p.MessageData
return
&
p2p
.
MessageData
{
ClientVersion
:
clientVersion
,
NodeId
:
peer
.
IDB58Encode
(
node
.
ID
()),
NodePubKey
:
string
(
nodePubKey
)
,
NodePubKey
:
nodePubKey
,
Timestamp
:
time
.
Now
()
.
Unix
(),
Id
:
messageId
,
Gossip
:
gossip
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment