github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/abci/types/application.go (about)

     1  package types
     2  
     3  import (
     4  	context "golang.org/x/net/context"
     5  )
     6  
     7  //go:generate ../../scripts/mockery_generate.sh Application
     8  
     9  // Application is an interface that enables any finite, deterministic state machine
    10  // to be driven by a blockchain-based replication engine via the ABCI.
    11  // All methods take a RequestXxx argument and return a ResponseXxx argument,
    12  // except CheckTx/DeliverTx, which take `tx []byte`, and `Commit`, which takes nothing.
    13  type Application interface {
    14  	// Info/Query Connection
    15  	Info(RequestInfo) ResponseInfo    // Return application info
    16  	Query(RequestQuery) ResponseQuery // Query for state
    17  
    18  	// Mempool Connection
    19  	CheckTx(RequestCheckTx) ResponseCheckTx // Validate a tx for the mempool
    20  
    21  	// Consensus Connection
    22  	InitChain(RequestInitChain) ResponseInitChain // Initialize blockchain w validators/other info from CometBFT
    23  	PrepareProposal(RequestPrepareProposal) ResponsePrepareProposal
    24  	ProcessProposal(RequestProcessProposal) ResponseProcessProposal
    25  	BeginBlock(RequestBeginBlock) ResponseBeginBlock // Signals the beginning of a block
    26  	DeliverTx(RequestDeliverTx) ResponseDeliverTx    // Deliver a tx for full processing
    27  	EndBlock(RequestEndBlock) ResponseEndBlock       // Signals the end of a block, returns changes to the validator set
    28  	Commit() ResponseCommit                          // Commit the state and return the application Merkle root hash
    29  
    30  	// State Sync Connection
    31  	ListSnapshots(RequestListSnapshots) ResponseListSnapshots                // List available snapshots
    32  	OfferSnapshot(RequestOfferSnapshot) ResponseOfferSnapshot                // Offer a snapshot to the application
    33  	LoadSnapshotChunk(RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk    // Load a snapshot chunk
    34  	ApplySnapshotChunk(RequestApplySnapshotChunk) ResponseApplySnapshotChunk // Apply a shapshot chunk
    35  }
    36  
    37  //-------------------------------------------------------
    38  // BaseApplication is a base form of Application
    39  
    40  var _ Application = (*BaseApplication)(nil)
    41  
    42  type BaseApplication struct {
    43  }
    44  
    45  func NewBaseApplication() *BaseApplication {
    46  	return &BaseApplication{}
    47  }
    48  
    49  func (BaseApplication) Info(req RequestInfo) ResponseInfo {
    50  	return ResponseInfo{}
    51  }
    52  
    53  func (BaseApplication) DeliverTx(req RequestDeliverTx) ResponseDeliverTx {
    54  	return ResponseDeliverTx{Code: CodeTypeOK}
    55  }
    56  
    57  func (BaseApplication) CheckTx(req RequestCheckTx) ResponseCheckTx {
    58  	return ResponseCheckTx{Code: CodeTypeOK}
    59  }
    60  
    61  func (BaseApplication) Commit() ResponseCommit {
    62  	return ResponseCommit{}
    63  }
    64  
    65  func (BaseApplication) Query(req RequestQuery) ResponseQuery {
    66  	return ResponseQuery{Code: CodeTypeOK}
    67  }
    68  
    69  func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
    70  	return ResponseInitChain{}
    71  }
    72  
    73  func (BaseApplication) BeginBlock(req RequestBeginBlock) ResponseBeginBlock {
    74  	return ResponseBeginBlock{}
    75  }
    76  
    77  func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
    78  	return ResponseEndBlock{}
    79  }
    80  
    81  func (BaseApplication) ListSnapshots(req RequestListSnapshots) ResponseListSnapshots {
    82  	return ResponseListSnapshots{}
    83  }
    84  
    85  func (BaseApplication) OfferSnapshot(req RequestOfferSnapshot) ResponseOfferSnapshot {
    86  	return ResponseOfferSnapshot{}
    87  }
    88  
    89  func (BaseApplication) LoadSnapshotChunk(req RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk {
    90  	return ResponseLoadSnapshotChunk{}
    91  }
    92  
    93  func (BaseApplication) ApplySnapshotChunk(req RequestApplySnapshotChunk) ResponseApplySnapshotChunk {
    94  	return ResponseApplySnapshotChunk{}
    95  }
    96  
    97  func (BaseApplication) PrepareProposal(req RequestPrepareProposal) ResponsePrepareProposal {
    98  	txs := make([][]byte, 0, len(req.Txs))
    99  	var totalBytes int64
   100  	for _, tx := range req.Txs {
   101  		totalBytes += int64(len(tx))
   102  		if totalBytes > req.MaxTxBytes {
   103  			break
   104  		}
   105  		txs = append(txs, tx)
   106  	}
   107  	return ResponsePrepareProposal{Txs: txs}
   108  }
   109  
   110  func (BaseApplication) ProcessProposal(req RequestProcessProposal) ResponseProcessProposal {
   111  	return ResponseProcessProposal{
   112  		Status: ResponseProcessProposal_ACCEPT}
   113  }
   114  
   115  //-------------------------------------------------------
   116  
   117  // GRPCApplication is a GRPC wrapper for Application
   118  type GRPCApplication struct {
   119  	app Application
   120  }
   121  
   122  func NewGRPCApplication(app Application) *GRPCApplication {
   123  	return &GRPCApplication{app}
   124  }
   125  
   126  func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
   127  	return &ResponseEcho{Message: req.Message}, nil
   128  }
   129  
   130  func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
   131  	return &ResponseFlush{}, nil
   132  }
   133  
   134  func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
   135  	res := app.app.Info(*req)
   136  	return &res, nil
   137  }
   138  
   139  func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
   140  	res := app.app.DeliverTx(*req)
   141  	return &res, nil
   142  }
   143  
   144  func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
   145  	res := app.app.CheckTx(*req)
   146  	return &res, nil
   147  }
   148  
   149  func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
   150  	res := app.app.Query(*req)
   151  	return &res, nil
   152  }
   153  
   154  func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
   155  	res := app.app.Commit()
   156  	return &res, nil
   157  }
   158  
   159  func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
   160  	res := app.app.InitChain(*req)
   161  	return &res, nil
   162  }
   163  
   164  func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
   165  	res := app.app.BeginBlock(*req)
   166  	return &res, nil
   167  }
   168  
   169  func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
   170  	res := app.app.EndBlock(*req)
   171  	return &res, nil
   172  }
   173  
   174  func (app *GRPCApplication) ListSnapshots(
   175  	ctx context.Context, req *RequestListSnapshots) (*ResponseListSnapshots, error) {
   176  	res := app.app.ListSnapshots(*req)
   177  	return &res, nil
   178  }
   179  
   180  func (app *GRPCApplication) OfferSnapshot(
   181  	ctx context.Context, req *RequestOfferSnapshot) (*ResponseOfferSnapshot, error) {
   182  	res := app.app.OfferSnapshot(*req)
   183  	return &res, nil
   184  }
   185  
   186  func (app *GRPCApplication) LoadSnapshotChunk(
   187  	ctx context.Context, req *RequestLoadSnapshotChunk) (*ResponseLoadSnapshotChunk, error) {
   188  	res := app.app.LoadSnapshotChunk(*req)
   189  	return &res, nil
   190  }
   191  
   192  func (app *GRPCApplication) ApplySnapshotChunk(
   193  	ctx context.Context, req *RequestApplySnapshotChunk) (*ResponseApplySnapshotChunk, error) {
   194  	res := app.app.ApplySnapshotChunk(*req)
   195  	return &res, nil
   196  }
   197  
   198  func (app *GRPCApplication) PrepareProposal(
   199  	ctx context.Context, req *RequestPrepareProposal) (*ResponsePrepareProposal, error) {
   200  	res := app.app.PrepareProposal(*req)
   201  	return &res, nil
   202  }
   203  
   204  func (app *GRPCApplication) ProcessProposal(
   205  	ctx context.Context, req *RequestProcessProposal) (*ResponseProcessProposal, error) {
   206  	res := app.app.ProcessProposal(*req)
   207  	return &res, nil
   208  }