|
| 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 ethapi |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "math/big" |
| 23 | + "reflect" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + ethereum "github.com/XinFinOrg/XDPoSChain" |
| 28 | + "github.com/XinFinOrg/XDPoSChain/XDCx" |
| 29 | + "github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate" |
| 30 | + "github.com/XinFinOrg/XDPoSChain/XDCxlending" |
| 31 | + "github.com/XinFinOrg/XDPoSChain/accounts" |
| 32 | + "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind" |
| 33 | + "github.com/XinFinOrg/XDPoSChain/common" |
| 34 | + "github.com/XinFinOrg/XDPoSChain/common/hexutil" |
| 35 | + "github.com/XinFinOrg/XDPoSChain/consensus" |
| 36 | + "github.com/XinFinOrg/XDPoSChain/core" |
| 37 | + "github.com/XinFinOrg/XDPoSChain/core/bloombits" |
| 38 | + "github.com/XinFinOrg/XDPoSChain/core/state" |
| 39 | + "github.com/XinFinOrg/XDPoSChain/core/types" |
| 40 | + "github.com/XinFinOrg/XDPoSChain/core/vm" |
| 41 | + "github.com/XinFinOrg/XDPoSChain/eth/downloader" |
| 42 | + "github.com/XinFinOrg/XDPoSChain/ethdb" |
| 43 | + "github.com/XinFinOrg/XDPoSChain/event" |
| 44 | + "github.com/XinFinOrg/XDPoSChain/params" |
| 45 | + "github.com/XinFinOrg/XDPoSChain/rpc" |
| 46 | +) |
| 47 | + |
| 48 | +// TestSetFeeDefaults tests the logic for filling in default fee values works as expected. |
| 49 | +func TestSetFeeDefaults(t *testing.T) { |
| 50 | + type test struct { |
| 51 | + name string |
| 52 | + isLondon bool |
| 53 | + in *TransactionArgs |
| 54 | + want *TransactionArgs |
| 55 | + err error |
| 56 | + } |
| 57 | + |
| 58 | + var ( |
| 59 | + b = newBackendMock() |
| 60 | + fortytwo = (*hexutil.Big)(big.NewInt(42)) |
| 61 | + maxFee = (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(b.current.BaseFee, big.NewInt(2)), fortytwo.ToInt())) |
| 62 | + al = &types.AccessList{types.AccessTuple{Address: common.Address{0xaa}, StorageKeys: []common.Hash{{0x01}}}} |
| 63 | + ) |
| 64 | + |
| 65 | + tests := []test{ |
| 66 | + // Legacy txs |
| 67 | + { |
| 68 | + "legacy tx pre-London", |
| 69 | + false, |
| 70 | + &TransactionArgs{}, |
| 71 | + &TransactionArgs{GasPrice: fortytwo}, |
| 72 | + nil, |
| 73 | + }, |
| 74 | + { |
| 75 | + "legacy tx post-London, explicit gas price", |
| 76 | + true, |
| 77 | + &TransactionArgs{GasPrice: fortytwo}, |
| 78 | + &TransactionArgs{GasPrice: fortytwo}, |
| 79 | + nil, |
| 80 | + }, |
| 81 | + |
| 82 | + // Access list txs |
| 83 | + { |
| 84 | + "access list tx pre-London", |
| 85 | + false, |
| 86 | + &TransactionArgs{AccessList: al}, |
| 87 | + &TransactionArgs{AccessList: al, GasPrice: fortytwo}, |
| 88 | + nil, |
| 89 | + }, |
| 90 | + { |
| 91 | + "access list tx post-London, explicit gas price", |
| 92 | + false, |
| 93 | + &TransactionArgs{AccessList: al, GasPrice: fortytwo}, |
| 94 | + &TransactionArgs{AccessList: al, GasPrice: fortytwo}, |
| 95 | + nil, |
| 96 | + }, |
| 97 | + { |
| 98 | + "access list tx post-London", |
| 99 | + true, |
| 100 | + &TransactionArgs{AccessList: al}, |
| 101 | + &TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, |
| 102 | + nil, |
| 103 | + }, |
| 104 | + { |
| 105 | + "access list tx post-London, only max fee", |
| 106 | + true, |
| 107 | + &TransactionArgs{AccessList: al, MaxFeePerGas: maxFee}, |
| 108 | + &TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, |
| 109 | + nil, |
| 110 | + }, |
| 111 | + { |
| 112 | + "access list tx post-London, only priority fee", |
| 113 | + true, |
| 114 | + &TransactionArgs{AccessList: al, MaxFeePerGas: maxFee}, |
| 115 | + &TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, |
| 116 | + nil, |
| 117 | + }, |
| 118 | + |
| 119 | + // Dynamic fee txs |
| 120 | + { |
| 121 | + "dynamic tx post-London", |
| 122 | + true, |
| 123 | + &TransactionArgs{}, |
| 124 | + &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, |
| 125 | + nil, |
| 126 | + }, |
| 127 | + { |
| 128 | + "dynamic tx post-London, only max fee", |
| 129 | + true, |
| 130 | + &TransactionArgs{MaxFeePerGas: maxFee}, |
| 131 | + &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, |
| 132 | + nil, |
| 133 | + }, |
| 134 | + { |
| 135 | + "dynamic tx post-London, only priority fee", |
| 136 | + true, |
| 137 | + &TransactionArgs{MaxFeePerGas: maxFee}, |
| 138 | + &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, |
| 139 | + nil, |
| 140 | + }, |
| 141 | + { |
| 142 | + "dynamic fee tx pre-London, maxFee set", |
| 143 | + false, |
| 144 | + &TransactionArgs{MaxFeePerGas: maxFee}, |
| 145 | + nil, |
| 146 | + fmt.Errorf("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"), |
| 147 | + }, |
| 148 | + { |
| 149 | + "dynamic fee tx pre-London, priorityFee set", |
| 150 | + false, |
| 151 | + &TransactionArgs{MaxPriorityFeePerGas: fortytwo}, |
| 152 | + nil, |
| 153 | + fmt.Errorf("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"), |
| 154 | + }, |
| 155 | + { |
| 156 | + "dynamic fee tx, maxFee < priorityFee", |
| 157 | + true, |
| 158 | + &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(1000))}, |
| 159 | + nil, |
| 160 | + fmt.Errorf("maxFeePerGas (0x3e) < maxPriorityFeePerGas (0x3e8)"), |
| 161 | + }, |
| 162 | + { |
| 163 | + "dynamic fee tx, maxFee < priorityFee while setting default", |
| 164 | + true, |
| 165 | + &TransactionArgs{MaxFeePerGas: (*hexutil.Big)(big.NewInt(7))}, |
| 166 | + nil, |
| 167 | + fmt.Errorf("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"), |
| 168 | + }, |
| 169 | + |
| 170 | + // Misc |
| 171 | + { |
| 172 | + "set all fee parameters", |
| 173 | + false, |
| 174 | + &TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, |
| 175 | + nil, |
| 176 | + fmt.Errorf("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), |
| 177 | + }, |
| 178 | + { |
| 179 | + "set gas price and maxPriorityFee", |
| 180 | + false, |
| 181 | + &TransactionArgs{GasPrice: fortytwo, MaxPriorityFeePerGas: fortytwo}, |
| 182 | + nil, |
| 183 | + fmt.Errorf("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), |
| 184 | + }, |
| 185 | + { |
| 186 | + "set gas price and maxFee", |
| 187 | + true, |
| 188 | + &TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee}, |
| 189 | + nil, |
| 190 | + fmt.Errorf("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), |
| 191 | + }, |
| 192 | + } |
| 193 | + |
| 194 | + ctx := context.Background() |
| 195 | + for i, test := range tests { |
| 196 | + if test.isLondon { |
| 197 | + b.activateLondon() |
| 198 | + } else { |
| 199 | + b.deactivateLondon() |
| 200 | + } |
| 201 | + got := test.in |
| 202 | + err := got.setFeeDefaults(ctx, b) |
| 203 | + if err != nil && err.Error() == test.err.Error() { |
| 204 | + // Test threw expected error. |
| 205 | + continue |
| 206 | + } else if err != nil { |
| 207 | + t.Fatalf("test %d (%s): unexpected error: %s", i, test.name, err) |
| 208 | + } |
| 209 | + if !reflect.DeepEqual(got, test.want) { |
| 210 | + t.Fatalf("test %d (%s): did not fill defaults as expected: (got: %v, want: %v)", i, test.name, got, test.want) |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +type backendMock struct { |
| 216 | + current *types.Header |
| 217 | + config *params.ChainConfig |
| 218 | +} |
| 219 | + |
| 220 | +func newBackendMock() *backendMock { |
| 221 | + config := ¶ms.ChainConfig{ |
| 222 | + ChainId: big.NewInt(42), |
| 223 | + HomesteadBlock: big.NewInt(0), |
| 224 | + DAOForkBlock: nil, |
| 225 | + DAOForkSupport: true, |
| 226 | + EIP150Block: big.NewInt(0), |
| 227 | + EIP155Block: big.NewInt(0), |
| 228 | + EIP158Block: big.NewInt(0), |
| 229 | + ByzantiumBlock: big.NewInt(0), |
| 230 | + ConstantinopleBlock: big.NewInt(0), |
| 231 | + PetersburgBlock: big.NewInt(0), |
| 232 | + IstanbulBlock: big.NewInt(0), |
| 233 | + BerlinBlock: big.NewInt(0), |
| 234 | + Eip1559Block: big.NewInt(1000), |
| 235 | + } |
| 236 | + return &backendMock{ |
| 237 | + current: &types.Header{ |
| 238 | + Difficulty: big.NewInt(10000000000), |
| 239 | + Number: big.NewInt(1100), |
| 240 | + GasLimit: 8_000_000, |
| 241 | + GasUsed: 8_000_000, |
| 242 | + Time: big.NewInt(555), |
| 243 | + Extra: make([]byte, 32), |
| 244 | + BaseFee: big.NewInt(10), |
| 245 | + }, |
| 246 | + config: config, |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +func (b *backendMock) activateLondon() { |
| 251 | + b.current.Number = big.NewInt(1100) |
| 252 | +} |
| 253 | + |
| 254 | +func (b *backendMock) deactivateLondon() { |
| 255 | + b.current.Number = big.NewInt(900) |
| 256 | +} |
| 257 | + |
| 258 | +func (b *backendMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { |
| 259 | + return big.NewInt(42), nil |
| 260 | +} |
| 261 | + |
| 262 | +func (b *backendMock) CurrentHeader() *types.Header { return b.current } |
| 263 | + |
| 264 | +func (b *backendMock) ChainConfig() *params.ChainConfig { return b.config } |
| 265 | + |
| 266 | +// Other methods needed to implement Backend interface. |
| 267 | +func (b *backendMock) SyncProgress() ethereum.SyncProgress { return ethereum.SyncProgress{} } |
| 268 | +func (b *backendMock) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { |
| 269 | + return nil, nil, nil, nil, nil |
| 270 | +} |
| 271 | +func (b *backendMock) ChainDb() ethdb.Database { return nil } |
| 272 | +func (b *backendMock) AccountManager() *accounts.Manager { return nil } |
| 273 | +func (b *backendMock) ExtRPCEnabled() bool { return false } |
| 274 | +func (b *backendMock) RPCGasCap() uint64 { return 0 } |
| 275 | +func (b *backendMock) RPCEVMTimeout() time.Duration { return time.Second } |
| 276 | +func (b *backendMock) RPCTxFeeCap() float64 { return 0 } |
| 277 | +func (b *backendMock) UnprotectedAllowed() bool { return false } |
| 278 | +func (b *backendMock) SetHead(number uint64) {} |
| 279 | + |
| 280 | +func (b *backendMock) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { |
| 281 | + return nil, nil |
| 282 | +} |
| 283 | + |
| 284 | +func (b *backendMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { |
| 285 | + return nil, nil |
| 286 | +} |
| 287 | + |
| 288 | +func (b *backendMock) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { |
| 289 | + return nil, nil |
| 290 | +} |
| 291 | + |
| 292 | +func (b *backendMock) CurrentBlock() *types.Block { return nil } |
| 293 | + |
| 294 | +func (b *backendMock) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { |
| 295 | + return nil, nil |
| 296 | +} |
| 297 | + |
| 298 | +func (b *backendMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { |
| 299 | + return nil, nil |
| 300 | +} |
| 301 | + |
| 302 | +func (b *backendMock) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { |
| 303 | + return nil, nil |
| 304 | +} |
| 305 | + |
| 306 | +func (b *backendMock) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { |
| 307 | + return nil, nil, nil |
| 308 | +} |
| 309 | + |
| 310 | +func (b *backendMock) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { |
| 311 | + return nil, nil, nil |
| 312 | +} |
| 313 | + |
| 314 | +func (b *backendMock) PendingBlockAndReceipts() (*types.Block, types.Receipts) { return nil, nil } |
| 315 | + |
| 316 | +func (b *backendMock) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { |
| 317 | + return nil, nil |
| 318 | +} |
| 319 | + |
| 320 | +func (b *backendMock) GetTd(common.Hash) *big.Int { |
| 321 | + return nil |
| 322 | +} |
| 323 | + |
| 324 | +func (b *backendMock) GetEVM(context.Context, core.Message, *state.StateDB, *tradingstate.TradingStateDB, *types.Header, *vm.Config) (*vm.EVM, func() error, error) { |
| 325 | + return nil, nil, nil |
| 326 | +} |
| 327 | + |
| 328 | +func (b *backendMock) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { return nil } |
| 329 | +func (b *backendMock) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { |
| 330 | + return nil |
| 331 | +} |
| 332 | +func (b *backendMock) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { |
| 333 | + return nil |
| 334 | +} |
| 335 | +func (b *backendMock) SendTx(ctx context.Context, signedTx *types.Transaction) error { return nil } |
| 336 | +func (b *backendMock) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { |
| 337 | + return nil, [32]byte{}, 0, 0, nil |
| 338 | +} |
| 339 | +func (b *backendMock) GetPoolTransactions() (types.Transactions, error) { return nil, nil } |
| 340 | +func (b *backendMock) GetPoolTransaction(txHash common.Hash) *types.Transaction { return nil } |
| 341 | +func (b *backendMock) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { |
| 342 | + return 0, nil |
| 343 | +} |
| 344 | +func (b *backendMock) Stats() (pending int, queued int) { return 0, 0 } |
| 345 | +func (b *backendMock) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { |
| 346 | + return nil, nil |
| 347 | +} |
| 348 | +func (b *backendMock) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) { |
| 349 | + return nil, nil |
| 350 | +} |
| 351 | +func (b *backendMock) SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription { return nil } |
| 352 | +func (b *backendMock) BloomStatus() (uint64, uint64) { return 0, 0 } |
| 353 | +func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) { |
| 354 | + return nil, nil |
| 355 | +} |
| 356 | +func (b *backendMock) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {} |
| 357 | +func (b *backendMock) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { return nil } |
| 358 | +func (b *backendMock) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { |
| 359 | + return nil |
| 360 | +} |
| 361 | +func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { |
| 362 | + return nil |
| 363 | +} |
| 364 | + |
| 365 | +func (b *backendMock) Engine() consensus.Engine { return nil } |
| 366 | + |
| 367 | +func (b *backendMock) AreTwoBlockSamePath(bh1 common.Hash, bh2 common.Hash) bool { |
| 368 | + return true |
| 369 | +} |
| 370 | + |
| 371 | +func (b *backendMock) Downloader() *downloader.Downloader { |
| 372 | + return nil |
| 373 | +} |
| 374 | + |
| 375 | +func (b *backendMock) EventMux() *event.TypeMux { |
| 376 | + return nil |
| 377 | +} |
| 378 | + |
| 379 | +func (b *backendMock) GetBlock(context.Context, common.Hash) (*types.Block, error) { |
| 380 | + return nil, nil |
| 381 | +} |
| 382 | + |
| 383 | +func (b *backendMock) GetBlocksHashCache(blockNr uint64) []common.Hash { |
| 384 | + return []common.Hash{} |
| 385 | +} |
| 386 | + |
| 387 | +func (b *backendMock) GetEngine() consensus.Engine { |
| 388 | + return nil |
| 389 | +} |
| 390 | + |
| 391 | +func (b *backendMock) GetEpochDuration() *big.Int { |
| 392 | + return nil |
| 393 | +} |
| 394 | + |
| 395 | +func (b *backendMock) GetIPCClient() (bind.ContractBackend, error) { |
| 396 | + return nil, nil |
| 397 | +} |
| 398 | + |
| 399 | +func (b *backendMock) GetMasternodesCap(uint64) map[common.Address]*big.Int { |
| 400 | + return nil |
| 401 | +} |
| 402 | + |
| 403 | +func (b *backendMock) GetOrderNonce(common.Hash) (uint64, error) { |
| 404 | + return 0, nil |
| 405 | +} |
| 406 | + |
| 407 | +func (b *backendMock) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) { |
| 408 | + return nil, nil |
| 409 | +} |
| 410 | + |
| 411 | +func (b *backendMock) GetRewardByHash(common.Hash) map[string]map[string]map[string]*big.Int { |
| 412 | + return nil |
| 413 | +} |
| 414 | + |
| 415 | +func (b *backendMock) GetVotersCap(*big.Int, common.Address, []common.Address) map[common.Address]*big.Int { |
| 416 | + return nil |
| 417 | +} |
| 418 | + |
| 419 | +func (b *backendMock) GetVotersRewards(common.Address) map[common.Address]*big.Int { |
| 420 | + return nil |
| 421 | +} |
| 422 | + |
| 423 | +func (b *backendMock) LendingService() *XDCxlending.Lending { |
| 424 | + return nil |
| 425 | +} |
| 426 | + |
| 427 | +func (b *backendMock) OrderStats() (int, int) { |
| 428 | + return 0, 0 |
| 429 | +} |
| 430 | + |
| 431 | +func (b *backendMock) OrderTxPoolContent() (map[common.Address]types.OrderTransactions, map[common.Address]types.OrderTransactions) { |
| 432 | + return nil, nil |
| 433 | +} |
| 434 | + |
| 435 | +func (b *backendMock) ProtocolVersion() int { |
| 436 | + return 0 |
| 437 | +} |
| 438 | + |
| 439 | +func (b *backendMock) SendLendingTx(context.Context, *types.LendingTransaction) error { |
| 440 | + return nil |
| 441 | +} |
| 442 | + |
| 443 | +func (b *backendMock) SendOrderTx(context.Context, *types.OrderTransaction) error { |
| 444 | + return nil |
| 445 | +} |
| 446 | + |
| 447 | +func (b *backendMock) XDCxService() *XDCx.XDCX { |
| 448 | + return nil |
| 449 | +} |
0 commit comments