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: use abi encoding of ICS20Transfer payload #117

Open
wants to merge 28 commits into
base: main
Choose a base branch
from

Conversation

gjermundgaraba
Copy link
Contributor

@gjermundgaraba gjermundgaraba commented Nov 24, 2024

Description

closes: #116

Based off of the work done in: #102


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Linked to GitHub issue with discussion and accepted design, OR link to spec that describes this work.
  • Wrote unit and integration tests.
  • Added relevant natspec and godoc comments.
  • Provide a conventional commit message to follow the repository standards.
  • Re-reviewed Files changed in the GitHub PR explorer.
  • Review SonarCloud Report in the comment section below once CI passes.

@gjermundgaraba gjermundgaraba linked an issue Nov 24, 2024 that may be closed by this pull request
Copy link

codecov bot commented Nov 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.89%. Comparing base (d952d31) to head (2efc62f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #117      +/-   ##
==========================================
- Coverage   94.10%   93.89%   -0.21%     
==========================================
  Files           9        9              
  Lines         339      311      -28     
==========================================
- Hits          319      292      -27     
+ Misses         20       19       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -0,0 +1,42 @@
module github.com/cosmos/solidity-ibc-eureka/abigen

go 1.22.7
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the setup as the e2e tests. But maybe we should just fully update it to go 1.23 soon...

Copy link
Member

Choose a reason for hiding this comment

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

Feel free to open a deps(e2e) pr anytime, and I'll approve fast :)

Repository: "ghcr.io/cosmos/ibc-go-wasm-simd", // FOR LOCAL IMAGE USE: Docker Image Name
Version: "feat-ibc-eureka", // FOR LOCAL IMAGE USE: Docker Image Tag
Repository: "ghcr.io/cosmos/ibc-go-wasm-simd", // FOR LOCAL IMAGE USE: Docker Image Name
Version: "gjermund-7591-ics20-abi-encoding", // FOR LOCAL IMAGE USE: Docker Image Tag
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one can only be updated once cosmos/ibc-go#7592 is merged. We could wait, but I'd rather just update this in a PR so we're not blocked on ibc-go reviewing which might take time.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, should it be merged soon ahead of the security audit?

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, I would think so.

@@ -39,12 +44,14 @@ generate-abi:
jq '.abi' out/ERC20.sol/ERC20.json > abi/ERC20.json
jq '.abi' out/IBCERC20.sol/IBCERC20.json > abi/IBCERC20.json
jq '.abi' out/IBCStore.sol/IBCStore.json > abi/IBCStore.json
abigen --abi abi/ICS02Client.json --pkg ics02client --type Contract --out e2e/interchaintestv8/types/ics02client/contract.go
Copy link
Contributor Author

@gjermundgaraba gjermundgaraba Nov 24, 2024

Choose a reason for hiding this comment

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

Mostly just different ordering to have some level of separation between types generated for e2e tests only and the ones that should be public in abigen/.

That, and adding ICS20Lib.

Copy link
Member

Choose a reason for hiding this comment

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

We should also remove the files from types that have been moved to abigen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was done already

@gjermundgaraba gjermundgaraba marked this pull request as ready for review November 24, 2024 18:54
@gjermundgaraba
Copy link
Contributor Author

A quick comment on the test coverage. I believe it went down a little bit because some of the old parsing logic was well tested... I looked over the main parts of the ICS20Lib that has some branches that are not unit tested right now, and it does not seem worth spending time on them, as they will all be replaced by new functions from OpenZeppelin in their next release (which should be early next year it looks like).

Copy link
Member

@srdtrk srdtrk left a comment

Choose a reason for hiding this comment

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

Lgtm. I left a couple comments

.github/workflows/foundry.yml Outdated Show resolved Hide resolved
README.md Outdated
Comment on lines 147 to 149
| `multicall/recvPacket` | Receiving _back_ an `ERC20` token. | ~228,000 | ~220,000 |
| `multicall/ackPacket` | Acknowledging an ICS20 packet. | ~141,000 | ~135,000 |
| `multicall/recvPacket` | Receiving _back_ an `ERC20` token. | ~208,000 | ~201,000 |
| `multicall/ackPacket` | Acknowledging an ICS20 packet. | ~121,000 | ~115,000 |
Copy link
Member

Choose a reason for hiding this comment

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

🚀

@@ -0,0 +1,42 @@
module github.com/cosmos/solidity-ibc-eureka/abigen

go 1.22.7
Copy link
Member

Choose a reason for hiding this comment

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

Feel free to open a deps(e2e) pr anytime, and I'll approve fast :)

Repository: "ghcr.io/cosmos/ibc-go-wasm-simd", // FOR LOCAL IMAGE USE: Docker Image Name
Version: "feat-ibc-eureka", // FOR LOCAL IMAGE USE: Docker Image Tag
Repository: "ghcr.io/cosmos/ibc-go-wasm-simd", // FOR LOCAL IMAGE USE: Docker Image Name
Version: "gjermund-7591-ics20-abi-encoding", // FOR LOCAL IMAGE USE: Docker Image Tag
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, should it be merged soon ahead of the security audit?

e2e/interchaintestv8/go.mod Outdated Show resolved Hide resolved
Comment on lines 50 to 118
/**
* @notice Encodes a `FungibleTokenPacketData` struct into ABI-encoded bytes.
*
* @dev This function uses `abi.encode` to convert a `FungibleTokenPacketData` struct into its ABI-encoded
* `bytes` representation. The resulting bytes can be transmitted or stored for decoding later.
*
* @param payload The `FungibleTokenPacketData` struct to encode.
* @return Encoded `bytes` representation of the input struct.
*
* @dev What this function does:
* - Converts the `FungibleTokenPacketData` struct into a `bytes` array using ABI encoding.
* - Ensures the resulting bytes are compatible with `abi.decode` for the same struct.
* - Preserves the field order and data structure of the input struct during encoding.
*
* @dev What this function does NOT do:
* - It does not validate the content of the `FungibleTokenPacketData` struct before encoding.
* For example:
* - Does not check if `amount` is greater than 0.
* - Does not verify that `receiver` or `sender` are valid addresses or non-empty strings.
* - Does not validate the format or expected content of `denom` or `memo`.
* - It does not ensure compatibility with external systems unless they adhere to the same ABI encoding rules.
*
* @dev Recommended validation to avoid issues:
* - Validate the fields of the `FungibleTokenPacketData` struct before calling this function:
* - Ensure `amount > 0`.
* - Check that `receiver` and `sender` are non-empty and, if required, valid address strings.
* - Validate `denom` against expected formats or whitelisted values, if applicable.
* - Optionally validate `memo` for length or allowed characters.
* - Ensure that the consumer of the encoded bytes uses the same ABI decoding standard.
*/
function encodePayload(FungibleTokenPacketData memory payload) internal pure returns (bytes memory) {
return abi.encode(payload);
}

/**
* @notice Decodes ABI-encoded bytes into a `FungibleTokenPacketData` struct.
*
* @dev This function uses `abi.decode` to decode a `bytes` payload into a `FungibleTokenPacketData` struct.
* It assumes that the input data is correctly ABI-encoded and matches the structure of `FungibleTokenPacketData`.
*
* @param data ABI-encoded bytes representing a `FungibleTokenPacketData`.
* @return Decoded `FungibleTokenPacketData` struct.
*
* @dev What this function does:
* - Decodes the `bytes` payload into the expected `FungibleTokenPacketData` struct.
* - Ensures that the payload conforms to the ABI encoding of `FungibleTokenPacketData` (field types and order).
* - Reverts if the input data is not properly ABI-encoded.
*
* @dev What this function does NOT do:
* - Validate the logical correctness or semantic meaning of the decoded fields.
* For example:
* - Does not check if `amount` is greater than 0.
* - Does not verify that `receiver` or `sender` are valid addresses or non-empty strings.
* - Does not validate the format, length, or expected content of `denom` or `memo`.
* - Does not validate whether the payload matches a specific JSON schema or key order.
*
* @dev Recommended validation to avoid exploits:
* - After decoding, validate each field of the struct:
* - Ensure `amount > 0`.
* - Check that `receiver` and `sender` are non-empty and, if required, valid address strings.
* - Validate `denom` against expected formats or whitelisted values, if applicable.
* - Optionally validate `memo` for length or allowed characters.
* - Implement JSON key-order validation if strict ordering is required.
* - Consider using a try/catch block for decoding, or handle decoding errors explicitly to ensure
* the function does not fail silently or revert without providing clear error messages.
*/
function decodePayload(bytes memory data) external pure returns (FungibleTokenPacketData memory) {
return abi.decode(data, (FungibleTokenPacketData));
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need these functions given that they don't perform any validation. I also don't support making these functions external to be used in e2e tests. We should keep the number of external functions as small as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is fair actually. I'll remove them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like this actually saved us some gas, so updated the benchmarks again :)

@@ -39,12 +44,14 @@ generate-abi:
jq '.abi' out/ERC20.sol/ERC20.json > abi/ERC20.json
jq '.abi' out/IBCERC20.sol/IBCERC20.json > abi/IBCERC20.json
jq '.abi' out/IBCStore.sol/IBCStore.json > abi/IBCStore.json
abigen --abi abi/ICS02Client.json --pkg ics02client --type Contract --out e2e/interchaintestv8/types/ics02client/contract.go
Copy link
Member

Choose a reason for hiding this comment

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

We should also remove the files from types that have been moved to abigen.

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

Successfully merging this pull request may close these issues.

Use ABI encoding in ICS20Transfer (solidity)
3 participants