github.com/core-coin/go-core/v2@v2.1.9/tests/transaction_test_util.go (about) 1 // Copyright 2015 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package tests 18 19 import ( 20 "fmt" 21 22 "github.com/core-coin/go-core/v2/common" 23 "github.com/core-coin/go-core/v2/common/hexutil" 24 "github.com/core-coin/go-core/v2/core" 25 "github.com/core-coin/go-core/v2/core/types" 26 "github.com/core-coin/go-core/v2/params" 27 "github.com/core-coin/go-core/v2/rlp" 28 ) 29 30 // TransactionTest checks RLP decoding and sender derivation of transactions. 31 type TransactionTest struct { 32 RLP hexutil.Bytes `json:"rlp"` 33 Nucleus ttFork 34 } 35 36 type ttFork struct { 37 Sender common.UnprefixedAddress `json:"sender"` 38 Hash common.UnprefixedHash `json:"hash"` 39 } 40 41 func (tt *TransactionTest) Run(config *params.ChainConfig) error { 42 validateTx := func(rlpData hexutil.Bytes, signer types.Signer) (*common.Address, *common.Hash, error) { 43 tx := new(types.Transaction) 44 if err := rlp.DecodeBytes(rlpData, tx); err != nil { 45 return nil, nil, err 46 } 47 sender, err := types.Sender(signer, tx) 48 if err != nil { 49 return nil, nil, err 50 } 51 // Intrinsic energy 52 requiredEnergy, err := core.IntrinsicEnergy(tx.Data(), tx.To() == nil) 53 if err != nil { 54 return nil, nil, err 55 } 56 if requiredEnergy > tx.Energy() { 57 return nil, nil, fmt.Errorf("insufficient energy ( %d < %d )", tx.Energy(), requiredEnergy) 58 } 59 h := tx.Hash() 60 return &sender, &h, nil 61 } 62 63 for _, testcase := range []struct { 64 name string 65 signer types.Signer 66 fork ttFork 67 }{ 68 {"Nucleus", types.NewNucleusSigner(config.NetworkID), tt.Nucleus}, 69 } { 70 sender, txhash, err := validateTx(tt.RLP, testcase.signer) 71 72 if testcase.fork.Sender == (common.UnprefixedAddress{}) { 73 if err == nil { 74 return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name) 75 } 76 continue 77 } 78 // Should resolve the right address 79 if err != nil { 80 return fmt.Errorf("got error, expected none: %v", err) 81 } 82 if sender == nil { 83 return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender)) 84 } 85 if *sender != common.Address(testcase.fork.Sender) { 86 return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender) 87 } 88 if txhash == nil { 89 return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash)) 90 } 91 if *txhash != common.Hash(testcase.fork.Hash) { 92 return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash) 93 } 94 } 95 return nil 96 }