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
3e38f8af
Commit
3e38f8af
authored
Dec 10, 2014
by
zelig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial commit for eth blockpool + test
parent
e74f9f27
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
712 additions
and
0 deletions
+712
-0
block_pool.go
eth/block_pool.go
+514
-0
block_pool_test.go
eth/block_pool_test.go
+198
-0
No files found.
eth/block_pool.go
0 → 100644
View file @
3e38f8af
This diff is collapsed.
Click to expand it.
eth/block_pool_test.go
0 → 100644
View file @
3e38f8af
package
eth
import
(
"bytes"
"fmt"
"log"
"os"
"sync"
"testing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
ethlogger
"github.com/ethereum/go-ethereum/logger"
)
var
sys
=
ethlogger
.
NewStdLogSystem
(
os
.
Stdout
,
log
.
LstdFlags
,
ethlogger
.
LogLevel
(
ethlogger
.
DebugDetailLevel
))
type
testChainManager
struct
{
knownBlock
func
(
hash
[]
byte
)
bool
addBlock
func
(
*
types
.
Block
)
error
checkPoW
func
(
*
types
.
Block
)
bool
}
func
(
self
*
testChainManager
)
KnownBlock
(
hash
[]
byte
)
bool
{
if
self
.
knownBlock
!=
nil
{
return
self
.
knownBlock
(
hash
)
}
return
false
}
func
(
self
*
testChainManager
)
AddBlock
(
block
*
types
.
Block
)
error
{
if
self
.
addBlock
!=
nil
{
return
self
.
addBlock
(
block
)
}
return
nil
}
func
(
self
*
testChainManager
)
CheckPoW
(
block
*
types
.
Block
)
bool
{
if
self
.
checkPoW
!=
nil
{
return
self
.
checkPoW
(
block
)
}
return
false
}
func
knownBlock
(
hashes
...
[]
byte
)
(
f
func
([]
byte
)
bool
)
{
f
=
func
(
block
[]
byte
)
bool
{
for
_
,
hash
:=
range
hashes
{
if
bytes
.
Compare
(
block
,
hash
)
==
0
{
return
true
}
}
return
false
}
return
}
func
addBlock
(
hashes
...
[]
byte
)
(
f
func
(
*
types
.
Block
)
error
)
{
f
=
func
(
block
*
types
.
Block
)
error
{
for
_
,
hash
:=
range
hashes
{
if
bytes
.
Compare
(
block
.
Hash
(),
hash
)
==
0
{
return
fmt
.
Errorf
(
"invalid by test"
)
}
}
return
nil
}
return
}
func
checkPoW
(
hashes
...
[]
byte
)
(
f
func
(
*
types
.
Block
)
bool
)
{
f
=
func
(
block
*
types
.
Block
)
bool
{
for
_
,
hash
:=
range
hashes
{
if
bytes
.
Compare
(
block
.
Hash
(),
hash
)
==
0
{
return
false
}
}
return
true
}
return
}
func
newTestChainManager
(
knownBlocks
[][]
byte
,
invalidBlocks
[][]
byte
,
invalidPoW
[][]
byte
)
*
testChainManager
{
return
&
testChainManager
{
knownBlock
:
knownBlock
(
knownBlocks
...
),
addBlock
:
addBlock
(
invalidBlocks
...
),
checkPoW
:
checkPoW
(
invalidPoW
...
),
}
}
type
intToHash
map
[
int
][]
byte
type
hashToInt
map
[
string
]
int
type
testHashPool
struct
{
intToHash
hashToInt
}
func
newHash
(
i
int
)
[]
byte
{
return
crypto
.
Sha3
([]
byte
(
string
(
i
)))
}
func
newTestBlockPool
(
knownBlockIndexes
[]
int
,
invalidBlockIndexes
[]
int
,
invalidPoWIndexes
[]
int
)
(
hashPool
*
testHashPool
,
blockPool
*
BlockPool
)
{
hashPool
=
&
testHashPool
{
make
(
intToHash
),
make
(
hashToInt
)}
knownBlocks
:=
hashPool
.
indexesToHashes
(
knownBlockIndexes
)
invalidBlocks
:=
hashPool
.
indexesToHashes
(
invalidBlockIndexes
)
invalidPoW
:=
hashPool
.
indexesToHashes
(
invalidPoWIndexes
)
blockPool
=
NewBlockPool
(
newTestChainManager
(
knownBlocks
,
invalidBlocks
,
invalidPoW
))
return
}
func
(
self
*
testHashPool
)
indexesToHashes
(
indexes
[]
int
)
(
hashes
[][]
byte
)
{
for
_
,
i
:=
range
indexes
{
hash
,
found
:=
self
.
intToHash
[
i
]
if
!
found
{
hash
=
newHash
(
i
)
self
.
intToHash
[
i
]
=
hash
self
.
hashToInt
[
string
(
hash
)]
=
i
}
hashes
=
append
(
hashes
,
hash
)
}
return
}
func
(
self
*
testHashPool
)
hashesToIndexes
(
hashes
[][]
byte
)
(
indexes
[]
int
)
{
for
_
,
hash
:=
range
hashes
{
i
,
found
:=
self
.
hashToInt
[
string
(
hash
)]
if
!
found
{
i
=
-
1
}
indexes
=
append
(
indexes
,
i
)
}
return
}
type
protocolChecker
struct
{
blockHashesRequests
[]
int
blocksRequests
[][]
int
invalidBlocks
[]
error
hashPool
*
testHashPool
lock
sync
.
Mutex
}
// -1 is special: not found (a hash never seen)
func
(
self
*
protocolChecker
)
requestBlockHashesCallBack
()
(
requestBlockHashesCallBack
func
([]
byte
)
error
)
{
requestBlockHashesCallBack
=
func
(
hash
[]
byte
)
error
{
indexes
:=
self
.
hashPool
.
hashesToIndexes
([][]
byte
{
hash
})
self
.
lock
.
Lock
()
defer
self
.
lock
.
Unlock
()
self
.
blockHashesRequests
=
append
(
self
.
blockHashesRequests
,
indexes
[
0
])
return
nil
}
return
}
func
(
self
*
protocolChecker
)
requestBlocksCallBack
()
(
requestBlocksCallBack
func
([][]
byte
)
error
)
{
requestBlocksCallBack
=
func
(
hashes
[][]
byte
)
error
{
indexes
:=
self
.
hashPool
.
hashesToIndexes
(
hashes
)
self
.
lock
.
Lock
()
defer
self
.
lock
.
Unlock
()
self
.
blocksRequests
=
append
(
self
.
blocksRequests
,
indexes
)
return
nil
}
return
}
func
(
self
*
protocolChecker
)
invalidBlockCallBack
()
(
invalidBlockCallBack
func
(
error
))
{
invalidBlockCallBack
=
func
(
err
error
)
{
self
.
invalidBlocks
=
append
(
self
.
invalidBlocks
,
err
)
}
return
}
func
TestAddPeer
(
t
*
testing
.
T
)
{
ethlogger
.
AddLogSystem
(
sys
)
knownBlockIndexes
:=
[]
int
{
0
,
1
}
invalidBlockIndexes
:=
[]
int
{
2
,
3
}
invalidPoWIndexes
:=
[]
int
{
4
,
5
}
hashPool
,
blockPool
:=
newTestBlockPool
(
knownBlockIndexes
,
invalidBlockIndexes
,
invalidPoWIndexes
)
// TODO:
// hashPool, blockPool, blockChainChecker = newTestBlockPool(knownBlockIndexes, invalidBlockIndexes, invalidPoWIndexes)
peer0
:=
&
protocolChecker
{
// blockHashesRequests: make([]int),
// blocksRequests: make([][]int),
// invalidBlocks: make([]error),
hashPool
:
hashPool
,
}
best
:=
blockPool
.
AddPeer
(
ethutil
.
Big1
,
newHash
(
100
),
"0"
,
peer0
.
requestBlockHashesCallBack
(),
peer0
.
requestBlocksCallBack
(),
peer0
.
invalidBlockCallBack
(),
)
if
!
best
{
t
.
Errorf
(
"peer not accepted as best"
)
}
blockPool
.
Stop
()
}
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