transaction_test_util.go 4.52 KB
Newer Older
1 2 3 4
package tests

import (
	"bytes"
5
	"errors"
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	"fmt"
	"runtime"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/rlp"
)

// Transaction Test JSON Format
type TtTransaction struct {
	Data     string
	GasLimit string
	GasPrice string
	Nonce    string
	R        string
	S        string
	To       string
	V        string
	Value    string
}

type TransactionTest struct {
	Rlp         string
	Sender      string
	Transaction TtTransaction
}

33
func RunTransactionTests(file string, notWorking map[string]bool) error {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	bt := make(map[string]TransactionTest)
	if err := LoadJSON(file, &bt); err != nil {
		return err
	}
	for name, in := range bt {
		var err error
		// TODO: remove this, we currently ignore some tests which are broken
		if !notWorking[name] {
			if err = runTest(in); err != nil {
				return fmt.Errorf("bad test %s: %v", name, err)
			}
			fmt.Println("Test passed:", name)
		}
	}
	return nil
}

func runTest(txTest TransactionTest) (err error) {
52 53
	tx := new(types.Transaction)
	err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
54 55

	if err != nil {
56 57
		if txTest.Sender == "" {
			// RLP decoding failed and this is expected (test OK)
58 59
			return nil
		} else {
60 61
			// RLP decoding failed but is expected to succeed (test FAIL)
			return errors.New("RLP decoding failed when expected to succeed")
62 63
		}
	}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	validationError := verifyTxFields(txTest, tx)
	if txTest.Sender == "" {
		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()
99 100 101 102
	if err != nil {
		return err
	}

103 104 105
	expectedSender := mustConvertAddress(txTest.Sender)
	if expectedSender != decodedSender {
		return fmt.Errorf("Sender mismatch: %v %v", expectedSender, decodedSender)
106
	}
107 108 109 110

	expectedData := mustConvertBytes(txTest.Transaction.Data)
	if !bytes.Equal(expectedData, decodedTx.Payload) {
		return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Payload)
111
	}
112 113 114 115

	expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
	if expectedGasLimit.Cmp(decodedTx.GasLimit) != 0 {
		return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.GasLimit)
116
	}
117 118 119 120

	expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
	if expectedGasPrice.Cmp(decodedTx.Price) != 0 {
		return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.Price)
121
	}
122 123 124 125

	expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
	if expectedNonce != decodedTx.AccountNonce {
		return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.AccountNonce)
126
	}
127 128 129 130

	expectedR := common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
	if expectedR.Cmp(decodedTx.R) != 0 {
		return fmt.Errorf("R mismatch: %v %v", expectedR, decodedTx.R)
131
	}
132 133 134 135

	expectedS := common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
	if expectedS.Cmp(decodedTx.S) != 0 {
		return fmt.Errorf("S mismatch: %v %v", expectedS, decodedTx.S)
136
	}
137 138 139 140

	expectedV := mustConvertUint(txTest.Transaction.V, 16)
	if expectedV != uint64(decodedTx.V) {
		return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(decodedTx.V))
141
	}
142 143 144

	expectedTo := mustConvertAddress(txTest.Transaction.To)
	if decodedTx.Recipient == nil {
145 146 147 148
		if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
			return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
		}
	} else {
149 150
		if expectedTo != *decodedTx.Recipient {
			return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.Recipient)
151
		}
152
	}
153

154 155 156
	expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
	if expectedValue.Cmp(decodedTx.Amount) != 0 {
		return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Amount)
157 158 159 160
	}

	return nil
}