Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wardend): burn 30% of the tx fee #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

r0t0r-r0t0r
Copy link
Contributor

@r0t0r-r0t0r r0t0r-r0t0r commented Mar 13, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced account permissions for the fee collector, allowing burn operations.
    • Introduced a new transaction handling mechanism to bypass ante operations.
    • Added functionality to burn a portion of transaction fees, improving economic model.
  • Bug Fixes

    • Improved error handling and validation for transaction processing and dependencies.
  • Tests

    • Added unit tests for the new fee burning feature and ante handler logic, ensuring robust transaction processing.
  • Chores

    • Updated module structures and interfaces to support enhanced transaction and fee management capabilities.

@r0t0r-r0t0r
Copy link
Contributor Author

This PR should be merged after warden-protocol/snapshots#1

@r0t0r-r0t0r r0t0r-r0t0r marked this pull request as ready for review March 18, 2024 07:20
SignModeHandler: in.TxConfig.SignModeHandler(),
FeegrantKeeper: in.FeeGrantKeeper,
SigGasConsumer: authante.DefaultSigVerificationGasConsumer,
FeeBurnRatio: math.LegacyNewDecWithPrec(3, 1), // 30%
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if we can get this number from the modules Params structure? This way it can get updated via governance proposals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we definitely can. Good point, I'll implement it.

@Pitasi
Copy link
Contributor

Pitasi commented Mar 18, 2024

Did you consider implementing this feature as a BeginBlocker instead of an AnteHandler? Do you know if there are limitations to one or the other possible implementations?

Copy link
Contributor

@Pitasi Pitasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider implementing this feature as a BeginBlocker instead of an AnteHandler? Do you know if there are limitations to one or the other possible implementations?

(marking this PR as changes requested so it can be filtered)

@Pitasi Pitasi added the blocked label Aug 13, 2024
Copy link
Contributor

coderabbitai bot commented Aug 13, 2024

Walkthrough

The changes introduce significant enhancements to the Warden protocol's transaction processing capabilities, focusing on account permissions, fee management, and the ante handler's structure. Notably, new features allow for fee burning and improved transaction handling logic, while unit tests and mock implementations bolster the testing framework. Overall, these modifications enhance security, efficiency, and flexibility within the application.

Changes

Files Change Summary
warden/app/app_config.go Updated account permissions for FeeCollectorName and altered transaction handling with SkipAnteHandler.
warden/x/warden/ante/*.go Introduced AnteHandler and BurnFeeDecorator for enhanced transaction processing and fee management.
warden/x/warden/testutil/*.go Added tests for BurnFeeDecorator and utility functions for setting up test environments.
warden/x/warden/module/module.go Enhanced module structure with new transaction configuration, error handling, and gRPC route management.
warden/x/warden/types/expected_keepers.go Added methods to AccountKeeper and BankKeeper, introducing a new FeegrantKeeper interface.
warden/x/warden/testutil/expected_keepers_mocks.go Created mock implementations for various keepers to facilitate testing.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AnteHandler
    participant BankKeeper
    participant FeeCollector
    participant MockTest

    User->>AnteHandler: Submit Transaction
    AnteHandler->>BankKeeper: Validate and Deduct Fees
    BankKeeper->>FeeCollector: Burn Portion of Fees
    FeeCollector-->>BankKeeper: Confirm Burn
    BankKeeper-->>AnteHandler: Fees Processed
    AnteHandler-->>User: Transaction Complete
Loading

🐇 In the realm of code, where changes delight,
New fees will burn, making things right.
With tests now in place, our paths intertwine,
Through modules and handlers, our code will shine!
🪄 Let’s hop to the future, with joy we’ll align!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c04bb16 and 205fb04.

Files selected for processing (8)
  • warden/app/app_config.go (2 hunks)
  • warden/x/warden/ante/ante.go (1 hunks)
  • warden/x/warden/ante/burn_fee.go (1 hunks)
  • warden/x/warden/ante/burn_fee_test.go (1 hunks)
  • warden/x/warden/ante/testutil_test.go (1 hunks)
  • warden/x/warden/module/module.go (5 hunks)
  • warden/x/warden/testutil/expected_keepers_mocks.go (1 hunks)
  • warden/x/warden/types/expected_keepers.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • warden/x/warden/testutil/expected_keepers_mocks.go
Additional context used
golangci-lint
warden/x/warden/ante/testutil_test.go

120-120: Error return value of acc.SetAccountNumber is not checked

(errcheck)


130-130: field desc is unused

(unused)


131-131: field malleate is unused

(unused)

Additional comments not posted (24)
warden/x/warden/ante/burn_fee.go (2)

16-19: Struct definition is appropriate.

The BurnFeeDecorator struct is well-defined with necessary fields for fee burning functionality.


21-26: Constructor is correctly implemented.

The NewBurnFeeDecorator function initializes the BurnFeeDecorator with the necessary dependencies.

warden/x/warden/ante/burn_fee_test.go (1)

19-46: Test implementation is comprehensive.

The TestDeductFees function effectively tests the BurnFeeDecorator logic using mocks and assertions.

warden/x/warden/types/expected_keepers.go (3)

16-20: Interface enhancements are appropriate.

The additions to the AccountKeeper interface provide necessary functionality for account and module management.


25-29: Interface enhancements are appropriate.

The additions to the BankKeeper interface support advanced financial operations, such as coin burning and transfers.


31-33: New interface addition is valuable.

The introduction of the FeegrantKeeper interface facilitates efficient fee management and transaction handling.

warden/x/warden/ante/ante.go (3)

3-15: Imports are well-organized and necessary.

The imports are correctly organized and include all necessary packages for the functionality provided in this file.


17-26: HandlerOptions struct is well-defined.

The HandlerOptions struct includes all necessary fields for configuring the AnteHandler, including the new FeeBurnRatio.


28-61: NewAnteHandler function is well-structured.

The function constructs an AnteHandler with appropriate decorators and error handling for missing dependencies. The inclusion of BurnFeeDecorator aligns with the PR's objective to burn transaction fees.

warden/x/warden/module/module.go (6)

Line range hint 5-28: Imports are necessary and well-organized.

The new imports are necessary for the functionalities introduced, such as transaction handling and ante handler setup.


89-92: Updated gRPC gateway handling mechanism.

The change from runtime.ServeMux to gwruntime.ServeMux likely reflects an update for compatibility or improved functionality.


188-192: ModuleInputs struct enhancement with TxConfig.

The addition of TxConfig is essential for transaction configuration, aligning with the changes in transaction handling.


198-201: ModuleOutputs struct enhancement with BaseAppOption.

The inclusion of BaseAppOption allows for custom ante processing logic, enhancing module flexibility.


203-234: newAnteHandler function is robust and well-implemented.

The function ensures essential dependencies are present before creating the ante handler, improving error handling and robustness.


Line range hint 234-267: ProvideModule function integrates custom ante processing logic.

The inclusion of a base application option that sets the ante handler allows for a more integrated approach to handling transactions.

warden/x/warden/ante/testutil_test.go (7)

3-37: Imports are appropriate for test utilities.

The imports include necessary testing packages and mock utilities, which are appropriate for the test utilities provided in this file.


39-43: TestAccount struct is straightforward and useful.

The TestAccount struct is useful for managing test accounts in a straightforward manner.


45-56: AnteTestSuite struct is well-structured for testing.

The struct provides all necessary components for comprehensive ante handler testing.


58-112: SetupTestSuite function ensures proper test environment setup.

The function is well-implemented, providing a proper test environment setup with mock controllers and context.


157-170: DeliverMsgs function is well-implemented for testing.

The function provides a clear way to construct and deliver transactions through the ante handler for testing purposes.


172-206: RunTestCase function is comprehensive for executing tests.

The function ensures that test cases are executed with appropriate checks for expected errors and outcomes.


208-259: CreateTestTx function is well-implemented for creating test transactions.

The function provides necessary functionality for creating test transactions, handling signatures and encoding effectively.

warden/app/app_config.go (2)

260-262: Flag potential implications of skipping the ante handler.

Setting SkipAnteHandler: true allows transactions to bypass the ante handler, which may affect fee deduction, signature verification, and other pre-processing tasks. Ensure that this change does not compromise transaction security or integrity.


175-175: Approve the addition of burner permission to the fee collector account.

The addition of Burner permission to the FeeCollectorName account aligns with the objective to burn transaction fees. Ensure that this change does not conflict with existing permissions or operations.

Verification successful

Verify compatibility of Burner permission with existing operations.

The addition of the Burner permission to the FeeCollectorName account appears consistent with its usage in burning operations. Ensure that no other permissions or operations are affected by this change.

  • Usage in burn_fee.go: Confirms the intent to burn coins.
  • Tests in burn_fee_test.go: Validate the burning functionality.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of the added Burner permission to the FeeCollectorName account.

# Test: Search for usage of FeeCollectorName in the codebase to ensure compatibility with the new permission.
rg --type go 'FeeCollectorName'

Length of output: 554

Comment on lines +28 to +46
func (bfd BurnFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

// Assume that default sdk FeeChecker is used. It follows that actual fee is always equal to feeTx.GetFee()
fee := feeTx.GetFee()

burnAmount, _ := sdk.NewDecCoinsFromCoins(fee...).MulDec(bfd.burnRatio).TruncateDecimal()

if !burnAmount.IsZero() {
err := bfd.bankKeeper.BurnCoins(ctx, authtypes.FeeCollectorName, burnAmount)
if err != nil {
return ctx, errorsmod.Wrap(sdkerrors.ErrLogic, "Failed to burn fee")
}
}

return next(ctx, tx, simulate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance error message for BurnCoins failure.

The error message when burning coins fails could be more descriptive to aid in debugging.

- return ctx, errorsmod.Wrap(sdkerrors.ErrLogic, "Failed to burn fee")
+ return ctx, errorsmod.Wrapf(sdkerrors.ErrLogic, "Failed to burn fee: %v", err)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (bfd BurnFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}
// Assume that default sdk FeeChecker is used. It follows that actual fee is always equal to feeTx.GetFee()
fee := feeTx.GetFee()
burnAmount, _ := sdk.NewDecCoinsFromCoins(fee...).MulDec(bfd.burnRatio).TruncateDecimal()
if !burnAmount.IsZero() {
err := bfd.bankKeeper.BurnCoins(ctx, authtypes.FeeCollectorName, burnAmount)
if err != nil {
return ctx, errorsmod.Wrap(sdkerrors.ErrLogic, "Failed to burn fee")
}
}
return next(ctx, tx, simulate)
func (bfd BurnFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}
// Assume that default sdk FeeChecker is used. It follows that actual fee is always equal to feeTx.GetFee()
fee := feeTx.GetFee()
burnAmount, _ := sdk.NewDecCoinsFromCoins(fee...).MulDec(bfd.burnRatio).TruncateDecimal()
if !burnAmount.IsZero() {
err := bfd.bankKeeper.BurnCoins(ctx, authtypes.FeeCollectorName, burnAmount)
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrLogic, "Failed to burn fee: %v", err)
}
}
return next(ctx, tx, simulate)

Comment on lines +128 to +135
// TestCase represents a test case used in test tables.
type TestCase struct {
desc string
malleate func(*AnteTestSuite) TestCaseArgs
simulate bool
expPass bool
expErr error
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address unused fields in TestCase struct.

The fields desc and malleate are unused. Consider utilizing them or removing them if they are unnecessary.

Tools
golangci-lint

130-130: field desc is unused

(unused)


131-131: field malleate is unused

(unused)

Comment on lines +114 to +126
func (suite *AnteTestSuite) CreateTestAccounts(numAccs int) []TestAccount {
var accounts []TestAccount

for i := 0; i < numAccs; i++ {
priv, _, addr := testdata.KeyTestPubAddr()
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr)
acc.SetAccountNumber(uint64(i + 1000))
suite.accountKeeper.SetAccount(suite.ctx, acc)
accounts = append(accounts, TestAccount{acc, priv})
}

return accounts
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the error return value of SetAccountNumber.

The error return value of SetAccountNumber should be checked to ensure that account setup is successful.

-  acc.SetAccountNumber(uint64(i + 1000))
+  err := acc.SetAccountNumber(uint64(i + 1000))
+  require.NoError(t, err)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (suite *AnteTestSuite) CreateTestAccounts(numAccs int) []TestAccount {
var accounts []TestAccount
for i := 0; i < numAccs; i++ {
priv, _, addr := testdata.KeyTestPubAddr()
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr)
acc.SetAccountNumber(uint64(i + 1000))
suite.accountKeeper.SetAccount(suite.ctx, acc)
accounts = append(accounts, TestAccount{acc, priv})
}
return accounts
}
func (suite *AnteTestSuite) CreateTestAccounts(numAccs int) []TestAccount {
var accounts []TestAccount
for i := 0; i < numAccs; i++ {
priv, _, addr := testdata.KeyTestPubAddr()
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr)
err := acc.SetAccountNumber(uint64(i + 1000))
require.NoError(t, err)
suite.accountKeeper.SetAccount(suite.ctx, acc)
accounts = append(accounts, TestAccount{acc, priv})
}
return accounts
}
Tools
golangci-lint

120-120: Error return value of acc.SetAccountNumber is not checked

(errcheck)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants