-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
343 lines (300 loc) · 9.15 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"encoding/hex"
"fmt"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
pt "github.com/GM-Publicchain/gm/plugin/dapp/paracross/types"
"github.com/pkg/errors"
)
// Query_GetTitle query paracross title
func (p *Paracross) Query_GetTitle(in *types.ReqString) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
return p.paracrossGetHeight(in.GetData())
}
// Query_GetTitleHeight query paracross status with title and height
func (p *Paracross) Query_GetTitleHeight(in *pt.ReqParacrossTitleHeight) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
stat, err := p.paracrossGetStateTitleHeight(in.Title, in.Height)
if err != nil {
clog.Error("paracross.GetTitleHeight", "title", title, "height", in.Height, "err", err.Error())
return nil, err
}
status := stat.(*pt.ParacrossHeightStatus)
res := &pt.ParacrossHeightStatusRsp{
Status: status.Status,
Title: status.Title,
Height: status.Height,
MainHeight: status.MainHeight,
MainHash: common.ToHex(status.MainHash),
}
for i, addr := range status.Details.Addrs {
res.CommitAddrs = append(res.CommitAddrs, addr)
res.CommitBlockHash = append(res.CommitBlockHash, common.ToHex(status.Details.BlockHash[i]))
}
return res, nil
}
// Query_GetTitleByHash query paracross title by block hash
func (p *Paracross) Query_GetTitleByHash(in *pt.ReqParacrossTitleHash) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
block, err := p.GetAPI().GetBlockOverview(&types.ReqHash{Hash: in.BlockHash})
if err != nil || block == nil {
return nil, types.ErrHashNotExist
}
return p.paracrossGetHeight(in.GetTitle())
}
//Query_GetNodeGroupAddrs get node group addrs
func (p *Paracross) Query_GetNodeGroupAddrs(in *pt.ReqParacrossNodeInfo) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
ret, _, err := getParacrossNodes(p.GetStateDB(), in.GetTitle())
if err != nil {
return nil, errors.Cause(err)
}
var nodes []string
for k := range ret {
nodes = append(nodes, k)
}
var reply types.ReplyConfig
reply.Key = string(calcParaNodeGroupAddrsKey(in.GetTitle()))
reply.Value = fmt.Sprint(nodes)
return &reply, nil
}
//Query_GetNodeAddrInfo get specific node addr info
func (p *Paracross) Query_GetNodeAddrInfo(in *pt.ReqParacrossNodeInfo) (types.Message, error) {
if in == nil || in.Title == "" || in.Addr == "" {
return nil, types.ErrInvalidParam
}
stat, err := getNodeAddr(p.GetStateDB(), in.Title, in.Addr)
if err != nil {
return nil, err
}
return stat, nil
}
//Query_GetNodeIDInfo get specific node addr info
func (p *Paracross) Query_GetNodeIDInfo(in *pt.ReqParacrossNodeInfo) (types.Message, error) {
if in == nil || in.Title == "" || in.Id == "" {
return nil, types.ErrInvalidParam
}
stat, err := getNodeID(p.GetStateDB(), in.Id)
if err != nil {
return nil, err
}
return stat, nil
}
//Query_ListNodeStatusInfo list node info by status
func (p *Paracross) Query_ListNodeStatusInfo(in *pt.ReqParacrossNodeInfo) (types.Message, error) {
if in == nil || in.Title == "" {
return nil, types.ErrInvalidParam
}
return listLocalNodeStatus(p.GetLocalDB(), in.Title, in.Status)
}
//Query_GetNodeGroupStatus get specific node addr info
func (p *Paracross) Query_GetNodeGroupStatus(in *pt.ReqParacrossNodeInfo) (types.Message, error) {
if in == nil || in.Title == "" {
return nil, types.ErrInvalidParam
}
stat, err := getNodeGroupStatus(p.GetStateDB(), in.Title)
if err != nil {
return nil, err
}
return stat, nil
}
//Query_ListNodeGroupStatus list node info by status
func (p *Paracross) Query_ListNodeGroupStatus(in *pt.ReqParacrossNodeInfo) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
return listLocalNodeGroupStatus(p.GetLocalDB(), in.Status)
}
//Query_ListTitles query paracross titles list
func (p *Paracross) Query_ListTitles(in *types.ReqNil) (types.Message, error) {
return p.paracrossListTitles()
}
// Query_GetDoneTitleHeight query title height
func (p *Paracross) Query_GetDoneTitleHeight(in *pt.ReqParacrossTitleHeight) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
return p.paracrossGetTitleHeight(in.Title, in.Height)
}
// Query_GetAssetTxResult query get asset tx reseult
func (p *Paracross) Query_GetAssetTxResult(in *types.ReqHash) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
return p.paracrossGetAssetTxResult(in.Hash)
}
// Query_GetMainBlockHash query get mainblockHash by tx
func (p *Paracross) Query_GetMainBlockHash(in *types.Transaction) (types.Message, error) {
if in == nil {
return nil, types.ErrInvalidParam
}
return p.paracrossGetMainBlockHash(in)
}
func (p *Paracross) paracrossGetMainBlockHash(tx *types.Transaction) (types.Message, error) {
var paraAction pt.ParacrossAction
err := types.Decode(tx.GetPayload(), ¶Action)
if err != nil {
return nil, err
}
if paraAction.GetTy() != pt.ParacrossActionMiner {
return nil, types.ErrCoinBaseTxType
}
if paraAction.GetMiner() == nil {
return nil, pt.ErrParaEmptyMinerTx
}
paraNodeStatus := paraAction.GetMiner().GetStatus()
if paraNodeStatus == nil {
return nil, types.ErrCoinBaseTxType
}
mainHashFromNode := paraNodeStatus.MainBlockHash
return &types.ReplyHash{Hash: mainHashFromNode}, nil
}
func (p *Paracross) paracrossGetHeight(title string) (types.Message, error) {
ret, err := getTitle(p.GetStateDB(), calcTitleKey(title))
if err != nil {
return nil, errors.Cause(err)
}
return ret, nil
}
func (p *Paracross) paracrossGetStateTitleHeight(title string, height int64) (types.Message, error) {
ret, err := getTitleHeight(p.GetStateDB(), calcTitleHeightKey(title, height))
if err != nil {
return nil, errors.Cause(err)
}
return ret, nil
}
func (p *Paracross) paracrossListTitles() (types.Message, error) {
return listLocalTitles(p.GetLocalDB())
}
func listLocalTitles(db dbm.KVDB) (types.Message, error) {
prefix := calcLocalTitlePrefix()
res, err := db.List(prefix, []byte(""), 0, 1)
if err != nil {
return nil, err
}
var resp pt.RespParacrossTitles
for _, r := range res {
var st pt.ReceiptParacrossDone
err = types.Decode(r, &st)
if err != nil {
panic(err)
}
rst := &pt.RespParacrossDone{
TotalNodes: st.TotalNodes,
TotalCommit: st.TotalCommit,
MostSameCommit: st.MostSameCommit,
Title: st.Title,
Height: st.Height,
StateHash: common.ToHex(st.StateHash),
TxCounts: st.TxCounts,
TxResult: hex.EncodeToString(st.TxResult),
}
resp.Titles = append(resp.Titles, rst)
}
return &resp, nil
}
func listNodeStatus(db dbm.KVDB, prefix []byte) (types.Message, error) {
res, err := db.List(prefix, []byte(""), 0, 1)
if err != nil {
return nil, err
}
var resp pt.RespParacrossNodeAddrs
for _, r := range res {
var st pt.ParaNodeIdStatus
err = types.Decode(r, &st)
if err != nil {
panic(err)
}
resp.Ids = append(resp.Ids, &st)
}
return &resp, nil
}
func listNodeGroupStatus(db dbm.KVDB, prefix []byte) (types.Message, error) {
res, err := db.List(prefix, []byte(""), 0, 1)
if err != nil {
return nil, err
}
var resp pt.RespParacrossNodeGroups
for _, r := range res {
var st pt.ParaNodeGroupStatus
err = types.Decode(r, &st)
if err != nil {
panic(err)
}
resp.Ids = append(resp.Ids, &st)
}
return &resp, nil
}
//按状态遍历
func listLocalNodeStatus(db dbm.KVDB, title string, status int32) (types.Message, error) {
var prefix []byte
if status == 0 {
prefix = calcLocalNodeTitlePrefix(title)
} else {
prefix = calcLocalNodeStatusPrefix(title, status)
}
return listNodeStatus(db, prefix)
}
func listLocalNodeGroupStatus(db dbm.KVDB, status int32) (types.Message, error) {
var prefix []byte
if status == 0 {
prefix = calcLocalNodeGroupAllPrefix()
} else {
prefix = calcLocalNodeGroupStatusPrefix(status)
}
return listNodeGroupStatus(db, prefix)
}
func loadLocalTitle(db dbm.KV, title string, height int64) (types.Message, error) {
key := calcLocalHeightKey(title, height)
res, err := db.Get(key)
if err != nil {
return nil, err
}
var st pt.ReceiptParacrossDone
err = types.Decode(res, &st)
if err != nil {
panic(err)
}
return &pt.RespParacrossDone{
TotalNodes: st.TotalNodes,
TotalCommit: st.TotalCommit,
MostSameCommit: st.MostSameCommit,
Title: st.Title,
Height: st.Height,
StateHash: common.ToHex(st.StateHash),
TxCounts: st.TxCounts,
TxResult: hex.EncodeToString(st.TxResult),
}, nil
}
func (p *Paracross) paracrossGetTitleHeight(title string, height int64) (types.Message, error) {
return loadLocalTitle(p.GetLocalDB(), title, height)
}
func (p *Paracross) paracrossGetAssetTxResult(hash []byte) (types.Message, error) {
if len(hash) == 0 {
return nil, types.ErrInvalidParam
}
key := calcLocalAssetKey(hash)
value, err := p.GetLocalDB().Get(key)
if err != nil {
return nil, err
}
var result pt.ParacrossAsset
err = types.Decode(value, &result)
if err != nil {
return nil, err
}
return &result, nil
}