github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/abci/client/local_client.go (about) 1 package abcicli 2 3 import ( 4 types "github.com/badrootd/celestia-core/abci/types" 5 "github.com/badrootd/celestia-core/libs/service" 6 cmtsync "github.com/badrootd/celestia-core/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 *cmtsync.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 *cmtsync.Mutex, app types.Application) Client { 30 if mtx == nil { 31 mtx = new(cmtsync.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) DeliverTxAsync(params types.RequestDeliverTx) *ReqRes { 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 ) 87 } 88 89 func (app *localClient) CheckTxAsync(req types.RequestCheckTx) *ReqRes { 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 ) 98 } 99 100 func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes { 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 ) 109 } 110 111 func (app *localClient) CommitAsync() *ReqRes { 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 ) 120 } 121 122 func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes { 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 ) 131 } 132 133 func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes { 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 ) 142 } 143 144 func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes { 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 ) 153 } 154 155 func (app *localClient) ListSnapshotsAsync(req types.RequestListSnapshots) *ReqRes { 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 ) 164 } 165 166 func (app *localClient) OfferSnapshotAsync(req types.RequestOfferSnapshot) *ReqRes { 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 ) 175 } 176 177 func (app *localClient) LoadSnapshotChunkAsync(req types.RequestLoadSnapshotChunk) *ReqRes { 178 app.mtx.Lock() 179 defer app.mtx.Unlock() 180 181 res := app.Application.LoadSnapshotChunk(req) 182 return app.callback( 183 types.ToRequestLoadSnapshotChunk(req), 184 types.ToResponseLoadSnapshotChunk(res), 185 ) 186 } 187 188 func (app *localClient) ApplySnapshotChunkAsync(req types.RequestApplySnapshotChunk) *ReqRes { 189 app.mtx.Lock() 190 defer app.mtx.Unlock() 191 192 res := app.Application.ApplySnapshotChunk(req) 193 return app.callback( 194 types.ToRequestApplySnapshotChunk(req), 195 types.ToResponseApplySnapshotChunk(res), 196 ) 197 } 198 199 func (app *localClient) PrepareProposalAsync( 200 req types.RequestPrepareProposal, 201 ) *ReqRes { 202 app.mtx.Lock() 203 defer app.mtx.Unlock() 204 205 res := app.Application.PrepareProposal(req) 206 return app.callback( 207 types.ToRequestPrepareProposal(req), 208 types.ToResponsePrepareProposal(res), 209 ) 210 } 211 212 func (app *localClient) ProcessProposalAsync( 213 req types.RequestProcessProposal, 214 ) *ReqRes { 215 app.mtx.Lock() 216 defer app.mtx.Unlock() 217 218 res := app.Application.ProcessProposal(req) 219 return app.callback( 220 types.ToRequestProcessProposal(req), 221 types.ToResponseProcessProposal(res), 222 ) 223 } 224 225 //------------------------------------------------------- 226 227 func (app *localClient) FlushSync() error { 228 return nil 229 } 230 231 func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) { 232 return &types.ResponseEcho{Message: msg}, nil 233 } 234 235 func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) { 236 app.mtx.Lock() 237 defer app.mtx.Unlock() 238 239 res := app.Application.Info(req) 240 return &res, nil 241 } 242 243 func (app *localClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) { 244 app.mtx.Lock() 245 defer app.mtx.Unlock() 246 247 res := app.Application.DeliverTx(req) 248 return &res, nil 249 } 250 251 func (app *localClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) { 252 app.mtx.Lock() 253 defer app.mtx.Unlock() 254 255 res := app.Application.CheckTx(req) 256 return &res, nil 257 } 258 259 func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) { 260 app.mtx.Lock() 261 defer app.mtx.Unlock() 262 263 res := app.Application.Query(req) 264 return &res, nil 265 } 266 267 func (app *localClient) CommitSync() (*types.ResponseCommit, error) { 268 app.mtx.Lock() 269 defer app.mtx.Unlock() 270 271 res := app.Application.Commit() 272 return &res, nil 273 } 274 275 func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) { 276 app.mtx.Lock() 277 defer app.mtx.Unlock() 278 279 res := app.Application.InitChain(req) 280 return &res, nil 281 } 282 283 func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) { 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(req types.RequestEndBlock) (*types.ResponseEndBlock, error) { 292 app.mtx.Lock() 293 defer app.mtx.Unlock() 294 295 res := app.Application.EndBlock(req) 296 return &res, nil 297 } 298 299 func (app *localClient) ListSnapshotsSync(req types.RequestListSnapshots) (*types.ResponseListSnapshots, error) { 300 app.mtx.Lock() 301 defer app.mtx.Unlock() 302 303 res := app.Application.ListSnapshots(req) 304 return &res, nil 305 } 306 307 func (app *localClient) OfferSnapshotSync(req types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error) { 308 app.mtx.Lock() 309 defer app.mtx.Unlock() 310 311 res := app.Application.OfferSnapshot(req) 312 return &res, nil 313 } 314 315 func (app *localClient) LoadSnapshotChunkSync( 316 req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) { 317 app.mtx.Lock() 318 defer app.mtx.Unlock() 319 320 res := app.Application.LoadSnapshotChunk(req) 321 return &res, nil 322 } 323 324 func (app *localClient) ApplySnapshotChunkSync( 325 req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) { 326 app.mtx.Lock() 327 defer app.mtx.Unlock() 328 329 res := app.Application.ApplySnapshotChunk(req) 330 return &res, nil 331 } 332 333 func (app *localClient) PrepareProposalSync( 334 req types.RequestPrepareProposal, 335 ) (*types.ResponsePrepareProposal, error) { 336 337 app.mtx.Lock() 338 defer app.mtx.Unlock() 339 340 res := app.Application.PrepareProposal(req) 341 return &res, nil 342 } 343 344 func (app *localClient) ProcessProposalSync( 345 req types.RequestProcessProposal, 346 ) (*types.ResponseProcessProposal, error) { 347 app.mtx.Lock() 348 defer app.mtx.Unlock() 349 350 res := app.Application.ProcessProposal(req) 351 return &res, nil 352 } 353 354 //------------------------------------------------------- 355 356 func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes { 357 app.Callback(req, res) 358 rr := newLocalReqRes(req, res) 359 rr.callbackInvoked = true 360 return rr 361 } 362 363 func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes { 364 reqRes := NewReqRes(req) 365 reqRes.Response = res 366 return reqRes 367 }