Skip to content

Commit

Permalink
Merge branch 'main' into releases/v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jan 31, 2025
2 parents 044d70b + fda9778 commit bf7acbc
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 212 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [#2104](https://github.com/NibiruChain/nibiru/pull/2074) - chore: update chain IDs

### Nibiru EVM

- [#2119](https://github.com/NibiruChain/nibiru/pull/2119) - fix(evm): Guarantee
Expand Down Expand Up @@ -90,7 +92,9 @@ needed to include double quotes around the hexadecimal string.
- [#2176](https://github.com/NibiruChain/nibiru/pull/2176) - tests(evm): add dirty state tests from code4rena audit
- [#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
- [#2187](https://github.com/NibiruChain/nibiru/pull/2187) - fix(evm): fix eip55 address encoding

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

The codebase went through a third-party [Code4rena
Expand Down
46 changes: 23 additions & 23 deletions app/appconst/appconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,39 @@ func RuntimeVersion() string {

// EIP 155 Chain IDs exported for tests.
const (
ETH_CHAIN_ID_MAINNET int64 = 7200
ETH_CHAIN_ID_MAINNET int64 = 6900

ETH_CHAIN_ID_TESTNET_1 int64 = 7210
ETH_CHAIN_ID_TESTNET_2 int64 = 7211
ETH_CHAIN_ID_TESTNET_3 int64 = 7212
ETH_CHAIN_ID_TESTNET_1 int64 = 6910
ETH_CHAIN_ID_TESTNET_2 int64 = 6911
ETH_CHAIN_ID_TESTNET_3 int64 = 6912

ETH_CHAIN_ID_DEVNET_1 int64 = 7220
ETH_CHAIN_ID_DEVNET_2 int64 = 7221
ETH_CHAIN_ID_DEVNET_3 int64 = 7222
ETH_CHAIN_ID_DEVNET_1 int64 = 6920
ETH_CHAIN_ID_DEVNET_2 int64 = 6921
ETH_CHAIN_ID_DEVNET_3 int64 = 6922

ETH_CHAIN_ID_LOCALNET_0 int64 = 7230
ETH_CHAIN_ID_LOCALNET_1 int64 = 7231
ETH_CHAIN_ID_LOCALNET_2 int64 = 7232
ETH_CHAIN_ID_LOCALNET_3 int64 = 7233
ETH_CHAIN_ID_LOCALNET_0 int64 = 6930
ETH_CHAIN_ID_LOCALNET_1 int64 = 6931
ETH_CHAIN_ID_LOCALNET_2 int64 = 6932
ETH_CHAIN_ID_LOCALNET_3 int64 = 6933

ETH_CHAIN_ID_DEFAULT int64 = 7230
ETH_CHAIN_ID_DEFAULT int64 = 6930
)

var knownEthChainIDMap = map[string]int64{
"cataclysm-1": 7200,
"cataclysm-1": 6900,

"nibiru-testnet-1": 7210,
"nibiru-testnet-2": 7211,
"nibiru-testnet-3": 7212,
"nibiru-testnet-1": 6910,
"nibiru-testnet-2": 6911,
"nibiru-testnet-3": 6912,

"nibiru-devnet-1": 7220,
"nibiru-devnet-2": 7221,
"nibiru-devnet-3": 7222,
"nibiru-devnet-1": 6920,
"nibiru-devnet-2": 6921,
"nibiru-devnet-3": 6922,

"nibiru-localnet-0": 7230,
"nibiru-localnet-1": 7231,
"nibiru-localnet-2": 7232,
"nibiru-localnet-3": 7233,
"nibiru-localnet-0": 6930,
"nibiru-localnet-1": 6931,
"nibiru-localnet-2": 6932,
"nibiru-localnet-3": 6933,
}

// GetEthChainID: Maps the given chain ID from the block's `sdk.Context` to an
Expand Down
17 changes: 6 additions & 11 deletions eth/eip55.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewEIP55AddrFromStr(input string) (EIP55Addr, error) {
// Marshal implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h EIP55Addr) Marshal() ([]byte, error) {
return []byte(h.Address.Hex()), nil
return h.Bytes(), nil
}

// MarshalJSON returns the [EIP55Addr] as JSON bytes.
Expand All @@ -51,18 +51,15 @@ func (h EIP55Addr) MarshalJSON() ([]byte, error) {
// Implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h *EIP55Addr) MarshalTo(data []byte) (n int, err error) {
copy(data, []byte(h.Address.Hex()))
copy(data, h.Bytes())
return h.Size(), nil

Check warning on line 55 in eth/eip55.go

View check run for this annotation

Codecov / codecov/patch

eth/eip55.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}

// Unmarshal implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h *EIP55Addr) Unmarshal(data []byte) error {
addr, err := NewEIP55AddrFromStr(string(data))
if err != nil {
return err
}
*h = addr
addr := gethcommon.BytesToAddress(data)
*h = EIP55Addr{Address: addr}
return nil
}

Expand All @@ -71,9 +68,7 @@ func (h *EIP55Addr) Unmarshal(data []byte) error {
func (h *EIP55Addr) UnmarshalJSON(bz []byte) error {
var addrStr string
if err := json.Unmarshal(bz, &addrStr); err != nil {
return fmt.Errorf(
"EIP55AddrError: UnmarhsalJSON had invalid input %s: %w", bz, err,
)
return err
}

Check warning on line 72 in eth/eip55.go

View check run for this annotation

Codecov / codecov/patch

eth/eip55.go#L71-L72

Added lines #L71 - L72 were not covered by tests
addr, err := NewEIP55AddrFromStr(addrStr)
if err != nil {
Expand All @@ -86,5 +81,5 @@ func (h *EIP55Addr) UnmarshalJSON(bz []byte) error {
// Size implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h EIP55Addr) Size() int {
return len([]byte(h.Address.Hex()))
return len(h.Bytes())
}
130 changes: 82 additions & 48 deletions eth/eip55_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package eth_test

import (
"encoding/json"
"fmt"
"strconv"
"strings"
"testing"

gethcommon "github.com/ethereum/go-ethereum/common"
Expand All @@ -12,25 +12,19 @@ import (
"github.com/NibiruChain/nibiru/v2/eth"
)

// MustNewEIP55AddrFromStr is the same as [NewEIP55AddrFromStr], except it panics
// mustNewEIP55AddrFromStr is the same as [NewEIP55AddrFromStr], except it panics
// when there's an error.
func MustNewEIP55AddrFromStr(input string) eth.EIP55Addr {
func mustNewEIP55AddrFromStr(input string) eth.EIP55Addr {
addr, err := eth.NewEIP55AddrFromStr(input)
if err != nil {
panic(err)
}
return addr
}

var threeValidAddrs []eth.EIP55Addr = []eth.EIP55Addr{
MustNewEIP55AddrFromStr("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"),
MustNewEIP55AddrFromStr("0xAe967917c465db8578ca9024c205720b1a3651A9"),
MustNewEIP55AddrFromStr("0x1111111111111111111112222222222223333323"),
}

func (s *EIP55AddrSuite) TestEquivalence() {
expectedGethAddr := gethcommon.HexToAddress("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
expectedEIP55Addr := MustNewEIP55AddrFromStr("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
expectedEIP55Addr := mustNewEIP55AddrFromStr("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")

equivalentAddrs := []string{
"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed",
Expand Down Expand Up @@ -110,54 +104,95 @@ func (s *EIP55AddrSuite) TestNewEIP55Addr() {
}
}

func (s *EIP55AddrSuite) TestProtobufEncoding() {
func (s *EIP55AddrSuite) TestJsonEncoding() {
for tcIdx, tc := range []struct {
input eth.EIP55Addr
expectedJson string
expectedJson json.RawMessage
wantErr string
}{
{
input: threeValidAddrs[0],
expectedJson: `"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"`,
input: mustNewEIP55AddrFromStr("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"),
expectedJson: []byte("\"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed\""),
},
{
input: threeValidAddrs[1],
expectedJson: `"0xAe967917c465db8578ca9024c205720b1a3651A9"`,
input: mustNewEIP55AddrFromStr("0xAe967917c465db8578ca9024c205720b1a3651A9"),
expectedJson: []byte("\"0xAe967917c465db8578ca9024c205720b1a3651A9\""),
},
{
input: threeValidAddrs[2],
expectedJson: `"0x1111111111111111111112222222222223333323"`,
input: mustNewEIP55AddrFromStr("0x1111111111111111111112222222222223333323"),
expectedJson: []byte("\"0x1111111111111111111112222222222223333323\""),
},
} {
s.Run(strconv.Itoa(tcIdx), func() {
givenMut := tc.input
jsonBz, err := givenMut.MarshalJSON()
s.NoError(err)
s.Equal(tc.expectedJson, string(jsonBz))
jsonBz, err := tc.input.MarshalJSON()
s.Require().NoError(err)
s.Require().EqualValues(tc.expectedJson, jsonBz)

eip55Addr := new(eth.EIP55Addr)
s.NoError(eip55Addr.UnmarshalJSON(jsonBz))
s.Equal(givenMut, tc.input,
"Given -> MarshalJSON -> UnmarshalJSON returns a different value than the given when it should be an identity operation (no-op). test case #%d", tcIdx)
s.Require().NoError(eip55Addr.UnmarshalJSON(jsonBz))
s.Require().EqualValues(tc.input, *eip55Addr)
})
}
}

func (s *EIP55AddrSuite) TestProtobufEncoding() {
for tcIdx, tc := range []struct {
input eth.EIP55Addr
expectedProtoBz []byte
wantErr string
}{
{
input: mustNewEIP55AddrFromStr("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"),
expectedProtoBz: []byte{90, 174, 182, 5, 63, 62, 148, 201, 185, 160, 159, 51, 102, 148, 53, 231, 239, 27, 234, 237},
},
{
input: mustNewEIP55AddrFromStr("0xAe967917c465db8578ca9024c205720b1a3651A9"),
expectedProtoBz: []byte{174, 150, 121, 23, 196, 101, 219, 133, 120, 202, 144, 36, 194, 5, 114, 11, 26, 54, 81, 169},
},
{
input: mustNewEIP55AddrFromStr("0x1111111111111111111112222222222223333323"),
expectedProtoBz: []byte{17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 34, 34, 34, 34, 34, 35, 51, 51, 35},
},
} {
s.Run(strconv.Itoa(tcIdx), func() {
bz, err := tc.input.Marshal()
s.NoError(err)
s.Equal(strings.Trim(tc.expectedJson, `"`), string(bz),
"Marshaling to bytes gives different value than the test case specifies. test case #%d", tcIdx)
s.Require().NoError(err)
s.Require().EqualValues(tc.expectedProtoBz, bz)

err = eip55Addr.Unmarshal(bz)
s.NoError(err)
s.Equal(tc.input.Address, eip55Addr.Address,
"Given -> Marshal -> Unmarshal returns a different value than the given when it should be an identity operation (no-op). test case #%d", tcIdx)
eip55Addr := new(eth.EIP55Addr)
s.Require().NoError(eip55Addr.Unmarshal(bz))
s.Require().Equal(tc.input.Address, eip55Addr.Address)
})
}
}

s.Equal(len([]byte(tc.input.Hex())), tc.input.Size())
s.Equal(len(tc.input.Hex()), tc.input.Size())
func (s *EIP55AddrSuite) TestSize() {
for idx, tc := range []struct {
input eth.EIP55Addr
expectedSize int
wantErr string
}{
{
input: mustNewEIP55AddrFromStr("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"),
expectedSize: 20,
},
{
input: mustNewEIP55AddrFromStr("0xAe967917c465db8578ca9024c205720b1a3651A9"),
expectedSize: 20,
},
{
input: mustNewEIP55AddrFromStr("0x1111111111111111111112222222222223333323"),
expectedSize: 20,
},
} {
s.Run(strconv.Itoa(idx), func() {
s.Require().EqualValues(tc.expectedSize, tc.input.Size())
})
}
}

// showcases how geth checks for valid hex addresses and treats invalid inputs
func (s *EIP55AddrSuite) TestIsEIP55Address() {
func (s *EIP55AddrSuite) TestHexAddress() {
s.True(gethcommon.IsHexAddress("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"))
s.True(gethcommon.IsHexAddress("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAED"))
s.False(gethcommon.IsHexAddress("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed1234"))
Expand All @@ -179,21 +214,20 @@ func TestEIP55AddrSuite(t *testing.T) {

func (s *EIP55AddrSuite) TestStringEncoding() {
addrHex := "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"
addr := new(eth.EIP55Addr)
err := addr.Unmarshal([]byte(addrHex))
s.NoError(err)
s.Equal(addrHex, addr.Address.Hex())
addr, err := eth.NewEIP55AddrFromStr(addrHex)
s.Require().NoError(err)
s.Require().Equal(addrHex, addr.Address.Hex())

addrBytes, err := addr.Marshal()
s.NoError(err)
s.Equal(addrHex, string(addrBytes))
addrBz, err := addr.Marshal()
s.Require().NoError(err)
s.Require().EqualValues(addr.Bytes(), addrBz)

bz, err := addr.MarshalJSON()
s.NoError(err)
s.Equal(fmt.Sprintf(`"%s"`, addrHex), string(bz))
s.Require().NoError(err)
s.Require().Equal(fmt.Sprintf(`"%s"`, addrHex), string(bz))

addrb := new(eth.EIP55Addr)
err = addrb.UnmarshalJSON([]byte(fmt.Sprintf(`"%s"`, addrHex)))
s.NoError(err)
s.EqualValues(addrb, addr)
newAddr := new(eth.EIP55Addr)
err = newAddr.UnmarshalJSON(bz)
s.Require().NoError(err)
s.Require().EqualValues(addrHex, newAddr.Hex())
}
2 changes: 2 additions & 0 deletions evm-e2e/.env_sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
JSON_RPC_ENDPOINT="http://127.0.0.1:8545"
MNEMONIC="guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host"
TEST_TIMEOUT=15000
TX_WAIT_TIMEOUT=5000
29 changes: 17 additions & 12 deletions evm-e2e/test/contract_infinite_loop_gas.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { describe, expect, it } from '@jest/globals';
import { toBigInt } from 'ethers';
import { deployContractInfiniteLoopGas } from './utils';
import { TEST_TIMEOUT } from './setup';

describe('Infinite loop gas contract', () => {
it('should fail due to out of gas error', async () => {
const contract = await deployContractInfiniteLoopGas();
it(
'should fail due to out of gas error',
async () => {
const contract = await deployContractInfiniteLoopGas();

expect(contract.counter()).resolves.toBe(toBigInt(0));
await expect(contract.counter()).resolves.toBe(toBigInt(0));

try {
const tx = await contract.forever({ gasLimit: 1e6 });
await tx.wait();
throw new Error('The transaction should have failed but did not.');
} catch (error) {
expect(error.message).toContain('transaction execution reverted');
}
try {
const tx = await contract.forever({ gasLimit: 1e6 });
await tx.wait();
throw new Error('The transaction should have failed but did not.');
} catch (error) {
expect(error.message).toContain('transaction execution reverted');
}

expect(contract.counter()).resolves.toBe(toBigInt(0));
}, 20e3);
await expect(contract.counter()).resolves.toBe(toBigInt(0));
},
TEST_TIMEOUT,
);
});
Loading

0 comments on commit bf7acbc

Please sign in to comment.