github.com/theQRL/go-zond@v0.2.1/internal/zondapi/transaction_args_test.go (about) 1 // Copyright 2022 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 zondapi 18 19 import ( 20 "context" 21 "errors" 22 "math/big" 23 "reflect" 24 "testing" 25 "time" 26 27 "github.com/theQRL/go-zond" 28 "github.com/theQRL/go-zond/accounts" 29 "github.com/theQRL/go-zond/common" 30 "github.com/theQRL/go-zond/common/hexutil" 31 "github.com/theQRL/go-zond/consensus" 32 "github.com/theQRL/go-zond/core" 33 "github.com/theQRL/go-zond/core/bloombits" 34 "github.com/theQRL/go-zond/core/state" 35 "github.com/theQRL/go-zond/core/types" 36 "github.com/theQRL/go-zond/core/vm" 37 "github.com/theQRL/go-zond/event" 38 "github.com/theQRL/go-zond/params" 39 "github.com/theQRL/go-zond/rpc" 40 "github.com/theQRL/go-zond/zonddb" 41 ) 42 43 // TestSetFeeDefaults tests the logic for filling in default fee values works as expected. 44 func TestSetFeeDefaults(t *testing.T) { 45 type test struct { 46 name string 47 in *TransactionArgs 48 want *TransactionArgs 49 err error 50 } 51 52 var ( 53 b = newBackendMock() 54 fortytwo = (*hexutil.Big)(big.NewInt(42)) 55 maxFee = (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(b.current.BaseFee, big.NewInt(2)), fortytwo.ToInt())) 56 al = &types.AccessList{types.AccessTuple{Address: common.Address{0xaa}, StorageKeys: []common.Hash{{0x01}}}} 57 ) 58 59 tests := []test{ 60 { 61 "dynamic tx", 62 &TransactionArgs{AccessList: al}, 63 &TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, 64 nil, 65 }, 66 { 67 "dynamic tx, only max fee", 68 &TransactionArgs{MaxFeePerGas: maxFee}, 69 &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, 70 nil, 71 }, 72 { 73 "dynamic tx, only priority fee", 74 &TransactionArgs{MaxFeePerGas: maxFee}, 75 &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, 76 nil, 77 }, 78 { 79 "dynamic fee tx, maxFee < priorityFee", 80 &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(1000))}, 81 nil, 82 errors.New("maxFeePerGas (0x3e) < maxPriorityFeePerGas (0x3e8)"), 83 }, 84 { 85 "dynamic fee tx, maxFee < priorityFee while setting default", 86 &TransactionArgs{MaxFeePerGas: (*hexutil.Big)(big.NewInt(7))}, 87 nil, 88 errors.New("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"), 89 }, 90 } 91 92 ctx := context.Background() 93 for i, test := range tests { 94 got := test.in 95 err := got.setFeeDefaults(ctx, b) 96 if err != nil && err.Error() == test.err.Error() { 97 // Test threw expected error. 98 continue 99 } else if err != nil { 100 t.Fatalf("test %d (%s): unexpected error: %s", i, test.name, err) 101 } 102 if !reflect.DeepEqual(got, test.want) { 103 t.Fatalf("test %d (%s): did not fill defaults as expected: (got: %v, want: %v)", i, test.name, got, test.want) 104 } 105 } 106 } 107 108 type backendMock struct { 109 current *types.Header 110 config *params.ChainConfig 111 } 112 113 func newBackendMock() *backendMock { 114 config := ¶ms.ChainConfig{ 115 ChainID: big.NewInt(42), 116 } 117 return &backendMock{ 118 current: &types.Header{ 119 Number: big.NewInt(1100), 120 GasLimit: 8_000_000, 121 GasUsed: 8_000_000, 122 Time: 555, 123 Extra: make([]byte, 32), 124 BaseFee: big.NewInt(10), 125 }, 126 config: config, 127 } 128 } 129 130 func (b *backendMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 131 return big.NewInt(42), nil 132 } 133 func (b *backendMock) CurrentHeader() *types.Header { return b.current } 134 func (b *backendMock) ChainConfig() *params.ChainConfig { return b.config } 135 136 // Other methods needed to implement Backend interface. 137 func (b *backendMock) SyncProgress() zond.SyncProgress { return zond.SyncProgress{} } 138 func (b *backendMock) FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { 139 return nil, nil, nil, nil, nil 140 } 141 func (b *backendMock) ChainDb() zonddb.Database { return nil } 142 func (b *backendMock) AccountManager() *accounts.Manager { return nil } 143 func (b *backendMock) ExtRPCEnabled() bool { return false } 144 func (b *backendMock) RPCGasCap() uint64 { return 0 } 145 func (b *backendMock) RPCZVMTimeout() time.Duration { return time.Second } 146 func (b *backendMock) RPCTxFeeCap() float64 { return 0 } 147 func (b *backendMock) SetHead(number uint64) {} 148 func (b *backendMock) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 149 return nil, nil 150 } 151 func (b *backendMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 152 return nil, nil 153 } 154 func (b *backendMock) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 155 return nil, nil 156 } 157 func (b *backendMock) CurrentBlock() *types.Header { return nil } 158 func (b *backendMock) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 159 return nil, nil 160 } 161 func (b *backendMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 162 return nil, nil 163 } 164 func (b *backendMock) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 165 return nil, nil 166 } 167 func (b *backendMock) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) { 168 return nil, nil 169 } 170 func (b *backendMock) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 171 return nil, nil, nil 172 } 173 func (b *backendMock) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 174 return nil, nil, nil 175 } 176 func (b *backendMock) Pending() (*types.Block, types.Receipts, *state.StateDB) { return nil, nil, nil } 177 func (b *backendMock) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 178 return nil, nil 179 } 180 func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) { 181 return nil, nil 182 } 183 func (b *backendMock) GetZVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.ZVM { 184 return nil 185 } 186 func (b *backendMock) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { return nil } 187 func (b *backendMock) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 188 return nil 189 } 190 func (b *backendMock) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 191 return nil 192 } 193 func (b *backendMock) SendTx(ctx context.Context, signedTx *types.Transaction) error { return nil } 194 func (b *backendMock) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 195 return nil, [32]byte{}, 0, 0, nil 196 } 197 func (b *backendMock) GetPoolTransactions() (types.Transactions, error) { return nil, nil } 198 func (b *backendMock) GetPoolTransaction(txHash common.Hash) *types.Transaction { return nil } 199 func (b *backendMock) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 200 return 0, nil 201 } 202 func (b *backendMock) Stats() (pending int, queued int) { return 0, 0 } 203 func (b *backendMock) TxPoolContent() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction) { 204 return nil, nil 205 } 206 func (b *backendMock) TxPoolContentFrom(addr common.Address) ([]*types.Transaction, []*types.Transaction) { 207 return nil, nil 208 } 209 func (b *backendMock) SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription { return nil } 210 func (b *backendMock) BloomStatus() (uint64, uint64) { return 0, 0 } 211 func (b *backendMock) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {} 212 func (b *backendMock) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { return nil } 213 func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 214 return nil 215 } 216 217 func (b *backendMock) Engine() consensus.Engine { return nil }