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!: bank precompile #2860

Open
wants to merge 34 commits into
base: develop
Choose a base branch
from
Open

feat!: bank precompile #2860

wants to merge 34 commits into from

Conversation

fbac
Copy link
Contributor

@fbac fbac commented Sep 10, 2024

Description

Closes #2791.

Implements a bank precompile, with the following functions:

  • deposit: deposits ZRC20 as a cosmos token.
  • withdraw: withdraw ZRC20 from a cosmos token.
  • balance: reports the EOA balance for a given ZRC20 cosmos token.

WIP

  • Deposit function
  • Deposit event
  • Withdraw function
  • Withdraw event
  • BalanceOf function
  • Remove DEBUG lines
  • Full e2e coverage direct call
  • Full e2e coverage through contract
  • Unit testing
  • Deposit and Withdraw to check tokens are ZRC20

How Has This Been Tested?

  • Tested CCTX in localnet
  • Tested in development environment
  • Go unit tests
  • Go integration tests
  • Tested via GitHub Actions

Summary by CodeRabbit

  • New Features

    • Integrated a bank precompiled contract for managing ZRC20 token deposits and withdrawals.
    • Added support for a new account configuration for user banking operations in local network setups.
    • Introduced end-to-end tests for bank precompile functionality, enhancing testing coverage.
  • Bug Fixes

    • Improved logic for managing blocked addresses to allow bank precompile interactions.
  • Documentation

    • Updated changelog to reflect the addition of the bank precompiled contract.
  • Tests

    • Expanded end-to-end testing suite to include tests for bank-related functionalities.
  • Chores

    • Minor adjustments to scripts and configurations for better clarity and functionality.

Copy link
Contributor

coderabbitai bot commented Sep 10, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Walkthrough

The pull request introduces a precompiled contract for managing banking operations within a blockchain environment, allowing for the deposit and withdrawal of ZRC20 tokens as Cosmos bank tokens. It includes updates to various files, implementing functions for balance inquiries, deposits, and withdrawals. Additionally, the changes enhance error handling and logging capabilities, ensuring robust interactions with the banking contract.

Changes

Files Change Summary
Dockerfile-localnet Modified COPY commands to include trailing slashes for clarity in binary placement.
app/app.go Integrated bankprecompile package; updated stateful contract handling and blocked addresses logic.
changelog.md Added entry for the bank precompiled contract.
cmd/zetae2e/config/localnet.yml Introduced new account configuration for user_bank_precompile.
cmd/zetae2e/local/local.go Added end-to-end test case for bank precompiles.
codecov.yml Updated ignore list to exclude precompiles/bank/IBank.go from coverage reports.
contrib/localnet/orchestrator/start-zetae2e.sh Renamed user account for precompile tests and removed redundant funding lines.
contrib/localnet/scripts/start-zetacored.sh Added new account for bank precompile in genesis block configuration.
e2e/e2etests/e2etests.go Introduced new constants and E2E test for bank precompiles.
e2e/e2etests/test_precompiles_bank.go Implemented E2E test for deposit and withdrawal operations of bank precompile.
precompiles/bank/*.go Introduced various functionalities for the banking contract, including deposit, withdraw, and balance inquiry methods.
precompiles/types/errors.go Enhanced error handling with new error types and detailed messages.
precompiles/types/types.go Added ContractCaller interface for invoking contract methods.

Assessment against linked issues

Objective Addressed Explanation
Develop a precompile allowing to convert ERC20 into Cosmos bank tokens ( #2791 )

Possibly related PRs

Suggested labels

no-changelog


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>, please review it.
    -- 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@fbac fbac force-pushed the feat/bank-precompile branch 3 times, most recently from 004c3af to 3eecbbf Compare September 11, 2024 11:02
@fbac fbac force-pushed the feat/bank-precompile branch 3 times, most recently from 977f122 to 55aaf96 Compare September 11, 2024 17:41
precompiles/bank/method_balanceof.go Outdated Show resolved Hide resolved
precompiles/bank/method_deposit.go Outdated Show resolved Hide resolved
precompiles/bank/method_deposit.go Outdated Show resolved Hide resolved
precompiles/bank/method_deposit.go Outdated Show resolved Hide resolved
precompiles/bank/method_deposit.go Outdated Show resolved Hide resolved
@fbac fbac force-pushed the feat/bank-precompile branch 3 times, most recently from 4099e86 to ab771a8 Compare September 16, 2024 10:11
@fbac fbac changed the title feat: bank precompile feat!: bank precompile Sep 17, 2024
Copy link

codecov bot commented Sep 17, 2024

Codecov Report

Attention: Patch coverage is 33.17536% with 282 lines in your changes missing coverage. Please review.

Please upload report for BASE (develop@17f26e6). Learn more about missing BASE report.
Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
precompiles/bank/method_withdraw.go 0.00% 88 Missing ⚠️
precompiles/bank/method_deposit.go 34.02% 55 Missing and 9 partials ⚠️
precompiles/bank/bank.go 62.50% 28 Missing and 8 partials ⚠️
precompiles/bank/method_balance_of.go 0.00% 34 Missing ⚠️
precompiles/types/types.go 0.00% 27 Missing ⚠️
precompiles/bank/logs.go 0.00% 18 Missing ⚠️
precompiles/bank/coin.go 35.29% 9 Missing and 2 partials ⚠️
precompiles/logs/logs.go 73.33% 2 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             develop    #2860   +/-   ##
==========================================
  Coverage           ?   66.52%           
==========================================
  Files              ?      386           
  Lines              ?    21580           
  Branches           ?        0           
==========================================
  Hits               ?    14357           
  Misses             ?     6536           
  Partials           ?      687           
Files with missing lines Coverage Δ
precompiles/precompiles.go 100.00% <100.00%> (ø)
precompiles/staking/logs.go 62.26% <100.00%> (ø)
precompiles/types/errors.go 100.00% <100.00%> (ø)
precompiles/logs/logs.go 81.25% <73.33%> (ø)
precompiles/bank/coin.go 35.29% <35.29%> (ø)
precompiles/bank/logs.go 0.00% <0.00%> (ø)
precompiles/types/types.go 20.58% <0.00%> (ø)
precompiles/bank/method_balance_of.go 0.00% <0.00%> (ø)
precompiles/bank/bank.go 62.50% <62.50%> (ø)
precompiles/bank/method_deposit.go 34.02% <34.02%> (ø)
... and 1 more

Dockerfile-localnet Show resolved Hide resolved
app/app.go Outdated Show resolved Hide resolved
cmd/zetae2e/config/localnet.yml Outdated Show resolved Hide resolved
e2e/e2etests/test_precompiles_bank.go Outdated Show resolved Hide resolved
e2e/e2etests/test_precompiles_bank.go Show resolved Hide resolved
// getEVMCallerAddress returns the caller address.
// Usually the caller is the contract.CallerAddress, which is the address of the contract that called the precompiled contract.
// If contract.CallerAddress != evm.Origin is true, it means the call was made through a contract,
// on which case there is a need to set the caller to the evm.Origin.
Copy link
Contributor

Choose a reason for hiding this comment

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

could you explain why is this set like this? if some contract is calling function and you replace caller by origin, you need to authorize all operations from origin to contract, or this is not needed?

Copy link
Contributor Author

@fbac fbac Sep 17, 2024

Choose a reason for hiding this comment

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

The current design expects the original caller approves the bank contract to spend X tokens on their behalf:

  • EOA runs ZRC20.approve(bankAddress, X), so bank can spend X ZRC20 on their behalf.
  • EOA calls bank.deposit(ZRC20Addr, X) or contract.depositToBank(ZRC20Addr, X).

Open to discussions here.

precompiles/bank/bank.go Show resolved Hide resolved
precompiles/bank/coin.go Outdated Show resolved Hide resolved
precompiles/bank/logs.go Outdated Show resolved Hide resolved
}

// Get the correct caller address.
caller, err := getEVMCallerAddress(evm, contract)
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens if acc -> smart contract and that smart contract calls deposit maliciously?

in that case caller is replaced with origin here and it is executed without checking that original caller authorized smart contract to deposit for account? or that is handled somehow, if that is the case i think it would be clearer to explain in comment a bit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If an EOA calls some function in a malicious contract that tries to deposit in bank:

  • the bank will try to transfer funds from the original caller (either if it's contract.Caller or evm.Origin)
  • if the bank doesn't have allowance from the original EOA, the call fails.
  • if the bank does have allowance, the new coins are minted and ZRC20 is locked in bank
  • new cosmos coins are minted and sent to the original EOA cosmos address, not to the contracts.

Copy link
Contributor

Choose a reason for hiding this comment

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

btw, in zetaclient, we have a compliance check for restricting actions for banned addresses. How can we integrate the same approach here?

precompiles/bank/bank.go Outdated Show resolved Hide resolved
cmd/zetae2e/config/localnet.yml Outdated Show resolved Hide resolved
precompiles/bank/bank.go Outdated Show resolved Hide resolved
precompiles/bank/bank.go Outdated Show resolved Hide resolved
precompiles/bank/const.go Outdated Show resolved Hide resolved
precompiles/bank/method_withdraw.go Show resolved Hide resolved
e2e/e2etests/test_precompiles_bank.go Show resolved Hide resolved
precompiles/bank/method_deposit.go Show resolved Hide resolved
e2e/e2etests/test_precompiles_bank.go Show resolved Hide resolved
precompiles/types/types.go Show resolved Hide resolved
precompiles/types/errors_test.go Show resolved Hide resolved
precompiles/bank/logs.go Show resolved Hide resolved
}

// Get the correct caller address.
caller, err := getEVMCallerAddress(evm, contract)
Copy link
Contributor

Choose a reason for hiding this comment

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

btw, in zetaclient, we have a compliance check for restricting actions for banned addresses. How can we integrate the same approach here?

precompiles/types/errors_test.go Show resolved Hide resolved
codecov.yml Outdated Show resolved Hide resolved
@kingpinXD kingpinXD added the TSS_MIGRATION_TESTS Run TSS migration tests label Sep 20, 2024
Copy link
Member

@lumtis lumtis left a comment

Choose a reason for hiding this comment

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

This looks good to me, I will do another more in-depth view as it is a big PR, but I think we can already work on the next steps on top of this branch in any case

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

Successfully merging this pull request may close these issues.

Add a precompile allowing to map ERC20s into bank tokens
6 participants