github.com/ylsGit/go-ethereum@v1.6.5/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  	"bytes"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"runtime"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/common/math"
    28  	"github.com/ethereum/go-ethereum/core/types"
    29  	"github.com/ethereum/go-ethereum/log"
    30  	"github.com/ethereum/go-ethereum/params"
    31  	"github.com/ethereum/go-ethereum/rlp"
    32  )
    33  
    34  // Transaction Test JSON Format
    35  type TtTransaction struct {
    36  	Data     string
    37  	GasLimit string
    38  	GasPrice string
    39  	Nonce    string
    40  	R        string
    41  	S        string
    42  	To       string
    43  	V        string
    44  	Value    string
    45  }
    46  
    47  type TransactionTest struct {
    48  	Blocknumber string
    49  	Rlp         string
    50  	Sender      string
    51  	Transaction TtTransaction
    52  }
    53  
    54  func RunTransactionTestsWithReader(config *params.ChainConfig, r io.Reader, skipTests []string) error {
    55  	skipTest := make(map[string]bool, len(skipTests))
    56  	for _, name := range skipTests {
    57  		skipTest[name] = true
    58  	}
    59  
    60  	bt := make(map[string]TransactionTest)
    61  	if err := readJson(r, &bt); err != nil {
    62  		return err
    63  	}
    64  
    65  	for name, test := range bt {
    66  		// if the test should be skipped, return
    67  		if skipTest[name] {
    68  			log.Info(fmt.Sprint("Skipping transaction test", name))
    69  			return nil
    70  		}
    71  		// test the block
    72  		if err := runTransactionTest(config, test); err != nil {
    73  			return err
    74  		}
    75  		log.Info(fmt.Sprint("Transaction test passed: ", name))
    76  
    77  	}
    78  	return nil
    79  }
    80  
    81  func RunTransactionTests(config *params.ChainConfig, file string, skipTests []string) error {
    82  	tests := make(map[string]TransactionTest)
    83  	if err := readJsonFile(file, &tests); err != nil {
    84  		return err
    85  	}
    86  
    87  	if err := runTransactionTests(config, tests, skipTests); err != nil {
    88  		return err
    89  	}
    90  	return nil
    91  }
    92  
    93  func runTransactionTests(config *params.ChainConfig, tests map[string]TransactionTest, skipTests []string) error {
    94  	skipTest := make(map[string]bool, len(skipTests))
    95  	for _, name := range skipTests {
    96  		skipTest[name] = true
    97  	}
    98  
    99  	for name, test := range tests {
   100  		// if the test should be skipped, return
   101  		if skipTest[name] {
   102  			log.Info(fmt.Sprint("Skipping transaction test", name))
   103  			return nil
   104  		}
   105  
   106  		// test the block
   107  		if err := runTransactionTest(config, test); err != nil {
   108  			return fmt.Errorf("%s: %v", name, err)
   109  		}
   110  		log.Info(fmt.Sprint("Transaction test passed: ", name))
   111  
   112  	}
   113  	return nil
   114  }
   115  
   116  func runTransactionTest(config *params.ChainConfig, txTest TransactionTest) (err error) {
   117  	tx := new(types.Transaction)
   118  	err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
   119  
   120  	if err != nil {
   121  		if txTest.Sender == "" {
   122  			// RLP decoding failed and this is expected (test OK)
   123  			return nil
   124  		} else {
   125  			// RLP decoding failed but is expected to succeed (test FAIL)
   126  			return fmt.Errorf("RLP decoding failed when expected to succeed: %s", err)
   127  		}
   128  	}
   129  
   130  	validationError := verifyTxFields(config, txTest, tx)
   131  	if txTest.Sender == "" {
   132  		if validationError != nil {
   133  			// RLP decoding works but validation should fail (test OK)
   134  			return nil
   135  		} else {
   136  			// RLP decoding works but validation should fail (test FAIL)
   137  			// (this should not be possible but added here for completeness)
   138  			return errors.New("Field validations succeeded but should fail")
   139  		}
   140  	}
   141  
   142  	if txTest.Sender != "" {
   143  		if validationError == nil {
   144  			// RLP decoding works and validations pass (test OK)
   145  			return nil
   146  		} else {
   147  			// RLP decoding works and validations pass (test FAIL)
   148  			return fmt.Errorf("Field validations failed after RLP decoding: %s", validationError)
   149  		}
   150  	}
   151  	return errors.New("Should not happen: verify RLP decoding and field validation")
   152  }
   153  
   154  func verifyTxFields(chainConfig *params.ChainConfig, txTest TransactionTest, decodedTx *types.Transaction) (err error) {
   155  	defer func() {
   156  		if recovered := recover(); recovered != nil {
   157  			buf := make([]byte, 64<<10)
   158  			buf = buf[:runtime.Stack(buf, false)]
   159  			err = fmt.Errorf("%v\n%s", recovered, buf)
   160  		}
   161  	}()
   162  
   163  	var decodedSender common.Address
   164  
   165  	signer := types.MakeSigner(chainConfig, math.MustParseBig256(txTest.Blocknumber))
   166  	decodedSender, err = types.Sender(signer, decodedTx)
   167  	if err != nil {
   168  		return err
   169  	}
   170  
   171  	expectedSender := mustConvertAddress(txTest.Sender)
   172  	if expectedSender != decodedSender {
   173  		return fmt.Errorf("Sender mismatch: %x %x", expectedSender, decodedSender)
   174  	}
   175  
   176  	expectedData := mustConvertBytes(txTest.Transaction.Data)
   177  	if !bytes.Equal(expectedData, decodedTx.Data()) {
   178  		return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Data())
   179  	}
   180  
   181  	expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
   182  	if expectedGasLimit.Cmp(decodedTx.Gas()) != 0 {
   183  		return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.Gas())
   184  	}
   185  
   186  	expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
   187  	if expectedGasPrice.Cmp(decodedTx.GasPrice()) != 0 {
   188  		return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.GasPrice())
   189  	}
   190  
   191  	expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
   192  	if expectedNonce != decodedTx.Nonce() {
   193  		return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.Nonce())
   194  	}
   195  
   196  	v, r, s := decodedTx.RawSignatureValues()
   197  	expectedR := mustConvertBigInt(txTest.Transaction.R, 16)
   198  	if r.Cmp(expectedR) != 0 {
   199  		return fmt.Errorf("R mismatch: %v %v", expectedR, r)
   200  	}
   201  	expectedS := mustConvertBigInt(txTest.Transaction.S, 16)
   202  	if s.Cmp(expectedS) != 0 {
   203  		return fmt.Errorf("S mismatch: %v %v", expectedS, s)
   204  	}
   205  	expectedV := mustConvertBigInt(txTest.Transaction.V, 16)
   206  	if v.Cmp(expectedV) != 0 {
   207  		return fmt.Errorf("V mismatch: %v %v", expectedV, v)
   208  	}
   209  
   210  	expectedTo := mustConvertAddress(txTest.Transaction.To)
   211  	if decodedTx.To() == nil {
   212  		if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
   213  			return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
   214  		}
   215  	} else {
   216  		if expectedTo != *decodedTx.To() {
   217  			return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.To())
   218  		}
   219  	}
   220  
   221  	expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
   222  	if expectedValue.Cmp(decodedTx.Value()) != 0 {
   223  		return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Value())
   224  	}
   225  
   226  	return nil
   227  }