OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / abci / client / local_client.go
1 package abcicli
2
3 import (
4         "sync"
5
6         types "github.com/tendermint/abci/types"
7         cmn "github.com/tendermint/tmlibs/common"
8 )
9
10 type localClient struct {
11         cmn.BaseService
12         mtx *sync.Mutex
13         types.Application
14         Callback
15 }
16
17 func NewLocalClient(mtx *sync.Mutex, app types.Application) *localClient {
18         if mtx == nil {
19                 mtx = new(sync.Mutex)
20         }
21         cli := &localClient{
22                 mtx:         mtx,
23                 Application: app,
24         }
25         cli.BaseService = *cmn.NewBaseService(nil, "localClient", cli)
26         return cli
27 }
28
29 func (app *localClient) SetResponseCallback(cb Callback) {
30         app.mtx.Lock()
31         defer app.mtx.Unlock()
32         app.Callback = cb
33 }
34
35 // TODO: change types.Application to include Error()?
36 func (app *localClient) Error() error {
37         return nil
38 }
39
40 func (app *localClient) FlushAsync() *ReqRes {
41         // Do nothing
42         return newLocalReqRes(types.ToRequestFlush(), nil)
43 }
44
45 func (app *localClient) EchoAsync(msg string) *ReqRes {
46         return app.callback(
47                 types.ToRequestEcho(msg),
48                 types.ToResponseEcho(msg),
49         )
50 }
51
52 func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes {
53         app.mtx.Lock()
54         resInfo := app.Application.Info(req)
55         app.mtx.Unlock()
56         return app.callback(
57                 types.ToRequestInfo(req),
58                 types.ToResponseInfo(resInfo),
59         )
60 }
61
62 func (app *localClient) SetOptionAsync(key string, value string) *ReqRes {
63         app.mtx.Lock()
64         log := app.Application.SetOption(key, value)
65         app.mtx.Unlock()
66         return app.callback(
67                 types.ToRequestSetOption(key, value),
68                 types.ToResponseSetOption(log),
69         )
70 }
71
72 func (app *localClient) DeliverTxAsync(tx []byte) *ReqRes {
73         app.mtx.Lock()
74         res := app.Application.DeliverTx(tx)
75         app.mtx.Unlock()
76         return app.callback(
77                 types.ToRequestDeliverTx(tx),
78                 types.ToResponseDeliverTx(res.Code, res.Data, res.Log),
79         )
80 }
81
82 func (app *localClient) CheckTxAsync(tx []byte) *ReqRes {
83         app.mtx.Lock()
84         res := app.Application.CheckTx(tx)
85         app.mtx.Unlock()
86         return app.callback(
87                 types.ToRequestCheckTx(tx),
88                 types.ToResponseCheckTx(res.Code, res.Data, res.Log),
89         )
90 }
91
92 func (app *localClient) QueryAsync(reqQuery types.RequestQuery) *ReqRes {
93         app.mtx.Lock()
94         resQuery := app.Application.Query(reqQuery)
95         app.mtx.Unlock()
96         return app.callback(
97                 types.ToRequestQuery(reqQuery),
98                 types.ToResponseQuery(resQuery),
99         )
100 }
101
102 func (app *localClient) CommitAsync() *ReqRes {
103         app.mtx.Lock()
104         res := app.Application.Commit()
105         app.mtx.Unlock()
106         return app.callback(
107                 types.ToRequestCommit(),
108                 types.ToResponseCommit(res.Code, res.Data, res.Log),
109         )
110 }
111
112 func (app *localClient) InitChainAsync(params types.RequestInitChain) *ReqRes {
113         app.mtx.Lock()
114         app.Application.InitChain(params)
115         reqRes := app.callback(
116                 types.ToRequestInitChain(params),
117                 types.ToResponseInitChain(),
118         )
119         app.mtx.Unlock()
120         return reqRes
121 }
122
123 func (app *localClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes {
124         app.mtx.Lock()
125         app.Application.BeginBlock(params)
126         app.mtx.Unlock()
127         return app.callback(
128                 types.ToRequestBeginBlock(params),
129                 types.ToResponseBeginBlock(),
130         )
131 }
132
133 func (app *localClient) EndBlockAsync(height uint64) *ReqRes {
134         app.mtx.Lock()
135         resEndBlock := app.Application.EndBlock(height)
136         app.mtx.Unlock()
137         return app.callback(
138                 types.ToRequestEndBlock(height),
139                 types.ToResponseEndBlock(resEndBlock),
140         )
141 }
142
143 //-------------------------------------------------------
144
145 func (app *localClient) FlushSync() error {
146         return nil
147 }
148
149 func (app *localClient) EchoSync(msg string) (res types.Result) {
150         return types.OK.SetData([]byte(msg))
151 }
152
153 func (app *localClient) InfoSync(req types.RequestInfo) (resInfo types.ResponseInfo, err error) {
154         app.mtx.Lock()
155         defer app.mtx.Unlock()
156         resInfo = app.Application.Info(req)
157         return resInfo, nil
158 }
159
160 func (app *localClient) SetOptionSync(key string, value string) (res types.Result) {
161         app.mtx.Lock()
162         log := app.Application.SetOption(key, value)
163         app.mtx.Unlock()
164         return types.OK.SetLog(log)
165 }
166
167 func (app *localClient) DeliverTxSync(tx []byte) (res types.Result) {
168         app.mtx.Lock()
169         res = app.Application.DeliverTx(tx)
170         app.mtx.Unlock()
171         return res
172 }
173
174 func (app *localClient) CheckTxSync(tx []byte) (res types.Result) {
175         app.mtx.Lock()
176         res = app.Application.CheckTx(tx)
177         app.mtx.Unlock()
178         return res
179 }
180
181 func (app *localClient) QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error) {
182         app.mtx.Lock()
183         resQuery = app.Application.Query(reqQuery)
184         app.mtx.Unlock()
185         return resQuery, nil
186 }
187
188 func (app *localClient) CommitSync() (res types.Result) {
189         app.mtx.Lock()
190         res = app.Application.Commit()
191         app.mtx.Unlock()
192         return res
193 }
194
195 func (app *localClient) InitChainSync(params types.RequestInitChain) (err error) {
196         app.mtx.Lock()
197         app.Application.InitChain(params)
198         app.mtx.Unlock()
199         return nil
200 }
201
202 func (app *localClient) BeginBlockSync(params types.RequestBeginBlock) (err error) {
203         app.mtx.Lock()
204         app.Application.BeginBlock(params)
205         app.mtx.Unlock()
206         return nil
207 }
208
209 func (app *localClient) EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error) {
210         app.mtx.Lock()
211         resEndBlock = app.Application.EndBlock(height)
212         app.mtx.Unlock()
213         return resEndBlock, nil
214 }
215
216 //-------------------------------------------------------
217
218 func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes {
219         app.Callback(req, res)
220         return newLocalReqRes(req, res)
221 }
222
223 func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes {
224         reqRes := NewReqRes(req)
225         reqRes.Response = res
226         reqRes.SetDone()
227         return reqRes
228 }