Skip to content

Commit

Permalink
fix(precompile-funtoken): modify StateDB after bank send operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Oct 24, 2024
1 parent be2c0a2 commit 0e6afd8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 35 deletions.
68 changes: 65 additions & 3 deletions x/evm/precompile/funtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
gethabi "github.com/ethereum/go-ethereum/accounts/abi"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"

"github.com/NibiruChain/nibiru/v2/app/keepers"
"github.com/NibiruChain/nibiru/v2/eth"
"github.com/NibiruChain/nibiru/v2/x/evm"
"github.com/NibiruChain/nibiru/v2/x/evm/embeds"
evmkeeper "github.com/NibiruChain/nibiru/v2/x/evm/keeper"
"github.com/NibiruChain/nibiru/v2/x/evm/statedb"
)

var _ vm.PrecompiledContract = (*precompileFunToken)(nil)
Expand Down Expand Up @@ -148,7 +151,7 @@ func (p precompileFunToken) bankSend(

// EVM account mints FunToken.BankDenom to module account
amt := math.NewIntFromBigInt(amount)
coins := sdk.NewCoins(sdk.NewCoin(funtoken.BankDenom, amt))
coinToSend := sdk.NewCoin(funtoken.BankDenom, amt)
if funtoken.IsMadeFromCoin {
// If the FunToken mapping was created from a bank coin, then the EVM account
// owns the ERC20 contract and was the original minter of the ERC20 tokens.
Expand All @@ -160,7 +163,7 @@ func (p precompileFunToken) bankSend(
return
}
} else {
err = p.bankKeeper.MintCoins(ctx, evm.ModuleName, coins)
err = SafeMintCoins(ctx, evm.ModuleName, coinToSend, p.bankKeeper, start.StateDB)

Check warning on line 166 in x/evm/precompile/funtoken.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/funtoken.go#L166

Added line #L166 was not covered by tests
if err != nil {
return nil, fmt.Errorf("mint failed for module \"%s\" (%s): contract caller %s: %w",
evm.ModuleName, evm.EVM_MODULE_ADDRESS.Hex(), caller.Hex(), err,
Expand All @@ -169,7 +172,14 @@ func (p precompileFunToken) bankSend(
}

// Transfer the bank coin
err = p.bankKeeper.SendCoinsFromModuleToAccount(ctx, evm.ModuleName, toAddr, coins)
err = SafeSendCoinFromModuleToAccount(
ctx,
evm.ModuleName,
toAddr,
coinToSend,
p.bankKeeper,
start.StateDB,
)
if err != nil {
return nil, fmt.Errorf("send failed for module \"%s\" (%s): contract caller %s: %w",
evm.ModuleName, evm.EVM_MODULE_ADDRESS.Hex(), caller.Hex(), err,
Expand All @@ -181,6 +191,58 @@ func (p precompileFunToken) bankSend(
return method.Outputs.Pack()
}

func SafeMintCoins(
ctx sdk.Context,
moduleName string,
amt sdk.Coin,
bk bankkeeper.Keeper,
db *statedb.StateDB,
) error {
err := bk.MintCoins(ctx, evm.ModuleName, sdk.NewCoins(amt))
if err != nil {
return err
}
if amt.Denom == evm.EVMBankDenom {
evmBech32Addr := auth.NewModuleAddress(evm.ModuleName)
balAfter := bk.GetBalance(ctx, evmBech32Addr, amt.Denom).Amount.BigInt()
db.SetBalanceWei(
evm.EVM_MODULE_ADDRESS,
evm.NativeToWei(balAfter),
)
}

Check warning on line 212 in x/evm/precompile/funtoken.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/funtoken.go#L200-L212

Added lines #L200 - L212 were not covered by tests

return nil

Check warning on line 214 in x/evm/precompile/funtoken.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/funtoken.go#L214

Added line #L214 was not covered by tests
}

func SafeSendCoinFromModuleToAccount(
ctx sdk.Context,
senderModule string,
recipientAddr sdk.AccAddress,
amt sdk.Coin,
bk bankkeeper.Keeper,
db *statedb.StateDB,
) error {
err := bk.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, sdk.NewCoins(amt))
if err != nil {
return err
}

Check warning on line 228 in x/evm/precompile/funtoken.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/funtoken.go#L227-L228

Added lines #L227 - L228 were not covered by tests
if amt.Denom == evm.EVMBankDenom {
evmBech32Addr := auth.NewModuleAddress(evm.ModuleName)
balAfterFrom := bk.GetBalance(ctx, evmBech32Addr, amt.Denom).Amount.BigInt()
db.SetBalanceWei(
evm.EVM_MODULE_ADDRESS,
evm.NativeToWei(balAfterFrom),
)

balAfterTo := bk.GetBalance(ctx, recipientAddr, amt.Denom).Amount.BigInt()
db.SetBalanceWei(
eth.NibiruAddrToEthAddr(recipientAddr),
evm.NativeToWei(balAfterTo),
)
}
return nil
}

func (p precompileFunToken) decomposeBankSendArgs(args []any) (
erc20 gethcommon.Address,
amount *big.Int,
Expand Down
23 changes: 0 additions & 23 deletions x/evm/statedb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,6 @@ func (s *StateDB) DirtiesCount() int {
dirtiesCount += dirtyCount
}
return dirtiesCount
// for addr := range s.Journal.dirties {
// obj := s.stateObjects[addr]
// // suicided without deletion means obj is dirty
// if obj.Suicided {
// dirtiesCount++
// // continue
// }
// // dirty code means obj is dirty
// if obj.code != nil && obj.DirtyCode {
// dirtiesCount++
// // continue
// }

// // mismatch between dirty storage and origin means obj is dirty
// for k, v := range obj.DirtyStorage {
// // All object (k,v) tuples matching between dirty and origin storage
// // signifies that the entry is committed.
// if v != obj.OriginStorage[k] {
// dirtiesCount++
// }
// }
// }
// return dirtiesCount
}

func (s *StateDB) Dirties() map[common.Address]int {
Expand Down
8 changes: 3 additions & 5 deletions x/evm/statedb/journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import (
"github.com/ethereum/go-ethereum/core/vm"

serverconfig "github.com/NibiruChain/nibiru/v2/app/server/config"

"github.com/NibiruChain/nibiru/v2/x/common/testutil/testapp"
"github.com/NibiruChain/nibiru/v2/x/evm"
"github.com/NibiruChain/nibiru/v2/x/evm/embeds"
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
"github.com/NibiruChain/nibiru/v2/x/evm/precompile/test"
precompiletest "github.com/NibiruChain/nibiru/v2/x/evm/precompile/test"
"github.com/NibiruChain/nibiru/v2/x/evm/statedb"
)

Expand All @@ -32,10 +30,10 @@ func (s *Suite) TestPrecompileSnapshots() {

s.T().Log("Set up helloworldcounter.wasm")

wasmContract := precompiletest.SetupWasmContracts(&deps, &s.Suite)[1]
wasmContract := test.SetupWasmContracts(&deps, &s.Suite)[1]
fmt.Printf("wasmContract: %s\n", wasmContract)
assertionsBeforeRun := func(deps *evmtest.TestDeps) {
precompiletest.AssertWasmCounterState(
test.AssertWasmCounterState(
&s.Suite, *deps, wasmContract, 0,
)
}
Expand All @@ -45,7 +43,7 @@ func (s *Suite) TestPrecompileSnapshots() {
)
}
assertionsAfterRun := func(deps *evmtest.TestDeps) {
precompiletest.AssertWasmCounterState(
test.AssertWasmCounterState(
&s.Suite, *deps, wasmContract, 7,
)
}
Expand Down
15 changes: 11 additions & 4 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,25 @@ func (s *StateDB) setStateObject(object *stateObject) {
*/

// AddBalance adds amount to the account associated with addr.
func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
func (s *StateDB) AddBalance(addr common.Address, wei *big.Int) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.AddBalance(amount)
stateObject.AddBalance(wei)
}
}

// SubBalance subtracts amount from the account associated with addr.
func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
func (s *StateDB) SubBalance(addr common.Address, wei *big.Int) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.SubBalance(amount)
stateObject.SubBalance(wei)
}
}

func (s *StateDB) SetBalanceWei(addr common.Address, wei *big.Int) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.SetBalance(wei)

Check warning on line 316 in x/evm/statedb/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/statedb/statedb.go#L313-L316

Added lines #L313 - L316 were not covered by tests
}
}

Expand Down

0 comments on commit 0e6afd8

Please sign in to comment.