Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
e5e91e9e
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,发现更多精彩内容 >>
提交
e5e91e9e
编写于
4月 16, 2015
作者:
P
Péter Szilágyi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
whisper: track active peers, add peer cache expiry test
上级
ee6531c5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
86 addition
and
22 deletion
+86
-22
whisper/peer_test.go
whisper/peer_test.go
+50
-0
whisper/whisper.go
whisper/whisper.go
+36
-22
未找到文件。
whisper/peer_test.go
浏览文件 @
e5e91e9e
...
...
@@ -190,3 +190,53 @@ func TestPeerDeliver(t *testing.T) {
t
.
Fatalf
(
"repeating message arrived"
)
}
}
func
TestPeerMessageExpiration
(
t
*
testing
.
T
)
{
// Start a tester and execute the handshake
tester
,
err
:=
startTestPeerInited
()
if
err
!=
nil
{
t
.
Fatalf
(
"failed to start initialized peer: %v"
,
err
)
}
defer
tester
.
stream
.
Close
()
// Fetch the peer instance for later inspection
tester
.
client
.
peerMu
.
RLock
()
if
peers
:=
len
(
tester
.
client
.
peers
);
peers
!=
1
{
t
.
Fatalf
(
"peer pool size mismatch: have %v, want %v"
,
peers
,
1
)
}
var
peer
*
peer
for
peer
,
_
=
range
tester
.
client
.
peers
{
break
}
tester
.
client
.
peerMu
.
RUnlock
()
// Construct a message and pass it through the tester
message
:=
NewMessage
([]
byte
(
"peer test message"
))
envelope
,
err
:=
message
.
Wrap
(
DefaultPoW
,
Options
{
TTL
:
time
.
Second
,
})
if
err
!=
nil
{
t
.
Fatalf
(
"failed to wrap message: %v"
,
err
)
}
if
err
:=
tester
.
client
.
Send
(
envelope
);
err
!=
nil
{
t
.
Fatalf
(
"failed to send message: %v"
,
err
)
}
payload
:=
[]
interface
{}{
envelope
}
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
messagesCode
,
payload
);
err
!=
nil
{
t
.
Fatalf
(
"message mismatch: %v"
,
err
)
}
// Check that the message is inside the cache
if
!
peer
.
known
.
Has
(
envelope
.
Hash
())
{
t
.
Fatalf
(
"message not found in cache"
)
}
// Discard messages until expiration and check cache again
exp
:=
time
.
Now
()
.
Add
(
time
.
Second
+
expirationCycle
)
for
time
.
Now
()
.
Before
(
exp
)
{
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
messagesCode
,
[]
interface
{}{});
err
!=
nil
{
t
.
Fatalf
(
"message mismatch: %v"
,
err
)
}
}
if
peer
.
known
.
Has
(
envelope
.
Hash
())
{
t
.
Fatalf
(
"message not expired from cache"
)
}
}
whisper/whisper.go
浏览文件 @
e5e91e9e
...
...
@@ -46,22 +46,26 @@ type Whisper struct {
protocol
p2p
.
Protocol
filters
*
filter
.
Filters
mmu
sync
.
RWMutex
// Message mutex to sync the below pool
messages
map
[
common
.
Hash
]
*
Envelope
// Pool of messages currently tracked by this node
expiry
map
[
uint32
]
*
set
.
SetNonTS
// Message expiration pool (TODO: something lighter)
keys
map
[
string
]
*
ecdsa
.
PrivateKey
quit
chan
struct
{}
messages
map
[
common
.
Hash
]
*
Envelope
// Pool of messages currently tracked by this node
expirations
map
[
uint32
]
*
set
.
SetNonTS
// Message expiration pool (TODO: something lighter)
poolMu
sync
.
RWMutex
// Mutex to sync the message and expiration pools
keys
map
[
string
]
*
ecdsa
.
PrivateKey
peers
map
[
*
peer
]
struct
{}
// Set of currently active peers
peerMu
sync
.
RWMutex
// Mutex to sync the active peer set
quit
chan
struct
{}
}
func
New
()
*
Whisper
{
whisper
:=
&
Whisper
{
messages
:
make
(
map
[
common
.
Hash
]
*
Envelope
),
filters
:
filter
.
New
(),
expiry
:
make
(
map
[
uint32
]
*
set
.
SetNonTS
),
quit
:
make
(
chan
struct
{}),
keys
:
make
(
map
[
string
]
*
ecdsa
.
PrivateKey
),
filters
:
filter
.
New
(),
keys
:
make
(
map
[
string
]
*
ecdsa
.
PrivateKey
),
messages
:
make
(
map
[
common
.
Hash
]
*
Envelope
),
expirations
:
make
(
map
[
uint32
]
*
set
.
SetNonTS
),
peers
:
make
(
map
[
*
peer
]
struct
{}),
quit
:
make
(
chan
struct
{}),
}
whisper
.
filters
.
Start
()
...
...
@@ -179,6 +183,16 @@ func (self *Whisper) handlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
whisperPeer
.
start
()
defer
whisperPeer
.
stop
()
// Start tracking the active peer
self
.
peerMu
.
Lock
()
self
.
peers
[
whisperPeer
]
=
struct
{}{}
self
.
peerMu
.
Unlock
()
defer
func
()
{
self
.
peerMu
.
Lock
()
delete
(
self
.
peers
,
whisperPeer
)
self
.
peerMu
.
Unlock
()
}()
// Read and process inbound messages directly to merge into client-global state
for
{
// Fetch the next packet and decode the contained envelopes
...
...
@@ -206,8 +220,8 @@ func (self *Whisper) handlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
// whisper network. It also inserts the envelope into the expiration pool at the
// appropriate time-stamp.
func
(
self
*
Whisper
)
add
(
envelope
*
Envelope
)
error
{
self
.
mm
u
.
Lock
()
defer
self
.
mm
u
.
Unlock
()
self
.
poolM
u
.
Lock
()
defer
self
.
poolM
u
.
Unlock
()
// Insert the message into the tracked pool
hash
:=
envelope
.
Hash
()
...
...
@@ -218,11 +232,11 @@ func (self *Whisper) add(envelope *Envelope) error {
self
.
messages
[
hash
]
=
envelope
// Insert the message into the expiration pool for later removal
if
self
.
expir
y
[
envelope
.
Expiry
]
==
nil
{
self
.
expir
y
[
envelope
.
Expiry
]
=
set
.
NewNonTS
()
if
self
.
expir
ations
[
envelope
.
Expiry
]
==
nil
{
self
.
expir
ations
[
envelope
.
Expiry
]
=
set
.
NewNonTS
()
}
if
!
self
.
expir
y
[
envelope
.
Expiry
]
.
Has
(
hash
)
{
self
.
expir
y
[
envelope
.
Expiry
]
.
Add
(
hash
)
if
!
self
.
expir
ations
[
envelope
.
Expiry
]
.
Has
(
hash
)
{
self
.
expir
ations
[
envelope
.
Expiry
]
.
Add
(
hash
)
// Notify the local node of a message arrival
go
self
.
postEvent
(
envelope
)
...
...
@@ -292,11 +306,11 @@ func (self *Whisper) update() {
// expire iterates over all the expiration timestamps, removing all stale
// messages from the pools.
func
(
self
*
Whisper
)
expire
()
{
self
.
mm
u
.
Lock
()
defer
self
.
mm
u
.
Unlock
()
self
.
poolM
u
.
Lock
()
defer
self
.
poolM
u
.
Unlock
()
now
:=
uint32
(
time
.
Now
()
.
Unix
())
for
then
,
hashSet
:=
range
self
.
expir
y
{
for
then
,
hashSet
:=
range
self
.
expir
ations
{
// Short circuit if a future time
if
then
>
now
{
continue
...
...
@@ -306,14 +320,14 @@ func (self *Whisper) expire() {
delete
(
self
.
messages
,
v
.
(
common
.
Hash
))
return
true
})
self
.
expir
y
[
then
]
.
Clear
()
self
.
expir
ations
[
then
]
.
Clear
()
}
}
// envelopes retrieves all the messages currently pooled by the node.
func
(
self
*
Whisper
)
envelopes
()
[]
*
Envelope
{
self
.
mm
u
.
RLock
()
defer
self
.
mm
u
.
RUnlock
()
self
.
poolM
u
.
RLock
()
defer
self
.
poolM
u
.
RUnlock
()
envelopes
:=
make
([]
*
Envelope
,
0
,
len
(
self
.
messages
))
for
_
,
envelope
:=
range
self
.
messages
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录