Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
f12f8a6c
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f12f8a6c
编写于
12月 10, 2016
作者:
Z
Zsolt Felfoldi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
les, light: add block availability check for ODR requests
上级
c57c54ce
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
71 addition
and
14 deletion
+71
-14
les/fetcher.go
les/fetcher.go
+11
-1
les/odr.go
les/odr.go
+3
-0
les/odr_requests.go
les/odr_requests.go
+29
-0
les/peer.go
les/peer.go
+12
-0
les/server.go
les/server.go
+4
-5
light/odr.go
light/odr.go
+9
-6
light/odr_util.go
light/odr_util.go
+3
-2
未找到文件。
les/fetcher.go
浏览文件 @
f12f8a6c
...
...
@@ -200,6 +200,12 @@ func (f *lightFetcher) syncLoop() {
// addPeer adds a new peer to the fetcher's peer set
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
()
defer
f
.
lock
.
Unlock
()
...
...
@@ -208,6 +214,10 @@ func (f *lightFetcher) addPeer(p *peer) {
// removePeer removes a new peer from the fetcher's peer set
func
(
f
*
lightFetcher
)
removePeer
(
p
*
peer
)
{
p
.
lock
.
Lock
()
p
.
hasBlock
=
nil
p
.
lock
.
Unlock
()
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
...
...
@@ -315,7 +325,7 @@ func (f *lightFetcher) announce(p *peer, head *announceData) {
// based on its announcements
func
(
f
*
lightFetcher
)
peerHasBlock
(
p
*
peer
,
hash
common
.
Hash
,
number
uint64
)
bool
{
f
.
lock
.
Lock
()
defer
f
.
lock
.
L
ock
()
defer
f
.
lock
.
Unl
ock
()
fp
:=
f
.
peers
[
p
]
if
fp
==
nil
||
fp
.
root
==
nil
{
...
...
les/odr.go
浏览文件 @
f12f8a6c
...
...
@@ -188,6 +188,9 @@ func (self *LesOdr) networkRequest(ctx context.Context, lreq LesOdrRequest) erro
var
p
*
peer
if
self
.
serverPool
!=
nil
{
p
=
self
.
serverPool
.
selectPeer
(
func
(
p
*
peer
)
(
bool
,
uint64
)
{
if
!
lreq
.
CanSend
(
p
)
{
return
false
,
0
}
return
true
,
p
.
fcServer
.
CanSend
(
lreq
.
GetCost
(
p
))
})
}
...
...
les/odr_requests.go
浏览文件 @
f12f8a6c
...
...
@@ -36,6 +36,7 @@ import (
type
LesOdrRequest
interface
{
GetCost
(
*
peer
)
uint64
CanSend
(
*
peer
)
bool
Request
(
uint64
,
*
peer
)
error
Valid
(
ethdb
.
Database
,
*
Msg
)
bool
// if true, keeps the retrieved object
}
...
...
@@ -66,6 +67,11 @@ func (self *BlockRequest) GetCost(peer *peer) uint64 {
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)
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
)
...
...
@@ -121,6 +127,11 @@ func (self *ReceiptsRequest) GetCost(peer *peer) uint64 {
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)
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
)
...
...
@@ -171,6 +182,11 @@ func (self *TrieRequest) GetCost(peer *peer) uint64 {
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)
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
)
...
...
@@ -221,6 +237,11 @@ func (self *CodeRequest) GetCost(peer *peer) uint64 {
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)
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
)
...
...
@@ -274,6 +295,14 @@ func (self *ChtRequest) GetCost(peer *peer) uint64 {
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)
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
)
...
...
les/peer.go
浏览文件 @
f12f8a6c
...
...
@@ -57,6 +57,7 @@ type peer struct {
announceChn
chan
announceData
poolEntry
*
poolEntry
hasBlock
func
(
common
.
Hash
,
uint64
)
bool
fcClient
*
flowcontrol
.
ClientNode
// nil if the peer is server 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{})
}
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
)
if
cost
>
p
.
fcServerParams
.
BufLimit
{
cost
=
p
.
fcServerParams
.
BufLimit
...
...
@@ -142,6 +146,14 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
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
// a hash notification.
func
(
p
*
peer
)
SendAnnounce
(
request
announceData
)
error
{
...
...
les/server.go
浏览文件 @
f12f8a6c
...
...
@@ -349,9 +349,8 @@ func (pm *ProtocolManager) blockLoop() {
}
var
(
lastChtKey
=
[]
byte
(
"LastChtNumber"
)
// chtNum (uint64 big endian)
chtPrefix
=
[]
byte
(
"cht"
)
// chtPrefix + chtNum (uint64 big endian) -> trie root hash
chtConfirmations
=
light
.
ChtFrequency
/
2
lastChtKey
=
[]
byte
(
"LastChtNumber"
)
// chtNum (uint64 big endian)
chtPrefix
=
[]
byte
(
"cht"
)
// chtPrefix + chtNum (uint64 big endian) -> trie root hash
)
func
getChtRoot
(
db
ethdb
.
Database
,
num
uint64
)
common
.
Hash
{
...
...
@@ -372,8 +371,8 @@ func makeCht(db ethdb.Database) bool {
headNum
:=
core
.
GetBlockNumber
(
db
,
headHash
)
var
newChtNum
uint64
if
headNum
>
c
htConfirmations
{
newChtNum
=
(
headNum
-
c
htConfirmations
)
/
light
.
ChtFrequency
if
headNum
>
light
.
C
htConfirmations
{
newChtNum
=
(
headNum
-
light
.
C
htConfirmations
)
/
light
.
ChtFrequency
}
var
lastChtNum
uint64
...
...
light/odr.go
浏览文件 @
f12f8a6c
...
...
@@ -48,6 +48,7 @@ type OdrRequest interface {
// TrieID identifies a state or account storage trie
type
TrieID
struct
{
BlockHash
,
Root
common
.
Hash
BlockNumber
uint64
AccKey
[]
byte
}
...
...
@@ -55,9 +56,10 @@ type TrieID struct {
// header.
func
StateTrieID
(
header
*
types
.
Header
)
*
TrieID
{
return
&
TrieID
{
BlockHash
:
header
.
Hash
(),
AccKey
:
nil
,
Root
:
header
.
Root
,
BlockHash
:
header
.
Hash
(),
BlockNumber
:
header
.
Number
.
Uint64
(),
AccKey
:
nil
,
Root
:
header
.
Root
,
}
}
...
...
@@ -66,9 +68,10 @@ func StateTrieID(header *types.Header) *TrieID {
// checking Merkle proofs.
func
StorageTrieID
(
state
*
TrieID
,
addr
common
.
Address
,
root
common
.
Hash
)
*
TrieID
{
return
&
TrieID
{
BlockHash
:
state
.
BlockHash
,
AccKey
:
crypto
.
Keccak256
(
addr
[
:
]),
Root
:
root
,
BlockHash
:
state
.
BlockHash
,
BlockNumber
:
state
.
BlockNumber
,
AccKey
:
crypto
.
Keccak256
(
addr
[
:
]),
Root
:
root
,
}
}
...
...
light/odr_util.go
浏览文件 @
f12f8a6c
...
...
@@ -38,8 +38,9 @@ var (
ErrNoTrustedCht
=
errors
.
New
(
"No trusted canonical hash trie"
)
ErrNoHeader
=
errors
.
New
(
"Header not found"
)
ChtFrequency
=
uint64
(
4096
)
trustedChtKey
=
[]
byte
(
"TrustedCHT"
)
ChtFrequency
=
uint64
(
4096
)
ChtConfirmations
=
uint64
(
2048
)
trustedChtKey
=
[]
byte
(
"TrustedCHT"
)
)
type
ChtNode
struct
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录