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
6c2b703c
Commit
6c2b703c
authored
Apr 21, 2015
by
obscuren
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' of github.com-obscure:ethereum/go-ethereum into develop
parents
f4cd66dc
1dc91975
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
294 additions
and
115 deletions
+294
-115
README.md
README.md
+1
-1
block_go_test.go
cmd/geth/block_go_test.go
+80
-0
blocktest.go
cmd/geth/blocktest.go
+65
-24
backend.go
eth/backend.go
+4
-1
responses.go
rpc/responses.go
+0
-6
block_test_util.go
tests/block_test_util.go
+63
-21
transaction_test_util.go
tests/transaction_test_util.go
+81
-62
No files found.
README.md
View file @
6c2b703c
...
@@ -40,7 +40,7 @@ godep go build -v ./cmd/geth
...
@@ -40,7 +40,7 @@ godep go build -v ./cmd/geth
Instead of
`build`
, you can use
`install`
which will also install the resulting binary.
Instead of
`build`
, you can use
`install`
which will also install the resulting binary.
For prerequisites and detailed build instructions please see the
[
Wiki
](
https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum
(Go
)
)
For prerequisites and detailed build instructions please see the
[
Wiki
](
https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum
)
If you intend to develop on go-ethereum, check the
[
Developers' Guide
](
https://github.com/ethereum/go-ethereum/wiki/Developers'-Guide
)
If you intend to develop on go-ethereum, check the
[
Developers' Guide
](
https://github.com/ethereum/go-ethereum/wiki/Developers'-Guide
)
...
...
cmd/geth/block_go_test.go
0 → 100644
View file @
6c2b703c
package
main
import
(
"path"
"testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/tests"
)
// TODO: refactor test setup & execution to better align with vm and tx tests
// TODO: refactor to avoid duplication with cmd/geth/blocktest.go
func
TestBcValidBlockTests
(
t
*
testing
.
T
)
{
runBlockTestsInFile
(
"../../tests/files/BlockTests/bcValidBlockTest.json"
,
t
)
}
/*
func TestBcUncleTests(t *testing.T) {
runBlockTestsInFile("../../tests/files/BlockTests/bcUncleTest.json", t)
}
*/
func
runBlockTestsInFile
(
filepath
string
,
t
*
testing
.
T
)
{
bt
,
err
:=
tests
.
LoadBlockTests
(
filepath
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
name
,
test
:=
range
bt
{
runTest
(
name
,
test
,
t
)
}
}
func
runTest
(
name
string
,
test
*
tests
.
BlockTest
,
t
*
testing
.
T
)
{
t
.
Log
(
"Running test: "
,
name
)
cfg
:=
testEthConfig
()
ethereum
,
err
:=
eth
.
New
(
cfg
)
if
err
!=
nil
{
t
.
Fatalf
(
"%v"
,
err
)
}
err
=
ethereum
.
Start
()
if
err
!=
nil
{
t
.
Fatalf
(
"%v"
,
err
)
}
// import the genesis block
ethereum
.
ResetWithGenesisBlock
(
test
.
Genesis
)
// import pre accounts
statedb
,
err
:=
test
.
InsertPreState
(
ethereum
.
StateDb
())
if
err
!=
nil
{
t
.
Fatalf
(
"InsertPreState: %v"
,
err
)
}
// insert the test blocks, which will execute all transactions
if
err
:=
test
.
InsertBlocks
(
ethereum
.
ChainManager
());
err
!=
nil
{
t
.
Fatalf
(
"Block Test load error: %v %T"
,
err
,
err
)
}
if
err
:=
test
.
ValidatePostState
(
statedb
);
err
!=
nil
{
t
.
Fatal
(
"post state validation failed: %v"
,
err
)
}
t
.
Log
(
"Test passed: "
,
name
)
}
func
testEthConfig
()
*
eth
.
Config
{
ks
:=
crypto
.
NewKeyStorePassphrase
(
path
.
Join
(
common
.
DefaultDataDir
(),
"keys"
))
return
&
eth
.
Config
{
DataDir
:
common
.
DefaultDataDir
(),
LogLevel
:
5
,
Etherbase
:
"primary"
,
AccountManager
:
accounts
.
NewManager
(
ks
),
NewDB
:
func
(
path
string
)
(
common
.
Database
,
error
)
{
return
ethdb
.
NewMemDatabase
()
},
}
}
cmd/geth/blocktest.go
View file @
6c2b703c
...
@@ -2,6 +2,7 @@ package main
...
@@ -2,6 +2,7 @@ package main
import
(
import
(
"fmt"
"fmt"
"os"
"github.com/codegangsta/cli"
"github.com/codegangsta/cli"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/cmd/utils"
...
@@ -12,7 +13,7 @@ import (
...
@@ -12,7 +13,7 @@ import (
)
)
var
blocktestCmd
=
cli
.
Command
{
var
blocktestCmd
=
cli
.
Command
{
Action
:
run
blockt
est
,
Action
:
run
BlockT
est
,
Name
:
"blocktest"
,
Name
:
"blocktest"
,
Usage
:
`loads a block test file`
,
Usage
:
`loads a block test file`
,
Description
:
`
Description
:
`
...
@@ -25,27 +26,78 @@ be able to interact with the chain defined by the test.
...
@@ -25,27 +26,78 @@ be able to interact with the chain defined by the test.
`
,
`
,
}
}
func
runblocktest
(
ctx
*
cli
.
Context
)
{
func
runBlockTest
(
ctx
*
cli
.
Context
)
{
if
len
(
ctx
.
Args
())
!=
3
{
var
(
utils
.
Fatalf
(
"Usage: ethereum blocktest <path-to-test-file> <test-name> {rpc, norpc}"
)
file
,
testname
string
rpc
bool
)
args
:=
ctx
.
Args
()
switch
{
case
len
(
args
)
==
1
:
file
=
args
[
0
]
case
len
(
args
)
==
2
:
file
,
testname
=
args
[
0
],
args
[
1
]
case
len
(
args
)
==
3
:
file
,
testname
=
args
[
0
],
args
[
1
]
rpc
=
true
default
:
utils
.
Fatalf
(
`Usage: ethereum blocktest <path-to-test-file> [ <test-name> [ "rpc" ] ]`
)
}
}
file
,
testname
,
startrpc
:=
ctx
.
Args
()[
0
],
ctx
.
Args
()[
1
],
ctx
.
Args
()[
2
]
bt
,
err
:=
tests
.
LoadBlockTests
(
file
)
bt
,
err
:=
tests
.
LoadBlockTests
(
file
)
if
err
!=
nil
{
if
err
!=
nil
{
utils
.
Fatalf
(
"%v"
,
err
)
utils
.
Fatalf
(
"%v"
,
err
)
}
}
// run all tests if no test name is specified
if
testname
==
""
{
ecode
:=
0
for
name
,
test
:=
range
bt
{
fmt
.
Printf
(
"----------------- Running Block Test %q
\n
"
,
name
)
ethereum
,
err
:=
runOneBlockTest
(
ctx
,
test
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
fmt
.
Println
(
"FAIL"
)
ecode
=
1
}
if
ethereum
!=
nil
{
ethereum
.
Stop
()
ethereum
.
WaitForShutdown
()
}
}
os
.
Exit
(
ecode
)
return
}
// otherwise, run the given test
test
,
ok
:=
bt
[
testname
]
test
,
ok
:=
bt
[
testname
]
if
!
ok
{
if
!
ok
{
utils
.
Fatalf
(
"Test file does not contain test named %q"
,
testname
)
utils
.
Fatalf
(
"Test file does not contain test named %q"
,
testname
)
}
}
ethereum
,
err
:=
runOneBlockTest
(
ctx
,
test
)
if
err
!=
nil
{
utils
.
Fatalf
(
"%v"
,
err
)
}
defer
ethereum
.
Stop
()
if
rpc
{
fmt
.
Println
(
"Block Test post state validated, starting RPC interface."
)
startEth
(
ctx
,
ethereum
)
utils
.
StartRPC
(
ethereum
,
ctx
)
ethereum
.
WaitForShutdown
()
}
}
func
runOneBlockTest
(
ctx
*
cli
.
Context
,
test
*
tests
.
BlockTest
)
(
*
eth
.
Ethereum
,
error
)
{
cfg
:=
utils
.
MakeEthConfig
(
ClientIdentifier
,
Version
,
ctx
)
cfg
:=
utils
.
MakeEthConfig
(
ClientIdentifier
,
Version
,
ctx
)
cfg
.
NewDB
=
func
(
path
string
)
(
common
.
Database
,
error
)
{
return
ethdb
.
NewMemDatabase
()
}
cfg
.
NewDB
=
func
(
path
string
)
(
common
.
Database
,
error
)
{
return
ethdb
.
NewMemDatabase
()
}
cfg
.
MaxPeers
=
0
// disable network
cfg
.
MaxPeers
=
0
// disable network
cfg
.
Shh
=
false
// disable whisper
cfg
.
NAT
=
nil
// disable port mapping
ethereum
,
err
:=
eth
.
New
(
cfg
)
ethereum
,
err
:=
eth
.
New
(
cfg
)
if
err
!=
nil
{
if
err
!=
nil
{
utils
.
Fatalf
(
"%v"
,
err
)
return
nil
,
err
}
if
err
:=
ethereum
.
Start
();
err
!=
nil
{
return
nil
,
err
}
}
// import the genesis block
// import the genesis block
...
@@ -54,27 +106,16 @@ func runblocktest(ctx *cli.Context) {
...
@@ -54,27 +106,16 @@ func runblocktest(ctx *cli.Context) {
// import pre accounts
// import pre accounts
statedb
,
err
:=
test
.
InsertPreState
(
ethereum
.
StateDb
())
statedb
,
err
:=
test
.
InsertPreState
(
ethereum
.
StateDb
())
if
err
!=
nil
{
if
err
!=
nil
{
utils
.
Fatalf
(
"could not insert genesis accounts
: %v"
,
err
)
return
ethereum
,
fmt
.
Errorf
(
"InsertPreState
: %v"
,
err
)
}
}
// insert the test blocks, which will execute all transactions
// insert the test blocks, which will execute all transactions
chain
:=
ethereum
.
ChainManager
()
if
err
:=
test
.
InsertBlocks
(
ethereum
.
ChainManager
());
err
!=
nil
{
if
err
:=
chain
.
InsertChain
(
test
.
Blocks
);
err
!=
nil
{
return
ethereum
,
fmt
.
Errorf
(
"Block Test load error: %v %T"
,
err
,
err
)
utils
.
Fatalf
(
"Block Test load error: %v %T"
,
err
,
err
)
}
else
{
fmt
.
Println
(
"Block Test chain loaded"
)
}
}
fmt
.
Println
(
"chain loaded"
)
if
err
:=
test
.
ValidatePostState
(
statedb
);
err
!=
nil
{
if
err
:=
test
.
ValidatePostState
(
statedb
);
err
!=
nil
{
utils
.
Fatalf
(
"post state validation failed: %v"
,
err
)
return
ethereum
,
fmt
.
Errorf
(
"post state validation failed: %v"
,
err
)
}
fmt
.
Println
(
"Block Test post state validated, starting ethereum."
)
if
startrpc
==
"rpc"
{
startEth
(
ctx
,
ethereum
)
utils
.
StartRPC
(
ethereum
,
ctx
)
ethereum
.
WaitForShutdown
()
}
else
{
startEth
(
ctx
,
ethereum
)
}
}
return
ethereum
,
nil
}
}
eth/backend.go
View file @
6c2b703c
...
@@ -173,7 +173,10 @@ func New(config *Config) (*Ethereum, error) {
...
@@ -173,7 +173,10 @@ func New(config *Config) (*Ethereum, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
extraDb
,
err
:=
ethdb
.
NewLDBDatabase
(
path
.
Join
(
config
.
DataDir
,
"extra"
))
extraDb
,
err
:=
newdb
(
path
.
Join
(
config
.
DataDir
,
"extra"
))
if
err
!=
nil
{
return
nil
,
err
}
// Perform database sanity checks
// Perform database sanity checks
d
,
_
:=
blockDb
.
Get
([]
byte
(
"ProtocolVersion"
))
d
,
_
:=
blockDb
.
Get
([]
byte
(
"ProtocolVersion"
))
...
...
rpc/responses.go
View file @
6c2b703c
...
@@ -24,7 +24,6 @@ type BlockRes struct {
...
@@ -24,7 +24,6 @@ type BlockRes struct {
Size
*
hexnum
`json:"size"`
Size
*
hexnum
`json:"size"`
ExtraData
*
hexdata
`json:"extraData"`
ExtraData
*
hexdata
`json:"extraData"`
GasLimit
*
hexnum
`json:"gasLimit"`
GasLimit
*
hexnum
`json:"gasLimit"`
MinGasPrice
*
hexnum
`json:"minGasPrice"`
GasUsed
*
hexnum
`json:"gasUsed"`
GasUsed
*
hexnum
`json:"gasUsed"`
UnixTimestamp
*
hexnum
`json:"timestamp"`
UnixTimestamp
*
hexnum
`json:"timestamp"`
Transactions
[]
*
TransactionRes
`json:"transactions"`
Transactions
[]
*
TransactionRes
`json:"transactions"`
...
@@ -48,7 +47,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
...
@@ -48,7 +47,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
Size
*
hexnum
`json:"size"`
Size
*
hexnum
`json:"size"`
ExtraData
*
hexdata
`json:"extraData"`
ExtraData
*
hexdata
`json:"extraData"`
GasLimit
*
hexnum
`json:"gasLimit"`
GasLimit
*
hexnum
`json:"gasLimit"`
MinGasPrice
*
hexnum
`json:"minGasPrice"`
GasUsed
*
hexnum
`json:"gasUsed"`
GasUsed
*
hexnum
`json:"gasUsed"`
UnixTimestamp
*
hexnum
`json:"timestamp"`
UnixTimestamp
*
hexnum
`json:"timestamp"`
Transactions
[]
*
TransactionRes
`json:"transactions"`
Transactions
[]
*
TransactionRes
`json:"transactions"`
...
@@ -69,7 +67,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
...
@@ -69,7 +67,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
ext
.
Size
=
b
.
Size
ext
.
Size
=
b
.
Size
ext
.
ExtraData
=
b
.
ExtraData
ext
.
ExtraData
=
b
.
ExtraData
ext
.
GasLimit
=
b
.
GasLimit
ext
.
GasLimit
=
b
.
GasLimit
ext
.
MinGasPrice
=
b
.
MinGasPrice
ext
.
GasUsed
=
b
.
GasUsed
ext
.
GasUsed
=
b
.
GasUsed
ext
.
UnixTimestamp
=
b
.
UnixTimestamp
ext
.
UnixTimestamp
=
b
.
UnixTimestamp
ext
.
Transactions
=
b
.
Transactions
ext
.
Transactions
=
b
.
Transactions
...
@@ -94,7 +91,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
...
@@ -94,7 +91,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
Size
*
hexnum
`json:"size"`
Size
*
hexnum
`json:"size"`
ExtraData
*
hexdata
`json:"extraData"`
ExtraData
*
hexdata
`json:"extraData"`
GasLimit
*
hexnum
`json:"gasLimit"`
GasLimit
*
hexnum
`json:"gasLimit"`
MinGasPrice
*
hexnum
`json:"minGasPrice"`
GasUsed
*
hexnum
`json:"gasUsed"`
GasUsed
*
hexnum
`json:"gasUsed"`
UnixTimestamp
*
hexnum
`json:"timestamp"`
UnixTimestamp
*
hexnum
`json:"timestamp"`
Transactions
[]
*
hexdata
`json:"transactions"`
Transactions
[]
*
hexdata
`json:"transactions"`
...
@@ -115,7 +111,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
...
@@ -115,7 +111,6 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
ext
.
Size
=
b
.
Size
ext
.
Size
=
b
.
Size
ext
.
ExtraData
=
b
.
ExtraData
ext
.
ExtraData
=
b
.
ExtraData
ext
.
GasLimit
=
b
.
GasLimit
ext
.
GasLimit
=
b
.
GasLimit
ext
.
MinGasPrice
=
b
.
MinGasPrice
ext
.
GasUsed
=
b
.
GasUsed
ext
.
GasUsed
=
b
.
GasUsed
ext
.
UnixTimestamp
=
b
.
UnixTimestamp
ext
.
UnixTimestamp
=
b
.
UnixTimestamp
ext
.
Transactions
=
make
([]
*
hexdata
,
len
(
b
.
Transactions
))
ext
.
Transactions
=
make
([]
*
hexdata
,
len
(
b
.
Transactions
))
...
@@ -151,7 +146,6 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
...
@@ -151,7 +146,6 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
res
.
Size
=
newHexNum
(
block
.
Size
()
.
Int64
())
res
.
Size
=
newHexNum
(
block
.
Size
()
.
Int64
())
res
.
ExtraData
=
newHexData
(
block
.
Header
()
.
Extra
)
res
.
ExtraData
=
newHexData
(
block
.
Header
()
.
Extra
)
res
.
GasLimit
=
newHexNum
(
block
.
GasLimit
())
res
.
GasLimit
=
newHexNum
(
block
.
GasLimit
())
// res.MinGasPrice =
res
.
GasUsed
=
newHexNum
(
block
.
GasUsed
())
res
.
GasUsed
=
newHexNum
(
block
.
GasUsed
())
res
.
UnixTimestamp
=
newHexNum
(
block
.
Time
())
res
.
UnixTimestamp
=
newHexNum
(
block
.
Time
())
...
...
tests/block
test
.go
→
tests/block
_test_util
.go
View file @
6c2b703c
...
@@ -12,6 +12,7 @@ import (
...
@@ -12,6 +12,7 @@ import (
"strings"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rlp"
...
@@ -73,8 +74,8 @@ type btBlock struct {
...
@@ -73,8 +74,8 @@ type btBlock struct {
type
BlockTest
struct
{
type
BlockTest
struct
{
Genesis
*
types
.
Block
Genesis
*
types
.
Block
Blocks
[]
*
types
.
Block
json
*
btJSON
preAccounts
map
[
string
]
btAccount
preAccounts
map
[
string
]
btAccount
}
}
...
@@ -88,7 +89,7 @@ func LoadBlockTests(file string) (map[string]*BlockTest, error) {
...
@@ -88,7 +89,7 @@ func LoadBlockTests(file string) (map[string]*BlockTest, error) {
for
name
,
in
:=
range
bt
{
for
name
,
in
:=
range
bt
{
var
err
error
var
err
error
if
out
[
name
],
err
=
convertTest
(
in
);
err
!=
nil
{
if
out
[
name
],
err
=
convertTest
(
in
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"bad test %q: %v"
,
err
)
return
out
,
fmt
.
Errorf
(
"bad test %q: %v"
,
name
,
err
)
}
}
}
}
return
out
,
nil
return
out
,
nil
...
@@ -124,6 +125,15 @@ func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) {
...
@@ -124,6 +125,15 @@ func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) {
return
statedb
,
nil
return
statedb
,
nil
}
}
// InsertBlocks loads the test's blocks into the given chain.
func
(
t
*
BlockTest
)
InsertBlocks
(
chain
*
core
.
ChainManager
)
error
{
blocks
,
err
:=
t
.
convertBlocks
()
if
err
!=
nil
{
return
err
}
return
chain
.
InsertChain
(
blocks
)
}
func
(
t
*
BlockTest
)
ValidatePostState
(
statedb
*
state
.
StateDB
)
error
{
func
(
t
*
BlockTest
)
ValidatePostState
(
statedb
*
state
.
StateDB
)
error
{
for
addrString
,
acct
:=
range
t
.
preAccounts
{
for
addrString
,
acct
:=
range
t
.
preAccounts
{
// XXX: is is worth it checking for errors here?
// XXX: is is worth it checking for errors here?
...
@@ -149,6 +159,21 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
...
@@ -149,6 +159,21 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
return
nil
return
nil
}
}
func
(
t
*
BlockTest
)
convertBlocks
()
(
blocks
[]
*
types
.
Block
,
err
error
)
{
// the conversion handles errors by catching panics.
// you might consider this ugly, but the alternative (passing errors)
// would be much harder to read.
defer
func
()
{
if
recovered
:=
recover
();
recovered
!=
nil
{
buf
:=
make
([]
byte
,
64
<<
10
)
buf
=
buf
[
:
runtime
.
Stack
(
buf
,
false
)]
err
=
fmt
.
Errorf
(
"%v
\n
%s"
,
recovered
,
buf
)
}
}()
blocks
=
mustConvertBlocks
(
t
.
json
.
Blocks
)
return
blocks
,
nil
}
func
convertTest
(
in
*
btJSON
)
(
out
*
BlockTest
,
err
error
)
{
func
convertTest
(
in
*
btJSON
)
(
out
*
BlockTest
,
err
error
)
{
// the conversion handles errors by catching panics.
// the conversion handles errors by catching panics.
// you might consider this ugly, but the alternative (passing errors)
// you might consider this ugly, but the alternative (passing errors)
...
@@ -160,9 +185,8 @@ func convertTest(in *btJSON) (out *BlockTest, err error) {
...
@@ -160,9 +185,8 @@ func convertTest(in *btJSON) (out *BlockTest, err error) {
err
=
fmt
.
Errorf
(
"%v
\n
%s"
,
recovered
,
buf
)
err
=
fmt
.
Errorf
(
"%v
\n
%s"
,
recovered
,
buf
)
}
}
}()
}()
out
=
&
BlockTest
{
preAccounts
:
in
.
Pre
}
out
=
&
BlockTest
{
preAccounts
:
in
.
Pre
,
json
:
in
}
out
.
Genesis
=
mustConvertGenesis
(
in
.
GenesisBlockHeader
)
out
.
Genesis
=
mustConvertGenesis
(
in
.
GenesisBlockHeader
)
out
.
Blocks
=
mustConvertBlocks
(
in
.
Blocks
)
return
out
,
err
return
out
,
err
}
}
...
@@ -187,13 +211,13 @@ func mustConvertHeader(in btHeader) *types.Header {
...
@@ -187,13 +211,13 @@ func mustConvertHeader(in btHeader) *types.Header {
UncleHash
:
mustConvertHash
(
in
.
UncleHash
),
UncleHash
:
mustConvertHash
(
in
.
UncleHash
),
ParentHash
:
mustConvertHash
(
in
.
ParentHash
),
ParentHash
:
mustConvertHash
(
in
.
ParentHash
),
Extra
:
mustConvertBytes
(
in
.
ExtraData
),
Extra
:
mustConvertBytes
(
in
.
ExtraData
),
GasUsed
:
mustConvertBigInt
(
in
.
GasUsed
),
GasUsed
:
mustConvertBigInt
(
in
.
GasUsed
,
10
),
GasLimit
:
mustConvertBigInt
(
in
.
GasLimit
),
GasLimit
:
mustConvertBigInt
(
in
.
GasLimit
,
10
),
Difficulty
:
mustConvertBigInt
(
in
.
Difficulty
),
Difficulty
:
mustConvertBigInt
(
in
.
Difficulty
,
10
),
Time
:
mustConvertUint
(
in
.
Timestamp
),
Time
:
mustConvertUint
(
in
.
Timestamp
,
10
),
}
}
// XXX cheats? :-)
// XXX cheats? :-)
header
.
SetNonce
(
common
.
BytesToHash
(
mustConvertBytes
(
in
.
Nonce
))
.
Big
()
.
Uint64
(
))
header
.
SetNonce
(
mustConvertUint
(
in
.
Nonce
,
16
))
return
header
return
header
}
}
...
@@ -203,7 +227,7 @@ func mustConvertBlocks(testBlocks []btBlock) []*types.Block {
...
@@ -203,7 +227,7 @@ func mustConvertBlocks(testBlocks []btBlock) []*types.Block {
var
b
types
.
Block
var
b
types
.
Block
r
:=
bytes
.
NewReader
(
mustConvertBytes
(
inb
.
Rlp
))
r
:=
bytes
.
NewReader
(
mustConvertBytes
(
inb
.
Rlp
))
if
err
:=
rlp
.
Decode
(
r
,
&
b
);
err
!=
nil
{
if
err
:=
rlp
.
Decode
(
r
,
&
b
);
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"invalid block %d: %q
"
,
i
,
inb
.
Rlp
))
panic
(
fmt
.
Errorf
(
"invalid block %d: %q
\n
error: %v"
,
i
,
inb
.
Rlp
,
err
))
}
}
out
=
append
(
out
,
&
b
)
out
=
append
(
out
,
&
b
)
}
}
...
@@ -214,10 +238,10 @@ func mustConvertBytes(in string) []byte {
...
@@ -214,10 +238,10 @@ func mustConvertBytes(in string) []byte {
if
in
==
"0x"
{
if
in
==
"0x"
{
return
[]
byte
{}
return
[]
byte
{}
}
}
h
:=
strings
.
TrimPrefix
(
unfuckCPPHexInts
(
in
),
"0x"
)
h
:=
unfuckFuckedHex
(
strings
.
TrimPrefix
(
in
,
"0x"
)
)
out
,
err
:=
hex
.
DecodeString
(
h
)
out
,
err
:=
hex
.
DecodeString
(
h
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"invalid hex: %q
"
,
h
))
panic
(
fmt
.
Errorf
(
"invalid hex: %q
: "
,
h
,
err
))
}
}
return
out
return
out
}
}
...
@@ -246,16 +270,18 @@ func mustConvertBloom(in string) types.Bloom {
...
@@ -246,16 +270,18 @@ func mustConvertBloom(in string) types.Bloom {
return
types
.
BytesToBloom
(
out
)
return
types
.
BytesToBloom
(
out
)
}
}
func
mustConvertBigInt
(
in
string
)
*
big
.
Int
{
func
mustConvertBigInt
(
in
string
,
base
int
)
*
big
.
Int
{
out
,
ok
:=
new
(
big
.
Int
)
.
SetString
(
unfuckCPPHexInts
(
in
),
0
)
in
=
prepInt
(
base
,
in
)
out
,
ok
:=
new
(
big
.
Int
)
.
SetString
(
in
,
base
)
if
!
ok
{
if
!
ok
{
panic
(
fmt
.
Errorf
(
"invalid integer: %q"
,
in
))
panic
(
fmt
.
Errorf
(
"invalid integer: %q"
,
in
))
}
}
return
out
return
out
}
}
func
mustConvertUint
(
in
string
)
uint64
{
func
mustConvertUint
(
in
string
,
base
int
)
uint64
{
out
,
err
:=
strconv
.
ParseUint
(
unfuckCPPHexInts
(
in
),
0
,
64
)
in
=
prepInt
(
base
,
in
)
out
,
err
:=
strconv
.
ParseUint
(
in
,
base
,
64
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"invalid integer: %q"
,
in
))
panic
(
fmt
.
Errorf
(
"invalid integer: %q"
,
in
))
}
}
...
@@ -292,12 +318,28 @@ func findLine(data []byte, offset int64) (line int) {
...
@@ -292,12 +318,28 @@ func findLine(data []byte, offset int64) (line int) {
return
return
}
}
func
unfuckCPPHexInts
(
s
string
)
string
{
// Nothing to see here, please move along...
if
s
==
"0x"
{
// no respect for the empty value :(
func
prepInt
(
base
int
,
s
string
)
string
{
return
"0x00"
if
base
==
16
{
if
strings
.
HasPrefix
(
s
,
"0x"
)
{
s
=
s
[
2
:
]
}
if
len
(
s
)
==
0
{
s
=
"00"
}
s
=
nibbleFix
(
s
)
}
}
if
(
len
(
s
)
%
2
)
!=
0
{
// motherfucking nibbles
return
s
return
"0x0"
+
s
[
2
:
]
}
// don't ask
func
unfuckFuckedHex
(
almostHex
string
)
string
{
return
nibbleFix
(
strings
.
Replace
(
almostHex
,
"v"
,
""
,
-
1
))
}
func
nibbleFix
(
s
string
)
string
{
if
len
(
s
)
%
2
!=
0
{
s
=
"0"
+
s
}
}
return
s
return
s
}
}
tests/transaction_test_util.go
View file @
6c2b703c
...
@@ -2,8 +2,8 @@ package tests
...
@@ -2,8 +2,8 @@ package tests
import
(
import
(
"bytes"
"bytes"
"errors"
"fmt"
"fmt"
"math/big"
"runtime"
"runtime"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
...
@@ -49,93 +49,112 @@ func RunTransactionTests(file string, notWorking map[string]bool) error {
...
@@ -49,93 +49,112 @@ func RunTransactionTests(file string, notWorking map[string]bool) error {
}
}
func
runTest
(
txTest
TransactionTest
)
(
err
error
)
{
func
runTest
(
txTest
TransactionTest
)
(
err
error
)
{
expectedSender
,
expectedTo
,
expectedData
,
rlpBytes
,
expectedGasLimit
,
expectedGasPrice
,
expectedValue
,
expectedR
,
expectedS
,
expectedNonce
,
expectedV
,
err
:=
convertTestTypes
(
txTest
)
tx
:=
new
(
types
.
Transaction
)
err
=
rlp
.
DecodeBytes
(
mustConvertBytes
(
txTest
.
Rlp
),
tx
)
if
err
!=
nil
{
if
err
!=
nil
{
if
txTest
.
Sender
==
""
{
// tx is invalid and this is expected (test OK)
if
txTest
.
Sender
==
""
{
// RLP decoding failed and this is expected (test OK)
return
nil
return
nil
}
else
{
}
else
{
return
err
// tx is invalid and this is NOT expected (test FAIL)
// RLP decoding failed but is expected to succeed (test FAIL)
return
errors
.
New
(
"RLP decoding failed when expected to succeed"
)
}
}
}
}
tx
:=
new
(
types
.
Transaction
)
rlp
.
DecodeBytes
(
rlpBytes
,
tx
)
validationError
:=
verifyTxFields
(
txTest
,
tx
)
//fmt.Println("HURR tx: %v", tx)
if
txTest
.
Sender
==
""
{
sender
,
err
:=
tx
.
From
()
if
validationError
!=
nil
{
// RLP decoding works but validation should fail (test OK)
return
nil
}
else
{
// RLP decoding works but validation should fail (test FAIL)
// (this should not be possible but added here for completeness)
return
errors
.
New
(
"Field validations succeeded but should fail"
)
}
}
if
txTest
.
Sender
!=
""
{
if
validationError
==
nil
{
// RLP decoding works and validations pass (test OK)
return
nil
}
else
{
// RLP decoding works and validations pass (test FAIL)
return
errors
.
New
(
"Field validations failed after RLP decoding"
)
}
}
return
errors
.
New
(
"Should not happen: verify RLP decoding and field validation"
)
}
func
verifyTxFields
(
txTest
TransactionTest
,
decodedTx
*
types
.
Transaction
)
(
err
error
)
{
defer
func
()
{
if
recovered
:=
recover
();
recovered
!=
nil
{
buf
:=
make
([]
byte
,
64
<<
10
)
buf
=
buf
[
:
runtime
.
Stack
(
buf
,
false
)]
err
=
fmt
.
Errorf
(
"%v
\n
%s"
,
recovered
,
buf
)
}
}()
decodedSender
,
err
:=
decodedTx
.
From
()
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
if
expectedSender
!=
sender
{
expectedSender
:=
mustConvertAddress
(
txTest
.
Sender
)
return
fmt
.
Errorf
(
"Sender mismatch: %v %v"
,
expectedSender
,
sender
)
if
expectedSender
!=
decodedSender
{
return
fmt
.
Errorf
(
"Sender mismatch: %v %v"
,
expectedSender
,
decodedSender
)
}
}
if
!
bytes
.
Equal
(
expectedData
,
tx
.
Payload
)
{
return
fmt
.
Errorf
(
"Tx input data mismatch: %#v %#v"
,
expectedData
,
tx
.
Payload
)
expectedData
:=
mustConvertBytes
(
txTest
.
Transaction
.
Data
)
if
!
bytes
.
Equal
(
expectedData
,
decodedTx
.
Payload
)
{
return
fmt
.
Errorf
(
"Tx input data mismatch: %#v %#v"
,
expectedData
,
decodedTx
.
Payload
)
}
}
if
expectedGasLimit
.
Cmp
(
tx
.
GasLimit
)
!=
0
{
return
fmt
.
Errorf
(
"GasLimit mismatch: %v %v"
,
expectedGasLimit
,
tx
.
GasLimit
)
expectedGasLimit
:=
mustConvertBigInt
(
txTest
.
Transaction
.
GasLimit
,
16
)
if
expectedGasLimit
.
Cmp
(
decodedTx
.
GasLimit
)
!=
0
{
return
fmt
.
Errorf
(
"GasLimit mismatch: %v %v"
,
expectedGasLimit
,
decodedTx
.
GasLimit
)
}
}
if
expectedGasPrice
.
Cmp
(
tx
.
Price
)
!=
0
{
return
fmt
.
Errorf
(
"GasPrice mismatch: %v %v"
,
expectedGasPrice
,
tx
.
Price
)
expectedGasPrice
:=
mustConvertBigInt
(
txTest
.
Transaction
.
GasPrice
,
16
)
if
expectedGasPrice
.
Cmp
(
decodedTx
.
Price
)
!=
0
{
return
fmt
.
Errorf
(
"GasPrice mismatch: %v %v"
,
expectedGasPrice
,
decodedTx
.
Price
)
}
}
if
expectedNonce
!=
tx
.
AccountNonce
{
return
fmt
.
Errorf
(
"Nonce mismatch: %v %v"
,
expectedNonce
,
tx
.
AccountNonce
)
expectedNonce
:=
mustConvertUint
(
txTest
.
Transaction
.
Nonce
,
16
)
if
expectedNonce
!=
decodedTx
.
AccountNonce
{
return
fmt
.
Errorf
(
"Nonce mismatch: %v %v"
,
expectedNonce
,
decodedTx
.
AccountNonce
)
}
}
if
expectedR
.
Cmp
(
tx
.
R
)
!=
0
{
return
fmt
.
Errorf
(
"R mismatch: %v %v"
,
expectedR
,
tx
.
R
)
expectedR
:=
common
.
Bytes2Big
(
mustConvertBytes
(
txTest
.
Transaction
.
R
))
if
expectedR
.
Cmp
(
decodedTx
.
R
)
!=
0
{
return
fmt
.
Errorf
(
"R mismatch: %v %v"
,
expectedR
,
decodedTx
.
R
)
}
}
if
expectedS
.
Cmp
(
tx
.
S
)
!=
0
{
return
fmt
.
Errorf
(
"S mismatch: %v %v"
,
expectedS
,
tx
.
S
)
expectedS
:=
common
.
Bytes2Big
(
mustConvertBytes
(
txTest
.
Transaction
.
S
))
if
expectedS
.
Cmp
(
decodedTx
.
S
)
!=
0
{
return
fmt
.
Errorf
(
"S mismatch: %v %v"
,
expectedS
,
decodedTx
.
S
)
}
}
if
expectedV
!=
uint64
(
tx
.
V
)
{
return
fmt
.
Errorf
(
"V mismatch: %v %v"
,
expectedV
,
uint64
(
tx
.
V
))
expectedV
:=
mustConvertUint
(
txTest
.
Transaction
.
V
,
16
)
if
expectedV
!=
uint64
(
decodedTx
.
V
)
{
return
fmt
.
Errorf
(
"V mismatch: %v %v"
,
expectedV
,
uint64
(
decodedTx
.
V
))
}
}
if
tx
.
Recipient
==
nil
{
expectedTo
:=
mustConvertAddress
(
txTest
.
Transaction
.
To
)
if
decodedTx
.
Recipient
==
nil
{
if
expectedTo
!=
common
.
BytesToAddress
([]
byte
{})
{
// "empty" or "zero" address
if
expectedTo
!=
common
.
BytesToAddress
([]
byte
{})
{
// "empty" or "zero" address
return
fmt
.
Errorf
(
"To mismatch when recipient is nil (contract creation): %v"
,
expectedTo
)
return
fmt
.
Errorf
(
"To mismatch when recipient is nil (contract creation): %v"
,
expectedTo
)
}
}
}
else
{
}
else
{
if
expectedTo
!=
*
t
x
.
Recipient
{
if
expectedTo
!=
*
decodedT
x
.
Recipient
{
return
fmt
.
Errorf
(
"To mismatch: %v %v"
,
expectedTo
,
*
t
x
.
Recipient
)
return
fmt
.
Errorf
(
"To mismatch: %v %v"
,
expectedTo
,
*
decodedT
x
.
Recipient
)
}
}
}
}
if
expectedValue
.
Cmp
(
tx
.
Amount
)
!=
0
{
expectedValue
:=
mustConvertBigInt
(
txTest
.
Transaction
.
Value
,
16
)
return
fmt
.
Errorf
(
"Value mismatch: %v %v"
,
expectedValue
,
tx
.
Amount
)
if
expectedValue
.
Cmp
(
decodedTx
.
Amount
)
!=
0
{
return
fmt
.
Errorf
(
"Value mismatch: %v %v"
,
expectedValue
,
decodedTx
.
Amount
)
}
}
return
nil
return
nil
}
}
func
convertTestTypes
(
txTest
TransactionTest
)
(
sender
,
to
common
.
Address
,
txInputData
,
rlpBytes
[]
byte
,
gasLimit
,
gasPrice
,
value
,
r
,
s
*
big
.
Int
,
nonce
,
v
uint64
,
err
error
)
{
defer
func
()
{
if
recovered
:=
recover
();
recovered
!=
nil
{
buf
:=
make
([]
byte
,
64
<<
10
)
buf
=
buf
[
:
runtime
.
Stack
(
buf
,
false
)]
err
=
fmt
.
Errorf
(
"%v
\n
%s"
,
recovered
,
buf
)
}
}()
sender
=
mustConvertAddress
(
txTest
.
Sender
)
to
=
mustConvertAddress
(
txTest
.
Transaction
.
To
)
txInputData
=
mustConvertBytes
(
txTest
.
Transaction
.
Data
)
rlpBytes
=
mustConvertBytes
(
txTest
.
Rlp
)
gasLimit
=
mustConvertBigInt
(
txTest
.
Transaction
.
GasLimit
)
gasPrice
=
mustConvertBigInt
(
txTest
.
Transaction
.
GasPrice
)
value
=
mustConvertBigInt
(
txTest
.
Transaction
.
Value
)
r
=
common
.
Bytes2Big
(
mustConvertBytes
(
txTest
.
Transaction
.
R
))
s
=
common
.
Bytes2Big
(
mustConvertBytes
(
txTest
.
Transaction
.
S
))
nonce
=
mustConvertUint
(
txTest
.
Transaction
.
Nonce
)
v
=
mustConvertUint
(
txTest
.
Transaction
.
V
)
return
sender
,
to
,
txInputData
,
rlpBytes
,
gasLimit
,
gasPrice
,
value
,
r
,
s
,
nonce
,
v
,
nil
}
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