Skip to content

Commit

Permalink
tests: make transaction tests run again, fix ethereum#19033
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed May 5, 2019
1 parent abeba0a commit e98dc75
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 195 deletions.
95 changes: 0 additions & 95 deletions tests/gen_tttransaction.go

This file was deleted.

38 changes: 19 additions & 19 deletions tests/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package tests

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/params"
Expand All @@ -27,26 +26,27 @@ func TestTransaction(t *testing.T) {
t.Parallel()

txt := new(testMatcher)
txt.config(`^Homestead/`, params.ChainConfig{
HomesteadBlock: big.NewInt(0),
})
txt.config(`^EIP155/`, params.ChainConfig{
HomesteadBlock: big.NewInt(0),
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ChainID: big.NewInt(1),
})
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),
})
// These can't be parsed, invalid hex in RLP
txt.skipLoad("^ttWrongRLP/.*")
// We don't allow more than uint64 in gas amount
// This is a pseudo-consensus vulnerability, but not in practice
// because of the gas limit
txt.skipLoad("^ttGasLimit/TransactionWithGasLimitxPriceOverflow.json")
// We _do_ allow more than uint64 in gas price, as opposed to the tests
// This is also not a concern, as long as tx.Cost() uses big.Int for
// calculating the final cozt
txt.skipLoad(".*TransactionWithGasPriceOverflow.*")

// 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) {
cfg := txt.findConfig(name)
cfg := params.MainnetChainConfig
if err := txt.checkFailure(t, name, test.Run(cfg)); err != nil {
t.Error(err)
}
Expand Down
146 changes: 65 additions & 81 deletions tests/transaction_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,116 +17,100 @@
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"
)

// 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"`
RLP hexutil.Bytes `json:"rlp"`
}

//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

validateTx := func(rlpData hexutil.Bytes, signer types.Signer, block *big.Int) (*common.Address, error) {
tx := new(types.Transaction)
if err := rlp.DecodeBytes(rlpData, tx); err != nil {
return nil, err
}
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)
sender, err := types.Sender(signer, tx)
if err != nil {
return nil, err
}
// Intrinsic gas
requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, config.IsHomestead(block))
if err != nil {
return nil, err
}
if requiredGas > tx.Gas() {
return nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
}
//if signer.Hash(tx) != common.Hash(fork.Hash) {
// return fmt.Errorf("Tx hash mismatch, got %v want %v", signer.Hash(tx), fork.Hash)
//}
return &sender, nil
}
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)
checkFork := func(block *big.Int, fork ttFork, rlpData hexutil.Bytes) error {
signer := types.MakeSigner(config, block)
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())
}

return nil
}
// Should resolve the right address
if err != nil {
return fmt.Errorf("Got error, expected none: %v", err)
}
if *sender != common.Address(fork.Sender) {
return fmt.Errorf("Sender mismatch: got %x, want %x", sender, fork.Sender)
}
return nil
}
if tx.Nonce() != tt.Nonce {
return fmt.Errorf("Nonce mismatch: got %v, want %v", tx.Nonce(), tt.Nonce)
if err := checkFork(new(big.Int), tt.Frontier, tt.RLP); err != nil {
return fmt.Errorf("Frontier: %v", err)
}
v, r, s := tx.RawSignatureValues()
if r.Cmp(tt.R) != 0 {
return fmt.Errorf("R mismatch: got %v, want %v", r, tt.R)
if err := checkFork(config.HomesteadBlock, tt.Homestead, tt.RLP); err != nil {
return fmt.Errorf("Homestead: %v", err)
}
if s.Cmp(tt.S) != 0 {
return fmt.Errorf("S mismatch: got %v, want %v", s, tt.S)
if err := checkFork(config.EIP150Block, tt.EIP150, tt.RLP); err != nil {
return fmt.Errorf("EIP150: %v", err)
}
if v.Cmp(tt.V) != 0 {
return fmt.Errorf("V mismatch: got %v, want %v", v, tt.V)
if err := checkFork(config.EIP158Block, tt.EIP158, tt.RLP); err != nil {
return fmt.Errorf("EIP158: %v", err)
}
if tx.To() == nil {
if tt.To != (common.Address{}) {
return fmt.Errorf("To mismatch when recipient is nil (contract creation): %x", tt.To)
}
} else if *tx.To() != tt.To {
return fmt.Errorf("To mismatch: got %x, want %x", *tx.To(), tt.To)
if err := checkFork(config.ByzantiumBlock, tt.Byzantium, tt.RLP); err != nil {
return fmt.Errorf("Byzantium: %v", err)
}
if tx.Value().Cmp(tt.Value) != 0 {
return fmt.Errorf("Value mismatch: got %x, want %x", tx.Value(), tt.Value)
if err := checkFork(config.ConstantinopleBlock, tt.Constantinople, tt.RLP); err != nil {
return fmt.Errorf("Constantinople: %v", err)
}
return nil
}

0 comments on commit e98dc75

Please sign in to comment.