github.com/okex/exchain@v1.8.0/libs/tendermint/abci/types/application.go (about)

     1  package types // nolint: goimports
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  )
     6  
     7  // Application is an interface that enables any finite, deterministic state machine
     8  // to be driven by a blockchain-based replication engine via the ABCI.
     9  // All methods take a RequestXxx argument and return a ResponseXxx argument,
    10  // except CheckTx/DeliverTx, which take `tx []byte`, and `Commit`, which takes nothing.
    11  type Application interface {
    12  	// Info/Query Connection
    13  	Info(RequestInfo) ResponseInfo                // Return application info
    14  	SetOption(RequestSetOption) ResponseSetOption // Set application option
    15  	Query(RequestQuery) ResponseQuery             // Query for state
    16  
    17  	// Mempool Connection
    18  	CheckTx(RequestCheckTx) ResponseCheckTx // Validate a tx for the mempool
    19  
    20  	// Consensus Connection
    21  	InitChain(RequestInitChain) ResponseInitChain    // Initialize blockchain w validators/other info from TendermintCore
    22  	BeginBlock(RequestBeginBlock) ResponseBeginBlock // Signals the beginning of a block
    23  	DeliverTx(RequestDeliverTx) ResponseDeliverTx    // Deliver a tx for full processing
    24  	EndBlock(RequestEndBlock) ResponseEndBlock       // Signals the end of a block, returns changes to the validator set
    25  
    26  	Commit(RequestCommit) ResponseCommit // Commit the state and return the application Merkle root hash
    27  	ParallelTxs(txs [][]byte, onlyCalSender bool) []*ResponseDeliverTx
    28  	// PreDeliverRealTx will try convert bytes of tx to TxEssentials, it should be thread safe,
    29  	// if return nil, it means failed or this Application doesn't support PreDeliverRealTx and you must use DeliverTx,
    30  	// else, you can call DeliverRealTx to process the TxEssentials
    31  	PreDeliverRealTx([]byte) TxEssentials
    32  	// DeliverRealTx deliver tx returned by PreDeliverRealTx, if PreDeliverRealTx returns nil, DeliverRealTx SHOULD NOT be called
    33  	DeliverRealTx(TxEssentials) ResponseDeliverTx
    34  }
    35  
    36  //-------------------------------------------------------
    37  // BaseApplication is a base form of Application
    38  
    39  var _ Application = (*BaseApplication)(nil)
    40  
    41  type BaseApplication struct {
    42  }
    43  
    44  func NewBaseApplication() *BaseApplication {
    45  	return &BaseApplication{}
    46  }
    47  
    48  func (BaseApplication) Info(req RequestInfo) ResponseInfo {
    49  	return ResponseInfo{}
    50  }
    51  
    52  func (BaseApplication) SetOption(req RequestSetOption) ResponseSetOption {
    53  	return ResponseSetOption{}
    54  }
    55  
    56  func (BaseApplication) DeliverTx(req RequestDeliverTx) ResponseDeliverTx {
    57  	return ResponseDeliverTx{Code: CodeTypeOK}
    58  }
    59  
    60  func (BaseApplication) PreDeliverRealTx([]byte) TxEssentials {
    61  	return nil
    62  }
    63  
    64  func (BaseApplication) DeliverRealTx(TxEssentials) ResponseDeliverTx {
    65  	panic("do not support DeliverRealTx")
    66  }
    67  
    68  func (BaseApplication) CheckTx(req RequestCheckTx) ResponseCheckTx {
    69  	return ResponseCheckTx{Code: CodeTypeOK}
    70  }
    71  
    72  func (BaseApplication) Commit(req RequestCommit) ResponseCommit {
    73  	return ResponseCommit{}
    74  }
    75  
    76  func (BaseApplication) Query(req RequestQuery) ResponseQuery {
    77  	return ResponseQuery{Code: CodeTypeOK}
    78  }
    79  
    80  func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
    81  	return ResponseInitChain{}
    82  }
    83  
    84  func (BaseApplication) BeginBlock(req RequestBeginBlock) ResponseBeginBlock {
    85  	return ResponseBeginBlock{}
    86  }
    87  
    88  func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
    89  	return ResponseEndBlock{}
    90  }
    91  
    92  func (a BaseApplication) ParallelTxs(_ [][]byte, onlyCalSender bool) []*ResponseDeliverTx {
    93  	return nil
    94  }
    95  
    96  //-------------------------------------------------------
    97  
    98  // GRPCApplication is a GRPC wrapper for Application
    99  type GRPCApplication struct {
   100  	app Application
   101  }
   102  
   103  func NewGRPCApplication(app Application) *GRPCApplication {
   104  	return &GRPCApplication{app}
   105  }
   106  
   107  func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
   108  	return &ResponseEcho{Message: req.Message}, nil
   109  }
   110  
   111  func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
   112  	return &ResponseFlush{}, nil
   113  }
   114  
   115  func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
   116  	res := app.app.Info(*req)
   117  	return &res, nil
   118  }
   119  
   120  func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
   121  	res := app.app.SetOption(*req)
   122  	return &res, nil
   123  }
   124  
   125  func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
   126  	res := app.app.DeliverTx(*req)
   127  	return &res, nil
   128  }
   129  
   130  func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
   131  	res := app.app.CheckTx(*req)
   132  	return &res, nil
   133  }
   134  
   135  func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
   136  	res := app.app.Query(*req)
   137  	return &res, nil
   138  }
   139  
   140  func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
   141  	res := app.app.Commit(*req)
   142  	return &res, nil
   143  }
   144  
   145  func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
   146  	res := app.app.InitChain(*req)
   147  	return &res, nil
   148  }
   149  
   150  func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
   151  	res := app.app.BeginBlock(*req)
   152  	return &res, nil
   153  }
   154  
   155  func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
   156  	res := app.app.EndBlock(*req)
   157  	return &res, nil
   158  }