From 51b4d8e625ee5d2059b32c0dfc1944db4f03f3a7 Mon Sep 17 00:00:00 2001 From: Maximilian Langenfeld <15726643+ezdac@users.noreply.github.com> Date: Wed, 8 May 2024 12:29:04 +0200 Subject: [PATCH] Add tests for CIP-64/66 compatible TransactionArgs --- internal/ethapi/transaction_args_test.go | 73 ++++++++++++++++++++---- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/internal/ethapi/transaction_args_test.go b/internal/ethapi/transaction_args_test.go index 7caf32bb07..679e33917f 100644 --- a/internal/ethapi/transaction_args_test.go +++ b/internal/ethapi/transaction_args_test.go @@ -51,11 +51,14 @@ func TestSetFeeDefaults(t *testing.T) { } var ( - b = newCeloBackendMock() - zero = (*hexutil.Big)(big.NewInt(0)) - fortytwo = (*hexutil.Big)(big.NewInt(42)) - maxFee = (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(b.current.BaseFee, big.NewInt(2)), fortytwo.ToInt())) - al = &types.AccessList{types.AccessTuple{Address: common.Address{0xaa}, StorageKeys: []common.Hash{{0x01}}}} + b = newCeloBackendMock() + zero = (*hexutil.Big)(big.NewInt(0)) + fortytwo = (*hexutil.Big)(big.NewInt(42)) + maxFee = (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(b.current.BaseFee, big.NewInt(2)), fortytwo.ToInt())) + al = &types.AccessList{types.AccessTuple{Address: common.Address{0xaa}, StorageKeys: []common.Hash{{0x01}}}} + feeCurrency = common.BigToAddress(big.NewInt(42)) + eightyfour = (*hexutil.Big)(big.NewInt(84)) + doubleMaxFee = (*hexutil.Big)(new(big.Int).Mul(maxFee.ToInt(), big.NewInt(2))) ) tests := []test{ @@ -235,6 +238,38 @@ func TestSetFeeDefaults(t *testing.T) { &TransactionArgs{BlobHashes: []common.Hash{}, BlobFeeCap: (*hexutil.Big)(big.NewInt(4)), MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, nil, }, + + // CIP-64 + { + "Fee-currency denominated tx, set maxPriorityFeePerGas in converted valued", + "cancun", + &TransactionArgs{MaxFeePerGas: doubleMaxFee, FeeCurrency: &feeCurrency}, + // maxPriorityFeePerGas is double in feeCurrency + &TransactionArgs{MaxFeePerGas: doubleMaxFee, MaxPriorityFeePerGas: eightyfour, FeeCurrency: &feeCurrency}, + nil, + }, + { + "Fee-currency denominated tx, set maxFeePerGas in converted valued", + "cancun", + &TransactionArgs{MaxPriorityFeePerGas: eightyfour, FeeCurrency: &feeCurrency}, + &TransactionArgs{MaxFeePerGas: doubleMaxFee, MaxPriorityFeePerGas: eightyfour, FeeCurrency: &feeCurrency}, + nil, + }, + // CIP-66 + { + "CIP-66 transaction, maxPriorityFeePerGas gets set in non-converted value", + "cancun", + &TransactionArgs{MaxFeePerGas: maxFee, MaxFeeInFeeCurrency: fortytwo, FeeCurrency: &feeCurrency}, + &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo, MaxFeeInFeeCurrency: fortytwo, FeeCurrency: &feeCurrency}, + nil, + }, + { + "set maxFeeInFeeCurrency without feeCurrency", + "cancun", + &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo, MaxFeeInFeeCurrency: fortytwo}, + nil, + errors.New("feeCurrency must be set when maxFeeInFeeCurrency is given"), + }, } ctx := context.Background() @@ -267,24 +302,28 @@ func newCeloBackendMock() *celoBackendMock { } func (c *celoBackendMock) GetFeeBalance(ctx context.Context, atBlock common.Hash, account common.Address, feeCurrency *common.Address) (*big.Int, error) { - // Celo specific backend features are currently not tested + // This Celo specific backend features are currently not tested return nil, errCeloNotImplemented } func (c *celoBackendMock) GetExchangeRates(ctx context.Context, atBlock common.Hash) (common.ExchangeRates, error) { var er common.ExchangeRates - // Celo specific backend features are currently not tested + // This Celo specific backend features are currently not tested return er, errCeloNotImplemented } func (c *celoBackendMock) ConvertToCurrency(ctx context.Context, atBlock common.Hash, value *big.Int, fromFeeCurrency *common.Address) (*big.Int, error) { - // Celo specific backend features are currently not tested - return nil, errCeloNotImplemented + if fromFeeCurrency == nil { + return value, nil + } + return new(big.Int).Mul(value, big.NewInt(2)), nil } func (c *celoBackendMock) ConvertToGold(ctx context.Context, atBlock common.Hash, value *big.Int, toFeeCurrency *common.Address) (*big.Int, error) { - // Celo specific backend features are currently not tested - return nil, errCeloNotImplemented + if toFeeCurrency == nil { + return value, nil + } + return new(big.Int).Div(value, big.NewInt(2)), nil } type backendMock struct { @@ -352,6 +391,7 @@ func (b *backendMock) ChainConfig() *params.ChainConfig { return b.config } // Other methods needed to implement Backend interface. func (b *backendMock) SyncProgress() ethereum.SyncProgress { return ethereum.SyncProgress{} } + func (b *backendMock) FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { return nil, nil, nil, nil, nil } @@ -366,9 +406,11 @@ func (b *backendMock) SetHead(number uint64) {} func (b *backendMock) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { return nil, nil } + func (b *backendMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { return nil, nil } + func (b *backendMock) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { return nil, nil } @@ -376,18 +418,23 @@ func (b *backendMock) CurrentBlock() *types.Header { return nil } func (b *backendMock) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { return nil, nil } + func (b *backendMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { return nil, nil } + func (b *backendMock) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { return nil, nil } + func (b *backendMock) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) { return nil, nil } + func (b *backendMock) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { return nil, nil, nil } + func (b *backendMock) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { return nil, nil, nil } @@ -395,6 +442,7 @@ func (b *backendMock) PendingBlockAndReceipts() (*types.Block, types.Receipts) { func (b *backendMock) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { return nil, nil } + func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) { return nil, nil } @@ -406,6 +454,7 @@ func (b *backendMock) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subsc func (b *backendMock) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { return nil } + func (b *backendMock) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { return nil } @@ -422,6 +471,7 @@ func (b *backendMock) Stats() (pending int, queued int) { return 0, 0 } func (b *backendMock) TxPoolContent() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction) { return nil, nil } + func (b *backendMock) TxPoolContentFrom(addr common.Address) ([]*types.Transaction, []*types.Transaction) { return nil, nil } @@ -432,6 +482,7 @@ func (b *backendMock) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscript func (b *backendMock) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { return nil } + func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { return nil }