github.com/wzbox/go-ethereum@v1.9.2/tests/transaction_test_util.go (about)

     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  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     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  // GNU Lesser General Public License for more details.
    13  //
    14  // 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  package tests
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"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  	"github.com/ethereum/go-ethereum/rlp"
    28  )
    29  
    30  // TransactionTest checks RLP decoding and sender derivation of transactions.
    31  type TransactionTest struct {
    32  	RLP            hexutil.Bytes `json:"rlp"`
    33  	Byzantium      ttFork
    34  	Constantinople ttFork
    35  	EIP150         ttFork
    36  	EIP158         ttFork
    37  	Frontier       ttFork
    38  	Homestead      ttFork
    39  }
    40  
    41  type ttFork struct {
    42  	Sender common.UnprefixedAddress `json:"sender"`
    43  	Hash   common.UnprefixedHash    `json:"hash"`
    44  }
    45  
    46  func (tt *TransactionTest) Run(config *params.ChainConfig) error {
    47  
    48  	validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool) (*common.Address, *common.Hash, error) {
    49  		tx := new(types.Transaction)
    50  		if err := rlp.DecodeBytes(rlpData, tx); err != nil {
    51  			return nil, nil, err
    52  		}
    53  		sender, err := types.Sender(signer, tx)
    54  		if err != nil {
    55  			return nil, nil, err
    56  		}
    57  		// Intrinsic gas
    58  		requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, isHomestead)
    59  		if err != nil {
    60  			return nil, nil, err
    61  		}
    62  		if requiredGas > tx.Gas() {
    63  			return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
    64  		}
    65  		h := tx.Hash()
    66  		return &sender, &h, nil
    67  	}
    68  
    69  	for _, testcase := range []struct {
    70  		name        string
    71  		signer      types.Signer
    72  		fork        ttFork
    73  		isHomestead bool
    74  	}{
    75  		{"Frontier", types.FrontierSigner{}, tt.Frontier, false},
    76  		{"Homestead", types.HomesteadSigner{}, tt.Homestead, true},
    77  		{"EIP150", types.HomesteadSigner{}, tt.EIP150, true},
    78  		{"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true},
    79  		{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true},
    80  		{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true},
    81  	} {
    82  		sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead)
    83  
    84  		if testcase.fork.Sender == (common.UnprefixedAddress{}) {
    85  			if err == nil {
    86  				return fmt.Errorf("Expected error, got none (address %v)", sender.String())
    87  			}
    88  			continue
    89  		}
    90  		// Should resolve the right address
    91  		if err != nil {
    92  			return fmt.Errorf("Got error, expected none: %v", err)
    93  		}
    94  		if sender == nil {
    95  			return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
    96  		}
    97  		if *sender != common.Address(testcase.fork.Sender) {
    98  			return fmt.Errorf("Sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
    99  		}
   100  		if txhash == nil {
   101  			return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
   102  		}
   103  		if *txhash != common.Hash(testcase.fork.Hash) {
   104  			return fmt.Errorf("Hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
   105  		}
   106  	}
   107  	return nil
   108  }