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