github.com/YoungNK/go-ethereum@v1.9.7/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  	Istanbul       ttFork
    36  	EIP150         ttFork
    37  	EIP158         ttFork
    38  	Frontier       ttFork
    39  	Homestead      ttFork
    40  }
    41  
    42  type ttFork struct {
    43  	Sender common.UnprefixedAddress `json:"sender"`
    44  	Hash   common.UnprefixedHash    `json:"hash"`
    45  }
    46  
    47  func (tt *TransactionTest) Run(config *params.ChainConfig) error {
    48  
    49  	validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool, isIstanbul bool) (*common.Address, *common.Hash, error) {
    50  		tx := new(types.Transaction)
    51  		if err := rlp.DecodeBytes(rlpData, tx); err != nil {
    52  			return nil, nil, err
    53  		}
    54  		sender, err := types.Sender(signer, tx)
    55  		if err != nil {
    56  			return nil, nil, err
    57  		}
    58  		// Intrinsic gas
    59  		requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, isHomestead, isIstanbul)
    60  		if err != nil {
    61  			return nil, nil, err
    62  		}
    63  		if requiredGas > tx.Gas() {
    64  			return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
    65  		}
    66  		h := tx.Hash()
    67  		return &sender, &h, nil
    68  	}
    69  
    70  	for _, testcase := range []struct {
    71  		name        string
    72  		signer      types.Signer
    73  		fork        ttFork
    74  		isHomestead bool
    75  		isIstanbul  bool
    76  	}{
    77  		{"Frontier", types.FrontierSigner{}, tt.Frontier, false, false},
    78  		{"Homestead", types.HomesteadSigner{}, tt.Homestead, true, false},
    79  		{"EIP150", types.HomesteadSigner{}, tt.EIP150, true, false},
    80  		{"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true, false},
    81  		{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true, false},
    82  		{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true, false},
    83  		//TODO! @holiman or @rjl493456442 : enable this after tests have been updated for Istanbul
    84  		//{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Istanbul, true, true},
    85  	} {
    86  		sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead, testcase.isIstanbul)
    87  
    88  		if testcase.fork.Sender == (common.UnprefixedAddress{}) {
    89  			if err == nil {
    90  				return fmt.Errorf("Expected error, got none (address %v)[%v]", sender.String(), testcase.name)
    91  			}
    92  			continue
    93  		}
    94  		// Should resolve the right address
    95  		if err != nil {
    96  			return fmt.Errorf("Got error, expected none: %v", err)
    97  		}
    98  		if sender == nil {
    99  			return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
   100  		}
   101  		if *sender != common.Address(testcase.fork.Sender) {
   102  			return fmt.Errorf("Sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
   103  		}
   104  		if txhash == nil {
   105  			return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
   106  		}
   107  		if *txhash != common.Hash(testcase.fork.Hash) {
   108  			return fmt.Errorf("Hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
   109  		}
   110  	}
   111  	return nil
   112  }