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(evm): prevent funtoken from erc20 contract to be created with non-complete contracts #2150

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
14 changes: 8 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2145](https://github.com/NibiruChain/nibiru/pull/2145) - chore(token-registry): add xNIBI Astrovault LST to registry
- [#2147](https://github.com/NibiruChain/nibiru/pull/2147) - fix(simapp): manually add x/vesting Cosmos-SDK module types to the codec in simulation tests since they are expected by default
- [#2149](https://github.com/NibiruChain/nibiru/pull/2149) - feat(evm-oracle):
add Solidity contract that we can use to expose the Nibiru Oracle in the
ChainLink interface. Publish all precompiled contracts and ABIs on npm under
the `@nibiruchain/solidity` package.
add Solidity contract that we can use to expose the Nibiru Oracle in the
ChainLink interface. Publish all precompiled contracts and ABIs on npm under
the `@nibiruchain/solidity` package.
- [#2151](https://github.com/NibiruChain/nibiru/pull/2151) - feat(evm): randao support for evm
- [#2152](https://github.com/NibiruChain/nibiru/pull/2152) - fix(precompile): consume gas for precompile calls regardless of error
- [#2154](https://github.com/NibiruChain/nibiru/pull/2154) - fix(evm):
JSON encoding for the `EIP55Addr` struct was not following the Go conventions and
needed to include double quotes around the hexadecimal string.
JSON encoding for the `EIP55Addr` struct was not following the Go conventions and
needed to include double quotes around the hexadecimal string.
- [#2156](https://github.com/NibiruChain/nibiru/pull/2156) - test(evm-e2e): add E2E test using the Nibiru Oracle's ChainLink impl
- [#2157](https://github.com/NibiruChain/nibiru/pull/2157) - fix(evm): Fix unit inconsistency related to AuthInfo.Fee and txData.Fee using effective fee
- [#2159](https://github.com/NibiruChain/nibiru/pull/2159) - chore(evm): Augment the Wasm msg handler so that wasm contracts cannot send MsgEthereumTx
Expand All @@ -93,7 +93,9 @@ needed to include double quotes around the hexadecimal string.
- [#2180](https://github.com/NibiruChain/nibiru/pull/2180) - fix(evm): apply gas consumption across the entire EVM codebase at `CallContractWithInput`
- [#2183](https://github.com/NibiruChain/nibiru/pull/2183) - fix(evm): bank keeper extension gas meter type
- [#2184](https://github.com/NibiruChain/nibiru/pull/2184) - test(evm): e2e tests configuration enhancements
-
- [#2150](https://github.com/NibiruChain/nibiru/pull/2150) - feat(evm): prevent funtoken from erc20 contract to be created with non-complete contracts
-

#### Nibiru EVM | Before Audit 2 - 2024-12-06

The codebase went through a third-party [Code4rena
Expand Down
2 changes: 1 addition & 1 deletion eth/rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func SendTransaction(s *BackendSuite, tx *gethcore.LegacyTx, waitForNextBlock bo

// WaitForReceipt waits for a transaction to be included in a block, returns block number, block hash and receipt
func WaitForReceipt(s *BackendSuite, txHash gethcommon.Hash) (*big.Int, *gethcommon.Hash, *backend.TransactionReceipt) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

for {
Expand Down
4 changes: 2 additions & 2 deletions eth/rpc/backend/gas_used_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *BackendSuite) TestGasUsedFunTokens() {
FromErc20: &erc20Addr,
})
s.Require().NoError(err)
s.Require().NotNil(txResp)
s.Require().NotNil(txResp) // This is no guarantee that tx was included in the block
s.Require().NoError(s.network.WaitForNextBlock())

randomNibiAddress := testutil.AccAddress()
Expand All @@ -103,7 +103,7 @@ func (s *BackendSuite) TestGasUsedFunTokens() {
Nonce: nonce,
To: &precompile.PrecompileAddr_FunToken,
Data: packedArgsPass,
Gas: 1_500_000,
Gas: 3_500_000,
GasPrice: big.NewInt(1),
},
false,
Expand Down
2 changes: 1 addition & 1 deletion eth/rpc/backend/tx_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *BackendSuite) TestLogs() {
Nonce: s.getCurrentNonce(s.fundedAccEthAddr),
To: &precompile.PrecompileAddr_FunToken,
Data: packedArgsPass,
Gas: 1_500_000,
Gas: 2_500_000,
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Standardize gas limit to 1,500,000 for consistency

The gas limit of 2,500,000 is inconsistent with similar operations in the codebase. Other token operations in the test files consistently use 1,500,000 gas, including:

  • eth/rpc/backend/gas_used_test.go
  • eth/rpc/backend/backend_suite_test.go

Consider reducing the gas limit to 1,500,000 to maintain consistency across the test suite.

🔗 Analysis chain

Maintain consistent gas limits across similar operations.

The gas limit of 2,500,000 for sending ERC20 tokens to coin seems inconsistent with similar operations in other tests. Consider standardizing gas limits across similar operations to ensure consistent testing and better detection of gas usage regressions.

Let's verify gas usage patterns for similar operations:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for similar ERC20 token operations and their gas limits
rg -A 5 "PrecompileAddr_FunToken.*Gas:" .

Length of output: 43


Script:

#!/bin/bash
# Search for gas limit patterns in test files
rg -A 2 "Gas:\s+\d" --type go --glob "*_test.go"

# Search specifically in the eth/rpc/backend directory for context
rg -A 5 "Gas:" eth/rpc/backend/

Length of output: 10817

GasPrice: big.NewInt(1),
},
false,
Expand Down
17 changes: 10 additions & 7 deletions evm-e2e/contracts/TestERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20} from '@openzeppelin/contracts/token/ERC20/ERC20.sol';

contract TestERC20 is ERC20 {
// Define the supply of TestERC20: 1,000,000
uint256 constant initialSupply = 1000000 * (10 ** 18);

// Define the supply of TestERC20: 1,000,000
uint256 constant initialSupply = 1000000 * (10**18);
// Constructor will be called on contract creation
constructor() ERC20('TestERC20', 'FOO') {
_mint(msg.sender, initialSupply);
}

// Constructor will be called on contract creation
constructor() ERC20("TestERC20", "FOO") {
_mint(msg.sender, initialSupply);
}
function mint(address to, uint256 amount) public {
_mint(to, amount);
}
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add access control to mint function

The mint function is public without any access restrictions, which could allow anyone to create unlimited tokens. Consider adding the OpenZeppelin Ownable contract and restricting minting to the contract owner.

+import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol';

-contract TestERC20 is ERC20 {
+contract TestERC20 is ERC20, Ownable {
   // ... other code ...

-  function mint(address to, uint256 amount) public {
+  function mint(address to, uint256 amount) public onlyOwner {
     _mint(to, amount);
   }

Committable suggestion skipped: line range outside the PR's diff.

}
4 changes: 2 additions & 2 deletions x/common/testutil/testnetwork/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func (chain *Network) BroadcastMsgs(
return nil, err
}

txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, math.NewInt(1000))))
txBuilder.SetGasLimit(uint64(10 * common.TO_MICRO))
txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, math.NewInt(100_000))))
txBuilder.SetGasLimit(uint64(1_000 * common.TO_MICRO))

acc, err := cfg.AccountRetriever.GetAccount(chain.Validators[0].ClientCtx, from)
if err != nil {
Expand Down
88 changes: 88 additions & 0 deletions x/evm/embeds/abi/DSAuth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[
{
"constant": false,
"inputs": [
{
"name": "owner_",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "authority_",
"type": "address"
}
],
"name": "setAuthority",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "authority",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "authority",
"type": "address"
}
],
"name": "LogSetAuthority",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
}
],
"name": "LogSetOwner",
"type": "event"
}
]
26 changes: 26 additions & 0 deletions x/evm/embeds/abi/DSAuthEvents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "authority",
"type": "address"
}
],
"name": "LogSetAuthority",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
}
],
"name": "LogSetOwner",
"type": "event"
}
]
29 changes: 29 additions & 0 deletions x/evm/embeds/abi/DSAuthority.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"constant": true,
"inputs": [
{
"name": "src",
"type": "address"
},
{
"name": "dst",
"type": "address"
},
{
"name": "sig",
"type": "bytes4"
}
],
"name": "canCall",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
1 change: 1 addition & 0 deletions x/evm/embeds/abi/DSMath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
39 changes: 39 additions & 0 deletions x/evm/embeds/abi/DSNote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
{
"anonymous": true,
"inputs": [
{
"indexed": true,
"name": "sig",
"type": "bytes4"
},
{
"indexed": true,
"name": "guy",
"type": "address"
},
{
"indexed": true,
"name": "foo",
"type": "bytes32"
},
{
"indexed": true,
"name": "bar",
"type": "bytes32"
},
{
"indexed": false,
"name": "wad",
"type": "uint256"
},
{
"indexed": false,
"name": "fax",
"type": "bytes"
}
],
"name": "LogNote",
"type": "event"
}
]
Loading
Loading