add tx info extracter

This commit is contained in:
Solovyov1796 2025-03-10 20:32:07 +08:00
parent 45508f5954
commit c2fdb3109e

View File

@ -19,6 +19,7 @@ import (
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types" snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
"github.com/cosmos/cosmos-sdk/store" "github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/crisis" "github.com/cosmos/cosmos-sdk/x/crisis"
ethermintflags "github.com/evmos/ethermint/server/flags" ethermintflags "github.com/evmos/ethermint/server/flags"
"github.com/spf13/cast" "github.com/spf13/cast"
@ -26,6 +27,8 @@ import (
"github.com/0glabs/0g-chain/app" "github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/app/params" "github.com/0glabs/0g-chain/app/params"
gethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
) )
const ( const (
@ -107,8 +110,6 @@ func (ac appCreator) newApp(
skipLoadLatest = cast.ToBool(appOpts.Get(flagSkipLoadLatest)) skipLoadLatest = cast.ToBool(appOpts.Get(flagSkipLoadLatest))
} }
mempool := app.NewPriorityMempool(app.PriorityNonceWithMaxTx(cast.ToInt(appOpts.Get(server.FlagMempoolMaxTxs))))
bApp := app.NewBaseApp(logger, db, ac.encodingConfig, bApp := app.NewBaseApp(logger, db, ac.encodingConfig,
baseapp.SetPruning(pruningOpts), baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(strings.Replace(cast.ToString(appOpts.Get(server.FlagMinGasPrices)), ";", ",", -1)), baseapp.SetMinGasPrices(strings.Replace(cast.ToString(appOpts.Get(server.FlagMinGasPrices)), ";", ",", -1)),
@ -123,8 +124,14 @@ func (ac appCreator) newApp(
baseapp.SetIAVLDisableFastNode(cast.ToBool(iavlDisableFastNode)), baseapp.SetIAVLDisableFastNode(cast.ToBool(iavlDisableFastNode)),
baseapp.SetIAVLLazyLoading(cast.ToBool(appOpts.Get(server.FlagIAVLLazyLoading))), baseapp.SetIAVLLazyLoading(cast.ToBool(appOpts.Get(server.FlagIAVLLazyLoading))),
baseapp.SetChainID(chainID), baseapp.SetChainID(chainID),
baseapp.SetMempool(mempool), baseapp.SetTxInfoExtracter(extractTxInfo),
) )
mempool := app.NewPriorityMempool(
app.PriorityNonceWithMaxTx(cast.ToInt(appOpts.Get(server.FlagMempoolMaxTxs))),
)
bApp.SetMempool(mempool)
bApp.SetTxEncoder(ac.encodingConfig.TxConfig.TxEncoder()) bApp.SetTxEncoder(ac.encodingConfig.TxConfig.TxEncoder())
abciProposalHandler := app.NewDefaultProposalHandler(mempool, bApp) abciProposalHandler := app.NewDefaultProposalHandler(mempool, bApp)
bApp.SetPrepareProposal(abciProposalHandler.PrepareProposalHandler()) bApp.SetPrepareProposal(abciProposalHandler.PrepareProposalHandler())
@ -199,3 +206,52 @@ func accAddressesFromBech32(addresses ...string) ([]sdk.AccAddress, error) {
} }
return decodedAddresses, nil return decodedAddresses, nil
} }
var ErrMustHaveSigner error = errors.New("tx must have at least one signer")
func extractTxInfo(ctx sdk.Context, tx sdk.Tx) (*sdk.TxInfo, error) {
sigs, err := tx.(signing.SigVerifiableTx).GetSignaturesV2()
if err != nil {
return nil, err
}
var sender string
var nonce uint64
var gasPrice uint64
var gasLimit uint64
var txType int32
if len(sigs) == 0 {
txType = 1
msgs := tx.GetMsgs()
if len(msgs) != 1 {
return nil, ErrMustHaveSigner
}
msgEthTx, ok := msgs[0].(*evmtypes.MsgEthereumTx)
if !ok {
return nil, ErrMustHaveSigner
}
ethTx := msgEthTx.AsTransaction()
signer := gethtypes.NewEIP2930Signer(ethTx.ChainId())
ethSender, err := signer.Sender(ethTx)
if err != nil {
return nil, ErrMustHaveSigner
}
sender = sdk.AccAddress(ethSender.Bytes()).String()
nonce = ethTx.Nonce()
gasPrice = ethTx.GasPrice().Uint64()
gasLimit = ethTx.Gas()
} else {
sig := sigs[0]
sender = sdk.AccAddress(sig.PubKey.Address()).String()
nonce = sig.Sequence
}
return &sdk.TxInfo{
SignerAddress: sender,
Nonce: nonce,
GasLimit: gasLimit,
GasPrice: gasPrice,
TxType: txType,
}, nil
}