github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/abci/client/local_client.go (about) 1 package abcicli 2 3 import ( 4 types "github.com/badrootd/nibiru-cometbft/abci/types" 5 "github.com/badrootd/nibiru-cometbft/libs/service" 6 cmtsync "github.com/badrootd/nibiru-cometbft/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(req types.RequestPrepareProposal) *ReqRes { 200 app.mtx.Lock() 201 defer app.mtx.Unlock() 202 203 res := app.Application.PrepareProposal(req) 204 return app.callback( 205 types.ToRequestPrepareProposal(req), 206 types.ToResponsePrepareProposal(res), 207 ) 208 } 209 210 func (app *localClient) ProcessProposalAsync(req types.RequestProcessProposal) *ReqRes { 211 app.mtx.Lock() 212 defer app.mtx.Unlock() 213 214 res := app.Application.ProcessProposal(req) 215 return app.callback( 216 types.ToRequestProcessProposal(req), 217 types.ToResponseProcessProposal(res), 218 ) 219 } 220 221 //------------------------------------------------------- 222 223 func (app *localClient) FlushSync() error { 224 return nil 225 } 226 227 func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) { 228 return &types.ResponseEcho{Message: msg}, nil 229 } 230 231 func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) { 232 app.mtx.Lock() 233 defer app.mtx.Unlock() 234 235 res := app.Application.Info(req) 236 return &res, nil 237 } 238 239 func (app *localClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) { 240 app.mtx.Lock() 241 defer app.mtx.Unlock() 242 243 res := app.Application.DeliverTx(req) 244 return &res, nil 245 } 246 247 func (app *localClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) { 248 app.mtx.Lock() 249 defer app.mtx.Unlock() 250 251 res := app.Application.CheckTx(req) 252 return &res, nil 253 } 254 255 func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) { 256 app.mtx.Lock() 257 defer app.mtx.Unlock() 258 259 res := app.Application.Query(req) 260 return &res, nil 261 } 262 263 func (app *localClient) CommitSync() (*types.ResponseCommit, error) { 264 app.mtx.Lock() 265 defer app.mtx.Unlock() 266 267 res := app.Application.Commit() 268 return &res, nil 269 } 270 271 func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) { 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(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) { 280 app.mtx.Lock() 281 defer app.mtx.Unlock() 282 283 res := app.Application.BeginBlock(req) 284 return &res, nil 285 } 286 287 func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) { 288 app.mtx.Lock() 289 defer app.mtx.Unlock() 290 291 res := app.Application.EndBlock(req) 292 return &res, nil 293 } 294 295 func (app *localClient) ListSnapshotsSync(req types.RequestListSnapshots) (*types.ResponseListSnapshots, error) { 296 app.mtx.Lock() 297 defer app.mtx.Unlock() 298 299 res := app.Application.ListSnapshots(req) 300 return &res, nil 301 } 302 303 func (app *localClient) OfferSnapshotSync(req types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error) { 304 app.mtx.Lock() 305 defer app.mtx.Unlock() 306 307 res := app.Application.OfferSnapshot(req) 308 return &res, nil 309 } 310 311 func (app *localClient) LoadSnapshotChunkSync( 312 req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) { 313 app.mtx.Lock() 314 defer app.mtx.Unlock() 315 316 res := app.Application.LoadSnapshotChunk(req) 317 return &res, nil 318 } 319 320 func (app *localClient) ApplySnapshotChunkSync( 321 req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) { 322 app.mtx.Lock() 323 defer app.mtx.Unlock() 324 325 res := app.Application.ApplySnapshotChunk(req) 326 return &res, nil 327 } 328 329 func (app *localClient) PrepareProposalSync(req types.RequestPrepareProposal) (*types.ResponsePrepareProposal, error) { 330 app.mtx.Lock() 331 defer app.mtx.Unlock() 332 333 res := app.Application.PrepareProposal(req) 334 return &res, nil 335 } 336 337 func (app *localClient) ProcessProposalSync(req types.RequestProcessProposal) (*types.ResponseProcessProposal, error) { 338 app.mtx.Lock() 339 defer app.mtx.Unlock() 340 341 res := app.Application.ProcessProposal(req) 342 return &res, nil 343 } 344 345 //------------------------------------------------------- 346 347 func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes { 348 app.Callback(req, res) 349 rr := newLocalReqRes(req, res) 350 rr.callbackInvoked = true 351 return rr 352 } 353 354 func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes { 355 reqRes := NewReqRes(req) 356 reqRes.Response = res 357 return reqRes 358 }