github.com/number571/tendermint@v0.34.11-gost/abci/client/local_client.go (about)

     1  package abcicli
     2  
     3  import (
     4  	"context"
     5  
     6  	types "github.com/number571/tendermint/abci/types"
     7  	tmsync "github.com/number571/tendermint/internal/libs/sync"
     8  	"github.com/number571/tendermint/libs/service"
     9  )
    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.RWMutex
    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.RWMutex, app types.Application) Client {
    30  	if mtx == nil {
    31  		mtx = &tmsync.RWMutex{}
    32  	}
    33  
    34  	cli := &localClient{
    35  		mtx:         mtx,
    36  		Application: app,
    37  	}
    38  
    39  	cli.BaseService = *service.NewBaseService(nil, "localClient", cli)
    40  	return cli
    41  }
    42  
    43  func (app *localClient) SetResponseCallback(cb Callback) {
    44  	app.mtx.Lock()
    45  	defer app.mtx.Unlock()
    46  	app.Callback = cb
    47  }
    48  
    49  // TODO: change types.Application to include Error()?
    50  func (app *localClient) Error() error {
    51  	return nil
    52  }
    53  
    54  func (app *localClient) FlushAsync(ctx context.Context) (*ReqRes, error) {
    55  	// Do nothing
    56  	return newLocalReqRes(types.ToRequestFlush(), nil), nil
    57  }
    58  
    59  func (app *localClient) EchoAsync(ctx context.Context, msg string) (*ReqRes, error) {
    60  	app.mtx.Lock()
    61  	defer app.mtx.Unlock()
    62  
    63  	return app.callback(
    64  		types.ToRequestEcho(msg),
    65  		types.ToResponseEcho(msg),
    66  	), nil
    67  }
    68  
    69  func (app *localClient) InfoAsync(ctx context.Context, req types.RequestInfo) (*ReqRes, error) {
    70  	app.mtx.RLock()
    71  	defer app.mtx.RUnlock()
    72  
    73  	res := app.Application.Info(req)
    74  	return app.callback(
    75  		types.ToRequestInfo(req),
    76  		types.ToResponseInfo(res),
    77  	), nil
    78  }
    79  
    80  func (app *localClient) DeliverTxAsync(ctx context.Context, params types.RequestDeliverTx) (*ReqRes, error) {
    81  	app.mtx.Lock()
    82  	defer app.mtx.Unlock()
    83  
    84  	res := app.Application.DeliverTx(params)
    85  	return app.callback(
    86  		types.ToRequestDeliverTx(params),
    87  		types.ToResponseDeliverTx(res),
    88  	), nil
    89  }
    90  
    91  func (app *localClient) CheckTxAsync(ctx context.Context, req types.RequestCheckTx) (*ReqRes, error) {
    92  	app.mtx.Lock()
    93  	defer app.mtx.Unlock()
    94  
    95  	res := app.Application.CheckTx(req)
    96  	return app.callback(
    97  		types.ToRequestCheckTx(req),
    98  		types.ToResponseCheckTx(res),
    99  	), nil
   100  }
   101  
   102  func (app *localClient) QueryAsync(ctx context.Context, req types.RequestQuery) (*ReqRes, error) {
   103  	app.mtx.RLock()
   104  	defer app.mtx.RUnlock()
   105  
   106  	res := app.Application.Query(req)
   107  	return app.callback(
   108  		types.ToRequestQuery(req),
   109  		types.ToResponseQuery(res),
   110  	), nil
   111  }
   112  
   113  func (app *localClient) CommitAsync(ctx context.Context) (*ReqRes, error) {
   114  	app.mtx.Lock()
   115  	defer app.mtx.Unlock()
   116  
   117  	res := app.Application.Commit()
   118  	return app.callback(
   119  		types.ToRequestCommit(),
   120  		types.ToResponseCommit(res),
   121  	), nil
   122  }
   123  
   124  func (app *localClient) InitChainAsync(ctx context.Context, req types.RequestInitChain) (*ReqRes, error) {
   125  	app.mtx.Lock()
   126  	defer app.mtx.Unlock()
   127  
   128  	res := app.Application.InitChain(req)
   129  	return app.callback(
   130  		types.ToRequestInitChain(req),
   131  		types.ToResponseInitChain(res),
   132  	), nil
   133  }
   134  
   135  func (app *localClient) BeginBlockAsync(ctx context.Context, req types.RequestBeginBlock) (*ReqRes, error) {
   136  	app.mtx.Lock()
   137  	defer app.mtx.Unlock()
   138  
   139  	res := app.Application.BeginBlock(req)
   140  	return app.callback(
   141  		types.ToRequestBeginBlock(req),
   142  		types.ToResponseBeginBlock(res),
   143  	), nil
   144  }
   145  
   146  func (app *localClient) EndBlockAsync(ctx context.Context, req types.RequestEndBlock) (*ReqRes, error) {
   147  	app.mtx.Lock()
   148  	defer app.mtx.Unlock()
   149  
   150  	res := app.Application.EndBlock(req)
   151  	return app.callback(
   152  		types.ToRequestEndBlock(req),
   153  		types.ToResponseEndBlock(res),
   154  	), nil
   155  }
   156  
   157  func (app *localClient) ListSnapshotsAsync(ctx context.Context, req types.RequestListSnapshots) (*ReqRes, error) {
   158  	app.mtx.Lock()
   159  	defer app.mtx.Unlock()
   160  
   161  	res := app.Application.ListSnapshots(req)
   162  	return app.callback(
   163  		types.ToRequestListSnapshots(req),
   164  		types.ToResponseListSnapshots(res),
   165  	), nil
   166  }
   167  
   168  func (app *localClient) OfferSnapshotAsync(ctx context.Context, req types.RequestOfferSnapshot) (*ReqRes, error) {
   169  	app.mtx.Lock()
   170  	defer app.mtx.Unlock()
   171  
   172  	res := app.Application.OfferSnapshot(req)
   173  	return app.callback(
   174  		types.ToRequestOfferSnapshot(req),
   175  		types.ToResponseOfferSnapshot(res),
   176  	), nil
   177  }
   178  
   179  func (app *localClient) LoadSnapshotChunkAsync(
   180  	ctx context.Context,
   181  	req types.RequestLoadSnapshotChunk,
   182  ) (*ReqRes, error) {
   183  	app.mtx.Lock()
   184  	defer app.mtx.Unlock()
   185  
   186  	res := app.Application.LoadSnapshotChunk(req)
   187  	return app.callback(
   188  		types.ToRequestLoadSnapshotChunk(req),
   189  		types.ToResponseLoadSnapshotChunk(res),
   190  	), nil
   191  }
   192  
   193  func (app *localClient) ApplySnapshotChunkAsync(
   194  	ctx context.Context,
   195  	req types.RequestApplySnapshotChunk,
   196  ) (*ReqRes, error) {
   197  	app.mtx.Lock()
   198  	defer app.mtx.Unlock()
   199  
   200  	res := app.Application.ApplySnapshotChunk(req)
   201  	return app.callback(
   202  		types.ToRequestApplySnapshotChunk(req),
   203  		types.ToResponseApplySnapshotChunk(res),
   204  	), nil
   205  }
   206  
   207  //-------------------------------------------------------
   208  
   209  func (app *localClient) FlushSync(ctx context.Context) error {
   210  	return nil
   211  }
   212  
   213  func (app *localClient) EchoSync(ctx context.Context, msg string) (*types.ResponseEcho, error) {
   214  	return &types.ResponseEcho{Message: msg}, nil
   215  }
   216  
   217  func (app *localClient) InfoSync(ctx context.Context, req types.RequestInfo) (*types.ResponseInfo, error) {
   218  	app.mtx.RLock()
   219  	defer app.mtx.RUnlock()
   220  
   221  	res := app.Application.Info(req)
   222  	return &res, nil
   223  }
   224  
   225  func (app *localClient) DeliverTxSync(
   226  	ctx context.Context,
   227  	req types.RequestDeliverTx,
   228  ) (*types.ResponseDeliverTx, error) {
   229  
   230  	app.mtx.Lock()
   231  	defer app.mtx.Unlock()
   232  
   233  	res := app.Application.DeliverTx(req)
   234  	return &res, nil
   235  }
   236  
   237  func (app *localClient) CheckTxSync(
   238  	ctx context.Context,
   239  	req types.RequestCheckTx,
   240  ) (*types.ResponseCheckTx, error) {
   241  	app.mtx.Lock()
   242  	defer app.mtx.Unlock()
   243  
   244  	res := app.Application.CheckTx(req)
   245  	return &res, nil
   246  }
   247  
   248  func (app *localClient) QuerySync(
   249  	ctx context.Context,
   250  	req types.RequestQuery,
   251  ) (*types.ResponseQuery, error) {
   252  	app.mtx.RLock()
   253  	defer app.mtx.RUnlock()
   254  
   255  	res := app.Application.Query(req)
   256  	return &res, nil
   257  }
   258  
   259  func (app *localClient) CommitSync(ctx context.Context) (*types.ResponseCommit, error) {
   260  	app.mtx.Lock()
   261  	defer app.mtx.Unlock()
   262  
   263  	res := app.Application.Commit()
   264  	return &res, nil
   265  }
   266  
   267  func (app *localClient) InitChainSync(
   268  	ctx context.Context,
   269  	req types.RequestInitChain,
   270  ) (*types.ResponseInitChain, error) {
   271  
   272  	app.mtx.Lock()
   273  	defer app.mtx.Unlock()
   274  
   275  	res := app.Application.InitChain(req)
   276  	return &res, nil
   277  }
   278  
   279  func (app *localClient) BeginBlockSync(
   280  	ctx context.Context,
   281  	req types.RequestBeginBlock,
   282  ) (*types.ResponseBeginBlock, error) {
   283  
   284  	app.mtx.Lock()
   285  	defer app.mtx.Unlock()
   286  
   287  	res := app.Application.BeginBlock(req)
   288  	return &res, nil
   289  }
   290  
   291  func (app *localClient) EndBlockSync(
   292  	ctx context.Context,
   293  	req types.RequestEndBlock,
   294  ) (*types.ResponseEndBlock, error) {
   295  
   296  	app.mtx.Lock()
   297  	defer app.mtx.Unlock()
   298  
   299  	res := app.Application.EndBlock(req)
   300  	return &res, nil
   301  }
   302  
   303  func (app *localClient) ListSnapshotsSync(
   304  	ctx context.Context,
   305  	req types.RequestListSnapshots,
   306  ) (*types.ResponseListSnapshots, error) {
   307  
   308  	app.mtx.Lock()
   309  	defer app.mtx.Unlock()
   310  
   311  	res := app.Application.ListSnapshots(req)
   312  	return &res, nil
   313  }
   314  
   315  func (app *localClient) OfferSnapshotSync(
   316  	ctx context.Context,
   317  	req types.RequestOfferSnapshot,
   318  ) (*types.ResponseOfferSnapshot, error) {
   319  
   320  	app.mtx.Lock()
   321  	defer app.mtx.Unlock()
   322  
   323  	res := app.Application.OfferSnapshot(req)
   324  	return &res, nil
   325  }
   326  
   327  func (app *localClient) LoadSnapshotChunkSync(
   328  	ctx context.Context,
   329  	req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) {
   330  
   331  	app.mtx.Lock()
   332  	defer app.mtx.Unlock()
   333  
   334  	res := app.Application.LoadSnapshotChunk(req)
   335  	return &res, nil
   336  }
   337  
   338  func (app *localClient) ApplySnapshotChunkSync(
   339  	ctx context.Context,
   340  	req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) {
   341  
   342  	app.mtx.Lock()
   343  	defer app.mtx.Unlock()
   344  
   345  	res := app.Application.ApplySnapshotChunk(req)
   346  	return &res, nil
   347  }
   348  
   349  //-------------------------------------------------------
   350  
   351  func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes {
   352  	app.Callback(req, res)
   353  	return newLocalReqRes(req, res)
   354  }
   355  
   356  func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes {
   357  	reqRes := NewReqRes(req)
   358  	reqRes.Response = res
   359  	reqRes.SetDone()
   360  	return reqRes
   361  }