Skip to content

Commit 805345d

Browse files
author
Gustav Simonsson
committed
Add block tests wrapper and fixes for tx tests
* Add fixes to parsing and converting of fields in tx tests * Correct logic in tx tests; validation of fields and correct logic for when RLP decoding works/fails and when this is expected or not * Rename files for consistency * Add block tests wrapper to run block tests with go test
1 parent c453f1f commit 805345d

3 files changed

Lines changed: 170 additions & 65 deletions

File tree

‎cmd/geth/block_go_test.go‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
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+
"github.com/ethereum/go-ethereum/tests"
13+
)
14+
15+
// TODO: refactor test setup & execution to better align with vm and tx tests
16+
// TODO: refactor to avoid duplication with cmd/geth/blocktest.go
17+
func TestBcValidBlockTests(t *testing.T) {
18+
runBlockTestsInFile("../../tests/files/BlockTests/bcValidBlockTest.json", t)
19+
}
20+
21+
/*
22+
func TestBcUncleTests(t *testing.T) {
23+
runBlockTestsInFile("../../tests/files/BlockTests/bcUncleTest.json", t)
24+
}
25+
*/
26+
27+
func runBlockTestsInFile(filepath string, t *testing.T) {
28+
bt, err := tests.LoadBlockTests(filepath)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
for name, test := range bt {
33+
runTest(name, test, t)
34+
}
35+
}
36+
37+
func runTest(name string, test *tests.BlockTest, t *testing.T) {
38+
t.Log("Running test: ", name)
39+
cfg := testEthConfig()
40+
ethereum, err := eth.New(cfg)
41+
if err != nil {
42+
t.Fatalf("%v", err)
43+
}
44+
45+
err = ethereum.Start()
46+
if err != nil {
47+
t.Fatalf("%v", err)
48+
}
49+
50+
// import the genesis block
51+
ethereum.ResetWithGenesisBlock(test.Genesis)
52+
53+
// import pre accounts
54+
statedb, err := test.InsertPreState(ethereum.StateDb())
55+
if err != nil {
56+
t.Fatalf("InsertPreState: %v", err)
57+
}
58+
59+
// insert the test blocks, which will execute all transactions
60+
if err := test.InsertBlocks(ethereum.ChainManager()); err != nil {
61+
t.Fatalf("Block Test load error: %v %T", err, err)
62+
}
63+
64+
if err := test.ValidatePostState(statedb); err != nil {
65+
t.Fatal("post state validation failed: %v", err)
66+
}
67+
t.Log("Test passed: ", name)
68+
}
69+
70+
func testEthConfig() *eth.Config {
71+
ks := crypto.NewKeyStorePassphrase(path.Join(common.DefaultDataDir(), "keys"))
72+
73+
return &eth.Config{
74+
DataDir: common.DefaultDataDir(),
75+
LogLevel: 5,
76+
Etherbase: "primary",
77+
AccountManager: accounts.NewManager(ks),
78+
NewDB: func(path string) (common.Database, error) { return ethdb.NewMemDatabase() },
79+
}
80+
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ func mustConvertBytes(in string) []byte {
238238
if in == "0x" {
239239
return []byte{}
240240
}
241-
h := nibbleFix(strings.TrimPrefix(in, "0x"))
241+
h := unfuckFuckedHex(strings.TrimPrefix(in, "0x"))
242242
out, err := hex.DecodeString(h)
243243
if err != nil {
244-
panic(fmt.Errorf("invalid hex: %q", h))
244+
panic(fmt.Errorf("invalid hex: %q: ", h, err))
245245
}
246246
return out
247247
}
@@ -255,7 +255,7 @@ func mustConvertHash(in string) common.Hash {
255255
}
256256

257257
func mustConvertAddress(in string) common.Address {
258-
out, err := hex.DecodeString(nibbleFix(strings.TrimPrefix(in, "0x")))
258+
out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
259259
if err != nil {
260260
panic(fmt.Errorf("invalid hex: %q", in))
261261
}
@@ -318,6 +318,7 @@ func findLine(data []byte, offset int64) (line int) {
318318
return
319319
}
320320

321+
// Nothing to see here, please move along...
321322
func prepInt(base int, s string) string {
322323
if base == 16 {
323324
if strings.HasPrefix(s, "0x") {
@@ -331,6 +332,11 @@ func prepInt(base int, s string) string {
331332
return s
332333
}
333334

335+
// don't ask
336+
func unfuckFuckedHex(almostHex string) string {
337+
return nibbleFix(strings.Replace(almostHex, "v", "", -1))
338+
}
339+
334340
func nibbleFix(s string) string {
335341
if len(s)%2 != 0 {
336342
s = "0" + s

‎tests/transaction_test_util.go‎

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package tests
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
6-
"math/big"
77
"runtime"
88

99
"github.com/ethereum/go-ethereum/common"
@@ -49,93 +49,112 @@ func RunTransactionTests(file string, notWorking map[string]bool) error {
4949
}
5050

5151
func runTest(txTest TransactionTest) (err error) {
52-
expectedSender, expectedTo, expectedData, rlpBytes, expectedGasLimit, expectedGasPrice, expectedValue, expectedR, expectedS, expectedNonce, expectedV, err := convertTestTypes(txTest)
52+
tx := new(types.Transaction)
53+
err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
5354

5455
if err != nil {
55-
if txTest.Sender == "" { // tx is invalid and this is expected (test OK)
56+
if txTest.Sender == "" {
57+
// RLP decoding failed and this is expected (test OK)
5658
return nil
5759
} else {
58-
return err // tx is invalid and this is NOT expected (test FAIL)
60+
// RLP decoding failed but is expected to succeed (test FAIL)
61+
return errors.New("RLP decoding failed when expected to succeed")
5962
}
6063
}
61-
tx := new(types.Transaction)
62-
rlp.DecodeBytes(rlpBytes, tx)
63-
//fmt.Println("HURR tx: %v", tx)
64-
sender, err := tx.From()
64+
65+
validationError := verifyTxFields(txTest, tx)
66+
if txTest.Sender == "" {
67+
if validationError != nil {
68+
// RLP decoding works but validation should fail (test OK)
69+
return nil
70+
} else {
71+
// RLP decoding works but validation should fail (test FAIL)
72+
// (this should not be possible but added here for completeness)
73+
return errors.New("Field validations succeeded but should fail")
74+
}
75+
}
76+
77+
if txTest.Sender != "" {
78+
if validationError == nil {
79+
// RLP decoding works and validations pass (test OK)
80+
return nil
81+
} else {
82+
// RLP decoding works and validations pass (test FAIL)
83+
return errors.New("Field validations failed after RLP decoding")
84+
}
85+
}
86+
return errors.New("Should not happen: verify RLP decoding and field validation")
87+
}
88+
89+
func verifyTxFields(txTest TransactionTest, decodedTx *types.Transaction) (err error) {
90+
defer func() {
91+
if recovered := recover(); recovered != nil {
92+
buf := make([]byte, 64<<10)
93+
buf = buf[:runtime.Stack(buf, false)]
94+
err = fmt.Errorf("%v\n%s", recovered, buf)
95+
}
96+
}()
97+
98+
decodedSender, err := decodedTx.From()
6599
if err != nil {
66100
return err
67101
}
68102

69-
if expectedSender != sender {
70-
return fmt.Errorf("Sender mismatch: %v %v", expectedSender, sender)
103+
expectedSender := mustConvertAddress(txTest.Sender)
104+
if expectedSender != decodedSender {
105+
return fmt.Errorf("Sender mismatch: %v %v", expectedSender, decodedSender)
71106
}
72-
if !bytes.Equal(expectedData, tx.Payload) {
73-
return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, tx.Payload)
107+
108+
expectedData := mustConvertBytes(txTest.Transaction.Data)
109+
if !bytes.Equal(expectedData, decodedTx.Payload) {
110+
return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Payload)
74111
}
75-
if expectedGasLimit.Cmp(tx.GasLimit) != 0 {
76-
return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, tx.GasLimit)
112+
113+
expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
114+
if expectedGasLimit.Cmp(decodedTx.GasLimit) != 0 {
115+
return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.GasLimit)
77116
}
78-
if expectedGasPrice.Cmp(tx.Price) != 0 {
79-
return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, tx.Price)
117+
118+
expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
119+
if expectedGasPrice.Cmp(decodedTx.Price) != 0 {
120+
return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.Price)
80121
}
81-
if expectedNonce != tx.AccountNonce {
82-
return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, tx.AccountNonce)
122+
123+
expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
124+
if expectedNonce != decodedTx.AccountNonce {
125+
return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.AccountNonce)
83126
}
84-
if expectedR.Cmp(tx.R) != 0 {
85-
return fmt.Errorf("R mismatch: %v %v", expectedR, tx.R)
127+
128+
expectedR := common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
129+
if expectedR.Cmp(decodedTx.R) != 0 {
130+
return fmt.Errorf("R mismatch: %v %v", expectedR, decodedTx.R)
86131
}
87-
if expectedS.Cmp(tx.S) != 0 {
88-
return fmt.Errorf("S mismatch: %v %v", expectedS, tx.S)
132+
133+
expectedS := common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
134+
if expectedS.Cmp(decodedTx.S) != 0 {
135+
return fmt.Errorf("S mismatch: %v %v", expectedS, decodedTx.S)
89136
}
90-
if expectedV != uint64(tx.V) {
91-
return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(tx.V))
137+
138+
expectedV := mustConvertUint(txTest.Transaction.V, 16)
139+
if expectedV != uint64(decodedTx.V) {
140+
return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(decodedTx.V))
92141
}
93-
if tx.Recipient == nil {
142+
143+
expectedTo := mustConvertAddress(txTest.Transaction.To)
144+
if decodedTx.Recipient == nil {
94145
if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
95146
return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
96147
}
97148
} else {
98-
if expectedTo != *tx.Recipient {
99-
return fmt.Errorf("To mismatch: %v %v", expectedTo, *tx.Recipient)
149+
if expectedTo != *decodedTx.Recipient {
150+
return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.Recipient)
100151
}
101152
}
102153

103-
if expectedValue.Cmp(tx.Amount) != 0 {
104-
return fmt.Errorf("Value mismatch: %v %v", expectedValue, tx.Amount)
154+
expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
155+
if expectedValue.Cmp(decodedTx.Amount) != 0 {
156+
return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Amount)
105157
}
106158

107159
return nil
108160
}
109-
110-
func convertTestTypes(txTest TransactionTest) (sender, to common.Address,
111-
txInputData, rlpBytes []byte,
112-
gasLimit, gasPrice, value, r, s *big.Int,
113-
nonce, v uint64,
114-
err error) {
115-
116-
defer func() {
117-
if recovered := recover(); recovered != nil {
118-
buf := make([]byte, 64<<10)
119-
buf = buf[:runtime.Stack(buf, false)]
120-
err = fmt.Errorf("%v\n%s", recovered, buf)
121-
}
122-
}()
123-
124-
sender = mustConvertAddress(txTest.Sender)
125-
to = mustConvertAddress(txTest.Transaction.To)
126-
127-
txInputData = mustConvertBytes(txTest.Transaction.Data)
128-
rlpBytes = mustConvertBytes(txTest.Rlp)
129-
130-
gasLimit = mustConvertBigInt(txTest.Transaction.GasLimit, 16)
131-
gasPrice = mustConvertBigInt(txTest.Transaction.GasPrice, 16)
132-
value = mustConvertBigInt(txTest.Transaction.Value, 16)
133-
134-
r = common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
135-
s = common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
136-
137-
nonce = mustConvertUint(txTest.Transaction.Nonce, 16)
138-
v = mustConvertUint(txTest.Transaction.V, 16)
139-
140-
return sender, to, txInputData, rlpBytes, gasLimit, gasPrice, value, r, s, nonce, v, nil
141-
}

0 commit comments

Comments
 (0)