github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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  	"github.com/intfoundation/intchain/core"
    22  
    23  	"github.com/intfoundation/intchain/common"
    24  	"github.com/intfoundation/intchain/common/hexutil"
    25  	"github.com/intfoundation/intchain/core/types"
    26  	"github.com/intfoundation/intchain/params"
    27  	"github.com/intfoundation/intchain/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  		{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Istanbul, true, true},
    84  	} {
    85  		sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead, testcase.isIstanbul)
    86  
    87  		if testcase.fork.Sender == (common.UnprefixedAddress{}) {
    88  			if err == nil {
    89  				return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name)
    90  			}
    91  			continue
    92  		}
    93  		// Should resolve the right address
    94  		if err != nil {
    95  			return fmt.Errorf("got error, expected none: %v", err)
    96  		}
    97  		if sender == nil {
    98  			return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
    99  		}
   100  		if *sender != common.Address(testcase.fork.Sender) {
   101  			return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
   102  		}
   103  		if txhash == nil {
   104  			return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
   105  		}
   106  		if *txhash != common.Hash(testcase.fork.Hash) {
   107  			return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
   108  		}
   109  	}
   110  	return nil
   111  }