github.com/vipernet-xyz/tm@v0.34.24/abci/client/local_client.go (about)

     1  package abcicli
     2  
     3  import (
     4  	types "github.com/vipernet-xyz/tm/abci/types"
     5  	"github.com/vipernet-xyz/tm/libs/service"
     6  	tmsync "github.com/vipernet-xyz/tm/libs/sync"
     7  )
     8  
     9  var _ Client = (*localClient)(nil)
    10  
    11  // NOTE: use defer to unlock mutex because Application might panic (e.g., in
    12  // case of malicious tx or query). It only makes sense for publicly exposed
    13  // methods like CheckTx (/broadcast_tx_* RPC endpoint) or Query (/abci_query
    14  // RPC endpoint), but defers are used everywhere for the sake of consistency.
    15  type localClient struct {
    16  	service.BaseService
    17  
    18  	mtx *tmsync.Mutex
    19  	types.Application
    20  	Callback
    21  }
    22  
    23  var _ Client = (*localClient)(nil)
    24  
    25  // NewLocalClient creates a local client, which will be directly calling the
    26  // methods of the given app.
    27  //
    28  // Both Async and Sync methods ignore the given context.Context parameter.
    29  func NewLocalClient(mtx *tmsync.Mutex, app types.Application) Client {
    30  	if mtx == nil {
    31  		mtx = new(tmsync.Mutex)
    32  	}
    33  	cli := &localClient{
    34  		mtx:         mtx,
    35  		Application: app,
    36  	}
    37  	cli.BaseService = *service.NewBaseService(nil, "localClient", cli)
    38  	return cli
    39  }
    40  
    41  func (app *localClient) SetResponseCallback(cb Callback) {
    42  	app.mtx.Lock()
    43  	app.Callback = cb
    44  	app.mtx.Unlock()
    45  }
    46  
    47  // TODO: change types.Application to include Error()?
    48  func (app *localClient) Error() error {
    49  	return nil
    50  }
    51  
    52  func (app *localClient) FlushAsync() *ReqRes {
    53  	// Do nothing
    54  	return newLocalReqRes(types.ToRequestFlush(), nil)
    55  }
    56  
    57  func (app *localClient) EchoAsync(msg string) *ReqRes {
    58  	app.mtx.Lock()
    59  	defer app.mtx.Unlock()
    60  
    61  	return app.callback(
    62  		types.ToRequestEcho(msg),
    63  		types.ToResponseEcho(msg),
    64  	)
    65  }
    66  
    67  func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes {
    68  	app.mtx.Lock()
    69  	defer app.mtx.Unlock()
    70  
    71  	res := app.Application.Info(req)
    72  	return app.callback(
    73  		types.ToRequestInfo(req),
    74  		types.ToResponseInfo(res),
    75  	)
    76  }
    77  
    78  func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
    79  	app.mtx.Lock()
    80  	defer app.mtx.Unlock()
    81  
    82  	res := app.Application.SetOption(req)
    83  	return app.callback(
    84  		types.ToRequestSetOption(req),
    85  		types.ToResponseSetOption(res),
    86  	)
    87  }
    88  
    89  func (app *localClient) DeliverTxAsync(params types.RequestDeliverTx) *ReqRes {
    90  	app.mtx.Lock()
    91  	defer app.mtx.Unlock()
    92  
    93  	res := app.Application.DeliverTx(params)
    94  	return app.callback(
    95  		types.ToRequestDeliverTx(params),
    96  		types.ToResponseDeliverTx(res),
    97  	)
    98  }
    99  
   100  func (app *localClient) CheckTxAsync(req types.RequestCheckTx) *ReqRes {
   101  	app.mtx.Lock()
   102  	defer app.mtx.Unlock()
   103  
   104  	res := app.Application.CheckTx(req)
   105  	return app.callback(
   106  		types.ToRequestCheckTx(req),
   107  		types.ToResponseCheckTx(res),
   108  	)
   109  }
   110  
   111  func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes {
   112  	app.mtx.Lock()
   113  	defer app.mtx.Unlock()
   114  
   115  	res := app.Application.Query(req)
   116  	return app.callback(
   117  		types.ToRequestQuery(req),
   118  		types.ToResponseQuery(res),
   119  	)
   120  }
   121  
   122  func (app *localClient) CommitAsync() *ReqRes {
   123  	app.mtx.Lock()
   124  	defer app.mtx.Unlock()
   125  
   126  	res := app.Application.Commit()
   127  	return app.callback(
   128  		types.ToRequestCommit(),
   129  		types.ToResponseCommit(res),
   130  	)
   131  }
   132  
   133  func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
   134  	app.mtx.Lock()
   135  	defer app.mtx.Unlock()
   136  
   137  	res := app.Application.InitChain(req)
   138  	return app.callback(
   139  		types.ToRequestInitChain(req),
   140  		types.ToResponseInitChain(res),
   141  	)
   142  }
   143  
   144  func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
   145  	app.mtx.Lock()
   146  	defer app.mtx.Unlock()
   147  
   148  	res := app.Application.BeginBlock(req)
   149  	return app.callback(
   150  		types.ToRequestBeginBlock(req),
   151  		types.ToResponseBeginBlock(res),
   152  	)
   153  }
   154  
   155  func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
   156  	app.mtx.Lock()
   157  	defer app.mtx.Unlock()
   158  
   159  	res := app.Application.EndBlock(req)
   160  	return app.callback(
   161  		types.ToRequestEndBlock(req),
   162  		types.ToResponseEndBlock(res),
   163  	)
   164  }
   165  
   166  func (app *localClient) ListSnapshotsAsync(req types.RequestListSnapshots) *ReqRes {
   167  	app.mtx.Lock()
   168  	defer app.mtx.Unlock()
   169  
   170  	res := app.Application.ListSnapshots(req)
   171  	return app.callback(
   172  		types.ToRequestListSnapshots(req),
   173  		types.ToResponseListSnapshots(res),
   174  	)
   175  }
   176  
   177  func (app *localClient) OfferSnapshotAsync(req types.RequestOfferSnapshot) *ReqRes {
   178  	app.mtx.Lock()
   179  	defer app.mtx.Unlock()
   180  
   181  	res := app.Application.OfferSnapshot(req)
   182  	return app.callback(
   183  		types.ToRequestOfferSnapshot(req),
   184  		types.ToResponseOfferSnapshot(res),
   185  	)
   186  }
   187  
   188  func (app *localClient) LoadSnapshotChunkAsync(req types.RequestLoadSnapshotChunk) *ReqRes {
   189  	app.mtx.Lock()
   190  	defer app.mtx.Unlock()
   191  
   192  	res := app.Application.LoadSnapshotChunk(req)
   193  	return app.callback(
   194  		types.ToRequestLoadSnapshotChunk(req),
   195  		types.ToResponseLoadSnapshotChunk(res),
   196  	)
   197  }
   198  
   199  func (app *localClient) ApplySnapshotChunkAsync(req types.RequestApplySnapshotChunk) *ReqRes {
   200  	app.mtx.Lock()
   201  	defer app.mtx.Unlock()
   202  
   203  	res := app.Application.ApplySnapshotChunk(req)
   204  	return app.callback(
   205  		types.ToRequestApplySnapshotChunk(req),
   206  		types.ToResponseApplySnapshotChunk(res),
   207  	)
   208  }
   209  
   210  //-------------------------------------------------------
   211  
   212  func (app *localClient) FlushSync() error {
   213  	return nil
   214  }
   215  
   216  func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) {
   217  	return &types.ResponseEcho{Message: msg}, nil
   218  }
   219  
   220  func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
   221  	app.mtx.Lock()
   222  	defer app.mtx.Unlock()
   223  
   224  	res := app.Application.Info(req)
   225  	return &res, nil
   226  }
   227  
   228  func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
   229  	app.mtx.Lock()
   230  	defer app.mtx.Unlock()
   231  
   232  	res := app.Application.SetOption(req)
   233  	return &res, nil
   234  }
   235  
   236  func (app *localClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) {
   237  	app.mtx.Lock()
   238  	defer app.mtx.Unlock()
   239  
   240  	res := app.Application.DeliverTx(req)
   241  	return &res, nil
   242  }
   243  
   244  func (app *localClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) {
   245  	app.mtx.Lock()
   246  	defer app.mtx.Unlock()
   247  
   248  	res := app.Application.CheckTx(req)
   249  	return &res, nil
   250  }
   251  
   252  func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
   253  	app.mtx.Lock()
   254  	defer app.mtx.Unlock()
   255  
   256  	res := app.Application.Query(req)
   257  	return &res, nil
   258  }
   259  
   260  func (app *localClient) CommitSync() (*types.ResponseCommit, error) {
   261  	app.mtx.Lock()
   262  	defer app.mtx.Unlock()
   263  
   264  	res := app.Application.Commit()
   265  	return &res, nil
   266  }
   267  
   268  func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
   269  	app.mtx.Lock()
   270  	defer app.mtx.Unlock()
   271  
   272  	res := app.Application.InitChain(req)
   273  	return &res, nil
   274  }
   275  
   276  func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
   277  	app.mtx.Lock()
   278  	defer app.mtx.Unlock()
   279  
   280  	res := app.Application.BeginBlock(req)
   281  	return &res, nil
   282  }
   283  
   284  func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
   285  	app.mtx.Lock()
   286  	defer app.mtx.Unlock()
   287  
   288  	res := app.Application.EndBlock(req)
   289  	return &res, nil
   290  }
   291  
   292  func (app *localClient) ListSnapshotsSync(req types.RequestListSnapshots) (*types.ResponseListSnapshots, error) {
   293  	app.mtx.Lock()
   294  	defer app.mtx.Unlock()
   295  
   296  	res := app.Application.ListSnapshots(req)
   297  	return &res, nil
   298  }
   299  
   300  func (app *localClient) OfferSnapshotSync(req types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error) {
   301  	app.mtx.Lock()
   302  	defer app.mtx.Unlock()
   303  
   304  	res := app.Application.OfferSnapshot(req)
   305  	return &res, nil
   306  }
   307  
   308  func (app *localClient) LoadSnapshotChunkSync(
   309  	req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) {
   310  	app.mtx.Lock()
   311  	defer app.mtx.Unlock()
   312  
   313  	res := app.Application.LoadSnapshotChunk(req)
   314  	return &res, nil
   315  }
   316  
   317  func (app *localClient) ApplySnapshotChunkSync(
   318  	req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) {
   319  	app.mtx.Lock()
   320  	defer app.mtx.Unlock()
   321  
   322  	res := app.Application.ApplySnapshotChunk(req)
   323  	return &res, nil
   324  }
   325  
   326  //-------------------------------------------------------
   327  
   328  func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes {
   329  	app.Callback(req, res)
   330  	rr := newLocalReqRes(req, res)
   331  	rr.callbackInvoked = true
   332  	return rr
   333  }
   334  
   335  func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes {
   336  	reqRes := NewReqRes(req)
   337  	reqRes.Response = res
   338  	return reqRes
   339  }