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
a9ada0b5
Commit
a9ada0b5
authored
Jun 17, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
eth/fetcher: make tests thread safe
parent
37c5ff39
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
22 deletions
+34
-22
fetcher_test.go
eth/fetcher/fetcher_test.go
+34
-22
No files found.
eth/fetcher/fetcher_test.go
View file @
a9ada0b5
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"encoding/binary"
"encoding/binary"
"errors"
"errors"
"math/big"
"math/big"
"sync"
"sync/atomic"
"sync/atomic"
"testing"
"testing"
"time"
"time"
...
@@ -67,15 +68,17 @@ func createBlocksFromHashes(hashes []common.Hash) map[common.Hash]*types.Block {
...
@@ -67,15 +68,17 @@ func createBlocksFromHashes(hashes []common.Hash) map[common.Hash]*types.Block {
type
fetcherTester
struct
{
type
fetcherTester
struct
{
fetcher
*
Fetcher
fetcher
*
Fetcher
ownHashes
[]
common
.
Hash
// Hash chain belonging to the tester
hashes
[]
common
.
Hash
// Hash chain belonging to the tester
ownBlocks
map
[
common
.
Hash
]
*
types
.
Block
// Blocks belonging to the tester
blocks
map
[
common
.
Hash
]
*
types
.
Block
// Blocks belonging to the tester
lock
sync
.
RWMutex
}
}
// newTester creates a new fetcher test mocker.
// newTester creates a new fetcher test mocker.
func
newTester
()
*
fetcherTester
{
func
newTester
()
*
fetcherTester
{
tester
:=
&
fetcherTester
{
tester
:=
&
fetcherTester
{
ownH
ashes
:
[]
common
.
Hash
{
knownHash
},
h
ashes
:
[]
common
.
Hash
{
knownHash
},
ownB
locks
:
map
[
common
.
Hash
]
*
types
.
Block
{
knownHash
:
genesis
},
b
locks
:
map
[
common
.
Hash
]
*
types
.
Block
{
knownHash
:
genesis
},
}
}
tester
.
fetcher
=
New
(
tester
.
hasBlock
,
tester
.
importBlock
,
tester
.
chainHeight
)
tester
.
fetcher
=
New
(
tester
.
hasBlock
,
tester
.
importBlock
,
tester
.
chainHeight
)
tester
.
fetcher
.
Start
()
tester
.
fetcher
.
Start
()
...
@@ -85,29 +88,38 @@ func newTester() *fetcherTester {
...
@@ -85,29 +88,38 @@ func newTester() *fetcherTester {
// hasBlock checks if a block is pres ent in the testers canonical chain.
// hasBlock checks if a block is pres ent in the testers canonical chain.
func
(
f
*
fetcherTester
)
hasBlock
(
hash
common
.
Hash
)
bool
{
func
(
f
*
fetcherTester
)
hasBlock
(
hash
common
.
Hash
)
bool
{
_
,
ok
:=
f
.
ownBlocks
[
hash
]
f
.
lock
.
RLock
()
defer
f
.
lock
.
RUnlock
()
_
,
ok
:=
f
.
blocks
[
hash
]
return
ok
return
ok
}
}
// importBlock injects a new blocks into the simulated chain.
// importBlock injects a new blocks into the simulated chain.
func
(
f
*
fetcherTester
)
importBlock
(
peer
string
,
block
*
types
.
Block
)
error
{
func
(
f
*
fetcherTester
)
importBlock
(
peer
string
,
block
*
types
.
Block
)
error
{
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
// Make sure the parent in known
// Make sure the parent in known
if
_
,
ok
:=
f
.
ownB
locks
[
block
.
ParentHash
()];
!
ok
{
if
_
,
ok
:=
f
.
b
locks
[
block
.
ParentHash
()];
!
ok
{
return
errors
.
New
(
"unknown parent"
)
return
errors
.
New
(
"unknown parent"
)
}
}
// Discard any new blocks if the same height already exists
// Discard any new blocks if the same height already exists
if
block
.
NumberU64
()
<=
f
.
ownBlocks
[
f
.
ownHashes
[
len
(
f
.
ownH
ashes
)
-
1
]]
.
NumberU64
()
{
if
block
.
NumberU64
()
<=
f
.
blocks
[
f
.
hashes
[
len
(
f
.
h
ashes
)
-
1
]]
.
NumberU64
()
{
return
nil
return
nil
}
}
// Otherwise build our current chain
// Otherwise build our current chain
f
.
ownHashes
=
append
(
f
.
ownH
ashes
,
block
.
Hash
())
f
.
hashes
=
append
(
f
.
h
ashes
,
block
.
Hash
())
f
.
ownB
locks
[
block
.
Hash
()]
=
block
f
.
b
locks
[
block
.
Hash
()]
=
block
return
nil
return
nil
}
}
// chainHeight retrieves the current height (block number) of the chain.
// chainHeight retrieves the current height (block number) of the chain.
func
(
f
*
fetcherTester
)
chainHeight
()
uint64
{
func
(
f
*
fetcherTester
)
chainHeight
()
uint64
{
return
f
.
ownBlocks
[
f
.
ownHashes
[
len
(
f
.
ownHashes
)
-
1
]]
.
NumberU64
()
f
.
lock
.
RLock
()
defer
f
.
lock
.
RUnlock
()
return
f
.
blocks
[
f
.
hashes
[
len
(
f
.
hashes
)
-
1
]]
.
NumberU64
()
}
}
// peerFetcher retrieves a fetcher associated with a simulated peer.
// peerFetcher retrieves a fetcher associated with a simulated peer.
...
@@ -149,7 +161,7 @@ func TestSequentialAnnouncements(t *testing.T) {
...
@@ -149,7 +161,7 @@ func TestSequentialAnnouncements(t *testing.T) {
tester
.
fetcher
.
Notify
(
"valid"
,
hashes
[
i
],
time
.
Now
()
.
Add
(
-
arriveTimeout
),
fetcher
)
tester
.
fetcher
.
Notify
(
"valid"
,
hashes
[
i
],
time
.
Now
()
.
Add
(
-
arriveTimeout
),
fetcher
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
}
}
if
imported
:=
len
(
tester
.
ownB
locks
);
imported
!=
targetBlocks
+
1
{
if
imported
:=
len
(
tester
.
b
locks
);
imported
!=
targetBlocks
+
1
{
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
}
}
}
}
...
@@ -179,7 +191,7 @@ func TestConcurrentAnnouncements(t *testing.T) {
...
@@ -179,7 +191,7 @@ func TestConcurrentAnnouncements(t *testing.T) {
time
.
Sleep
(
50
*
time
.
Millisecond
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
}
}
if
imported
:=
len
(
tester
.
ownB
locks
);
imported
!=
targetBlocks
+
1
{
if
imported
:=
len
(
tester
.
b
locks
);
imported
!=
targetBlocks
+
1
{
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
}
}
// Make sure no blocks were retrieved twice
// Make sure no blocks were retrieved twice
...
@@ -207,7 +219,7 @@ func TestOverlappingAnnouncements(t *testing.T) {
...
@@ -207,7 +219,7 @@ func TestOverlappingAnnouncements(t *testing.T) {
}
}
time
.
Sleep
(
overlap
*
delay
)
time
.
Sleep
(
overlap
*
delay
)
if
imported
:=
len
(
tester
.
ownB
locks
);
imported
!=
targetBlocks
+
1
{
if
imported
:=
len
(
tester
.
b
locks
);
imported
!=
targetBlocks
+
1
{
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
}
}
}
}
...
@@ -242,7 +254,7 @@ func TestPendingDeduplication(t *testing.T) {
...
@@ -242,7 +254,7 @@ func TestPendingDeduplication(t *testing.T) {
time
.
Sleep
(
delay
)
time
.
Sleep
(
delay
)
// Check that all blocks were imported and none fetched twice
// Check that all blocks were imported and none fetched twice
if
imported
:=
len
(
tester
.
ownB
locks
);
imported
!=
2
{
if
imported
:=
len
(
tester
.
b
locks
);
imported
!=
2
{
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
2
)
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
2
)
}
}
if
int
(
counter
)
!=
1
{
if
int
(
counter
)
!=
1
{
...
@@ -273,7 +285,7 @@ func TestRandomArrivalImport(t *testing.T) {
...
@@ -273,7 +285,7 @@ func TestRandomArrivalImport(t *testing.T) {
tester
.
fetcher
.
Notify
(
"valid"
,
hashes
[
skip
],
time
.
Now
()
.
Add
(
-
arriveTimeout
),
fetcher
)
tester
.
fetcher
.
Notify
(
"valid"
,
hashes
[
skip
],
time
.
Now
()
.
Add
(
-
arriveTimeout
),
fetcher
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
if
imported
:=
len
(
tester
.
ownB
locks
);
imported
!=
targetBlocks
+
1
{
if
imported
:=
len
(
tester
.
b
locks
);
imported
!=
targetBlocks
+
1
{
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
}
}
}
}
...
@@ -301,7 +313,7 @@ func TestQueueGapFill(t *testing.T) {
...
@@ -301,7 +313,7 @@ func TestQueueGapFill(t *testing.T) {
tester
.
fetcher
.
Enqueue
(
"valid"
,
blocks
[
hashes
[
skip
]])
tester
.
fetcher
.
Enqueue
(
"valid"
,
blocks
[
hashes
[
skip
]])
time
.
Sleep
(
50
*
time
.
Millisecond
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
if
imported
:=
len
(
tester
.
ownB
locks
);
imported
!=
targetBlocks
+
1
{
if
imported
:=
len
(
tester
.
b
locks
);
imported
!=
targetBlocks
+
1
{
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
targetBlocks
+
1
)
}
}
}
}
...
@@ -334,7 +346,7 @@ func TestImportDeduplication(t *testing.T) {
...
@@ -334,7 +346,7 @@ func TestImportDeduplication(t *testing.T) {
tester
.
fetcher
.
Enqueue
(
"valid"
,
blocks
[
hashes
[
1
]])
tester
.
fetcher
.
Enqueue
(
"valid"
,
blocks
[
hashes
[
1
]])
time
.
Sleep
(
50
*
time
.
Millisecond
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
if
imported
:=
len
(
tester
.
ownB
locks
);
imported
!=
3
{
if
imported
:=
len
(
tester
.
b
locks
);
imported
!=
3
{
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
3
)
t
.
Fatalf
(
"synchronised block mismatch: have %v, want %v"
,
imported
,
3
)
}
}
if
counter
!=
2
{
if
counter
!=
2
{
...
@@ -353,8 +365,8 @@ func TestDistantDiscarding(t *testing.T) {
...
@@ -353,8 +365,8 @@ func TestDistantDiscarding(t *testing.T) {
// Create a tester and simulate a head block being the middle of the above chain
// Create a tester and simulate a head block being the middle of the above chain
tester
:=
newTester
()
tester
:=
newTester
()
tester
.
ownH
ashes
=
[]
common
.
Hash
{
head
}
tester
.
h
ashes
=
[]
common
.
Hash
{
head
}
tester
.
ownB
locks
=
map
[
common
.
Hash
]
*
types
.
Block
{
head
:
blocks
[
head
]}
tester
.
b
locks
=
map
[
common
.
Hash
]
*
types
.
Block
{
head
:
blocks
[
head
]}
// Ensure that a block with a lower number than the threshold is discarded
// Ensure that a block with a lower number than the threshold is discarded
tester
.
fetcher
.
Enqueue
(
"lower"
,
blocks
[
hashes
[
0
]])
tester
.
fetcher
.
Enqueue
(
"lower"
,
blocks
[
hashes
[
0
]])
...
@@ -413,10 +425,10 @@ func TestCompetingImports(t *testing.T) {
...
@@ -413,10 +425,10 @@ func TestCompetingImports(t *testing.T) {
tester
.
fetcher
.
Enqueue
(
"chain C"
,
blocksC
[
hashesC
[
len
(
hashesC
)
-
2
]])
tester
.
fetcher
.
Enqueue
(
"chain C"
,
blocksC
[
hashesC
[
len
(
hashesC
)
-
2
]])
start
:=
time
.
Now
()
start
:=
time
.
Now
()
for
len
(
tester
.
ownH
ashes
)
!=
len
(
hashesA
)
&&
time
.
Since
(
start
)
<
time
.
Second
{
for
len
(
tester
.
h
ashes
)
!=
len
(
hashesA
)
&&
time
.
Since
(
start
)
<
time
.
Second
{
time
.
Sleep
(
50
*
time
.
Millisecond
)
time
.
Sleep
(
50
*
time
.
Millisecond
)
}
}
if
len
(
tester
.
ownH
ashes
)
!=
len
(
hashesA
)
{
if
len
(
tester
.
h
ashes
)
!=
len
(
hashesA
)
{
t
.
Fatalf
(
"chain length mismatch: have %v, want %v"
,
len
(
tester
.
ownH
ashes
),
len
(
hashesA
))
t
.
Fatalf
(
"chain length mismatch: have %v, want %v"
,
len
(
tester
.
h
ashes
),
len
(
hashesA
))
}
}
}
}
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