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