Skip to content

Commit

Permalink
test(evm): additional tests and refactors (#2176)
Browse files Browse the repository at this point in the history
* chore: cleanup funtoken test

* Update funtoken_test.go

* refactor: update wasm tests

* Update export.go

* Update debug.go

* fix: TestJournalReversion

* test(evm): add dirty state attack tests

* Update wasm_test.go

* test: add TestWasmPrecompileDirtyStateAttack4

* Update wasm_test.go

* Update CHANGELOG.md

* test(evm): add wasm precompile staking test

* fix: total supply calc in tests

* test(evm): remove dirty state attacks 1-3 due to being duplicates

* refactor: variable names
  • Loading branch information
k-yang authored Jan 28, 2025
1 parent 3c2bcc5 commit bc29b88
Show file tree
Hide file tree
Showing 24 changed files with 693 additions and 332 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ needed to include double quotes around the hexadecimal string.
- [#2172](https://github.com/NibiruChain/nibiru/pull/2172) - chore: close iterator in IterateEpochInfo
- [#2173](https://github.com/NibiruChain/nibiru/pull/2173) - fix(evm): clear `StateDB` between calls
- [#2177](https://github.com/NibiruChain/nibiru/pull/2177) - fix(cmd): Continue from #2127 and unwire vesting flags and logic from genaccounts.go
- [#2176](https://github.com/NibiruChain/nibiru/pull/2176) - tests(evm): add dirty state tests from code4rena audit

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

Expand Down
11 changes: 7 additions & 4 deletions x/common/testutil/testapp/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

nibiruapp "github.com/NibiruChain/nibiru/v2/app"
"github.com/NibiruChain/nibiru/v2/app/appconst"
)

// GenesisStateWithSingleValidator initializes GenesisState with a single validator and genesis accounts
Expand All @@ -41,7 +42,7 @@ func GenesisStateWithSingleValidator(codec codec.Codec, genesisState nibiruapp.G

balances = append(balances, banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100000000000000))),
Coins: sdk.NewCoins(sdk.NewCoin(appconst.BondDenom, math.NewIntFromUint64(1e14))),
})

genesisState, err = genesisStateWithValSet(codec, genesisState, valSet, []authtypes.GenesisAccount{acc}, balances...)
Expand Down Expand Up @@ -93,7 +94,9 @@ func genesisStateWithValSet(
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), math.LegacyOneDec()))
}
// set validators and delegations
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
stakingParams := stakingtypes.DefaultParams()
stakingParams.BondDenom = appconst.BondDenom
stakingGenesis := stakingtypes.NewGenesisState(stakingParams, validators, delegations)
genesisState[stakingtypes.ModuleName] = cdc.MustMarshalJSON(stakingGenesis)

totalSupply := sdk.NewCoins()
Expand All @@ -104,13 +107,13 @@ func genesisStateWithValSet(

for range delegations {
// add delegated tokens to total supply
totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))
totalSupply = totalSupply.Add(sdk.NewCoin(appconst.BondDenom, bondAmt))
}

// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
Coins: sdk.Coins{sdk.NewCoin(appconst.BondDenom, bondAmt)},
})

// update total supply
Expand Down
2 changes: 2 additions & 0 deletions x/common/testutil/testapp/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/NibiruChain/nibiru/v2/app"
"github.com/NibiruChain/nibiru/v2/app/appconst"
cryptocodec "github.com/NibiruChain/nibiru/v2/eth/crypto/codec"
"github.com/NibiruChain/nibiru/v2/x/common/asset"
"github.com/NibiruChain/nibiru/v2/x/common/denoms"
"github.com/NibiruChain/nibiru/v2/x/common/testutil"
Expand Down Expand Up @@ -126,6 +127,7 @@ func NewNibiruTestApp(gen app.GenesisState, baseAppOptions ...func(*baseapp.Base
logger := log.NewNopLogger()

encoding := app.MakeEncodingConfig()
cryptocodec.RegisterInterfaces(encoding.InterfaceRegistry)
SetDefaultSudoGenesis(gen)

app := app.NewNibiruApp(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "TestDirtyStateAttack4",
"sourceName": "contracts/TestDirtyStateAttack4.sol",
"abi": [
{
"inputs": [],
"stateMutability": "payable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "string",
"name": "wasmAddr",
"type": "string"
},
{
"internalType": "bytes",
"name": "msgArgs",
"type": "bytes"
}
],
"name": "attack",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getCounter",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "0x60806040526000805561084a806100176000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806333c889971461003b5780638ada066e14610057575b600080fd5b610055600480360381019061005091906102d6565b610075565b005b61005f6101e4565b60405161006c9190610370565b60405180910390f35b600080815480929190610087906103ba565b91905055506000600167ffffffffffffffff8111156100a9576100a8610402565b5b6040519080825280602002602001820160405280156100e257816020015b6100cf6101ed565b8152602001906001900390816100c75790505b50905060405180604001604052806040518060400160405280600581526020017f756e6962690000000000000000000000000000000000000000000000000000008152508152602001620f42408152508160008151811061014657610145610431565b5b602002602001018190525061080273ffffffffffffffffffffffffffffffffffffffff166361ffaee486868686866040518663ffffffff1660e01b8152600401610194959493929190610689565b6000604051808303816000875af11580156101b3573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f820116820180604052508101906101dc91906107cb565b505050505050565b60008054905090565b604051806040016040528060608152602001600081525090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f8401126102405761023f61021b565b5b8235905067ffffffffffffffff81111561025d5761025c610220565b5b60208301915083600182028301111561027957610278610225565b5b9250929050565b60008083601f8401126102965761029561021b565b5b8235905067ffffffffffffffff8111156102b3576102b2610220565b5b6020830191508360018202830111156102cf576102ce610225565b5b9250929050565b600080600080604085870312156102f0576102ef610211565b5b600085013567ffffffffffffffff81111561030e5761030d610216565b5b61031a8782880161022a565b9450945050602085013567ffffffffffffffff81111561033d5761033c610216565b5b61034987828801610280565b925092505092959194509250565b6000819050919050565b61036a81610357565b82525050565b60006020820190506103856000830184610361565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006103c582610357565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82036103f7576103f661038b565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b600061049d8385610460565b93506104aa838584610471565b6104b383610480565b840190509392505050565b600082825260208201905092915050565b60006104db83856104be565b93506104e8838584610471565b6104f183610480565b840190509392505050565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610562578082015181840152602081019050610547565b60008484015250505050565b600061057982610528565b6105838185610533565b9350610593818560208601610544565b61059c81610480565b840191505092915050565b6105b081610357565b82525050565b600060408301600083015184820360008601526105d3828261056e565b91505060208301516105e860208601826105a7565b508091505092915050565b60006105ff83836105b6565b905092915050565b6000602082019050919050565b600061061f826104fc565b6106298185610507565b93508360208202850161063b85610518565b8060005b85811015610677578484038952815161065885826105f3565b945061066383610607565b925060208a0199505060018101905061063f565b50829750879550505050505092915050565b600060608201905081810360008301526106a4818789610491565b905081810360208301526106b98185876104cf565b905081810360408301526106cd8184610614565b90509695505050505050565b600080fd5b6106e782610480565b810181811067ffffffffffffffff8211171561070657610705610402565b5b80604052505050565b6000610719610207565b905061072582826106de565b919050565b600067ffffffffffffffff82111561074557610744610402565b5b61074e82610480565b9050602081019050919050565b600061076e6107698461072a565b61070f565b90508281526020810184848401111561078a576107896106d9565b5b610795848285610544565b509392505050565b600082601f8301126107b2576107b161021b565b5b81516107c284826020860161075b565b91505092915050565b6000602082840312156107e1576107e0610211565b5b600082015167ffffffffffffffff8111156107ff576107fe610216565b5b61080b8482850161079d565b9150509291505056fea26469706673582212208caac82ecde5bb31fb5b34bd6b569b73277f17eb9b132d65942a718f83178c9c64736f6c63430008180033",
"deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100365760003560e01c806333c889971461003b5780638ada066e14610057575b600080fd5b610055600480360381019061005091906102d6565b610075565b005b61005f6101e4565b60405161006c9190610370565b60405180910390f35b600080815480929190610087906103ba565b91905055506000600167ffffffffffffffff8111156100a9576100a8610402565b5b6040519080825280602002602001820160405280156100e257816020015b6100cf6101ed565b8152602001906001900390816100c75790505b50905060405180604001604052806040518060400160405280600581526020017f756e6962690000000000000000000000000000000000000000000000000000008152508152602001620f42408152508160008151811061014657610145610431565b5b602002602001018190525061080273ffffffffffffffffffffffffffffffffffffffff166361ffaee486868686866040518663ffffffff1660e01b8152600401610194959493929190610689565b6000604051808303816000875af11580156101b3573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f820116820180604052508101906101dc91906107cb565b505050505050565b60008054905090565b604051806040016040528060608152602001600081525090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f8401126102405761023f61021b565b5b8235905067ffffffffffffffff81111561025d5761025c610220565b5b60208301915083600182028301111561027957610278610225565b5b9250929050565b60008083601f8401126102965761029561021b565b5b8235905067ffffffffffffffff8111156102b3576102b2610220565b5b6020830191508360018202830111156102cf576102ce610225565b5b9250929050565b600080600080604085870312156102f0576102ef610211565b5b600085013567ffffffffffffffff81111561030e5761030d610216565b5b61031a8782880161022a565b9450945050602085013567ffffffffffffffff81111561033d5761033c610216565b5b61034987828801610280565b925092505092959194509250565b6000819050919050565b61036a81610357565b82525050565b60006020820190506103856000830184610361565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006103c582610357565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82036103f7576103f661038b565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b600061049d8385610460565b93506104aa838584610471565b6104b383610480565b840190509392505050565b600082825260208201905092915050565b60006104db83856104be565b93506104e8838584610471565b6104f183610480565b840190509392505050565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610562578082015181840152602081019050610547565b60008484015250505050565b600061057982610528565b6105838185610533565b9350610593818560208601610544565b61059c81610480565b840191505092915050565b6105b081610357565b82525050565b600060408301600083015184820360008601526105d3828261056e565b91505060208301516105e860208601826105a7565b508091505092915050565b60006105ff83836105b6565b905092915050565b6000602082019050919050565b600061061f826104fc565b6106298185610507565b93508360208202850161063b85610518565b8060005b85811015610677578484038952815161065885826105f3565b945061066383610607565b925060208a0199505060018101905061063f565b50829750879550505050505092915050565b600060608201905081810360008301526106a4818789610491565b905081810360208301526106b98185876104cf565b905081810360408301526106cd8184610614565b90509695505050505050565b600080fd5b6106e782610480565b810181811067ffffffffffffffff8211171561070657610705610402565b5b80604052505050565b6000610719610207565b905061072582826106de565b919050565b600067ffffffffffffffff82111561074557610744610402565b5b61074e82610480565b9050602081019050919050565b600061076e6107698461072a565b61070f565b90508281526020810184848401111561078a576107896106d9565b5b610795848285610544565b509392505050565b600082601f8301126107b2576107b161021b565b5b81516107c284826020860161075b565b91505092915050565b6000602082840312156107e1576107e0610211565b5b600082015167ffffffffffffffff8111156107ff576107fe610216565b5b61080b8482850161079d565b9150509291505056fea26469706673582212208caac82ecde5bb31fb5b34bd6b569b73277f17eb9b132d65942a718f83178c9c64736f6c63430008180033",
"linkReferences": {},
"deployedLinkReferences": {}
}
Loading

0 comments on commit bc29b88

Please sign in to comment.