-
Notifications
You must be signed in to change notification settings - Fork 818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use per-tx escrow for Wei balance #1314
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/sei-protocol/sei-chain/x/evm/state" | ||
) | ||
|
||
func (k *Keeper) SettleWeiEscrowAccounts(ctx sdk.Context, evmTxDeferredInfoList []EvmTxDeferredInfo) { | ||
denom := k.GetBaseDenom(ctx) | ||
// settle surplus escrow first | ||
for _, info := range evmTxDeferredInfoList { | ||
escrow := state.GetTempWeiEscrowAddress(info.TxIndx) | ||
seiBalance := k.BankKeeper().GetBalance(ctx, escrow, denom) | ||
if !seiBalance.Amount.IsPositive() { | ||
continue | ||
} | ||
if err := k.BankKeeper().SendCoinsFromAccountToModule(ctx, escrow, banktypes.WeiEscrowName, sdk.NewCoins(seiBalance)); err != nil { | ||
ctx.Logger().Error(fmt.Sprintf("failed to send %s from escrow %d to global escrow", seiBalance.String(), info.TxIndx)) | ||
// This should not happen in any case. We want to halt the chain if it does | ||
panic(err) | ||
} | ||
} | ||
// settle deficit escrows | ||
for _, info := range evmTxDeferredInfoList { | ||
escrow := state.GetTempWeiEscrowAddress(info.TxIndx) | ||
seiBalance := k.BankKeeper().GetBalance(ctx, escrow, denom) | ||
if !seiBalance.Amount.IsNegative() { | ||
continue | ||
} | ||
settleAmt := sdk.NewCoin(seiBalance.Denom, seiBalance.Amount.Neg()) | ||
if err := k.BankKeeper().SendCoinsFromModuleToAccount(ctx, banktypes.WeiEscrowName, escrow, sdk.NewCoins(settleAmt)); err != nil { | ||
ctx.Logger().Error(fmt.Sprintf("failed to send %s from global escrow to escrow %d", settleAmt.String(), info.TxIndx)) | ||
// This should not happen in any case. We want to halt the chain if it does | ||
panic(err) | ||
Check warning Code scanning / CodeQL Panic in BeginBock or EndBlock consensus methods Warning
Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
|
||
} | ||
} | ||
// sanity check | ||
for _, info := range evmTxDeferredInfoList { | ||
escrow := state.GetTempWeiEscrowAddress(info.TxIndx) | ||
seiBalance := k.BankKeeper().GetBalance(ctx, escrow, denom) | ||
if !seiBalance.Amount.IsZero() { | ||
panic(fmt.Sprintf("failed to settle escrow account %d which still has a balance of %s", info.TxIndx, seiBalance.String())) | ||
Check warning Code scanning / CodeQL Panic in BeginBock or EndBlock consensus methods Warning
Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper" | ||
"github.com/sei-protocol/sei-chain/x/evm/state" | ||
"github.com/sei-protocol/sei-chain/x/evm/types" | ||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
func TestSettleCommon(t *testing.T) { | ||
k, ctx := testkeeper.MockEVMKeeper() | ||
|
||
_, addr1 := testkeeper.MockAddressPair() | ||
_, addr2 := testkeeper.MockAddressPair() | ||
_, addr3 := testkeeper.MockAddressPair() | ||
amt := sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(10))) | ||
k.BankKeeper().MintCoins(ctx, types.ModuleName, amt) | ||
k.BankKeeper().SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.AccAddress(addr1[:]), amt) | ||
|
||
// addr1 send one sei and one Wei to addr2 | ||
// escrow 1 would have a one-sei surplus | ||
s := state.NewDBImpl(ctx.WithTxIndex(1), k, false) | ||
s.SubBalance(addr1, big.NewInt(1_000_000_000_001)) | ||
s.AddBalance(addr2, big.NewInt(1_000_000_000_001)) | ||
require.Nil(t, s.Finalize()) | ||
k.AppendToEvmTxDeferredInfo(ctx.WithTxIndex(1), ethtypes.Bloom{}, common.Hash{}) | ||
|
||
// addr2 send two weis to addr3 | ||
// escrow 2 would have a one-sei surplus | ||
s = state.NewDBImpl(ctx.WithTxIndex(2), k, false) | ||
s.SubBalance(addr2, big.NewInt(2)) | ||
s.AddBalance(addr3, big.NewInt(2)) | ||
require.Nil(t, s.Finalize()) | ||
k.AppendToEvmTxDeferredInfo(ctx.WithTxIndex(2), ethtypes.Bloom{}, common.Hash{}) | ||
|
||
// addr1 send one wei to addr2 | ||
// escrow 3 would have a one-sei deficit | ||
s = state.NewDBImpl(ctx.WithTxIndex(3), k, false) | ||
s.SubBalance(addr1, big.NewInt(1)) | ||
s.AddBalance(addr2, big.NewInt(1)) | ||
require.Nil(t, s.Finalize()) | ||
k.AppendToEvmTxDeferredInfo(ctx.WithTxIndex(3), ethtypes.Bloom{}, common.Hash{}) | ||
|
||
globalEscrowBalance := k.BankKeeper().GetBalance(ctx, k.AccountKeeper().GetModuleAddress(banktypes.WeiEscrowName), "usei") | ||
require.True(t, globalEscrowBalance.Amount.IsZero()) | ||
|
||
k.SetTxResults([]*abci.ExecTxResult{{Code: 0}, {Code: 0}, {Code: 0}, {Code: 0}}) | ||
deferredInfo := k.GetEVMTxDeferredInfo(ctx) | ||
k.SettleWeiEscrowAccounts(ctx, deferredInfo) | ||
globalEscrowBalance = k.BankKeeper().GetBalance(ctx, k.AccountKeeper().GetModuleAddress(banktypes.WeiEscrowName), "usei") | ||
require.Equal(t, int64(1), globalEscrowBalance.Amount.Int64()) | ||
} | ||
|
||
func TestSettleMultiRedeem(t *testing.T) { | ||
k, ctx := testkeeper.MockEVMKeeper() | ||
|
||
_, addr1 := testkeeper.MockAddressPair() | ||
_, addr2 := testkeeper.MockAddressPair() | ||
_, addr3 := testkeeper.MockAddressPair() | ||
amt := sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(1))) | ||
k.BankKeeper().MintCoins(ctx, types.ModuleName, amt) | ||
k.BankKeeper().SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.AccAddress(addr1[:]), amt) | ||
k.BankKeeper().MintCoins(ctx, types.ModuleName, amt) | ||
k.BankKeeper().SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.AccAddress(addr2[:]), amt) | ||
k.BankKeeper().MintCoins(ctx, types.ModuleName, amt) | ||
k.BankKeeper().SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.AccAddress(addr3[:]), amt) | ||
|
||
// addr1 send one Wei to addr3 | ||
// addr2 send one Wei to addr3 | ||
// escrow 1 would have a two-sei surplus | ||
s := state.NewDBImpl(ctx.WithTxIndex(1), k, false) | ||
s.SubBalance(addr1, big.NewInt(1)) | ||
s.AddBalance(addr3, big.NewInt(1)) | ||
s.SubBalance(addr2, big.NewInt(1)) | ||
s.AddBalance(addr3, big.NewInt(1)) | ||
require.Nil(t, s.Finalize()) | ||
k.AppendToEvmTxDeferredInfo(ctx.WithTxIndex(1), ethtypes.Bloom{}, common.Hash{}) | ||
|
||
// addr3 send one wei to addr1 | ||
// addr3 send one wei to addr2 | ||
// addr3 send one wei to addr1 | ||
// escrow 2 would have a one-sei deficit | ||
s = state.NewDBImpl(ctx.WithTxIndex(2), k, false) | ||
s.SubBalance(addr3, big.NewInt(1)) | ||
s.AddBalance(addr1, big.NewInt(1)) | ||
s.SubBalance(addr3, big.NewInt(1)) | ||
s.AddBalance(addr2, big.NewInt(1)) | ||
s.SubBalance(addr3, big.NewInt(1)) | ||
s.AddBalance(addr1, big.NewInt(1)) | ||
require.Nil(t, s.Finalize()) | ||
k.AppendToEvmTxDeferredInfo(ctx.WithTxIndex(2), ethtypes.Bloom{}, common.Hash{}) | ||
|
||
globalEscrowBalance := k.BankKeeper().GetBalance(ctx, k.AccountKeeper().GetModuleAddress(banktypes.WeiEscrowName), "usei") | ||
require.True(t, globalEscrowBalance.Amount.IsZero()) | ||
|
||
k.SetTxResults([]*abci.ExecTxResult{{Code: 0}, {Code: 0}, {Code: 0}}) | ||
deferredInfo := k.GetEVMTxDeferredInfo(ctx) | ||
k.SettleWeiEscrowAccounts(ctx, deferredInfo) | ||
globalEscrowBalance = k.BankKeeper().GetBalance(ctx, k.AccountKeeper().GetModuleAddress(banktypes.WeiEscrowName), "usei") | ||
require.Equal(t, int64(1), globalEscrowBalance.Amount.Int64()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,18 +171,19 @@ | |
// returns no validator updates. | ||
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { | ||
evmTxDeferredInfoList := am.keeper.GetEVMTxDeferredInfo(ctx) | ||
am.keeper.SettleWeiEscrowAccounts(ctx, evmTxDeferredInfoList) | ||
Check warning Code scanning / CodeQL Panic in BeginBock or EndBlock consensus methods Warning
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the future, if we allow evm txs into the prioritized execution section, is it necessary to settle the balances after EACH execution group, or is it fine to simply settle in endblock? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's fine to settle in the endblock. Technically I don't think we ever need to settle, but having negative balances may break some UI (if someone deliberately query the escrow accounts) so we settle it, but as long as end-of-block state is settled we should be ok |
||
denom := am.keeper.GetBaseDenom(ctx) | ||
for _, deferredInfo := range evmTxDeferredInfoList { | ||
idx := deferredInfo.TxIndx | ||
middleManAddress := state.GetMiddleManAddress(sdk.Context{}.WithTxIndex(idx)) | ||
middleManAddress := state.GetMiddleManAddress(idx) | ||
balance := am.keeper.BankKeeper().GetBalance(ctx, middleManAddress, denom) | ||
weiBalance := am.keeper.BankKeeper().GetWeiBalance(ctx, middleManAddress) | ||
if !balance.Amount.IsZero() || !weiBalance.IsZero() { | ||
if err := am.keeper.BankKeeper().SendCoinsAndWei(ctx, middleManAddress, am.keeper.AccountKeeper().GetModuleAddress(types.ModuleName), nil, denom, balance.Amount, weiBalance); err != nil { | ||
panic(err) | ||
} | ||
} | ||
coinbaseAddress := state.GetCoinbaseAddress(sdk.Context{}.WithTxIndex(idx)) | ||
coinbaseAddress := state.GetCoinbaseAddress(idx) | ||
balance = am.keeper.BankKeeper().GetBalance(ctx, coinbaseAddress, denom) | ||
weiBalance = am.keeper.BankKeeper().GetWeiBalance(ctx, coinbaseAddress) | ||
if !balance.Amount.IsZero() || !weiBalance.IsZero() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,19 +13,26 @@ var UseiToSweiMultiplier = big.NewInt(1_000_000_000_000) | |
|
||
var MiddleManAddressPrefix = []byte("evm_middleman") | ||
var CoinbaseAddressPrefix = []byte("evm_coinbase") | ||
var WeiTmpEscrowPrefix = []byte("evm_weiescrow") | ||
|
||
func GetMiddleManAddress(ctx sdk.Context) sdk.AccAddress { | ||
func GetMiddleManAddress(txIdx int) sdk.AccAddress { | ||
txIndexBz := make([]byte, 8) | ||
binary.BigEndian.PutUint64(txIndexBz, uint64(ctx.TxIndex())) | ||
binary.BigEndian.PutUint64(txIndexBz, uint64(txIdx)) | ||
return sdk.AccAddress(append(MiddleManAddressPrefix, txIndexBz...)) | ||
} | ||
|
||
func GetCoinbaseAddress(ctx sdk.Context) sdk.AccAddress { | ||
func GetCoinbaseAddress(txIdx int) sdk.AccAddress { | ||
txIndexBz := make([]byte, 8) | ||
binary.BigEndian.PutUint64(txIndexBz, uint64(ctx.TxIndex())) | ||
binary.BigEndian.PutUint64(txIndexBz, uint64(txIdx)) | ||
return sdk.AccAddress(append(CoinbaseAddressPrefix, txIndexBz...)) | ||
} | ||
|
||
func GetTempWeiEscrowAddress(txIdx int) sdk.AccAddress { | ||
txIndexBz := make([]byte, 8) | ||
binary.BigEndian.PutUint64(txIndexBz, uint64(txIdx)) | ||
return sdk.AccAddress(append(WeiTmpEscrowPrefix, txIndexBz...)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have a guarantee that this cannot conflict with other user addresses? I'm assuming it wont because there are fewer bytes used in generating the Acc Address? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah the bytes are different |
||
} | ||
|
||
func SplitUseiWeiAmount(amt *big.Int) (usei *big.Int, wei *big.Int) { | ||
wei = new(big.Int).Mod(amt, UseiToSweiMultiplier) | ||
usei = new(big.Int).Quo(amt, UseiToSweiMultiplier) | ||
|
Check warning
Code scanning / CodeQL
Panic in BeginBock or EndBlock consensus methods Warning