tests: make transaction tests run again, fix #19033 (#19529)

* tests: make transaction tests run again, fix #19033

* tests: refactor transaction tests
This commit is contained in:
Martin Holst Swende
2019-05-21 13:58:06 +02:00
committed by Péter Szilágyi
parent a54142987c
commit 2cd6059e51
3 changed files with 86 additions and 205 deletions

View File

@ -17,14 +17,11 @@
package tests
import (
"bytes"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
@ -32,101 +29,80 @@ import (
// TransactionTest checks RLP decoding and sender derivation of transactions.
type TransactionTest struct {
json ttJSON
RLP hexutil.Bytes `json:"rlp"`
Byzantium ttFork
Constantinople ttFork
EIP150 ttFork
EIP158 ttFork
Frontier ttFork
Homestead ttFork
}
type ttJSON struct {
BlockNumber math.HexOrDecimal64 `json:"blockNumber"`
RLP hexutil.Bytes `json:"rlp"`
Sender hexutil.Bytes `json:"sender"`
Transaction *ttTransaction `json:"transaction"`
}
//go:generate gencodec -type ttTransaction -field-override ttTransactionMarshaling -out gen_tttransaction.go
type ttTransaction struct {
Data []byte `gencodec:"required"`
GasLimit uint64 `gencodec:"required"`
GasPrice *big.Int `gencodec:"required"`
Nonce uint64 `gencodec:"required"`
Value *big.Int `gencodec:"required"`
R *big.Int `gencodec:"required"`
S *big.Int `gencodec:"required"`
V *big.Int `gencodec:"required"`
To common.Address `gencodec:"required"`
}
type ttTransactionMarshaling struct {
Data hexutil.Bytes
GasLimit math.HexOrDecimal64
GasPrice *math.HexOrDecimal256
Nonce math.HexOrDecimal64
Value *math.HexOrDecimal256
R *math.HexOrDecimal256
S *math.HexOrDecimal256
V *math.HexOrDecimal256
type ttFork struct {
Sender common.UnprefixedAddress `json:"sender"`
Hash common.UnprefixedHash `json:"hash"`
}
func (tt *TransactionTest) Run(config *params.ChainConfig) error {
tx := new(types.Transaction)
if err := rlp.DecodeBytes(tt.json.RLP, tx); err != nil {
if tt.json.Transaction == nil {
return nil
}
return fmt.Errorf("RLP decoding failed: %v", err)
}
// Check sender derivation.
signer := types.MakeSigner(config, new(big.Int).SetUint64(uint64(tt.json.BlockNumber)))
sender, err := types.Sender(signer, tx)
if err != nil {
return err
}
if sender != common.BytesToAddress(tt.json.Sender) {
return fmt.Errorf("Sender mismatch: got %x, want %x", sender, tt.json.Sender)
}
// Check decoded fields.
err = tt.json.Transaction.verify(signer, tx)
if tt.json.Sender == nil && err == nil {
return errors.New("field validations succeeded but should fail")
}
if tt.json.Sender != nil && err != nil {
return fmt.Errorf("field validations failed after RLP decoding: %s", err)
}
return nil
}
func (tt *ttTransaction) verify(signer types.Signer, tx *types.Transaction) error {
if !bytes.Equal(tx.Data(), tt.Data) {
return fmt.Errorf("Tx input data mismatch: got %x want %x", tx.Data(), tt.Data)
}
if tx.Gas() != tt.GasLimit {
return fmt.Errorf("GasLimit mismatch: got %d, want %d", tx.Gas(), tt.GasLimit)
}
if tx.GasPrice().Cmp(tt.GasPrice) != 0 {
return fmt.Errorf("GasPrice mismatch: got %v, want %v", tx.GasPrice(), tt.GasPrice)
}
if tx.Nonce() != tt.Nonce {
return fmt.Errorf("Nonce mismatch: got %v, want %v", tx.Nonce(), tt.Nonce)
}
v, r, s := tx.RawSignatureValues()
if r.Cmp(tt.R) != 0 {
return fmt.Errorf("R mismatch: got %v, want %v", r, tt.R)
}
if s.Cmp(tt.S) != 0 {
return fmt.Errorf("S mismatch: got %v, want %v", s, tt.S)
}
if v.Cmp(tt.V) != 0 {
return fmt.Errorf("V mismatch: got %v, want %v", v, tt.V)
}
if tx.To() == nil {
if tt.To != (common.Address{}) {
return fmt.Errorf("To mismatch when recipient is nil (contract creation): %x", tt.To)
validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool) (*common.Address, *common.Hash, error) {
tx := new(types.Transaction)
if err := rlp.DecodeBytes(rlpData, tx); err != nil {
return nil, nil, err
}
} else if *tx.To() != tt.To {
return fmt.Errorf("To mismatch: got %x, want %x", *tx.To(), tt.To)
sender, err := types.Sender(signer, tx)
if err != nil {
return nil, nil, err
}
// Intrinsic gas
requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, isHomestead)
if err != nil {
return nil, nil, err
}
if requiredGas > tx.Gas() {
return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
}
h := tx.Hash()
return &sender, &h, nil
}
if tx.Value().Cmp(tt.Value) != 0 {
return fmt.Errorf("Value mismatch: got %x, want %x", tx.Value(), tt.Value)
for _, testcase := range []struct {
name string
signer types.Signer
fork ttFork
isHomestead bool
}{
{"Frontier", types.FrontierSigner{}, tt.Frontier, false},
{"Homestead", types.HomesteadSigner{}, tt.Homestead, true},
{"EIP150", types.HomesteadSigner{}, tt.EIP150, true},
{"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true},
{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true},
{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true},
} {
sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead)
if testcase.fork.Sender == (common.UnprefixedAddress{}) {
if err == nil {
return fmt.Errorf("Expected error, got none (address %v)", sender.String())
}
continue
}
// Should resolve the right address
if err != nil {
return fmt.Errorf("Got error, expected none: %v", err)
}
if sender == nil {
return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
}
if *sender != common.Address(testcase.fork.Sender) {
return fmt.Errorf("Sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
}
if txhash == nil {
return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
}
if *txhash != common.Hash(testcase.fork.Hash) {
return fmt.Errorf("Hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
}
}
return nil
}