tests: make transaction tests run again, fix #19033

This commit is contained in:
Martin Holst Swende 2019-05-06 00:21:38 +02:00
parent abeba0a1de
commit e98dc75d6c
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 89 additions and 200 deletions

View file

@ -1,95 +0,0 @@
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package tests
import (
"encoding/json"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
)
var _ = (*ttTransactionMarshaling)(nil)
func (t ttTransaction) MarshalJSON() ([]byte, error) {
type ttTransaction struct {
Data hexutil.Bytes `gencodec:"required"`
GasLimit math.HexOrDecimal64 `gencodec:"required"`
GasPrice *math.HexOrDecimal256 `gencodec:"required"`
Nonce math.HexOrDecimal64 `gencodec:"required"`
Value *math.HexOrDecimal256 `gencodec:"required"`
R *math.HexOrDecimal256 `gencodec:"required"`
S *math.HexOrDecimal256 `gencodec:"required"`
V *math.HexOrDecimal256 `gencodec:"required"`
To common.Address `gencodec:"required"`
}
var enc ttTransaction
enc.Data = t.Data
enc.GasLimit = math.HexOrDecimal64(t.GasLimit)
enc.GasPrice = (*math.HexOrDecimal256)(t.GasPrice)
enc.Nonce = math.HexOrDecimal64(t.Nonce)
enc.Value = (*math.HexOrDecimal256)(t.Value)
enc.R = (*math.HexOrDecimal256)(t.R)
enc.S = (*math.HexOrDecimal256)(t.S)
enc.V = (*math.HexOrDecimal256)(t.V)
enc.To = t.To
return json.Marshal(&enc)
}
func (t *ttTransaction) UnmarshalJSON(input []byte) error {
type ttTransaction struct {
Data *hexutil.Bytes `gencodec:"required"`
GasLimit *math.HexOrDecimal64 `gencodec:"required"`
GasPrice *math.HexOrDecimal256 `gencodec:"required"`
Nonce *math.HexOrDecimal64 `gencodec:"required"`
Value *math.HexOrDecimal256 `gencodec:"required"`
R *math.HexOrDecimal256 `gencodec:"required"`
S *math.HexOrDecimal256 `gencodec:"required"`
V *math.HexOrDecimal256 `gencodec:"required"`
To *common.Address `gencodec:"required"`
}
var dec ttTransaction
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.Data == nil {
return errors.New("missing required field 'data' for ttTransaction")
}
t.Data = *dec.Data
if dec.GasLimit == nil {
return errors.New("missing required field 'gasLimit' for ttTransaction")
}
t.GasLimit = uint64(*dec.GasLimit)
if dec.GasPrice == nil {
return errors.New("missing required field 'gasPrice' for ttTransaction")
}
t.GasPrice = (*big.Int)(dec.GasPrice)
if dec.Nonce == nil {
return errors.New("missing required field 'nonce' for ttTransaction")
}
t.Nonce = uint64(*dec.Nonce)
if dec.Value == nil {
return errors.New("missing required field 'value' for ttTransaction")
}
t.Value = (*big.Int)(dec.Value)
if dec.R == nil {
return errors.New("missing required field 'r' for ttTransaction")
}
t.R = (*big.Int)(dec.R)
if dec.S == nil {
return errors.New("missing required field 's' for ttTransaction")
}
t.S = (*big.Int)(dec.S)
if dec.V == nil {
return errors.New("missing required field 'v' for ttTransaction")
}
t.V = (*big.Int)(dec.V)
if dec.To == nil {
return errors.New("missing required field 'to' for ttTransaction")
}
t.To = *dec.To
return nil
}

View file

@ -17,7 +17,6 @@
package tests package tests
import ( import (
"math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
@ -27,26 +26,27 @@ func TestTransaction(t *testing.T) {
t.Parallel() t.Parallel()
txt := new(testMatcher) txt := new(testMatcher)
txt.config(`^Homestead/`, params.ChainConfig{ // These can't be parsed, invalid hex in RLP
HomesteadBlock: big.NewInt(0), txt.skipLoad("^ttWrongRLP/.*")
}) // We don't allow more than uint64 in gas amount
txt.config(`^EIP155/`, params.ChainConfig{ // This is a pseudo-consensus vulnerability, but not in practice
HomesteadBlock: big.NewInt(0), // because of the gas limit
EIP150Block: big.NewInt(0), txt.skipLoad("^ttGasLimit/TransactionWithGasLimitxPriceOverflow.json")
EIP155Block: big.NewInt(0), // We _do_ allow more than uint64 in gas price, as opposed to the tests
EIP158Block: big.NewInt(0), // This is also not a concern, as long as tx.Cost() uses big.Int for
ChainID: big.NewInt(1), // calculating the final cozt
}) txt.skipLoad(".*TransactionWithGasPriceOverflow.*")
txt.config(`^Byzantium/`, params.ChainConfig{
HomesteadBlock: big.NewInt(0),
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
})
// The nonce is too large for uint64. Not a concern, it means geth won't
// accept transactions at a certain point in the distant future
txt.skipLoad("^ttNonce/TransactionWithHighNonce256.json")
// The value is larger than uint64, which according to the test is invalid.
// Geth accepts it, which is not a consensus issue since we use big.Int's
// internally to calculate the cost
txt.skipLoad("^ttValue/TransactionWithHighValueOverflow.json")
txt.walk(t, transactionTestDir, func(t *testing.T, name string, test *TransactionTest) { txt.walk(t, transactionTestDir, func(t *testing.T, name string, test *TransactionTest) {
cfg := txt.findConfig(name) cfg := params.MainnetChainConfig
if err := txt.checkFailure(t, name, test.Run(cfg)); err != nil { if err := txt.checkFailure(t, name, test.Run(cfg)); err != nil {
t.Error(err) t.Error(err)
} }

View file

@ -17,14 +17,12 @@
package tests package tests
import ( import (
"bytes"
"errors"
"fmt" "fmt"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "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/core/types"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
@ -32,101 +30,87 @@ import (
// TransactionTest checks RLP decoding and sender derivation of transactions. // TransactionTest checks RLP decoding and sender derivation of transactions.
type TransactionTest struct { 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 { type ttJSON struct {
BlockNumber math.HexOrDecimal64 `json:"blockNumber"`
RLP hexutil.Bytes `json:"rlp"` 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 ttFork struct {
Sender common.UnprefixedAddress `json:"sender"`
type ttTransaction struct { Hash common.UnprefixedHash `json:"hash"`
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
} }
func (tt *TransactionTest) Run(config *params.ChainConfig) error { func (tt *TransactionTest) Run(config *params.ChainConfig) error {
validateTx := func(rlpData hexutil.Bytes, signer types.Signer, block *big.Int) (*common.Address, error) {
tx := new(types.Transaction) tx := new(types.Transaction)
if err := rlp.DecodeBytes(tt.json.RLP, tx); err != nil { if err := rlp.DecodeBytes(rlpData, tx); err != nil {
if tt.json.Transaction == nil { return nil, err
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) sender, err := types.Sender(signer, tx)
if err != nil { if err != nil {
return err return nil, err
} }
if sender != common.BytesToAddress(tt.json.Sender) { // Intrinsic gas
return fmt.Errorf("Sender mismatch: got %x, want %x", sender, tt.json.Sender) requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, config.IsHomestead(block))
if err != nil {
return nil, err
} }
// Check decoded fields. if requiredGas > tx.Gas() {
err = tt.json.Transaction.verify(signer, tx) return nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
if tt.json.Sender == nil && err == nil {
return errors.New("field validations succeeded but should fail")
} }
if tt.json.Sender != nil && err != nil { //if signer.Hash(tx) != common.Hash(fork.Hash) {
return fmt.Errorf("field validations failed after RLP decoding: %s", err) // return fmt.Errorf("Tx hash mismatch, got %v want %v", signer.Hash(tx), fork.Hash)
} //}
return nil return &sender, nil
} }
func (tt *ttTransaction) verify(signer types.Signer, tx *types.Transaction) error { checkFork := func(block *big.Int, fork ttFork, rlpData hexutil.Bytes) error {
if !bytes.Equal(tx.Data(), tt.Data) { signer := types.MakeSigner(config, block)
return fmt.Errorf("Tx input data mismatch: got %x want %x", tx.Data(), tt.Data) sender, err := validateTx(rlpData, signer, block)
// This testcase has an invalid tx
if fork.Sender == (common.UnprefixedAddress{}) {
if err == nil {
return fmt.Errorf("Expected error, got none (address %v)", sender.String())
} }
if tx.Gas() != tt.GasLimit {
return fmt.Errorf("GasLimit mismatch: got %d, want %d", tx.Gas(), tt.GasLimit) return nil
} }
if tx.GasPrice().Cmp(tt.GasPrice) != 0 { // Should resolve the right address
return fmt.Errorf("GasPrice mismatch: got %v, want %v", tx.GasPrice(), tt.GasPrice) if err != nil {
return fmt.Errorf("Got error, expected none: %v", err)
} }
if tx.Nonce() != tt.Nonce { if *sender != common.Address(fork.Sender) {
return fmt.Errorf("Nonce mismatch: got %v, want %v", tx.Nonce(), tt.Nonce) return fmt.Errorf("Sender mismatch: got %x, want %x", sender, fork.Sender)
} }
v, r, s := tx.RawSignatureValues() return nil
if r.Cmp(tt.R) != 0 { }
return fmt.Errorf("R mismatch: got %v, want %v", r, tt.R) if err := checkFork(new(big.Int), tt.Frontier, tt.RLP); err != nil {
} return fmt.Errorf("Frontier: %v", err)
if s.Cmp(tt.S) != 0 { }
return fmt.Errorf("S mismatch: got %v, want %v", s, tt.S) if err := checkFork(config.HomesteadBlock, tt.Homestead, tt.RLP); err != nil {
} return fmt.Errorf("Homestead: %v", err)
if v.Cmp(tt.V) != 0 { }
return fmt.Errorf("V mismatch: got %v, want %v", v, tt.V) if err := checkFork(config.EIP150Block, tt.EIP150, tt.RLP); err != nil {
} return fmt.Errorf("EIP150: %v", err)
if tx.To() == nil { }
if tt.To != (common.Address{}) { if err := checkFork(config.EIP158Block, tt.EIP158, tt.RLP); err != nil {
return fmt.Errorf("To mismatch when recipient is nil (contract creation): %x", tt.To) return fmt.Errorf("EIP158: %v", err)
} }
} else if *tx.To() != tt.To { if err := checkFork(config.ByzantiumBlock, tt.Byzantium, tt.RLP); err != nil {
return fmt.Errorf("To mismatch: got %x, want %x", *tx.To(), tt.To) return fmt.Errorf("Byzantium: %v", err)
} }
if tx.Value().Cmp(tt.Value) != 0 { if err := checkFork(config.ConstantinopleBlock, tt.Constantinople, tt.RLP); err != nil {
return fmt.Errorf("Value mismatch: got %x, want %x", tx.Value(), tt.Value) return fmt.Errorf("Constantinople: %v", err)
} }
return nil return nil
} }