Skip to content

Commit

Permalink
Add tests for CIP-64/66 compatible TransactionArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ezdac committed May 8, 2024
1 parent 08dd9c9 commit 51b4d8e
Showing 1 changed file with 62 additions and 11 deletions.
73 changes: 62 additions & 11 deletions internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -366,35 +406,43 @@ 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
}
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
}
func (b *backendMock) PendingBlockAndReceipts() (*types.Block, types.Receipts) { return nil, nil }
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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 51b4d8e

Please sign in to comment.