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
f12f8a6c
Commit
f12f8a6c
authored
Dec 10, 2016
by
Zsolt Felfoldi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
les, light: add block availability check for ODR requests
parent
c57c54ce
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
71 additions
and
14 deletions
+71
-14
fetcher.go
les/fetcher.go
+11
-1
odr.go
les/odr.go
+3
-0
odr_requests.go
les/odr_requests.go
+29
-0
peer.go
les/peer.go
+12
-0
server.go
les/server.go
+4
-5
odr.go
light/odr.go
+9
-6
odr_util.go
light/odr_util.go
+3
-2
No files found.
les/fetcher.go
View file @
f12f8a6c
...
@@ -200,6 +200,12 @@ func (f *lightFetcher) syncLoop() {
...
@@ -200,6 +200,12 @@ func (f *lightFetcher) syncLoop() {
// addPeer adds a new peer to the fetcher's peer set
// addPeer adds a new peer to the fetcher's peer set
func
(
f
*
lightFetcher
)
addPeer
(
p
*
peer
)
{
func
(
f
*
lightFetcher
)
addPeer
(
p
*
peer
)
{
p
.
lock
.
Lock
()
p
.
hasBlock
=
func
(
hash
common
.
Hash
,
number
uint64
)
bool
{
return
f
.
peerHasBlock
(
p
,
hash
,
number
)
}
p
.
lock
.
Unlock
()
f
.
lock
.
Lock
()
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
defer
f
.
lock
.
Unlock
()
...
@@ -208,6 +214,10 @@ func (f *lightFetcher) addPeer(p *peer) {
...
@@ -208,6 +214,10 @@ func (f *lightFetcher) addPeer(p *peer) {
// removePeer removes a new peer from the fetcher's peer set
// removePeer removes a new peer from the fetcher's peer set
func
(
f
*
lightFetcher
)
removePeer
(
p
*
peer
)
{
func
(
f
*
lightFetcher
)
removePeer
(
p
*
peer
)
{
p
.
lock
.
Lock
()
p
.
hasBlock
=
nil
p
.
lock
.
Unlock
()
f
.
lock
.
Lock
()
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
defer
f
.
lock
.
Unlock
()
...
@@ -315,7 +325,7 @@ func (f *lightFetcher) announce(p *peer, head *announceData) {
...
@@ -315,7 +325,7 @@ func (f *lightFetcher) announce(p *peer, head *announceData) {
// based on its announcements
// based on its announcements
func
(
f
*
lightFetcher
)
peerHasBlock
(
p
*
peer
,
hash
common
.
Hash
,
number
uint64
)
bool
{
func
(
f
*
lightFetcher
)
peerHasBlock
(
p
*
peer
,
hash
common
.
Hash
,
number
uint64
)
bool
{
f
.
lock
.
Lock
()
f
.
lock
.
Lock
()
defer
f
.
lock
.
L
ock
()
defer
f
.
lock
.
Unl
ock
()
fp
:=
f
.
peers
[
p
]
fp
:=
f
.
peers
[
p
]
if
fp
==
nil
||
fp
.
root
==
nil
{
if
fp
==
nil
||
fp
.
root
==
nil
{
...
...
les/odr.go
View file @
f12f8a6c
...
@@ -188,6 +188,9 @@ func (self *LesOdr) networkRequest(ctx context.Context, lreq LesOdrRequest) erro
...
@@ -188,6 +188,9 @@ func (self *LesOdr) networkRequest(ctx context.Context, lreq LesOdrRequest) erro
var
p
*
peer
var
p
*
peer
if
self
.
serverPool
!=
nil
{
if
self
.
serverPool
!=
nil
{
p
=
self
.
serverPool
.
selectPeer
(
func
(
p
*
peer
)
(
bool
,
uint64
)
{
p
=
self
.
serverPool
.
selectPeer
(
func
(
p
*
peer
)
(
bool
,
uint64
)
{
if
!
lreq
.
CanSend
(
p
)
{
return
false
,
0
}
return
true
,
p
.
fcServer
.
CanSend
(
lreq
.
GetCost
(
p
))
return
true
,
p
.
fcServer
.
CanSend
(
lreq
.
GetCost
(
p
))
})
})
}
}
...
...
les/odr_requests.go
View file @
f12f8a6c
...
@@ -36,6 +36,7 @@ import (
...
@@ -36,6 +36,7 @@ import (
type
LesOdrRequest
interface
{
type
LesOdrRequest
interface
{
GetCost
(
*
peer
)
uint64
GetCost
(
*
peer
)
uint64
CanSend
(
*
peer
)
bool
Request
(
uint64
,
*
peer
)
error
Request
(
uint64
,
*
peer
)
error
Valid
(
ethdb
.
Database
,
*
Msg
)
bool
// if true, keeps the retrieved object
Valid
(
ethdb
.
Database
,
*
Msg
)
bool
// if true, keeps the retrieved object
}
}
...
@@ -66,6 +67,11 @@ func (self *BlockRequest) GetCost(peer *peer) uint64 {
...
@@ -66,6 +67,11 @@ func (self *BlockRequest) GetCost(peer *peer) uint64 {
return
peer
.
GetRequestCost
(
GetBlockBodiesMsg
,
1
)
return
peer
.
GetRequestCost
(
GetBlockBodiesMsg
,
1
)
}
}
// CanSend tells if a certain peer is suitable for serving the given request
func
(
self
*
BlockRequest
)
CanSend
(
peer
*
peer
)
bool
{
return
peer
.
HasBlock
(
self
.
Hash
,
self
.
Number
)
}
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func
(
self
*
BlockRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
func
(
self
*
BlockRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting body of block %08x from peer %v"
,
self
.
Hash
[
:
4
],
peer
.
id
)
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting body of block %08x from peer %v"
,
self
.
Hash
[
:
4
],
peer
.
id
)
...
@@ -121,6 +127,11 @@ func (self *ReceiptsRequest) GetCost(peer *peer) uint64 {
...
@@ -121,6 +127,11 @@ func (self *ReceiptsRequest) GetCost(peer *peer) uint64 {
return
peer
.
GetRequestCost
(
GetReceiptsMsg
,
1
)
return
peer
.
GetRequestCost
(
GetReceiptsMsg
,
1
)
}
}
// CanSend tells if a certain peer is suitable for serving the given request
func
(
self
*
ReceiptsRequest
)
CanSend
(
peer
*
peer
)
bool
{
return
peer
.
HasBlock
(
self
.
Hash
,
self
.
Number
)
}
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func
(
self
*
ReceiptsRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
func
(
self
*
ReceiptsRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting receipts for block %08x from peer %v"
,
self
.
Hash
[
:
4
],
peer
.
id
)
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting receipts for block %08x from peer %v"
,
self
.
Hash
[
:
4
],
peer
.
id
)
...
@@ -171,6 +182,11 @@ func (self *TrieRequest) GetCost(peer *peer) uint64 {
...
@@ -171,6 +182,11 @@ func (self *TrieRequest) GetCost(peer *peer) uint64 {
return
peer
.
GetRequestCost
(
GetProofsMsg
,
1
)
return
peer
.
GetRequestCost
(
GetProofsMsg
,
1
)
}
}
// CanSend tells if a certain peer is suitable for serving the given request
func
(
self
*
TrieRequest
)
CanSend
(
peer
*
peer
)
bool
{
return
peer
.
HasBlock
(
self
.
Id
.
BlockHash
,
self
.
Id
.
BlockNumber
)
}
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func
(
self
*
TrieRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
func
(
self
*
TrieRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting trie root %08x key %08x from peer %v"
,
self
.
Id
.
Root
[
:
4
],
self
.
Key
[
:
4
],
peer
.
id
)
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting trie root %08x key %08x from peer %v"
,
self
.
Id
.
Root
[
:
4
],
self
.
Key
[
:
4
],
peer
.
id
)
...
@@ -221,6 +237,11 @@ func (self *CodeRequest) GetCost(peer *peer) uint64 {
...
@@ -221,6 +237,11 @@ func (self *CodeRequest) GetCost(peer *peer) uint64 {
return
peer
.
GetRequestCost
(
GetCodeMsg
,
1
)
return
peer
.
GetRequestCost
(
GetCodeMsg
,
1
)
}
}
// CanSend tells if a certain peer is suitable for serving the given request
func
(
self
*
CodeRequest
)
CanSend
(
peer
*
peer
)
bool
{
return
peer
.
HasBlock
(
self
.
Id
.
BlockHash
,
self
.
Id
.
BlockNumber
)
}
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func
(
self
*
CodeRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
func
(
self
*
CodeRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting node data for hash %08x from peer %v"
,
self
.
Hash
[
:
4
],
peer
.
id
)
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting node data for hash %08x from peer %v"
,
self
.
Hash
[
:
4
],
peer
.
id
)
...
@@ -274,6 +295,14 @@ func (self *ChtRequest) GetCost(peer *peer) uint64 {
...
@@ -274,6 +295,14 @@ func (self *ChtRequest) GetCost(peer *peer) uint64 {
return
peer
.
GetRequestCost
(
GetHeaderProofsMsg
,
1
)
return
peer
.
GetRequestCost
(
GetHeaderProofsMsg
,
1
)
}
}
// CanSend tells if a certain peer is suitable for serving the given request
func
(
self
*
ChtRequest
)
CanSend
(
peer
*
peer
)
bool
{
peer
.
lock
.
RLock
()
defer
peer
.
lock
.
RUnlock
()
return
self
.
ChtNum
<=
(
peer
.
headInfo
.
Number
-
light
.
ChtConfirmations
)
/
light
.
ChtFrequency
}
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func
(
self
*
ChtRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
func
(
self
*
ChtRequest
)
Request
(
reqID
uint64
,
peer
*
peer
)
error
{
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting CHT #%d block #%d from peer %v"
,
self
.
ChtNum
,
self
.
BlockNum
,
peer
.
id
)
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"ODR: requesting CHT #%d block #%d from peer %v"
,
self
.
ChtNum
,
self
.
BlockNum
,
peer
.
id
)
...
...
les/peer.go
View file @
f12f8a6c
...
@@ -57,6 +57,7 @@ type peer struct {
...
@@ -57,6 +57,7 @@ type peer struct {
announceChn
chan
announceData
announceChn
chan
announceData
poolEntry
*
poolEntry
poolEntry
*
poolEntry
hasBlock
func
(
common
.
Hash
,
uint64
)
bool
fcClient
*
flowcontrol
.
ClientNode
// nil if the peer is server only
fcClient
*
flowcontrol
.
ClientNode
// nil if the peer is server only
fcServer
*
flowcontrol
.
ServerNode
// nil if the peer is client only
fcServer
*
flowcontrol
.
ServerNode
// nil if the peer is client only
...
@@ -135,6 +136,9 @@ func sendResponse(w p2p.MsgWriter, msgcode, reqID, bv uint64, data interface{})
...
@@ -135,6 +136,9 @@ func sendResponse(w p2p.MsgWriter, msgcode, reqID, bv uint64, data interface{})
}
}
func
(
p
*
peer
)
GetRequestCost
(
msgcode
uint64
,
amount
int
)
uint64
{
func
(
p
*
peer
)
GetRequestCost
(
msgcode
uint64
,
amount
int
)
uint64
{
p
.
lock
.
RLock
()
defer
p
.
lock
.
RUnlock
()
cost
:=
p
.
fcCosts
[
msgcode
]
.
baseCost
+
p
.
fcCosts
[
msgcode
]
.
reqCost
*
uint64
(
amount
)
cost
:=
p
.
fcCosts
[
msgcode
]
.
baseCost
+
p
.
fcCosts
[
msgcode
]
.
reqCost
*
uint64
(
amount
)
if
cost
>
p
.
fcServerParams
.
BufLimit
{
if
cost
>
p
.
fcServerParams
.
BufLimit
{
cost
=
p
.
fcServerParams
.
BufLimit
cost
=
p
.
fcServerParams
.
BufLimit
...
@@ -142,6 +146,14 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
...
@@ -142,6 +146,14 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
return
cost
return
cost
}
}
// HasBlock checks if the peer has a given block
func
(
p
*
peer
)
HasBlock
(
hash
common
.
Hash
,
number
uint64
)
bool
{
p
.
lock
.
RLock
()
hashBlock
:=
p
.
hasBlock
p
.
lock
.
RUnlock
()
return
hashBlock
!=
nil
&&
hashBlock
(
hash
,
number
)
}
// SendAnnounce announces the availability of a number of blocks through
// SendAnnounce announces the availability of a number of blocks through
// a hash notification.
// a hash notification.
func
(
p
*
peer
)
SendAnnounce
(
request
announceData
)
error
{
func
(
p
*
peer
)
SendAnnounce
(
request
announceData
)
error
{
...
...
les/server.go
View file @
f12f8a6c
...
@@ -349,9 +349,8 @@ func (pm *ProtocolManager) blockLoop() {
...
@@ -349,9 +349,8 @@ func (pm *ProtocolManager) blockLoop() {
}
}
var
(
var
(
lastChtKey
=
[]
byte
(
"LastChtNumber"
)
// chtNum (uint64 big endian)
lastChtKey
=
[]
byte
(
"LastChtNumber"
)
// chtNum (uint64 big endian)
chtPrefix
=
[]
byte
(
"cht"
)
// chtPrefix + chtNum (uint64 big endian) -> trie root hash
chtPrefix
=
[]
byte
(
"cht"
)
// chtPrefix + chtNum (uint64 big endian) -> trie root hash
chtConfirmations
=
light
.
ChtFrequency
/
2
)
)
func
getChtRoot
(
db
ethdb
.
Database
,
num
uint64
)
common
.
Hash
{
func
getChtRoot
(
db
ethdb
.
Database
,
num
uint64
)
common
.
Hash
{
...
@@ -372,8 +371,8 @@ func makeCht(db ethdb.Database) bool {
...
@@ -372,8 +371,8 @@ func makeCht(db ethdb.Database) bool {
headNum
:=
core
.
GetBlockNumber
(
db
,
headHash
)
headNum
:=
core
.
GetBlockNumber
(
db
,
headHash
)
var
newChtNum
uint64
var
newChtNum
uint64
if
headNum
>
c
htConfirmations
{
if
headNum
>
light
.
C
htConfirmations
{
newChtNum
=
(
headNum
-
c
htConfirmations
)
/
light
.
ChtFrequency
newChtNum
=
(
headNum
-
light
.
C
htConfirmations
)
/
light
.
ChtFrequency
}
}
var
lastChtNum
uint64
var
lastChtNum
uint64
...
...
light/odr.go
View file @
f12f8a6c
...
@@ -48,6 +48,7 @@ type OdrRequest interface {
...
@@ -48,6 +48,7 @@ type OdrRequest interface {
// TrieID identifies a state or account storage trie
// TrieID identifies a state or account storage trie
type
TrieID
struct
{
type
TrieID
struct
{
BlockHash
,
Root
common
.
Hash
BlockHash
,
Root
common
.
Hash
BlockNumber
uint64
AccKey
[]
byte
AccKey
[]
byte
}
}
...
@@ -55,9 +56,10 @@ type TrieID struct {
...
@@ -55,9 +56,10 @@ type TrieID struct {
// header.
// header.
func
StateTrieID
(
header
*
types
.
Header
)
*
TrieID
{
func
StateTrieID
(
header
*
types
.
Header
)
*
TrieID
{
return
&
TrieID
{
return
&
TrieID
{
BlockHash
:
header
.
Hash
(),
BlockHash
:
header
.
Hash
(),
AccKey
:
nil
,
BlockNumber
:
header
.
Number
.
Uint64
(),
Root
:
header
.
Root
,
AccKey
:
nil
,
Root
:
header
.
Root
,
}
}
}
}
...
@@ -66,9 +68,10 @@ func StateTrieID(header *types.Header) *TrieID {
...
@@ -66,9 +68,10 @@ func StateTrieID(header *types.Header) *TrieID {
// checking Merkle proofs.
// checking Merkle proofs.
func
StorageTrieID
(
state
*
TrieID
,
addr
common
.
Address
,
root
common
.
Hash
)
*
TrieID
{
func
StorageTrieID
(
state
*
TrieID
,
addr
common
.
Address
,
root
common
.
Hash
)
*
TrieID
{
return
&
TrieID
{
return
&
TrieID
{
BlockHash
:
state
.
BlockHash
,
BlockHash
:
state
.
BlockHash
,
AccKey
:
crypto
.
Keccak256
(
addr
[
:
]),
BlockNumber
:
state
.
BlockNumber
,
Root
:
root
,
AccKey
:
crypto
.
Keccak256
(
addr
[
:
]),
Root
:
root
,
}
}
}
}
...
...
light/odr_util.go
View file @
f12f8a6c
...
@@ -38,8 +38,9 @@ var (
...
@@ -38,8 +38,9 @@ var (
ErrNoTrustedCht
=
errors
.
New
(
"No trusted canonical hash trie"
)
ErrNoTrustedCht
=
errors
.
New
(
"No trusted canonical hash trie"
)
ErrNoHeader
=
errors
.
New
(
"Header not found"
)
ErrNoHeader
=
errors
.
New
(
"Header not found"
)
ChtFrequency
=
uint64
(
4096
)
ChtFrequency
=
uint64
(
4096
)
trustedChtKey
=
[]
byte
(
"TrustedCHT"
)
ChtConfirmations
=
uint64
(
2048
)
trustedChtKey
=
[]
byte
(
"TrustedCHT"
)
)
)
type
ChtNode
struct
{
type
ChtNode
struct
{
...
...
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