/
item.go
163 lines (148 loc) · 4.72 KB
/
item.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
// 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 (
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
auty "github.com/33cn/plugin/plugin/dapp/autonomy/types"
"github.com/pkg/errors"
)
func (a *Autonomy) execAutoLocalItem(tx *types.Transaction, receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
set, err := a.execLocalItem(receiptData)
if err != nil {
return set, err
}
dbSet := &types.LocalDBSet{}
dbSet.KV = a.AddRollbackKV(tx, tx.Execer, set.KV)
return dbSet, nil
}
func (a *Autonomy) execLocalItem(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
table := NewItemTable(a.GetLocalDB())
for _, log := range receiptData.Logs {
switch log.Ty {
case auty.TyLogPropItem,
auty.TyLogRvkPropItem,
auty.TyLogVotePropItem,
auty.TyLogTmintPropItem:
{
var receipt auty.ReceiptProposalItem
err := types.Decode(log.Log, &receipt)
if err != nil {
return nil, err
}
err = table.Replace(receipt.Current)
if err != nil {
return nil, err
}
}
default:
break
}
}
kvs, err := table.Save()
if err != nil {
return nil, err
}
dbSet := &types.LocalDBSet{}
dbSet.KV = append(dbSet.KV, kvs...)
return dbSet, nil
}
func (a *Autonomy) listProposalItem(req *auty.ReqQueryProposalItem) (types.Message, error) {
if req == nil {
return nil, types.ErrInvalidParam
}
localDb := a.GetLocalDB()
query := NewItemTable(localDb).GetQuery(localDb)
var primary []byte
if req.Height > 0 {
primary = []byte(dapp.HeightIndexStr(req.Height, int64(req.Index)))
}
indexName := ""
if req.Status > 0 && req.Addr != "" {
indexName = "addr_status"
} else if req.Status > 0 {
indexName = "status"
} else if req.Addr != "" {
indexName = "addr"
}
cur := &ItemRow{
AutonomyProposalItem: &auty.AutonomyProposalItem{},
}
cur.Address = req.Addr
cur.Status = req.Status
cur.Height = req.Height
cur.Index = req.Index
prefix, err := cur.Get(indexName)
if err != nil {
alog.Error("Get", "indexName", indexName, "err", err)
return nil, err
}
rows, err := query.ListIndex(indexName, prefix, primary, req.Count, req.Direction)
if err != nil {
alog.Error("query List failed", "indexName", indexName, "prefix", "prefix", "key", string(primary), "err", err)
return nil, err
}
if len(rows) == 0 {
return nil, types.ErrNotFound
}
var rep auty.ReplyQueryProposalItem
for _, row := range rows {
r, ok := row.Data.(*auty.AutonomyProposalItem)
if !ok {
alog.Error("listProposalItem", "err", "bad row type")
return nil, types.ErrDecode
}
rep.PropItems = append(rep.PropItems, r)
}
return &rep, nil
}
func getProposalItem(db dbm.KV, req *types.ReqString) (*auty.ReplyQueryProposalItem, error) {
if req == nil || len(req.Data) <= 0 {
return nil, errors.Wrapf(types.ErrInvalidParam, "invalid parameter")
}
value, err := db.Get(propItemID(req.Data))
if err != nil {
return nil, errors.Wrapf(err, "fail,db.get item id=%s", req.Data)
}
prop := &auty.AutonomyProposalItem{}
err = types.Decode(value, prop)
if err != nil {
return nil, errors.Wrapf(err, "decode item fail")
}
rep := &auty.ReplyQueryProposalItem{}
rep.PropItems = append(rep.PropItems, prop)
return rep, nil
}
// IsAutonomyApprovedItem get 2 parameters: autonomyItemID, applyTxHash
func IsAutonomyApprovedItem(db dbm.KV, req *types.ReqMultiStrings) (types.Message, error) {
if req == nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "req is nil")
}
if len(req.Datas) < 2 {
return nil, errors.Wrapf(types.ErrInvalidParam, "req datas less 2 parameters")
}
autonomyItemID := req.Datas[0]
applyTxHash := req.Datas[1]
res, err := getProposalItem(db, &types.ReqString{Data: autonomyItemID})
if err != nil {
return nil, err
}
if len(res.GetPropItems()) <= 0 {
return nil, errors.Wrapf(types.ErrNotFound, "not get item")
}
if res.PropItems[0].ProposalID != autonomyItemID {
return nil, errors.Wrapf(types.ErrInvalidParam, "get prop id=%s not equal req=%s", res.PropItems[0].ProposalID, autonomyItemID)
}
if res.PropItems[0].PropItem.ItemTxHash != applyTxHash {
return nil, errors.Wrapf(types.ErrInvalidParam, "get item id=%s != req=%s", res.PropItems[0].PropItem.ItemTxHash, applyTxHash)
}
if res.PropItems[0].Status == auty.AutonomyStatusTmintPropItem && res.PropItems[0].BoardVoteRes.Pass {
return &types.Reply{IsOk: true}, nil
}
if res.PropItems[0].Status != auty.AutonomyStatusTmintPropItem {
return nil, errors.Wrapf(types.ErrNotAllow, "item status =%d not terminate", res.PropItems[0].Status)
}
return nil, errors.Wrapf(types.ErrNotAllow, "item vote status not pass = %v", res.PropItems[0].BoardVoteRes.Pass)
}