github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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 validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool, isIstanbul bool) (*common.Address, *common.Hash, error) { 49 tx := new(types.Transaction) 50 if err := rlp.DecodeBytes(rlpData, tx); err != nil { 51 return nil, nil, err 52 } 53 sender, err := types.Sender(signer, tx) 54 if err != nil { 55 return nil, nil, err 56 } 57 // Intrinsic gas 58 requiredGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, isHomestead, isIstanbul) 59 if err != nil { 60 return nil, nil, err 61 } 62 if requiredGas > tx.Gas() { 63 return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas) 64 } 65 h := tx.Hash() 66 return &sender, &h, nil 67 } 68 69 for _, testcase := range []struct { 70 name string 71 signer types.Signer 72 fork ttFork 73 isHomestead bool 74 isIstanbul bool 75 }{ 76 {"Frontier", types.FrontierSigner{}, tt.Frontier, false, false}, 77 {"Homestead", types.HomesteadSigner{}, tt.Homestead, true, false}, 78 {"EIP150", types.HomesteadSigner{}, tt.EIP150, true, false}, 79 {"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true, false}, 80 {"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true, false}, 81 {"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true, false}, 82 {"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Istanbul, true, true}, 83 } { 84 sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead, testcase.isIstanbul) 85 86 if testcase.fork.Sender == (common.UnprefixedAddress{}) { 87 if err == nil { 88 return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name) 89 } 90 continue 91 } 92 // Should resolve the right address 93 if err != nil { 94 return fmt.Errorf("got error, expected none: %v", err) 95 } 96 if sender == nil { 97 return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender)) 98 } 99 if *sender != common.Address(testcase.fork.Sender) { 100 return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender) 101 } 102 if txhash == nil { 103 return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash)) 104 } 105 if *txhash != common.Hash(testcase.fork.Hash) { 106 return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash) 107 } 108 } 109 return nil 110 }