github.com/okex/exchain@v1.8.0/libs/tendermint/abci/client/local_client.go (about) 1 package abcicli 2 3 import ( 4 "sync" 5 6 "github.com/okex/exchain/libs/tendermint/abci/types" 7 "github.com/okex/exchain/libs/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 if !types.GetDisableABCIQueryMutex() { 64 app.mtx.Lock() 65 defer app.mtx.Unlock() 66 } 67 res := app.Application.Info(req) 68 return app.callback( 69 types.ToRequestInfo(req), 70 types.ToResponseInfo(res), 71 ) 72 } 73 74 func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes { 75 app.mtx.Lock() 76 defer app.mtx.Unlock() 77 78 res := app.Application.SetOption(req) 79 return app.callback( 80 types.ToRequestSetOption(req), 81 types.ToResponseSetOption(res), 82 ) 83 } 84 85 func (app *localClient) DeliverTxAsync(params types.RequestDeliverTx) *ReqRes { 86 app.mtx.Lock() 87 defer app.mtx.Unlock() 88 89 res := app.Application.DeliverTx(params) 90 return app.callback( 91 types.ToRequestDeliverTx(params), 92 types.ToResponseDeliverTx(res), 93 ) 94 } 95 96 func (app *localClient) PreDeliverRealTxAsync(params []byte) types.TxEssentials { 97 return app.Application.PreDeliverRealTx(params) 98 } 99 100 func (app *localClient) DeliverRealTxAsync(params types.TxEssentials) *ReqRes { 101 app.mtx.Lock() 102 defer app.mtx.Unlock() 103 104 res := app.Application.DeliverRealTx(params) 105 return app.callback( 106 types.ToRequestDeliverTx(types.RequestDeliverTx{Tx: params.GetRaw()}), 107 types.ToResponseDeliverTx(res), 108 ) 109 } 110 111 func (app *localClient) CheckTxAsync(req types.RequestCheckTx) *ReqRes { 112 if !types.GetDisableABCIQueryMutex() { 113 app.mtx.Lock() 114 defer app.mtx.Unlock() 115 } 116 117 res := app.Application.CheckTx(req) 118 return app.callback( 119 types.ToRequestCheckTx(req), 120 types.ToResponseCheckTx(res), 121 ) 122 } 123 124 func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes { 125 if !types.GetDisableABCIQueryMutex() { 126 app.mtx.Lock() 127 defer app.mtx.Unlock() 128 } 129 res := app.Application.Query(req) 130 return app.callback( 131 types.ToRequestQuery(req), 132 types.ToResponseQuery(res), 133 ) 134 } 135 136 func (app *localClient) CommitAsync(req types.RequestCommit) *ReqRes { 137 app.mtx.Lock() 138 defer app.mtx.Unlock() 139 140 res := app.Application.Commit(req) 141 return app.callback( 142 types.ToRequestCommit(req), 143 types.ToResponseCommit(res), 144 ) 145 } 146 147 func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes { 148 app.mtx.Lock() 149 defer app.mtx.Unlock() 150 151 res := app.Application.InitChain(req) 152 return app.callback( 153 types.ToRequestInitChain(req), 154 types.ToResponseInitChain(res), 155 ) 156 } 157 158 func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes { 159 app.mtx.Lock() 160 defer app.mtx.Unlock() 161 162 res := app.Application.BeginBlock(req) 163 return app.callback( 164 types.ToRequestBeginBlock(req), 165 types.ToResponseBeginBlock(res), 166 ) 167 } 168 169 func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes { 170 app.mtx.Lock() 171 defer app.mtx.Unlock() 172 173 res := app.Application.EndBlock(req) 174 return app.callback( 175 types.ToRequestEndBlock(req), 176 types.ToResponseEndBlock(res), 177 ) 178 } 179 180 func (app *localClient) ParallelTxs(txs [][]byte, onlyCalSender bool) []*types.ResponseDeliverTx { 181 app.mtx.Lock() 182 defer app.mtx.Unlock() 183 return app.Application.ParallelTxs(txs, onlyCalSender) 184 } 185 186 //------------------------------------------------------- 187 188 func (app *localClient) FlushSync() error { 189 return nil 190 } 191 192 func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) { 193 return &types.ResponseEcho{Message: msg}, nil 194 } 195 196 func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) { 197 if !types.GetDisableABCIQueryMutex() { 198 app.mtx.Lock() 199 defer app.mtx.Unlock() 200 } 201 res := app.Application.Info(req) 202 return &res, nil 203 } 204 205 func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) { 206 app.mtx.Lock() 207 defer app.mtx.Unlock() 208 209 res := app.Application.SetOption(req) 210 return &res, nil 211 } 212 213 func (app *localClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) { 214 app.mtx.Lock() 215 defer app.mtx.Unlock() 216 217 res := app.Application.DeliverTx(req) 218 return &res, nil 219 } 220 221 func (app *localClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) { 222 if !types.GetDisableABCIQueryMutex() { 223 app.mtx.Lock() 224 defer app.mtx.Unlock() 225 } 226 227 res := app.Application.CheckTx(req) 228 return &res, nil 229 } 230 231 func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) { 232 if !types.GetDisableABCIQueryMutex() { 233 app.mtx.Lock() 234 defer app.mtx.Unlock() 235 } 236 res := app.Application.Query(req) 237 return &res, nil 238 } 239 240 func (app *localClient) CommitSync(req types.RequestCommit) (*types.ResponseCommit, error) { 241 app.mtx.Lock() 242 defer app.mtx.Unlock() 243 244 res := app.Application.Commit(req) 245 return &res, nil 246 } 247 248 func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) { 249 app.mtx.Lock() 250 defer app.mtx.Unlock() 251 252 res := app.Application.InitChain(req) 253 return &res, nil 254 } 255 256 func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) { 257 app.mtx.Lock() 258 defer app.mtx.Unlock() 259 260 res := app.Application.BeginBlock(req) 261 return &res, nil 262 } 263 264 func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) { 265 app.mtx.Lock() 266 defer app.mtx.Unlock() 267 268 res := app.Application.EndBlock(req) 269 return &res, nil 270 } 271 272 //------------------------------------------------------- 273 274 func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes { 275 app.Callback(req, res) 276 return newLocalReqRes(req, res) 277 } 278 279 func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes { 280 reqRes := NewReqRes(req) 281 reqRes.Response = res 282 reqRes.SetDone() 283 return reqRes 284 }