-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Created NibiruBankKeeper with safety around NIBI transfers inside of EthereumTx. 2. The "PrecompileCalled" JournalChange now has a propery implementation and a strong test case to show that reverting the precompile calls works as intended. 3. Remove unneeded functions created for testing with low-level struct fields.
- Loading branch information
1 parent
80dd2d7
commit c624912
Showing
17 changed files
with
289 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package keeper | ||
|
||
import ( | ||
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" | ||
|
||
"github.com/NibiruChain/nibiru/v2/eth" | ||
"github.com/NibiruChain/nibiru/v2/x/evm" | ||
"github.com/NibiruChain/nibiru/v2/x/evm/statedb" | ||
) | ||
|
||
var ( | ||
_ bankkeeper.Keeper = &NibiruBankKeeper{} | ||
_ bankkeeper.SendKeeper = &NibiruBankKeeper{} | ||
) | ||
|
||
type NibiruBankKeeper struct { | ||
bankkeeper.BaseKeeper | ||
StateDB *statedb.StateDB | ||
balanceChangesForStateDB uint64 | ||
} | ||
|
||
func (evmKeeper *Keeper) NewStateDB( | ||
ctx sdk.Context, txConfig statedb.TxConfig, | ||
) *statedb.StateDB { | ||
stateDB := statedb.New(ctx, evmKeeper, txConfig) | ||
bk := evmKeeper.bankKeeper | ||
bk.StateDB = stateDB | ||
bk.balanceChangesForStateDB = 0 | ||
return stateDB | ||
} | ||
|
||
// BalanceChangesForStateDB returns the count of [statedb.JournalChange] entries | ||
// that were added to the current [statedb.StateDB] | ||
func (bk *NibiruBankKeeper) BalanceChangesForStateDB() uint64 { return bk.balanceChangesForStateDB } | ||
|
||
func (bk NibiruBankKeeper) MintCoins( | ||
ctx sdk.Context, | ||
moduleName string, | ||
coins sdk.Coins, | ||
) error { | ||
// Use the embedded function from [bankkeeper.Keeper] | ||
if err := bk.BaseKeeper.MintCoins(ctx, moduleName, coins); err != nil { | ||
return err | ||
} | ||
if findEtherBalanceChangeFromCoins(coins) { | ||
moduleBech32Addr := auth.NewModuleAddress(evm.ModuleName) | ||
bk.SyncStateDBWithAccount(ctx, moduleBech32Addr) | ||
} | ||
return nil | ||
} | ||
|
||
func (bk NibiruBankKeeper) BurnCoins( | ||
ctx sdk.Context, | ||
moduleName string, | ||
coins sdk.Coins, | ||
) error { | ||
// Use the embedded function from [bankkeeper.Keeper] | ||
if err := bk.BaseKeeper.BurnCoins(ctx, moduleName, coins); err != nil { | ||
return err | ||
} | ||
if findEtherBalanceChangeFromCoins(coins) { | ||
moduleBech32Addr := auth.NewModuleAddress(evm.ModuleName) | ||
bk.SyncStateDBWithAccount(ctx, moduleBech32Addr) | ||
} | ||
return nil | ||
} | ||
|
||
func (bk NibiruBankKeeper) SendCoins( | ||
ctx sdk.Context, | ||
fromAddr sdk.AccAddress, | ||
toAddr sdk.AccAddress, | ||
coins sdk.Coins, | ||
) error { | ||
// Use the embedded function from [bankkeeper.Keeper] | ||
if err := bk.BaseKeeper.SendCoins(ctx, fromAddr, toAddr, coins); err != nil { | ||
return err | ||
} | ||
if findEtherBalanceChangeFromCoins(coins) { | ||
bk.SyncStateDBWithAccount(ctx, fromAddr) | ||
bk.SyncStateDBWithAccount(ctx, toAddr) | ||
} | ||
return nil | ||
} | ||
|
||
func (bk *NibiruBankKeeper) SyncStateDBWithAccount( | ||
ctx sdk.Context, acc sdk.AccAddress, | ||
) { | ||
// If there's no StateDB set, it means we're not in an EthereumTx. | ||
if bk.StateDB == nil { | ||
return | ||
} | ||
balanceWei := evm.NativeToWei( | ||
bk.GetBalance(ctx, acc, evm.EVMBankDenom).Amount.BigInt(), | ||
) | ||
bk.StateDB.SetBalanceWei(eth.NibiruAddrToEthAddr(acc), balanceWei) | ||
bk.balanceChangesForStateDB += 1 | ||
} | ||
|
||
func findEtherBalanceChangeFromCoins(coins sdk.Coins) (found bool) { | ||
for _, c := range coins { | ||
if c.Denom == evm.EVMBankDenom { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (bk NibiruBankKeeper) SendCoinsFromAccountToModule( | ||
ctx sdk.Context, | ||
senderAddr sdk.AccAddress, | ||
recipientModule string, | ||
coins sdk.Coins, | ||
) error { | ||
// Use the embedded function from [bankkeeper.Keeper] | ||
if err := bk.BaseKeeper.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, coins); err != nil { | ||
return err | ||
} | ||
if findEtherBalanceChangeFromCoins(coins) { | ||
bk.SyncStateDBWithAccount(ctx, senderAddr) | ||
moduleBech32Addr := auth.NewModuleAddress(recipientModule) | ||
bk.SyncStateDBWithAccount(ctx, moduleBech32Addr) | ||
} | ||
return nil | ||
} | ||
|
||
func (bk NibiruBankKeeper) SendCoinsFromModuleToAccount( | ||
ctx sdk.Context, | ||
senderModule string, | ||
recipientAddr sdk.AccAddress, | ||
coins sdk.Coins, | ||
) error { | ||
// Use the embedded function from [bankkeeper.Keeper] | ||
if err := bk.BaseKeeper.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, coins); err != nil { | ||
return err | ||
} | ||
if findEtherBalanceChangeFromCoins(coins) { | ||
moduleBech32Addr := auth.NewModuleAddress(senderModule) | ||
bk.SyncStateDBWithAccount(ctx, moduleBech32Addr) | ||
bk.SyncStateDBWithAccount(ctx, recipientAddr) | ||
} | ||
return nil | ||
} | ||
|
||
func (bk NibiruBankKeeper) SendCoinsFromModuleToModule( | ||
ctx sdk.Context, | ||
senderModule string, | ||
recipientModule string, | ||
coins sdk.Coins, | ||
) error { | ||
// Use the embedded function from [bankkeeper.Keeper] | ||
if err := bk.BaseKeeper.SendCoinsFromModuleToModule(ctx, senderModule, recipientModule, coins); err != nil { | ||
return err | ||
} | ||
if findEtherBalanceChangeFromCoins(coins) { | ||
senderBech32Addr := auth.NewModuleAddress(senderModule) | ||
recipientBech32Addr := auth.NewModuleAddress(recipientModule) | ||
bk.SyncStateDBWithAccount(ctx, senderBech32Addr) | ||
bk.SyncStateDBWithAccount(ctx, recipientBech32Addr) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.