Skip to content

Commit

Permalink
refactor: simplify account retrieval (#2141)
Browse files Browse the repository at this point in the history
* refactor: simplify account retrieval

* Update CHANGELOG.md

* test: fix tests

---------

Co-authored-by: Oleg Nikonychev <[email protected]>
  • Loading branch information
k-yang and onikonychev authored Jan 6, 2025
1 parent 86d6b3f commit 8cd4ceb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ documentation.
- [#2125](https://github.com/NibiruChain/nibiru/pull/2125) - feat(evm-precompile):Emit EVM events created to reflect the ABCI events that occur outside the EVM to make sure that block explorers and indexers can find indexed ABCI event information.
- [#2129](https://github.com/NibiruChain/nibiru/pull/2129) - fix(evm): issue with infinite recursion in erc20 funtoken contracts
- [#2134](https://github.com/NibiruChain/nibiru/pull/2134) - fix(evm): query of NIBI should use bank state, not the StateDB
- [#2141](https://github.com/NibiruChain/nibiru/pull/2141) - refactor: simplify account retrieval operation in `nibid q evm account`.
- [#2142](https://github.com/NibiruChain/nibiru/pull/2142) - fix(bank): add additional missing methods to the NibiruBankKeeper

#### Nibiru EVM | Before Audit 2 - 2024-12-06
Expand Down
7 changes: 5 additions & 2 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (k Keeper) EthAccount(
}

ctx := sdk.UnwrapSDKContext(goCtx)
acct := k.GetAccountOrEmpty(ctx, addrEth)
acct := k.getAccountWithoutBalance(ctx, addrEth)
if acct == nil {
return nil, fmt.Errorf("account not found for %s", addrEth.Hex())
}
balNative := k.Bank.GetBalance(ctx, addrBech32, evm.EVMBankDenom).Amount.BigInt()

return &evm.QueryEthAccountResponse{
Expand Down Expand Up @@ -205,7 +208,7 @@ func (k Keeper) Code(
ctx := sdk.UnwrapSDKContext(goCtx)

address := gethcommon.HexToAddress(req.Address)
acct := k.GetAccountWithoutBalance(ctx, address)
acct := k.getAccountWithoutBalance(ctx, address)

var code []byte
if acct != nil && acct.IsContract() {
Expand Down
22 changes: 4 additions & 18 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,10 @@ func (s *Suite) TestQueryEvmAccount() {
req = &evm.QueryEthAccountRequest{
Address: ethAcc.EthAddr.String(),
}
wantResp = &evm.QueryEthAccountResponse{
Balance: "0",
BalanceWei: "0",
CodeHash: gethcommon.BytesToHash(evm.EmptyCodeHash).Hex(),
Nonce: 0,
EthAddress: ethAcc.EthAddr.String(),
Bech32Address: ethAcc.NibiruAddr.String(),
}
wantResp = nil
return req, wantResp
},
wantErr: "",
wantErr: "account not found for",
},
{
name: "happy: nonexistent account (bech32 input)",
Expand All @@ -163,17 +156,10 @@ func (s *Suite) TestQueryEvmAccount() {
req = &evm.QueryEthAccountRequest{
Address: ethAcc.NibiruAddr.String(),
}
wantResp = &evm.QueryEthAccountResponse{
Balance: "0",
BalanceWei: "0",
CodeHash: gethcommon.BytesToHash(evm.EmptyCodeHash).Hex(),
Nonce: 0,
EthAddress: ethAcc.EthAddr.String(),
Bech32Address: ethAcc.NibiruAddr.String(),
}
wantResp = nil
return req, wantResp
},
wantErr: "",
wantErr: "account not found for",
},
}

Expand Down
25 changes: 4 additions & 21 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var _ statedb.Keeper = &Keeper{}
// Implements the `statedb.Keeper` interface.
// Returns nil if the account does not exist or has the wrong type.
func (k *Keeper) GetAccount(ctx sdk.Context, addr gethcommon.Address) *statedb.Account {
acct := k.GetAccountWithoutBalance(ctx, addr)
acct := k.getAccountWithoutBalance(ctx, addr)
if acct == nil {
return nil
}
Expand Down Expand Up @@ -184,11 +184,10 @@ func (k *Keeper) DeleteAccount(ctx sdk.Context, addr gethcommon.Address) error {
return nil
}

// GetAccountWithoutBalance load nonce and codehash without balance,
// getAccountWithoutBalance load nonce and codehash without balance,
// more efficient in cases where balance is not needed.
func (k *Keeper) GetAccountWithoutBalance(ctx sdk.Context, addr gethcommon.Address) *statedb.Account {
nibiruAddr := sdk.AccAddress(addr.Bytes())
acct := k.accountKeeper.GetAccount(ctx, nibiruAddr)
func (k *Keeper) getAccountWithoutBalance(ctx sdk.Context, addr gethcommon.Address) *statedb.Account {
acct := k.accountKeeper.GetAccount(ctx, eth.EthAddrToNibiruAddr(addr))
if acct == nil {
return nil
}
Expand All @@ -204,19 +203,3 @@ func (k *Keeper) GetAccountWithoutBalance(ctx sdk.Context, addr gethcommon.Addre
CodeHash: codeHash,
}
}

// GetAccountOrEmpty returns empty account if not exist, returns error if it's not `EthAccount`
func (k *Keeper) GetAccountOrEmpty(
ctx sdk.Context, addr gethcommon.Address,
) statedb.Account {
acct := k.GetAccount(ctx, addr)
if acct != nil {
return *acct
}

// empty account
return statedb.Account{
BalanceNative: new(big.Int),
CodeHash: evm.EmptyCodeHash,
}
}

0 comments on commit 8cd4ceb

Please sign in to comment.