github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/txs/base/common.go (about)

     1  package base
     2  
     3  import (
     4  	"math/big"
     5  	"sync"
     6  
     7  	"github.com/ethereum/go-ethereum/common"
     8  	ethermint "github.com/fibonacci-chain/fbc/app/types"
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    11  	"github.com/fibonacci-chain/fbc/x/evm/types"
    12  )
    13  
    14  var commitStateDBPool = &sync.Pool{
    15  	New: func() interface{} {
    16  		return &types.CommitStateDB{GuFactor: types.DefaultGuFactor}
    17  	},
    18  }
    19  
    20  func getCommitStateDB() *types.CommitStateDB {
    21  	return commitStateDBPool.Get().(*types.CommitStateDB)
    22  }
    23  
    24  func putCommitStateDB(st *types.CommitStateDB) {
    25  	commitStateDBPool.Put(st)
    26  }
    27  
    28  func msg2st(ctx *sdk.Context, k *Keeper, msg *types.MsgEthereumTx, st *types.StateTransition) (reuseCsdb bool, err error) {
    29  	var chainIDEpoch *big.Int
    30  	chainIDEpoch, err = ethermint.ParseChainID(ctx.ChainID())
    31  	if err != nil {
    32  		return
    33  	}
    34  
    35  	var sender common.Address
    36  	// Verify signature and retrieve sender address
    37  	sender, err = getSender(ctx, chainIDEpoch, msg)
    38  	if err != nil {
    39  		return
    40  	}
    41  
    42  	txHash := tmtypes.Tx(ctx.TxBytes()).Hash(ctx.BlockHeight())
    43  	ethHash := common.BytesToHash(txHash)
    44  
    45  	st.AccountNonce = msg.Data.AccountNonce
    46  	st.Price = msg.Data.Price
    47  	st.GasLimit = msg.Data.GasLimit
    48  	st.Recipient = msg.Data.Recipient
    49  	st.Amount = msg.Data.Amount
    50  	st.Payload = msg.Data.Payload
    51  	st.ChainID = chainIDEpoch
    52  	st.TxHash = &ethHash
    53  	st.Sender = sender
    54  	st.Simulate = ctx.IsCheckTx()
    55  	st.TraceTx = ctx.IsTraceTx()
    56  	st.TraceTxLog = ctx.IsTraceTxLog()
    57  
    58  	if tmtypes.HigherThanMars(ctx.BlockHeight()) && ctx.IsDeliver() {
    59  		st.Csdb = k.EvmStateDb.WithContext(*ctx)
    60  	} else {
    61  		csdb := getCommitStateDB()
    62  		types.ResetCommitStateDB(csdb, k.GenerateCSDBParams(), ctx)
    63  		st.Csdb = csdb
    64  		reuseCsdb = true
    65  	}
    66  
    67  	return
    68  }
    69  
    70  func getSender(ctx *sdk.Context, chainIDEpoch *big.Int, msg *types.MsgEthereumTx) (sender common.Address, err error) {
    71  	if ctx.IsCheckTx() {
    72  		if from := ctx.From(); len(from) > 0 {
    73  			return common.HexToAddress(from), nil
    74  		}
    75  	}
    76  	err = msg.VerifySig(chainIDEpoch, ctx.BlockHeight())
    77  	if err == nil {
    78  		sender = msg.EthereumAddress()
    79  	}
    80  
    81  	return
    82  }