Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
aed060a4
Commit
aed060a4
authored
Jan 17, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated the wire protocol
parent
df7967c5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
33 deletions
+51
-33
peer.go
peer.go
+40
-21
server.go
server.go
+11
-12
No files found.
peer.go
View file @
aed060a4
...
@@ -90,8 +90,8 @@ func (p *Peer) writeMessage(msg *ethwire.InOutMsg) {
...
@@ -90,8 +90,8 @@ func (p *Peer) writeMessage(msg *ethwire.InOutMsg) {
}
}
if
!
p
.
versionKnown
{
if
!
p
.
versionKnown
{
switch
msg
.
Msg
Type
{
switch
msg
.
Type
{
case
"verack"
:
// Ok
case
ethwire
.
MsgHandshakeTy
:
// Ok
default
:
// Anything but ack is allowed
default
:
// Anything but ack is allowed
return
return
}
}
...
@@ -108,6 +108,8 @@ func (p *Peer) writeMessage(msg *ethwire.InOutMsg) {
...
@@ -108,6 +108,8 @@ func (p *Peer) writeMessage(msg *ethwire.InOutMsg) {
// Outbound message handler. Outbound messages are handled here
// Outbound message handler. Outbound messages are handled here
func
(
p
*
Peer
)
HandleOutbound
()
{
func
(
p
*
Peer
)
HandleOutbound
()
{
// The ping timer. Makes sure that every 2 minutes a ping is send to the peer
tickleTimer
:=
time
.
NewTimer
(
2
*
time
.
Minute
)
out
:
out
:
for
{
for
{
select
{
select
{
...
@@ -116,6 +118,10 @@ out:
...
@@ -116,6 +118,10 @@ out:
p
.
writeMessage
(
msg
)
p
.
writeMessage
(
msg
)
p
.
lastSend
=
time
.
Now
()
p
.
lastSend
=
time
.
Now
()
case
<-
tickleTimer
.
C
:
p
.
writeMessage
(
&
ethwire
.
InOutMsg
{
Type
:
ethwire
.
MsgPingTy
})
// Break out of the for loop if a quit message is posted
// Break out of the for loop if a quit message is posted
case
<-
p
.
quit
:
case
<-
p
.
quit
:
break
out
break
out
...
@@ -126,7 +132,7 @@ clean:
...
@@ -126,7 +132,7 @@ clean:
// This loop is for draining the output queue and anybody waiting for us
// This loop is for draining the output queue and anybody waiting for us
for
{
for
{
select
{
select
{
case
<-
p
.
outputQueue
:
case
<-
p
.
outputQueue
:
// TODO
// TODO
default
:
default
:
break
clean
break
clean
...
@@ -148,23 +154,32 @@ out:
...
@@ -148,23 +154,32 @@ out:
}
}
if
Debug
{
if
Debug
{
log
.
Printf
(
"Received %s
\n
"
,
msg
.
MsgType
)
log
.
Printf
(
"Received %s
\n
"
,
msg
.
Type
.
String
()
)
}
}
// TODO Hash data and check if for existence (= ignore)
// TODO Hash data and check if for existence (= ignore)
switch
msg
.
Msg
Type
{
switch
msg
.
Type
{
case
"verack"
:
case
ethwire
.
MsgHandshakeTy
:
// Version message
// Version message
p
.
handle
VersionAck
(
msg
)
p
.
handle
Handshake
(
msg
)
case
"block"
:
case
ethwire
.
MsgBlockTy
:
err
:=
p
.
server
.
blockManager
.
ProcessBlock
(
ethutil
.
NewBlock
(
msg
.
Data
))
err
:=
p
.
server
.
blockManager
.
ProcessBlock
(
ethutil
.
NewBlock
(
ethutil
.
Encode
(
msg
.
Data
)
))
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
}
}
case
ethwire
.
MsgTxTy
:
case
ethwire
.
MsgInvTy
:
case
ethwire
.
MsgGetPeersTy
:
case
ethwire
.
MsgPeersTy
:
case
ethwire
.
MsgPingTy
:
case
ethwire
.
MsgPongTy
:
/*
case "blockmine":
case "blockmine":
d, _ := ethutil.Decode(msg.Data, 0)
d, _ := ethutil.Decode(msg.Data, 0)
log.Printf("block mined %s\n", d)
log.Printf("block mined %s\n", d)
*/
}
}
}
}
...
@@ -173,7 +188,7 @@ out:
...
@@ -173,7 +188,7 @@ out:
func
(
p
*
Peer
)
Start
()
{
func
(
p
*
Peer
)
Start
()
{
if
!
p
.
inbound
{
if
!
p
.
inbound
{
err
:=
p
.
push
VersionAck
()
err
:=
p
.
push
Handshake
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"Peer can't send outbound version ack"
,
err
)
log
.
Printf
(
"Peer can't send outbound version ack"
,
err
)
...
@@ -200,17 +215,21 @@ func (p *Peer) Stop() {
...
@@ -200,17 +215,21 @@ func (p *Peer) Stop() {
log
.
Println
(
"Peer shutdown"
)
log
.
Println
(
"Peer shutdown"
)
}
}
func
(
p
*
Peer
)
pushVersionAck
()
error
{
func
(
p
*
Peer
)
pushHandshake
()
error
{
msg
:=
ethwire
.
NewMessage
(
"verack"
,
p
.
server
.
Nonce
,
[]
byte
(
"01"
))
msg
:=
ethwire
.
NewMessage
(
ethwire
.
MsgHandshakeTy
,
ethutil
.
Encode
([]
interface
{}{
1
,
0
,
p
.
server
.
Nonce
,
}))
p
.
QueueMessage
(
msg
)
p
.
QueueMessage
(
msg
)
return
nil
return
nil
}
}
func
(
p
*
Peer
)
handleVersionAck
(
msg
*
ethwire
.
InOutMsg
)
{
func
(
p
*
Peer
)
handleHandshake
(
msg
*
ethwire
.
InOutMsg
)
{
// Detect self connect
c
:=
ethutil
.
Conv
(
msg
.
Data
)
if
msg
.
Nonce
==
p
.
server
.
Nonce
{
// [PROTOCOL_VERSION, NETWORK_ID, CLIENT_ID]
if
c
.
Get
(
2
)
.
AsUint
()
==
p
.
server
.
Nonce
{
//if msg.Nonce == p.server.Nonce {
log
.
Println
(
"Peer connected to self, disconnecting"
)
log
.
Println
(
"Peer connected to self, disconnecting"
)
p
.
Stop
()
p
.
Stop
()
...
@@ -222,7 +241,7 @@ func (p *Peer) handleVersionAck(msg *ethwire.InOutMsg) {
...
@@ -222,7 +241,7 @@ func (p *Peer) handleVersionAck(msg *ethwire.InOutMsg) {
// If this is an inbound connection send an ack back
// If this is an inbound connection send an ack back
if
p
.
inbound
{
if
p
.
inbound
{
err
:=
p
.
push
VersionAck
()
err
:=
p
.
push
Handshake
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"Peer can't send ack back"
)
log
.
Println
(
"Peer can't send ack back"
)
...
...
server.go
View file @
aed060a4
...
@@ -7,8 +7,8 @@ import (
...
@@ -7,8 +7,8 @@ import (
"github.com/ethereum/ethwire-go"
"github.com/ethereum/ethwire-go"
"log"
"log"
"net"
"net"
"time"
"sync/atomic"
"sync/atomic"
"time"
)
)
func
eachPeer
(
peers
*
list
.
List
,
callback
func
(
*
Peer
,
*
list
.
Element
))
{
func
eachPeer
(
peers
*
list
.
List
,
callback
func
(
*
Peer
,
*
list
.
Element
))
{
...
@@ -20,7 +20,6 @@ func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) {
...
@@ -20,7 +20,6 @@ func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) {
}
}
}
}
type
Server
struct
{
type
Server
struct
{
// Channel for shutting down the server
// Channel for shutting down the server
shutdownChan
chan
bool
shutdownChan
chan
bool
...
@@ -75,9 +74,9 @@ func (s *Server) ConnectToPeer(addr string) error {
...
@@ -75,9 +74,9 @@ func (s *Server) ConnectToPeer(addr string) error {
return
nil
return
nil
}
}
func
(
s
*
Server
)
Broadcast
(
msgType
string
,
data
[]
byte
)
{
func
(
s
*
Server
)
Broadcast
(
msgType
ethwire
.
MsgType
,
data
[]
byte
)
{
eachPeer
(
s
.
peers
,
func
(
p
*
Peer
,
e
*
list
.
Element
)
{
eachPeer
(
s
.
peers
,
func
(
p
*
Peer
,
e
*
list
.
Element
)
{
p
.
QueueMessage
(
ethwire
.
NewMessage
(
msgType
,
0
,
data
))
p
.
QueueMessage
(
ethwire
.
NewMessage
(
msgType
,
data
))
})
})
}
}
...
@@ -141,7 +140,7 @@ func (s *Server) Start() {
...
@@ -141,7 +140,7 @@ func (s *Server) Start() {
/*
/*
go func() {
go func() {
for {
for {
s.Broadcast("block", s.blockManager.bc.GenesisBlock().MarshalRlp
())
s.Broadcast("block", s.blockManager.bc.GenesisBlock().RlpEncode
())
time.Sleep(1000 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
}
}
...
...
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