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