github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/bft/abci/client/local_client.go (about)

     1  package abcicli
     2  
     3  import (
     4  	"sync"
     5  
     6  	abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
     7  	"github.com/gnolang/gno/tm2/pkg/service"
     8  )
     9  
    10  var _ Client = (*localClient)(nil)
    11  
    12  // NOTE: use defer to unlock mutex because Application might panic (e.g., in
    13  // case of malicious tx or query). It only makes sense for publicly exposed
    14  // methods like CheckTx (/broadcast_tx_* RPC endpoint) or Query (/abci_query
    15  // RPC endpoint), but defers are used everywhere for the sake of consistency.
    16  type localClient struct {
    17  	service.BaseService
    18  
    19  	mtx *sync.Mutex
    20  	abci.Application
    21  	Callback
    22  }
    23  
    24  func NewLocalClient(mtx *sync.Mutex, app abci.Application) *localClient {
    25  	if mtx == nil {
    26  		mtx = new(sync.Mutex)
    27  	}
    28  	cli := &localClient{
    29  		mtx:         mtx,
    30  		Application: app,
    31  	}
    32  	cli.BaseService = *service.NewBaseService(nil, "localClient", cli)
    33  	return cli
    34  }
    35  
    36  func (app *localClient) SetResponseCallback(cb Callback) {
    37  	app.mtx.Lock()
    38  	app.Callback = cb
    39  	app.mtx.Unlock()
    40  }
    41  
    42  // TODO: change abci.Application to include Error()?
    43  func (app *localClient) Error() error {
    44  	return nil
    45  }
    46  
    47  func (app *localClient) FlushAsync() *ReqRes {
    48  	// Do nothing
    49  	return newLocalReqRes(abci.RequestFlush{}, nil)
    50  }
    51  
    52  func (app *localClient) EchoAsync(msg string) *ReqRes {
    53  	app.mtx.Lock()
    54  	defer app.mtx.Unlock()
    55  
    56  	return app.completeRequest(
    57  		abci.RequestEcho{Message: msg},
    58  		abci.ResponseEcho{Message: msg},
    59  	)
    60  }
    61  
    62  func (app *localClient) InfoAsync(req abci.RequestInfo) *ReqRes {
    63  	app.mtx.Lock()
    64  	defer app.mtx.Unlock()
    65  
    66  	res := app.Application.Info(req)
    67  	return app.completeRequest(req, res)
    68  }
    69  
    70  func (app *localClient) SetOptionAsync(req abci.RequestSetOption) *ReqRes {
    71  	app.mtx.Lock()
    72  	defer app.mtx.Unlock()
    73  
    74  	res := app.Application.SetOption(req)
    75  	return app.completeRequest(req, res)
    76  }
    77  
    78  func (app *localClient) DeliverTxAsync(req abci.RequestDeliverTx) *ReqRes {
    79  	app.mtx.Lock()
    80  	defer app.mtx.Unlock()
    81  
    82  	res := app.Application.DeliverTx(req)
    83  	return app.completeRequest(req, res)
    84  }
    85  
    86  func (app *localClient) CheckTxAsync(req abci.RequestCheckTx) *ReqRes {
    87  	app.mtx.Lock()
    88  	defer app.mtx.Unlock()
    89  
    90  	res := app.Application.CheckTx(req)
    91  	return app.completeRequest(req, res)
    92  }
    93  
    94  func (app *localClient) QueryAsync(req abci.RequestQuery) *ReqRes {
    95  	app.mtx.Lock()
    96  	defer app.mtx.Unlock()
    97  
    98  	res := app.Application.Query(req)
    99  	return app.completeRequest(req, res)
   100  }
   101  
   102  func (app *localClient) CommitAsync() *ReqRes {
   103  	app.mtx.Lock()
   104  	defer app.mtx.Unlock()
   105  
   106  	res := app.Application.Commit()
   107  	return app.completeRequest(abci.RequestCommit{}, res)
   108  }
   109  
   110  func (app *localClient) InitChainAsync(req abci.RequestInitChain) *ReqRes {
   111  	app.mtx.Lock()
   112  	defer app.mtx.Unlock()
   113  
   114  	res := app.Application.InitChain(req)
   115  	return app.completeRequest(req, res)
   116  }
   117  
   118  func (app *localClient) BeginBlockAsync(req abci.RequestBeginBlock) *ReqRes {
   119  	app.mtx.Lock()
   120  	defer app.mtx.Unlock()
   121  
   122  	res := app.Application.BeginBlock(req)
   123  	return app.completeRequest(req, res)
   124  }
   125  
   126  func (app *localClient) EndBlockAsync(req abci.RequestEndBlock) *ReqRes {
   127  	app.mtx.Lock()
   128  	defer app.mtx.Unlock()
   129  
   130  	res := app.Application.EndBlock(req)
   131  	return app.completeRequest(req, res)
   132  }
   133  
   134  //-------------------------------------------------------
   135  
   136  func (app *localClient) FlushSync() error {
   137  	return nil
   138  }
   139  
   140  func (app *localClient) EchoSync(msg string) (abci.ResponseEcho, error) {
   141  	return abci.ResponseEcho{Message: msg}, nil
   142  }
   143  
   144  func (app *localClient) InfoSync(req abci.RequestInfo) (abci.ResponseInfo, error) {
   145  	app.mtx.Lock()
   146  	defer app.mtx.Unlock()
   147  
   148  	res := app.Application.Info(req)
   149  	return res, nil
   150  }
   151  
   152  func (app *localClient) SetOptionSync(req abci.RequestSetOption) (abci.ResponseSetOption, error) {
   153  	app.mtx.Lock()
   154  	defer app.mtx.Unlock()
   155  
   156  	res := app.Application.SetOption(req)
   157  	return res, nil
   158  }
   159  
   160  func (app *localClient) DeliverTxSync(req abci.RequestDeliverTx) (abci.ResponseDeliverTx, error) {
   161  	app.mtx.Lock()
   162  	defer app.mtx.Unlock()
   163  
   164  	res := app.Application.DeliverTx(req)
   165  	return res, nil
   166  }
   167  
   168  func (app *localClient) CheckTxSync(req abci.RequestCheckTx) (abci.ResponseCheckTx, error) {
   169  	app.mtx.Lock()
   170  	defer app.mtx.Unlock()
   171  
   172  	res := app.Application.CheckTx(req)
   173  	return res, nil
   174  }
   175  
   176  func (app *localClient) QuerySync(req abci.RequestQuery) (abci.ResponseQuery, error) {
   177  	app.mtx.Lock()
   178  	defer app.mtx.Unlock()
   179  
   180  	res := app.Application.Query(req)
   181  	return res, nil
   182  }
   183  
   184  func (app *localClient) CommitSync() (abci.ResponseCommit, error) {
   185  	app.mtx.Lock()
   186  	defer app.mtx.Unlock()
   187  
   188  	res := app.Application.Commit()
   189  	return res, nil
   190  }
   191  
   192  func (app *localClient) InitChainSync(req abci.RequestInitChain) (abci.ResponseInitChain, error) {
   193  	app.mtx.Lock()
   194  	defer app.mtx.Unlock()
   195  
   196  	res := app.Application.InitChain(req)
   197  	return res, nil
   198  }
   199  
   200  func (app *localClient) BeginBlockSync(req abci.RequestBeginBlock) (abci.ResponseBeginBlock, error) {
   201  	app.mtx.Lock()
   202  	defer app.mtx.Unlock()
   203  
   204  	res := app.Application.BeginBlock(req)
   205  	return res, nil
   206  }
   207  
   208  func (app *localClient) EndBlockSync(req abci.RequestEndBlock) (abci.ResponseEndBlock, error) {
   209  	app.mtx.Lock()
   210  	defer app.mtx.Unlock()
   211  
   212  	res := app.Application.EndBlock(req)
   213  	return res, nil
   214  }
   215  
   216  //-------------------------------------------------------
   217  
   218  func (app *localClient) completeRequest(req abci.Request, res abci.Response) *ReqRes {
   219  	app.Callback(req, res)
   220  	return newLocalReqRes(req, res)
   221  }
   222  
   223  func newLocalReqRes(req abci.Request, res abci.Response) *ReqRes {
   224  	reqRes := NewReqRes(req)
   225  	reqRes.SetResponse(res)
   226  	return reqRes
   227  }