github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/baseapp/helpers_okchain.go (about)

     1  package baseapp
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
     6  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
     7  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
     8  )
     9  
    10  func (app *BaseApp) PushAnteHandler(ah sdk.AnteHandler) {
    11  	app.anteHandler = ah
    12  }
    13  
    14  func (app *BaseApp) GetDeliverStateCtx() sdk.Context {
    15  	return app.deliverState.ctx
    16  }
    17  
    18  // TraceTx returns the trace log for the target tx
    19  // To trace the target tx, the context must be set to the specific block at first,
    20  // and the predesessors in the same block must be run before tracing the tx.
    21  // The runtx procedure for TraceTx is nearly same with that for DeliverTx,  but the
    22  // state was saved in different Cache in app.
    23  func (app *BaseApp) TraceTx(queryTraceTx sdk.QueryTraceTx, targetTx sdk.Tx, txIndex uint32, block *tmtypes.Block) (*sdk.Result, error) {
    24  
    25  	//get first tx
    26  	targetTxData := queryTraceTx.TxHash.Bytes()
    27  	var initialTxBytes []byte
    28  	predesessors := block.Txs[:txIndex]
    29  	if len(predesessors) == 0 {
    30  		initialTxBytes = targetTxData
    31  	} else {
    32  		initialTxBytes = predesessors[0]
    33  	}
    34  
    35  	//begin trace block to init traceState and traceBlockCache
    36  	traceState, err := app.beginBlockForTracing(initialTxBytes, block)
    37  	if err != nil {
    38  		return nil, sdkerrors.Wrap(err, "failed to beginblock for tracing")
    39  	}
    40  
    41  	traceState.ctx.SetIsTraceTxLog(false)
    42  	//pre deliver prodesessor tx to get the right state
    43  	for _, predesessor := range block.Txs[:txIndex] {
    44  		tx, err := app.txDecoder(predesessor, block.Height)
    45  		if err != nil {
    46  			return nil, sdkerrors.Wrap(err, "invalid prodesessor")
    47  		}
    48  		app.tracetx(predesessor, tx, block.Height, traceState)
    49  		//ignore the err when run prodesessor
    50  	}
    51  
    52  	//trace tx
    53  	traceState.ctx.SetIsTraceTxLog(true)
    54  	traceState.ctx.SetTraceTxLogConfig(queryTraceTx.ConfigBytes)
    55  	info, err := app.tracetx(targetTxData, targetTx, block.Height, traceState)
    56  	if info == nil {
    57  		return nil, err
    58  	}
    59  	return info.result, err
    60  }
    61  func (app *BaseApp) tracetx(txBytes []byte, tx sdk.Tx, height int64, traceState *state) (info *runTxInfo, err error) {
    62  
    63  	mode := runTxModeTrace
    64  	//prepare runTxInfo to runtx
    65  	info = &runTxInfo{}
    66  	//init info.ctx
    67  	info.ctx = traceState.ctx
    68  	info.ctx.SetTxBytes(txBytes).
    69  		SetVoteInfos(app.voteInfos).
    70  		SetConsensusParams(app.consensusParams)
    71  
    72  	err = app.runtxWithInfo(info, mode, txBytes, tx, height)
    73  	return info, err
    74  }
    75  func (app *BaseApp) beginBlockForTracing(firstTx []byte, block *tmtypes.Block) (*state, error) {
    76  
    77  	req := abci.RequestBeginBlock{
    78  		Hash:   block.Hash(),
    79  		Header: tmtypes.TM2PB.Header(&block.Header),
    80  	}
    81  
    82  	//set traceState instead of app.deliverState
    83  	//need to reset to version = req.Header.Height-1
    84  	traceState, err := app.newTraceState(req.Header, req.Header.Height-1)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	// use the same block gas meter with deliver mode
    90  	var gasMeter sdk.GasMeter
    91  	if maxGas := app.getMaximumBlockGas(); maxGas > 0 {
    92  		gasMeter = sdk.NewGasMeter(maxGas)
    93  	} else {
    94  		gasMeter = sdk.NewInfiniteGasMeter()
    95  	}
    96  
    97  	traceState.ctx.SetBlockGasMeter(gasMeter)
    98  
    99  	//set the trace mode to prevent the ante handler to check the nounce
   100  	traceState.ctx.SetIsTraceTx(true)
   101  	traceState.ctx.SetIsCheckTx(true)
   102  
   103  	//app begin block
   104  	if app.beginBlocker != nil {
   105  		_ = app.beginBlocker(traceState.ctx, req)
   106  	}
   107  
   108  	// No need to set the signed validators for addition to context in deliverTx
   109  	return traceState, nil
   110  }