Skip to content

Commit

Permalink
fix cmtx nonce (#3190)
Browse files Browse the repository at this point in the history
* set nonce of IbcTx

* use ethAddr as fromAddr in mempool for all types of tx

* fix tm rpc
  • Loading branch information
yann-sjtu authored Jul 6, 2023
1 parent 22376e2 commit 691f8c3
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
10 changes: 7 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,13 @@ func NewEvmSysContractAddressHandler(ak *evm.Keeper) sdk.EvmSysContractAddressHa

func NewUpdateCMTxNonceHandler() sdk.UpdateCMTxNonceHandler {
return func(tx sdk.Tx, nonce uint64) {
stdtx, ok := tx.(*authtypes.StdTx)
if ok && nonce != 0 {
stdtx.Nonce = nonce
if nonce != 0 {
switch v := tx.(type) {
case *authtypes.StdTx:
v.Nonce = nonce
case *authtypes.IbcTx:
v.Nonce = nonce
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions libs/cosmos-sdk/types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Tx interface {

GetRaw() []byte
GetFrom() string
GetEthAddr() string
GetSender(ctx Context) string
GetNonce() uint64
TxHash() []byte
Expand Down Expand Up @@ -86,6 +87,7 @@ func (tx *BaseTx) GetSigners() []AccAddress { return nil }
func (tx *BaseTx) GetGas() uint64 { return 0 }
func (tx *BaseTx) GetNonce() uint64 { return tx.Nonce }
func (tx *BaseTx) GetFrom() string { return tx.From }
func (tx *BaseTx) GetEthAddr() string { return tx.GetFrom() }
func (tx *BaseTx) GetRaw() []byte { return tx.Raw }
func (tx *BaseTx) TxHash() []byte { return tx.Hash }
func (tx *BaseTx) SetRaw(raw []byte) { tx.Raw = raw }
Expand Down
8 changes: 5 additions & 3 deletions libs/cosmos-sdk/x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ante
import (
"bytes"
"encoding/hex"

ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/okex/exchain/app/crypto/ethsecp256k1"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
Expand Down Expand Up @@ -242,13 +244,13 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
}
if ctx.IsCheckTx() {
if txNonce != 0 { // txNonce first
err := nonceVerification(ctx, signerAccs[i].GetSequence(), txNonce, signerAddrs[i].String(), simulate)
err := nonceVerification(ctx, signerAccs[i].GetSequence(), txNonce, ethcmn.BytesToAddress(signerAddrs[i]).String(), simulate)
if err != nil {
return ctx, err
}
signerAccs[i].SetSequence(txNonce)
} else { // for adaptive pending tx in mempool just in checkTx but not deliverTx
pendingNonce := getCheckTxNonceFromMempool(signerAddrs[i].String())
pendingNonce := getCheckTxNonceFromMempool(ethcmn.BytesToAddress(signerAddrs[i]).String())
if pendingNonce != 0 {
signerAccs[i].SetSequence(pendingNonce)
}
Expand Down Expand Up @@ -327,7 +329,7 @@ func (isd IncrementSequenceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
acc := isd.ak.GetAccount(ctx, addr)
// for adaptive pending tx in mempool just in checkTx but not deliverTx
if ctx.IsCheckTx() && !ctx.IsReCheckTx() {
pendingNonce := getCheckTxNonceFromMempool(addr.String())
pendingNonce := getCheckTxNonceFromMempool(ethcmn.BytesToAddress(addr).String())
if pendingNonce != 0 {
acc.SetSequence(pendingNonce)
}
Expand Down
14 changes: 11 additions & 3 deletions libs/cosmos-sdk/x/auth/types/stdtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"fmt"
"math/big"

ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/tendermint/go-amino"
yaml "gopkg.in/yaml.v2"

"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
sdkerrors "github.com/okex/exchain/libs/cosmos-sdk/types/errors"
"github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported"
"github.com/okex/exchain/libs/tendermint/crypto"
cryptoamino "github.com/okex/exchain/libs/tendermint/crypto/encoding/amino"
"github.com/okex/exchain/libs/tendermint/crypto/multisig"
"github.com/tendermint/go-amino"
yaml "gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -305,6 +305,14 @@ func (tx *StdTx) GetFrom() string {
return signers[0].String()
}

func (tx *StdTx) GetEthAddr() string {
signers := tx.GetSigners()
if len(signers) == 0 {
return ""
}
return ethcmn.BytesToAddress(signers[0]).String()
}

func (tx *StdTx) GetSender(_ sdk.Context) string {
return tx.GetFrom()
}
Expand Down
4 changes: 2 additions & 2 deletions libs/tendermint/abci/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "math/big"
type TxEssentials interface {
GetRaw() []byte
TxHash() []byte
GetFrom() string
GetEthAddr() string
GetNonce() uint64
GetGasPrice() *big.Int
}
Expand All @@ -26,7 +26,7 @@ func (tx MockTx) TxHash() []byte {
return tx.Hash
}

func (tx MockTx) GetFrom() string {
func (tx MockTx) GetEthAddr() string {
return tx.From
}

Expand Down
6 changes: 3 additions & 3 deletions libs/tendermint/mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func (mem *CListMempool) resCbFirstTime(
realTx: r.CheckTx.Tx,
nodeKey: txInfo.wtx.GetNodeKey(),
signature: txInfo.wtx.GetSignature(),
from: r.CheckTx.Tx.GetFrom(),
from: r.CheckTx.Tx.GetEthAddr(),
senderNonce: r.CheckTx.SenderNonce,
}

Expand Down Expand Up @@ -789,7 +789,7 @@ func (mem *CListMempool) resCbRecheck(req *abci.Request, res *abci.Response) {
if mem.config.PendingRemoveEvent {
mem.rmPendingTxChan <- types.EventDataRmPendingTx{
memTx.realTx.TxHash(),
memTx.realTx.GetFrom(),
memTx.realTx.GetEthAddr(),
memTx.realTx.GetNonce(),
types.Recheck,
}
Expand Down Expand Up @@ -1495,7 +1495,7 @@ func (mem *CListMempool) deleteMinGPTxOnlyFull() {
mem.cache.RemoveKey(txOrTxHashToKey(removeMemTx.tx, removeMemTxHash, removeMemTx.Height()))

if mem.config.PendingRemoveEvent {
mem.rmPendingTxChan <- types.EventDataRmPendingTx{removeMemTxHash, removeMemTx.realTx.GetFrom(), removeMemTx.realTx.GetNonce(), types.MinGasPrice}
mem.rmPendingTxChan <- types.EventDataRmPendingTx{removeMemTxHash, removeMemTx.realTx.GetEthAddr(), removeMemTx.realTx.GetNonce(), types.MinGasPrice}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions libs/tendermint/rpc/core/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,20 @@ func UserUnconfirmedTxs(address string, limit int) (*ctypes.ResultUserUnconfirme
Txs: txs}, nil
}

func TmUserUnconfirmedTxs(ctx *rpctypes.Context, address string, limit int) (*ctypes.ResultUserUnconfirmedTxs, error) {
return UserUnconfirmedTxs(address, limit)
}

func UserNumUnconfirmedTxs(address string) (*ctypes.ResultUserUnconfirmedTxs, error) {
nums := env.Mempool.ReapUserTxsCnt(address)
return &ctypes.ResultUserUnconfirmedTxs{
Count: nums}, nil
}

func TmUserNumUnconfirmedTxs(ctx *rpctypes.Context, address string) (*ctypes.ResultUserUnconfirmedTxs, error) {
return UserNumUnconfirmedTxs(address)
}

func GetUnconfirmedTxByHash(hash [sha256.Size]byte) (types.Tx, error) {
return env.Mempool.GetTxByHash(hash)
}
Expand All @@ -189,6 +197,10 @@ func GetAddressList() (*ctypes.ResultUnconfirmedAddresses, error) {
}, nil
}

func TmGetAddressList(ctx *rpctypes.Context) (*ctypes.ResultUnconfirmedAddresses, error) {
return GetAddressList()
}

func GetPendingNonce(address string) (*ctypes.ResultPendingNonce, bool) {
nonce, ok := env.Mempool.GetPendingNonce(address)
if !ok {
Expand Down
6 changes: 3 additions & 3 deletions libs/tendermint/rpc/core/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var Routes = map[string]*rpc.RPCFunc{
"consensus_params": rpc.NewRPCFunc(ConsensusParams, "height"),
"unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxs, "limit"),
"num_unconfirmed_txs": rpc.NewRPCFunc(NumUnconfirmedTxs, ""),
"user_unconfirmed_txs": rpc.NewRPCFunc(UserUnconfirmedTxs, "address,limit"),
"user_num_unconfirmed_txs": rpc.NewRPCFunc(UserNumUnconfirmedTxs, "address"),
"get_address_list": rpc.NewRPCFunc(GetAddressList, ""),
"user_unconfirmed_txs": rpc.NewRPCFunc(TmUserUnconfirmedTxs, "address,limit"),
"user_num_unconfirmed_txs": rpc.NewRPCFunc(TmUserNumUnconfirmedTxs, "address"),
"get_address_list": rpc.NewRPCFunc(TmGetAddressList, ""),
"block_search": rpc.NewRPCFunc(BlockSearch, "query,page,per_page,order_by"),

// tx broadcast API
Expand Down
4 changes: 4 additions & 0 deletions x/evm/types/msg_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (tx *MsgEthereumTx) GetFrom() string {
return from
}

func (tx *MsgEthereumTx) GetEthAddr() string {
return tx.GetFrom()
}

func (msg MsgEthereumTx) GetSender(ctx sdk.Context) string {
chainID, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
Expand Down

0 comments on commit 691f8c3

Please sign in to comment.