github.com/evdatsion/aphelion-dpos-bft@v0.32.1/abci/client/local_client.go (about) 1 package abcicli 2 3 import ( 4 "sync" 5 6 types "github.com/evdatsion/aphelion-dpos-bft/abci/types" 7 cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common" 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 cmn.BaseService 18 19 mtx *sync.Mutex 20 types.Application 21 Callback 22 } 23 24 func NewLocalClient(mtx *sync.Mutex, app types.Application) *localClient { 25 if mtx == nil { 26 mtx = new(sync.Mutex) 27 } 28 cli := &localClient{ 29 mtx: mtx, 30 Application: app, 31 } 32 cli.BaseService = *cmn.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 //------------------------------------------------------- 162 163 func (app *localClient) FlushSync() error { 164 return nil 165 } 166 167 func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) { 168 return &types.ResponseEcho{Message: msg}, nil 169 } 170 171 func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) { 172 app.mtx.Lock() 173 defer app.mtx.Unlock() 174 175 res := app.Application.Info(req) 176 return &res, nil 177 } 178 179 func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) { 180 app.mtx.Lock() 181 defer app.mtx.Unlock() 182 183 res := app.Application.SetOption(req) 184 return &res, nil 185 } 186 187 func (app *localClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) { 188 app.mtx.Lock() 189 defer app.mtx.Unlock() 190 191 res := app.Application.DeliverTx(req) 192 return &res, nil 193 } 194 195 func (app *localClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) { 196 app.mtx.Lock() 197 defer app.mtx.Unlock() 198 199 res := app.Application.CheckTx(req) 200 return &res, nil 201 } 202 203 func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) { 204 app.mtx.Lock() 205 defer app.mtx.Unlock() 206 207 res := app.Application.Query(req) 208 return &res, nil 209 } 210 211 func (app *localClient) CommitSync() (*types.ResponseCommit, error) { 212 app.mtx.Lock() 213 defer app.mtx.Unlock() 214 215 res := app.Application.Commit() 216 return &res, nil 217 } 218 219 func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) { 220 app.mtx.Lock() 221 defer app.mtx.Unlock() 222 223 res := app.Application.InitChain(req) 224 return &res, nil 225 } 226 227 func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) { 228 app.mtx.Lock() 229 defer app.mtx.Unlock() 230 231 res := app.Application.BeginBlock(req) 232 return &res, nil 233 } 234 235 func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) { 236 app.mtx.Lock() 237 defer app.mtx.Unlock() 238 239 res := app.Application.EndBlock(req) 240 return &res, nil 241 } 242 243 //------------------------------------------------------- 244 245 func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes { 246 app.Callback(req, res) 247 return newLocalReqRes(req, res) 248 } 249 250 func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes { 251 reqRes := NewReqRes(req) 252 reqRes.Response = res 253 reqRes.SetDone() 254 return reqRes 255 }