github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/tests/transaction_test_util.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package tests
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/AigarNetwork/aigar/common"
    24  	"github.com/AigarNetwork/aigar/common/hexutil"
    25  	"github.com/AigarNetwork/aigar/core"
    26  	"github.com/AigarNetwork/aigar/core/types"
    27  	"github.com/AigarNetwork/aigar/params"
    28  	"github.com/AigarNetwork/aigar/rlp"
    29  )
    30  
    31  // TransactionTest checks RLP decoding and sender derivation of transactions.
    32  type TransactionTest struct {
    33  	RLP            hexutil.Bytes `json:"rlp"`
    34  	Byzantium      ttFork
    35  	Constantinople ttFork
    36  	Istanbul       ttFork
    37  	EIP150         ttFork
    38  	EIP158         ttFork
    39  	Frontier       ttFork
    40  	Homestead      ttFork
    41  }
    42  
    43  type ttFork struct {
    44  	Sender common.UnprefixedAddress `json:"sender"`
    45  	Hash   common.UnprefixedHash    `json:"hash"`
    46  }
    47  
    48  func (tt *TransactionTest) Run(config *params.ChainConfig) error {
    49  
    50  	validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool, isIstanbul bool) (*common.Address, *common.Hash, error) {
    51  		tx := new(types.Transaction)
    52  		if err := rlp.DecodeBytes(rlpData, tx); err != nil {
    53  			return nil, nil, err
    54  		}
    55  		sender, err := types.Sender(signer, tx)
    56  		if err != nil {
    57  			return nil, nil, err
    58  		}
    59  		// Intrinsic gas
    60  		requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, isHomestead, isIstanbul)
    61  		if err != nil {
    62  			return nil, nil, err
    63  		}
    64  		if requiredGas > tx.Gas() {
    65  			return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
    66  		}
    67  		h := tx.Hash()
    68  		return &sender, &h, nil
    69  	}
    70  
    71  	for _, testcase := range []struct {
    72  		name        string
    73  		signer      types.Signer
    74  		fork        ttFork
    75  		isHomestead bool
    76  		isIstanbul  bool
    77  	}{
    78  		{"Frontier", types.FrontierSigner{}, tt.Frontier, false, false},
    79  		{"Homestead", types.HomesteadSigner{}, tt.Homestead, true, false},
    80  		{"EIP150", types.HomesteadSigner{}, tt.EIP150, true, false},
    81  		{"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true, false},
    82  		{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true, false},
    83  		{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true, false},
    84  		//TODO! @holiman or @rjl493456442 : enable this after tests have been updated for Istanbul
    85  		//{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Istanbul, true, true},
    86  	} {
    87  		sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead, testcase.isIstanbul)
    88  
    89  		if testcase.fork.Sender == (common.UnprefixedAddress{}) {
    90  			if err == nil {
    91  				return fmt.Errorf("Expected error, got none (address %v)[%v]", sender.String(), testcase.name)
    92  			}
    93  			continue
    94  		}
    95  		// Should resolve the right address
    96  		if err != nil {
    97  			return fmt.Errorf("Got error, expected none: %v", err)
    98  		}
    99  		if sender == nil {
   100  			return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
   101  		}
   102  		if *sender != common.Address(testcase.fork.Sender) {
   103  			return fmt.Errorf("Sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
   104  		}
   105  		if txhash == nil {
   106  			return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
   107  		}
   108  		if *txhash != common.Hash(testcase.fork.Hash) {
   109  			return fmt.Errorf("Hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
   110  		}
   111  	}
   112  	return nil
   113  }