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
c2fb9f06
Commit
c2fb9f06
authored
Feb 13, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring RlpValue => Value
parent
67de76c2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
43 deletions
+81
-43
ethereum.go
ethereum.go
+1
-0
peer.go
peer.go
+80
-43
No files found.
ethereum.go
View file @
c2fb9f06
...
@@ -247,6 +247,7 @@ func (s *Ethereum) Start() {
...
@@ -247,6 +247,7 @@ func (s *Ethereum) Start() {
s
.
TxPool
.
Start
()
s
.
TxPool
.
Start
()
if
ethutil
.
Config
.
Seed
{
if
ethutil
.
Config
.
Seed
{
log
.
Println
(
"Seeding"
)
// Testnet seed bootstrapping
// Testnet seed bootstrapping
resp
,
err
:=
http
.
Get
(
"http://www.ethereum.org/servers.poc2.txt"
)
resp
,
err
:=
http
.
Get
(
"http://www.ethereum.org/servers.poc2.txt"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
peer.go
View file @
c2fb9f06
package
eth
package
eth
import
(
import
(
_
"bytes"
"bytes"
"fmt"
"fmt"
"github.com/ethereum/ethchain-go"
"github.com/ethereum/ethchain-go"
"github.com/ethereum/ethutil-go"
"github.com/ethereum/ethutil-go"
...
@@ -123,9 +123,14 @@ type Peer struct {
...
@@ -123,9 +123,14 @@ type Peer struct {
// Indicated whether the node is catching up or not
// Indicated whether the node is catching up or not
catchingUp
bool
catchingUp
bool
Version
string
}
}
func
NewPeer
(
conn
net
.
Conn
,
ethereum
*
Ethereum
,
inbound
bool
)
*
Peer
{
func
NewPeer
(
conn
net
.
Conn
,
ethereum
*
Ethereum
,
inbound
bool
)
*
Peer
{
data
,
_
:=
ethutil
.
Config
.
Db
.
Get
([]
byte
(
"KeyRing"
))
pubkey
:=
ethutil
.
NewValueFromBytes
(
data
)
.
Get
(
2
)
.
Bytes
()
return
&
Peer
{
return
&
Peer
{
outputQueue
:
make
(
chan
*
ethwire
.
Msg
,
outputBufferSize
),
outputQueue
:
make
(
chan
*
ethwire
.
Msg
,
outputBufferSize
),
quit
:
make
(
chan
bool
),
quit
:
make
(
chan
bool
),
...
@@ -135,6 +140,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
...
@@ -135,6 +140,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
disconnect
:
0
,
disconnect
:
0
,
connected
:
1
,
connected
:
1
,
port
:
30303
,
port
:
30303
,
pubkey
:
pubkey
,
}
}
}
}
...
@@ -148,6 +154,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
...
@@ -148,6 +154,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
connected
:
0
,
connected
:
0
,
disconnect
:
0
,
disconnect
:
0
,
caps
:
caps
,
caps
:
caps
,
Version
:
fmt
.
Sprintf
(
"/Ethereum(G) v%s/%s"
,
ethutil
.
Config
.
Ver
,
runtime
.
GOOS
),
}
}
// Set up the connection in another goroutine so we don't block the main thread
// Set up the connection in another goroutine so we don't block the main thread
...
@@ -267,7 +274,7 @@ func (p *Peer) HandleInbound() {
...
@@ -267,7 +274,7 @@ func (p *Peer) HandleInbound() {
}
}
case
ethwire
.
MsgDiscTy
:
case
ethwire
.
MsgDiscTy
:
p
.
Stop
()
p
.
Stop
()
log
.
Println
(
"Disconnect peer:"
,
DiscReason
(
msg
.
Data
.
Get
(
0
)
.
As
Uint
()))
log
.
Println
(
"Disconnect peer:"
,
DiscReason
(
msg
.
Data
.
Get
(
0
)
.
Uint
()))
case
ethwire
.
MsgPingTy
:
case
ethwire
.
MsgPingTy
:
// Respond back with pong
// Respond back with pong
p
.
QueueMessage
(
ethwire
.
NewMessage
(
ethwire
.
MsgPongTy
,
""
))
p
.
QueueMessage
(
ethwire
.
NewMessage
(
ethwire
.
MsgPongTy
,
""
))
...
@@ -279,30 +286,47 @@ func (p *Peer) HandleInbound() {
...
@@ -279,30 +286,47 @@ func (p *Peer) HandleInbound() {
case
ethwire
.
MsgBlockTy
:
case
ethwire
.
MsgBlockTy
:
// Get all blocks and process them
// Get all blocks and process them
msg
.
Data
=
msg
.
Data
msg
.
Data
=
msg
.
Data
var
block
*
ethchain
.
Block
var
block
,
lastBlock
*
ethchain
.
Block
for
i
:=
msg
.
Data
.
Length
()
-
1
;
i
>=
0
;
i
--
{
var
err
error
block
=
ethchain
.
NewBlockFromRlpValue
(
ethutil
.
NewValue
(
msg
.
Data
.
Get
(
i
)
.
AsRaw
()))
for
i
:=
msg
.
Data
.
Len
()
-
1
;
i
>=
0
;
i
--
{
err
:=
p
.
ethereum
.
BlockManager
.
ProcessBlock
(
block
)
block
=
ethchain
.
NewBlockFromRlpValue
(
msg
.
Data
.
Get
(
i
))
err
=
p
.
ethereum
.
BlockManager
.
ProcessBlock
(
block
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
break
}
else
{
lastBlock
=
block
}
}
}
}
// If we're catching up, try to catch up further.
if
err
!=
nil
{
if
p
.
catchingUp
&&
msg
.
Data
.
Length
()
>
1
{
// If the parent is unknown try to catch up with this peer
if
ethutil
.
Config
.
Debug
{
if
ethchain
.
IsParentErr
(
err
)
{
blockInfo
:=
p
.
ethereum
.
BlockManager
.
BlockChain
()
.
BlockInfo
(
block
)
log
.
Println
(
"Attempting to catch up"
)
log
.
Printf
(
"Synced to block height #%d
\n
"
,
blockInfo
.
Number
)
p
.
catchingUp
=
false
p
.
CatchupWithPeer
()
}
if
ethchain
.
IsValidationErr
(
err
)
{
// TODO
}
}
else
{
// XXX Do we want to catch up if there were errors?
// If we're catching up, try to catch up further.
if
p
.
catchingUp
&&
msg
.
Data
.
Len
()
>
1
{
if
ethutil
.
Config
.
Debug
&&
lastBlock
!=
nil
{
blockInfo
:=
lastBlock
.
BlockInfo
()
log
.
Printf
(
"Synced to block height #%d %x %x
\n
"
,
blockInfo
.
Number
,
lastBlock
.
Hash
(),
blockInfo
.
Hash
)
}
p
.
catchingUp
=
false
p
.
CatchupWithPeer
()
}
}
p
.
catchingUp
=
false
p
.
CatchupWithPeer
()
}
}
case
ethwire
.
MsgTxTy
:
case
ethwire
.
MsgTxTy
:
// If the message was a transaction queue the transaction
// If the message was a transaction queue the transaction
// in the TxPool where it will undergo validation and
// in the TxPool where it will undergo validation and
// processing when a new block is found
// processing when a new block is found
for
i
:=
0
;
i
<
msg
.
Data
.
Len
gth
();
i
++
{
for
i
:=
0
;
i
<
msg
.
Data
.
Len
();
i
++
{
p
.
ethereum
.
TxPool
.
QueueTransaction
(
ethchain
.
NewTransactionFromData
(
msg
.
Data
.
Get
(
i
)
.
Encode
()))
p
.
ethereum
.
TxPool
.
QueueTransaction
(
ethchain
.
NewTransactionFromData
(
msg
.
Data
.
Get
(
i
)
.
Encode
()))
}
}
case
ethwire
.
MsgGetPeersTy
:
case
ethwire
.
MsgGetPeersTy
:
...
@@ -317,10 +341,10 @@ func (p *Peer) HandleInbound() {
...
@@ -317,10 +341,10 @@ func (p *Peer) HandleInbound() {
//if p.requestedPeerList {
//if p.requestedPeerList {
data
:=
msg
.
Data
data
:=
msg
.
Data
// Create new list of possible peers for the ethereum to process
// Create new list of possible peers for the ethereum to process
peers
:=
make
([]
string
,
data
.
Len
gth
())
peers
:=
make
([]
string
,
data
.
Len
())
// Parse each possible peer
// Parse each possible peer
for
i
:=
0
;
i
<
data
.
Len
gth
();
i
++
{
for
i
:=
0
;
i
<
data
.
Len
();
i
++
{
value
:=
ethutil
.
NewValue
(
data
.
Get
(
i
)
.
AsRaw
()
)
value
:=
data
.
Get
(
i
)
peers
[
i
]
=
unpackAddr
(
value
.
Get
(
0
),
value
.
Get
(
1
)
.
Uint
())
peers
[
i
]
=
unpackAddr
(
value
.
Get
(
0
),
value
.
Get
(
1
)
.
Uint
())
}
}
...
@@ -333,7 +357,7 @@ func (p *Peer) HandleInbound() {
...
@@ -333,7 +357,7 @@ func (p *Peer) HandleInbound() {
case
ethwire
.
MsgGetChainTy
:
case
ethwire
.
MsgGetChainTy
:
var
parent
*
ethchain
.
Block
var
parent
*
ethchain
.
Block
// Length minus one since the very last element in the array is a count
// Length minus one since the very last element in the array is a count
l
:=
msg
.
Data
.
Len
gth
()
-
1
l
:=
msg
.
Data
.
Len
()
-
1
// Ignore empty get chains
// Ignore empty get chains
if
l
==
0
{
if
l
==
0
{
break
break
...
@@ -345,7 +369,7 @@ func (p *Peer) HandleInbound() {
...
@@ -345,7 +369,7 @@ func (p *Peer) HandleInbound() {
// Check each SHA block hash from the message and determine whether
// Check each SHA block hash from the message and determine whether
// the SHA is in the database
// the SHA is in the database
for
i
:=
0
;
i
<
l
;
i
++
{
for
i
:=
0
;
i
<
l
;
i
++
{
if
data
:=
msg
.
Data
.
Get
(
i
)
.
As
Bytes
();
p
.
ethereum
.
BlockManager
.
BlockChain
()
.
HasBlock
(
data
)
{
if
data
:=
msg
.
Data
.
Get
(
i
)
.
Bytes
();
p
.
ethereum
.
BlockManager
.
BlockChain
()
.
HasBlock
(
data
)
{
parent
=
p
.
ethereum
.
BlockManager
.
BlockChain
()
.
GetBlock
(
data
)
parent
=
p
.
ethereum
.
BlockManager
.
BlockChain
()
.
GetBlock
(
data
)
break
break
}
}
...
@@ -359,8 +383,8 @@ func (p *Peer) HandleInbound() {
...
@@ -359,8 +383,8 @@ func (p *Peer) HandleInbound() {
// If no blocks are found we send back a reply with msg not in chain
// If no blocks are found we send back a reply with msg not in chain
// and the last hash from get chain
// and the last hash from get chain
lastHash
:=
msg
.
Data
.
Get
(
l
-
1
)
lastHash
:=
msg
.
Data
.
Get
(
l
-
1
)
log
.
Printf
(
"Sending not in chain with hash %x
\n
"
,
lastHash
.
AsRaw
())
//
log.Printf("Sending not in chain with hash %x\n", lastHash.AsRaw())
p
.
QueueMessage
(
ethwire
.
NewMessage
(
ethwire
.
MsgNotInChainTy
,
[]
interface
{}{
lastHash
.
As
Raw
()}))
p
.
QueueMessage
(
ethwire
.
NewMessage
(
ethwire
.
MsgNotInChainTy
,
[]
interface
{}{
lastHash
.
Raw
()}))
}
}
case
ethwire
.
MsgNotInChainTy
:
case
ethwire
.
MsgNotInChainTy
:
log
.
Printf
(
"Not in chain %x
\n
"
,
msg
.
Data
)
log
.
Printf
(
"Not in chain %x
\n
"
,
msg
.
Data
)
...
@@ -368,7 +392,7 @@ func (p *Peer) HandleInbound() {
...
@@ -368,7 +392,7 @@ func (p *Peer) HandleInbound() {
// Unofficial but fun nonetheless
// Unofficial but fun nonetheless
case
ethwire
.
MsgTalkTy
:
case
ethwire
.
MsgTalkTy
:
log
.
Printf
(
"%v says: %s
\n
"
,
p
.
conn
.
RemoteAddr
(),
msg
.
Data
.
AsString
())
log
.
Printf
(
"%v says: %s
\n
"
,
p
.
conn
.
RemoteAddr
(),
msg
.
Data
.
Str
())
}
}
}
}
}
}
...
@@ -441,9 +465,8 @@ func (p *Peer) pushHandshake() error {
...
@@ -441,9 +465,8 @@ func (p *Peer) pushHandshake() error {
data
,
_
:=
ethutil
.
Config
.
Db
.
Get
([]
byte
(
"KeyRing"
))
data
,
_
:=
ethutil
.
Config
.
Db
.
Get
([]
byte
(
"KeyRing"
))
pubkey
:=
ethutil
.
NewValueFromBytes
(
data
)
.
Get
(
2
)
.
Bytes
()
pubkey
:=
ethutil
.
NewValueFromBytes
(
data
)
.
Get
(
2
)
.
Bytes
()
clientId
:=
fmt
.
Sprintf
(
"/Ethereum(G) v%s/%s"
,
ethutil
.
Config
.
Ver
,
runtime
.
GOOS
)
msg
:=
ethwire
.
NewMessage
(
ethwire
.
MsgHandshakeTy
,
[]
interface
{}{
msg
:=
ethwire
.
NewMessage
(
ethwire
.
MsgHandshakeTy
,
[]
interface
{}{
uint32
(
4
),
uint32
(
0
),
clientId
,
byte
(
p
.
caps
),
p
.
port
,
pubkey
,
uint32
(
4
),
uint32
(
0
),
p
.
Version
,
byte
(
p
.
caps
),
p
.
port
,
pubkey
,
})
})
p
.
QueueMessage
(
msg
)
p
.
QueueMessage
(
msg
)
...
@@ -470,7 +493,7 @@ func (p *Peer) pushPeers() {
...
@@ -470,7 +493,7 @@ func (p *Peer) pushPeers() {
func
(
p
*
Peer
)
handleHandshake
(
msg
*
ethwire
.
Msg
)
{
func
(
p
*
Peer
)
handleHandshake
(
msg
*
ethwire
.
Msg
)
{
c
:=
msg
.
Data
c
:=
msg
.
Data
if
c
.
Get
(
0
)
.
As
Uint
()
!=
4
{
if
c
.
Get
(
0
)
.
Uint
()
!=
4
{
log
.
Println
(
"Invalid peer version. Require protocol v4"
)
log
.
Println
(
"Invalid peer version. Require protocol v4"
)
p
.
Stop
()
p
.
Stop
()
return
return
...
@@ -479,35 +502,49 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
...
@@ -479,35 +502,49 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
// [PROTOCOL_VERSION, NETWORK_ID, CLIENT_ID, CAPS, PORT, PUBKEY]
// [PROTOCOL_VERSION, NETWORK_ID, CLIENT_ID, CAPS, PORT, PUBKEY]
p
.
versionKnown
=
true
p
.
versionKnown
=
true
var
istr
string
// 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
{
p
.
pubkey
=
c
.
Get
(
5
)
.
As
Bytes
()
p
.
pubkey
=
c
.
Get
(
5
)
.
Bytes
()
p
.
port
=
uint16
(
c
.
Get
(
4
)
.
As
Uint
())
p
.
port
=
uint16
(
c
.
Get
(
4
)
.
Uint
())
// Self connect detection
// Self connect detection
/*
data
,
_
:=
ethutil
.
Config
.
Db
.
Get
([]
byte
(
"KeyRing"
))
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
pubkey
:=
ethutil
.
NewValueFromBytes
(
data
)
.
Get
(
2
)
.
Bytes
()
pubkey := ethutil.NewValueFromBytes(data).Get(2).Bytes()
if
bytes
.
Compare
(
pubkey
,
p
.
pubkey
)
==
0
{
if bytes.Compare(pubkey, p.pubkey) == 0 {
p
.
Stop
()
p.Stop()
return
return
}
}
*/
}
p
.
CatchupWithPeer
()
// Catch up with the connected peer
p
.
CatchupWithPeer
()
istr
=
"inbound"
// Set the peer's caps
}
else
{
p
.
caps
=
Caps
(
c
.
Get
(
3
)
.
Byte
())
p
.
CatchupWithPeer
()
// Get a reference to the peers version
p
.
Version
=
c
.
Get
(
2
)
.
Str
()
istr
=
"outbound"
log
.
Println
(
p
)
}
func
(
p
*
Peer
)
String
()
string
{
var
strBoundType
string
if
p
.
inbound
{
strBoundType
=
"inbound"
}
else
{
strBoundType
=
"outbound"
}
var
strConnectType
string
if
atomic
.
LoadInt32
(
&
p
.
disconnect
)
==
0
{
strConnectType
=
"connected"
}
else
{
strConnectType
=
"disconnected"
}
}
p
.
caps
=
Caps
(
c
.
Get
(
3
)
.
AsByte
()
)
return
fmt
.
Sprintf
(
"peer [%s] (%s) %v %s [%s]"
,
strConnectType
,
strBoundType
,
p
.
conn
.
RemoteAddr
(),
p
.
Version
,
p
.
caps
)
log
.
Printf
(
"peer connect (%s) %v %s [%s]
\n
"
,
istr
,
p
.
conn
.
RemoteAddr
(),
c
.
Get
(
2
)
.
AsString
(),
p
.
caps
)
}
}
func
(
p
*
Peer
)
CatchupWithPeer
()
{
func
(
p
*
Peer
)
CatchupWithPeer
()
{
...
...
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