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