Skip to content

Commit 2f4cc72

Browse files
committed
Merge pull request bnb-chain#765 from Gustav-Simonsson/more_block_test_improvements
Further fixes to block test wrapper
2 parents 15550dc + b448390 commit 2f4cc72

4 files changed

Lines changed: 185 additions & 130 deletions

File tree

‎cmd/geth/block_go_test.go‎

Lines changed: 0 additions & 80 deletions
This file was deleted.

‎cmd/geth/blocktest.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, er
109109
return ethereum, fmt.Errorf("InsertPreState: %v", err)
110110
}
111111

112-
// insert the test blocks, which will execute all transactions
113-
if err := test.InsertBlocks(ethereum.ChainManager()); err != nil {
114-
return ethereum, fmt.Errorf("Block Test load error: %v %T", err, err)
112+
if err := test.TryBlocksInsert(ethereum.ChainManager()); err != nil {
113+
return ethereum, fmt.Errorf("Block Test load error: %v", err)
115114
}
115+
116116
fmt.Println("chain loaded")
117117
if err := test.ValidatePostState(statedb); err != nil {
118118
return ethereum, fmt.Errorf("post state validation failed: %v", err)

‎tests/block_test.go‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package tests
2+
3+
import (
4+
"path"
5+
"testing"
6+
7+
"github.com/ethereum/go-ethereum/accounts"
8+
"github.com/ethereum/go-ethereum/common"
9+
"github.com/ethereum/go-ethereum/crypto"
10+
"github.com/ethereum/go-ethereum/eth"
11+
"github.com/ethereum/go-ethereum/ethdb"
12+
)
13+
14+
// TODO: refactor test setup & execution to better align with vm and tx tests
15+
func TestBcValidBlockTests(t *testing.T) {
16+
t.Skip("Skipped in lieu of performance fixes.")
17+
runBlockTestsInFile("files/BlockTests/bcValidBlockTest.json", []string{}, t)
18+
}
19+
20+
func TestBcUncleTests(t *testing.T) {
21+
t.Skip("Skipped in lieu of performance fixes.")
22+
runBlockTestsInFile("files/BlockTests/bcUncleTest.json", []string{}, t)
23+
}
24+
25+
func TestBcUncleHeaderValidityTests(t *testing.T) {
26+
t.Skip("Skipped in lieu of performance fixes.")
27+
runBlockTestsInFile("files/BlockTests/bcUncleHeaderValiditiy.json", []string{}, t)
28+
}
29+
30+
func TestBcInvalidHeaderTests(t *testing.T) {
31+
t.Skip("Skipped in lieu of performance fixes.")
32+
snafus := []string{
33+
"wrongUncleHash", // TODO: why does this fail?
34+
}
35+
runBlockTestsInFile("files/BlockTests/bcInvalidHeaderTest.json", snafus, t)
36+
}
37+
38+
func TestBcInvalidRLPTests(t *testing.T) {
39+
t.Skip("Skipped in lieu of performance fixes.")
40+
snafus := []string{
41+
// TODO: why does these fail?
42+
"TRANSCT__ZeroByteAtTheEnd",
43+
"TRANSCT__RandomByteAtTheEnd",
44+
"BLOCK__ZeroByteAtTheEnd",
45+
"BLOCK__RandomByteAtTheEnd",
46+
}
47+
runBlockTestsInFile("files/BlockTests/bcInvalidRLPTest.json", snafus, t)
48+
}
49+
50+
func TestBcJSAPITests(t *testing.T) {
51+
t.Skip("Skipped in lieu of performance fixes.")
52+
runBlockTestsInFile("files/BlockTests/bcJS_API_Test.json", []string{}, t)
53+
}
54+
55+
func TestBcRPCAPITests(t *testing.T) {
56+
t.Skip("Skipped in lieu of performance fixes.")
57+
runBlockTestsInFile("files/BlockTests/bcRPC_API_Test.json", []string{}, t)
58+
}
59+
60+
func TestBcForkBlockTests(t *testing.T) {
61+
t.Skip("Skipped in lieu of performance fixes.")
62+
runBlockTestsInFile("files/BlockTests/bcForkBlockTest.json", []string{}, t)
63+
}
64+
65+
func runBlockTestsInFile(filepath string, snafus []string, t *testing.T) {
66+
bt, err := LoadBlockTests(filepath)
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
71+
notWorking := make(map[string]bool, 100)
72+
for _, name := range snafus {
73+
notWorking[name] = true
74+
}
75+
76+
for name, test := range bt {
77+
if !notWorking[name] {
78+
runBlockTest(name, test, t)
79+
}
80+
}
81+
}
82+
83+
func runBlockTest(name string, test *BlockTest, t *testing.T) {
84+
t.Log("Running test: ", name)
85+
cfg := testEthConfig()
86+
ethereum, err := eth.New(cfg)
87+
if err != nil {
88+
t.Fatalf("%v", err)
89+
}
90+
91+
err = ethereum.Start()
92+
if err != nil {
93+
t.Fatalf("%v", err)
94+
}
95+
96+
// import the genesis block
97+
ethereum.ResetWithGenesisBlock(test.Genesis)
98+
99+
// import pre accounts
100+
statedb, err := test.InsertPreState(ethereum.StateDb())
101+
if err != nil {
102+
t.Fatalf("InsertPreState: %v", err)
103+
}
104+
105+
err = test.TryBlocksInsert(ethereum.ChainManager())
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
if err = test.ValidatePostState(statedb); err != nil {
111+
t.Fatal("post state validation failed: %v", err)
112+
}
113+
t.Log("Test passed: ", name)
114+
}
115+
116+
func testEthConfig() *eth.Config {
117+
ks := crypto.NewKeyStorePassphrase(path.Join(common.DefaultDataDir(), "keys"))
118+
119+
return &eth.Config{
120+
DataDir: common.DefaultDataDir(),
121+
LogLevel: 5,
122+
Etherbase: "primary",
123+
AccountManager: accounts.NewManager(ks),
124+
NewDB: func(path string) (common.Database, error) { return ethdb.NewMemDatabase() },
125+
}
126+
}

‎tests/block_test_util.go‎

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@ import (
1919
)
2020

2121
// Block Test JSON Format
22+
type BlockTest struct {
23+
Genesis *types.Block
24+
25+
Json *btJSON
26+
preAccounts map[string]btAccount
27+
}
28+
2229
type btJSON struct {
2330
Blocks []btBlock
2431
GenesisBlockHeader btHeader
2532
Pre map[string]btAccount
2633
PostState map[string]btAccount
2734
}
2835

36+
type btBlock struct {
37+
BlockHeader *btHeader
38+
Rlp string
39+
Transactions []btTransaction
40+
UncleHeaders []*btHeader
41+
}
42+
2943
type btAccount struct {
3044
Balance string
3145
Code string
@@ -65,20 +79,6 @@ type btTransaction struct {
6579
Value string
6680
}
6781

68-
type btBlock struct {
69-
BlockHeader *btHeader
70-
Rlp string
71-
Transactions []btTransaction
72-
UncleHeaders []*btHeader
73-
}
74-
75-
type BlockTest struct {
76-
Genesis *types.Block
77-
78-
json *btJSON
79-
preAccounts map[string]btAccount
80-
}
81-
8282
// LoadBlockTests loads a block test JSON file.
8383
func LoadBlockTests(file string) (map[string]*BlockTest, error) {
8484
bt := make(map[string]*btJSON)
@@ -125,13 +125,43 @@ func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) {
125125
return statedb, nil
126126
}
127127

128-
// InsertBlocks loads the test's blocks into the given chain.
129-
func (t *BlockTest) InsertBlocks(chain *core.ChainManager) error {
130-
blocks, err := t.convertBlocks()
131-
if err != nil {
132-
return err
128+
/* See https://github.com/ethereum/tests/wiki/Blockchain-Tests-II
129+
130+
Whether a block is valid or not is a bit subtle, it's defined by presence of
131+
blockHeader, transactions and uncleHeaders fields. If they are missing, the block is
132+
invalid and we must verify that we do not accept it.
133+
134+
Since some tests mix valid and invalid blocks we need to check this for every block.
135+
136+
If a block is invalid it does not necessarily fail the test, if it's invalidness is
137+
expected we are expected to ignore it and continue processing and then validate the
138+
post state.
139+
*/
140+
func (t *BlockTest) TryBlocksInsert(chainManager *core.ChainManager) error {
141+
// insert the test blocks, which will execute all transactions
142+
for _, b := range t.Json.Blocks {
143+
cb, err := mustConvertBlock(b)
144+
if err != nil {
145+
if b.BlockHeader == nil {
146+
continue // OK - block is supposed to be invalid, continue with next block
147+
} else {
148+
return fmt.Errorf("Block RLP decoding failed when expected to succeed: ", err)
149+
}
150+
}
151+
// RLP decoding worked, try to insert into chain:
152+
err = chainManager.InsertChain(types.Blocks{cb})
153+
if err != nil {
154+
if b.BlockHeader == nil {
155+
continue // OK - block is supposed to be invalid, continue with next block
156+
} else {
157+
return fmt.Errorf("Block insertion into chain failed: ", err)
158+
}
159+
}
160+
if b.BlockHeader == nil {
161+
return fmt.Errorf("Block insertion should have failed")
162+
}
133163
}
134-
return chain.InsertChain(blocks)
164+
return nil
135165
}
136166

137167
func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
@@ -159,21 +189,6 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
159189
return nil
160190
}
161191

162-
func (t *BlockTest) convertBlocks() (blocks []*types.Block, err error) {
163-
// the conversion handles errors by catching panics.
164-
// you might consider this ugly, but the alternative (passing errors)
165-
// would be much harder to read.
166-
defer func() {
167-
if recovered := recover(); recovered != nil {
168-
buf := make([]byte, 64<<10)
169-
buf = buf[:runtime.Stack(buf, false)]
170-
err = fmt.Errorf("%v\n%s", recovered, buf)
171-
}
172-
}()
173-
blocks = mustConvertBlocks(t.json.Blocks)
174-
return blocks, nil
175-
}
176-
177192
func convertTest(in *btJSON) (out *BlockTest, err error) {
178193
// the conversion handles errors by catching panics.
179194
// you might consider this ugly, but the alternative (passing errors)
@@ -185,7 +200,7 @@ func convertTest(in *btJSON) (out *BlockTest, err error) {
185200
err = fmt.Errorf("%v\n%s", recovered, buf)
186201
}
187202
}()
188-
out = &BlockTest{preAccounts: in.Pre, json: in}
203+
out = &BlockTest{preAccounts: in.Pre, Json: in}
189204
out.Genesis = mustConvertGenesis(in.GenesisBlockHeader)
190205
return out, err
191206
}
@@ -221,17 +236,11 @@ func mustConvertHeader(in btHeader) *types.Header {
221236
return header
222237
}
223238

224-
func mustConvertBlocks(testBlocks []btBlock) []*types.Block {
225-
var out []*types.Block
226-
for i, inb := range testBlocks {
227-
var b types.Block
228-
r := bytes.NewReader(mustConvertBytes(inb.Rlp))
229-
if err := rlp.Decode(r, &b); err != nil {
230-
panic(fmt.Errorf("invalid block %d: %q\nerror: %v", i, inb.Rlp, err))
231-
}
232-
out = append(out, &b)
233-
}
234-
return out
239+
func mustConvertBlock(testBlock btBlock) (*types.Block, error) {
240+
var b types.Block
241+
r := bytes.NewReader(mustConvertBytes(testBlock.Rlp))
242+
err := rlp.Decode(r, &b)
243+
return &b, err
235244
}
236245

237246
func mustConvertBytes(in string) []byte {

0 commit comments

Comments
 (0)