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