github.com/fiagdao/tendermint@v0.32.11-0.20220824195748-2087fcc480c1/abci/client/local_client.go (about) 1 package abcicli 2 3 import ( 4 types2 "github.com/tendermint/tendermint/types" 5 "sync" 6 "time" 7 8 types "github.com/tendermint/tendermint/abci/types" 9 "github.com/tendermint/tendermint/libs/service" 10 ) 11 12 var _ Client = (*localClient)(nil) 13 14 // NOTE: use defer to unlock mutex because Application might panic (e.g., in 15 // case of malicious tx or query). It only makes sense for publicly exposed 16 // methods like CheckTx (/broadcast_tx_* RPC endpoint) or Query (/abci_query 17 // RPC endpoint), but defers are used everywhere for the sake of consistency. 18 type localClient struct { 19 service.BaseService 20 21 mtx *sync.Mutex 22 types.Application 23 Callback 24 } 25 26 func NewLocalClient(mtx *sync.Mutex, app types.Application) Client { 27 if mtx == nil { 28 mtx = new(sync.Mutex) 29 } 30 cli := &localClient{ 31 mtx: mtx, 32 Application: app, 33 } 34 cli.BaseService = *service.NewBaseService(nil, "localClient", cli) 35 return cli 36 } 37 38 func (app *localClient) SetResponseCallback(cb Callback) { 39 app.mtx.Lock() 40 app.Callback = cb 41 app.mtx.Unlock() 42 } 43 44 // TODO: change types.Application to include Error()? 45 func (app *localClient) Error() error { 46 return nil 47 } 48 49 func (app *localClient) FlushAsync() *ReqRes { 50 // Do nothing 51 return newLocalReqRes(types.ToRequestFlush(), nil) 52 } 53 54 func (app *localClient) EchoAsync(msg string) *ReqRes { 55 app.mtx.Lock() 56 defer app.mtx.Unlock() 57 58 return app.callback( 59 types.ToRequestEcho(msg), 60 types.ToResponseEcho(msg), 61 ) 62 } 63 64 func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes { 65 app.mtx.Lock() 66 defer app.mtx.Unlock() 67 68 res := app.Application.Info(req) 69 return app.callback( 70 types.ToRequestInfo(req), 71 types.ToResponseInfo(res), 72 ) 73 } 74 75 func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes { 76 app.mtx.Lock() 77 defer app.mtx.Unlock() 78 79 res := app.Application.SetOption(req) 80 return app.callback( 81 types.ToRequestSetOption(req), 82 types.ToResponseSetOption(res), 83 ) 84 } 85 86 func (app *localClient) DeliverTxAsync(params types.RequestDeliverTx) *ReqRes { 87 app.mtx.Lock() 88 defer app.mtx.Unlock() 89 90 res := app.Application.DeliverTx(params) 91 return app.callback( 92 types.ToRequestDeliverTx(params), 93 types.ToResponseDeliverTx(res), 94 ) 95 } 96 97 func (app *localClient) CheckTxAsync(req types.RequestCheckTx) *ReqRes { 98 app.mtx.Lock() 99 defer app.mtx.Unlock() 100 101 res := app.Application.CheckTx(req) 102 return app.callback( 103 types.ToRequestCheckTx(req), 104 types.ToResponseCheckTx(res), 105 ) 106 } 107 108 func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes { 109 app.mtx.Lock() 110 defer app.mtx.Unlock() 111 112 res := app.Application.Query(req) 113 return app.callback( 114 types.ToRequestQuery(req), 115 types.ToResponseQuery(res), 116 ) 117 } 118 119 func (app *localClient) CommitAsync() *ReqRes { 120 app.mtx.Lock() 121 defer app.mtx.Unlock() 122 123 res := app.Application.Commit() 124 return app.callback( 125 types.ToRequestCommit(), 126 types.ToResponseCommit(res), 127 ) 128 } 129 130 func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes { 131 app.mtx.Lock() 132 defer app.mtx.Unlock() 133 134 res := app.Application.InitChain(req) 135 return app.callback( 136 types.ToRequestInitChain(req), 137 types.ToResponseInitChain(res), 138 ) 139 } 140 141 func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes { 142 app.mtx.Lock() 143 defer app.mtx.Unlock() 144 145 res := app.Application.BeginBlock(req) 146 return app.callback( 147 types.ToRequestBeginBlock(req), 148 types.ToResponseBeginBlock(res), 149 ) 150 } 151 152 func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes { 153 app.mtx.Lock() 154 defer app.mtx.Unlock() 155 156 res := app.Application.EndBlock(req) 157 return app.callback( 158 types.ToRequestEndBlock(req), 159 types.ToResponseEndBlock(res), 160 ) 161 } 162 163 //------------------------------------------------------- 164 165 func (app *localClient) FlushSync() error { 166 return nil 167 } 168 169 func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) { 170 return &types.ResponseEcho{Message: msg}, nil 171 } 172 173 func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) { 174 app.mtx.Lock() 175 defer app.mtx.Unlock() 176 177 res := app.Application.Info(req) 178 return &res, nil 179 } 180 181 func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) { 182 app.mtx.Lock() 183 defer app.mtx.Unlock() 184 185 res := app.Application.SetOption(req) 186 return &res, nil 187 } 188 189 func (app *localClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) { 190 app.mtx.Lock() 191 defer app.mtx.Unlock() 192 193 res := app.Application.DeliverTx(req) 194 return &res, nil 195 } 196 197 func (app *localClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) { 198 app.mtx.Lock() 199 defer app.mtx.Unlock() 200 201 res := app.Application.CheckTx(req) 202 return &res, nil 203 } 204 205 func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) { 206 app.mtx.Lock() 207 defer app.mtx.Unlock() 208 209 res := app.Application.Query(req) 210 return &res, nil 211 } 212 213 func (app *localClient) CommitSync() (*types.ResponseCommit, error) { 214 defer types2.TimeTrack(time.Now(), app.Logger) 215 216 app.mtx.Lock() 217 defer app.mtx.Unlock() 218 219 res := app.Application.Commit() 220 return &res, nil 221 } 222 223 func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) { 224 app.mtx.Lock() 225 defer app.mtx.Unlock() 226 227 res := app.Application.InitChain(req) 228 return &res, nil 229 } 230 231 func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) { 232 defer types2.TimeTrack(time.Now(), app.Logger) 233 app.mtx.Lock() 234 defer app.mtx.Unlock() 235 236 res := app.Application.BeginBlock(req) 237 return &res, nil 238 } 239 240 func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) { 241 defer types2.TimeTrack(time.Now(), app.Logger) 242 app.mtx.Lock() 243 defer app.mtx.Unlock() 244 245 res := app.Application.EndBlock(req) 246 return &res, nil 247 } 248 249 //------------------------------------------------------- 250 251 func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes { 252 app.Callback(req, res) 253 return newLocalReqRes(req, res) 254 } 255 256 func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes { 257 reqRes := NewReqRes(req) 258 reqRes.Response = res 259 reqRes.SetDone() 260 return reqRes 261 }