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
4fb7ab5d
Commit
4fb7ab5d
authored
Apr 14, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
whisper: mock tests to use simulated peers
parent
86372b20
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
71 deletions
+55
-71
common_test.go
whisper/common_test.go
+32
-0
whisper_test.go
whisper/whisper_test.go
+23
-71
No files found.
whisper/common_test.go
View file @
4fb7ab5d
...
@@ -3,7 +3,9 @@
...
@@ -3,7 +3,9 @@
package
whisper
package
whisper
import
(
import
(
"bytes"
"fmt"
"fmt"
"io/ioutil"
"math/rand"
"math/rand"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p"
...
@@ -36,3 +38,33 @@ func whisperCaps() []p2p.Cap {
...
@@ -36,3 +38,33 @@ func whisperCaps() []p2p.Cap {
},
},
}
}
}
}
// bufMsgPipe creates a buffered message pipe between two endpoints.
func
bufMsgPipe
()
(
*
p2p
.
MsgPipeRW
,
*
p2p
.
MsgPipeRW
)
{
A
,
midA
:=
p2p
.
MsgPipe
()
midB
,
B
:=
p2p
.
MsgPipe
()
go
copyMsgPipe
(
midA
,
midB
)
go
copyMsgPipe
(
midB
,
midA
)
return
A
,
B
}
// copyMsgPipe copies messages from the src pipe to the dest.
func
copyMsgPipe
(
dst
,
src
*
p2p
.
MsgPipeRW
)
{
defer
dst
.
Close
()
for
{
msg
,
err
:=
src
.
ReadMsg
()
if
err
!=
nil
{
return
}
data
,
err
:=
ioutil
.
ReadAll
(
msg
.
Payload
)
if
err
!=
nil
{
return
}
msg
.
Payload
=
bytes
.
NewReader
(
data
)
if
err
:=
dst
.
WriteMsg
(
msg
);
err
!=
nil
{
return
}
}
}
whisper/whisper_test.go
View file @
4fb7ab5d
package
whisper
package
whisper
import
(
import
(
"fmt"
"testing"
"testing"
"time"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/nat"
)
)
type
testNode
struct
{
func
startTestCluster
(
n
int
)
[]
*
Whisper
{
server
*
p2p
.
Server
// Create the batch of simulated peers
client
*
Whisper
nodes
:=
make
([]
*
p2p
.
Peer
,
n
)
}
func
startNodes
(
n
int
)
([]
*
testNode
,
error
)
{
// Start up the cluster of nodes
cluster
:=
make
([]
*
testNode
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
for
i
:=
0
;
i
<
n
;
i
++
{
shh
:=
New
()
nodes
[
i
]
=
p2p
.
NewPeer
(
randomNodeID
(),
randomNodeName
(),
whisperCaps
())
// Generate the node identity
key
,
err
:=
crypto
.
GenerateKey
()
if
err
!=
nil
{
return
nil
,
err
}
name
:=
common
.
MakeName
(
fmt
.
Sprintf
(
"whisper-go-test-%d"
,
i
),
"1.0"
)
// Create an Ethereum server to communicate through
server
:=
&
p2p
.
Server
{
PrivateKey
:
key
,
MaxPeers
:
10
,
Name
:
name
,
Protocols
:
[]
p2p
.
Protocol
{
shh
.
Protocol
()},
ListenAddr
:
fmt
.
Sprintf
(
":%d"
,
30300
+
i
),
NAT
:
nat
.
Any
(),
}
if
err
:=
server
.
Start
();
err
!=
nil
{
return
nil
,
err
}
// Peer online, store and iterate
cluster
=
append
(
cluster
,
&
testNode
{
server
:
server
,
client
:
shh
,
})
}
}
// Manually wire together the cluster nodes
whispers
:=
make
([]
*
Whisper
,
n
)
root
:=
cluster
[
0
]
.
server
.
Self
()
for
i
:=
0
;
i
<
n
;
i
++
{
for
_
,
node
:=
range
cluster
[
1
:
]
{
whispers
[
i
]
=
New
()
node
.
server
.
SuggestPeer
(
root
)
whispers
[
i
]
.
Start
(
)
}
}
return
cluster
,
nil
// Wire all the peers to the root one
}
for
i
:=
1
;
i
<
n
;
i
++
{
src
,
dst
:=
bufMsgPipe
()
func
stopNodes
(
cluster
[]
*
testNode
)
{
go
whispers
[
0
]
.
handlePeer
(
nodes
[
i
],
src
)
for
_
,
node
:=
range
cluster
{
go
whispers
[
i
]
.
handlePeer
(
nodes
[
0
],
dst
)
node
.
server
.
Stop
()
}
}
return
whispers
}
}
func
TestSelfMessage
(
t
*
testing
.
T
)
{
func
TestSelfMessage
(
t
*
testing
.
T
)
{
// Start the single node cluster
// Start the single node cluster
cluster
,
err
:=
startNodes
(
1
)
client
:=
startTestCluster
(
1
)[
0
]
if
err
!=
nil
{
t
.
Fatalf
(
"failed to boot test cluster: %v"
,
err
)
}
defer
stopNodes
(
cluster
)
client
:=
cluster
[
0
]
.
client
// Start watching for self messages, signal any arrivals
// Start watching for self messages, signal any arrivals
self
:=
client
.
NewIdentity
()
self
:=
client
.
NewIdentity
()
...
@@ -104,16 +65,12 @@ func TestSelfMessage(t *testing.T) {
...
@@ -104,16 +65,12 @@ func TestSelfMessage(t *testing.T) {
func
TestDirectMessage
(
t
*
testing
.
T
)
{
func
TestDirectMessage
(
t
*
testing
.
T
)
{
// Start the sender-recipient cluster
// Start the sender-recipient cluster
cluster
,
err
:=
startNodes
(
2
)
cluster
:=
startTestCluster
(
2
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to boot test cluster: %v"
,
err
)
}
defer
stopNodes
(
cluster
)
sender
:=
cluster
[
0
]
.
client
sender
:=
cluster
[
0
]
senderId
:=
sender
.
NewIdentity
()
senderId
:=
sender
.
NewIdentity
()
recipient
:=
cluster
[
1
]
.
client
recipient
:=
cluster
[
1
]
recipientId
:=
recipient
.
NewIdentity
()
recipientId
:=
recipient
.
NewIdentity
()
// Watch for arriving messages on the recipient
// Watch for arriving messages on the recipient
...
@@ -155,18 +112,13 @@ func TestIdentifiedBroadcast(t *testing.T) {
...
@@ -155,18 +112,13 @@ func TestIdentifiedBroadcast(t *testing.T) {
func
testBroadcast
(
anonymous
bool
,
t
*
testing
.
T
)
{
func
testBroadcast
(
anonymous
bool
,
t
*
testing
.
T
)
{
// Start the single sender multi recipient cluster
// Start the single sender multi recipient cluster
cluster
,
err
:=
startNodes
(
3
)
cluster
:=
startTestCluster
(
3
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to boot test cluster: %v"
,
err
)
}
defer
stopNodes
(
cluster
)
sender
:=
cluster
[
0
]
.
client
sender
:=
cluster
[
1
]
targets
:=
make
([]
*
Whisper
,
len
(
cluster
)
-
1
)
targets
:=
cluster
[
1
:
]
for
i
,
node
:=
range
cluster
[
1
:
]
{
for
_
,
target
:=
range
targets
{
targets
[
i
]
=
node
.
client
if
!
anonymous
{
if
!
anonymous
{
target
s
[
i
]
.
NewIdentity
()
target
.
NewIdentity
()
}
}
}
}
// Watch for arriving messages on the recipients
// Watch for arriving messages on the recipients
...
...
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