transaction_test_util.go 3.82 KB
Newer Older
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

17 18 19 20 21 22
package tests

import (
	"fmt"

	"github.com/ethereum/go-ethereum/common"
23
	"github.com/ethereum/go-ethereum/common/hexutil"
24
	"github.com/ethereum/go-ethereum/core"
25
	"github.com/ethereum/go-ethereum/core/types"
26
	"github.com/ethereum/go-ethereum/params"
27 28 29
	"github.com/ethereum/go-ethereum/rlp"
)

30
// TransactionTest checks RLP decoding and sender derivation of transactions.
31
type TransactionTest struct {
32 33 34
	RLP            hexutil.Bytes `json:"rlp"`
	Byzantium      ttFork
	Constantinople ttFork
35
	Istanbul       ttFork
36 37 38 39
	EIP150         ttFork
	EIP158         ttFork
	Frontier       ttFork
	Homestead      ttFork
40 41
}

42 43 44
type ttFork struct {
	Sender common.UnprefixedAddress `json:"sender"`
	Hash   common.UnprefixedHash    `json:"hash"`
45 46
}

47
func (tt *TransactionTest) Run(config *params.ChainConfig) error {
48
	validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool, isIstanbul bool) (*common.Address, *common.Hash, error) {
49 50 51
		tx := new(types.Transaction)
		if err := rlp.DecodeBytes(rlpData, tx); err != nil {
			return nil, nil, err
52
		}
53 54 55 56 57
		sender, err := types.Sender(signer, tx)
		if err != nil {
			return nil, nil, err
		}
		// Intrinsic gas
58
		requiredGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, isHomestead, isIstanbul)
59 60 61 62 63 64 65 66
		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
67
	}
68

69 70 71 72 73
	for _, testcase := range []struct {
		name        string
		signer      types.Signer
		fork        ttFork
		isHomestead bool
74
		isIstanbul  bool
75
	}{
76 77 78 79 80 81
		{"Frontier", types.FrontierSigner{}, tt.Frontier, false, false},
		{"Homestead", types.HomesteadSigner{}, tt.Homestead, true, false},
		{"EIP150", types.HomesteadSigner{}, tt.EIP150, true, false},
		{"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true, false},
		{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true, false},
		{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true, false},
82
		{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Istanbul, true, true},
83
	} {
84
		sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead, testcase.isIstanbul)
85 86 87

		if testcase.fork.Sender == (common.UnprefixedAddress{}) {
			if err == nil {
88
				return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name)
89 90 91 92 93
			}
			continue
		}
		// Should resolve the right address
		if err != nil {
94
			return fmt.Errorf("got error, expected none: %v", err)
95 96 97 98 99
		}
		if sender == nil {
			return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
		}
		if *sender != common.Address(testcase.fork.Sender) {
100
			return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
101 102 103 104 105
		}
		if txhash == nil {
			return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
		}
		if *txhash != common.Hash(testcase.fork.Hash) {
106
			return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
107
		}
108 109 110
	}
	return nil
}