5
5
package executor
6
6
7
7
import (
8
+ "github.com/33cn/chain33/account"
9
+ "github.com/33cn/chain33/client"
10
+ "github.com/33cn/chain33/common"
8
11
dbm "github.com/33cn/chain33/common/db"
9
- pty "github.com/33cn/chain33/system/dapp/manage/types"
12
+ "github.com/33cn/chain33/system/dapp"
13
+ mty "github.com/33cn/chain33/system/dapp/manage/types"
10
14
"github.com/33cn/chain33/types"
15
+ "github.com/golang/protobuf/proto"
16
+ "github.com/pkg/errors"
11
17
)
12
18
13
- // Action attribute
14
- type Action struct {
15
- db dbm.KV
16
- fromaddr string
17
- height int64
18
- cfg * types.Chain33Config
19
+ type action struct {
20
+ api client.QueueProtocolAPI
21
+ coinsAccount * account.DB
22
+ db dbm.KV
23
+ txhash []byte
24
+ fromaddr string
25
+ height int64
26
+ index int32
27
+ execaddr string
19
28
}
20
29
21
- // NewAction new a action object
22
- func NewAction (m * Manage , tx * types.Transaction ) * Action {
23
- types .AssertConfig (m .GetAPI ())
24
- return & Action {db : m .GetStateDB (), fromaddr : tx .From (), height : m .GetHeight (), cfg : m .GetAPI ().GetConfig ()}
25
-
30
+ func newAction (m * Manage , tx * types.Transaction , index int32 ) * action {
31
+ return & action {m .GetAPI (), m .GetCoinsAccount (), m .GetStateDB (), tx .Hash (), tx .From (),
32
+ m .GetHeight (), index , dapp .ExecAddress (string (tx .Execer ))}
26
33
}
27
34
28
- func (m * Action ) modifyConfig (modify * types.ModifyConfig ) (* types.Receipt , error ) {
35
+ func (a * action ) modifyConfig (modify * types.ModifyConfig ) (* types.Receipt , error ) {
29
36
30
37
//if modify.Key == "Manager-managers" && !wallet.IsSuperManager(modify.GetAddr()) {
31
38
// return nil, types.ErrNoPrivilege
@@ -34,23 +41,20 @@ func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error
34
41
// return nil, types.ErrNoPrivilege
35
42
//}
36
43
37
- if ! IsSuperManager (m .cfg , m .fromaddr ) {
38
- return nil , pty .ErrNoPrivilege
39
- }
40
44
if len (modify .Key ) == 0 {
41
- return nil , pty .ErrBadConfigKey
45
+ return nil , mty .ErrBadConfigKey
42
46
}
43
47
if modify .Op != "add" && modify .Op != "delete" {
44
- return nil , pty .ErrBadConfigOp
48
+ return nil , mty .ErrBadConfigOp
45
49
}
46
50
47
51
var item types.ConfigItem
48
- value , err := m .db .Get ([] byte ( types . ManageKey ( modify .Key ) ))
52
+ value , err := a .db .Get (manageKey ( modify .Key ))
49
53
if err != nil {
50
54
value = nil
51
55
}
52
56
if value == nil {
53
- value , err = m .db .Get ([] byte ( types . ConfigKey ( modify .Key ) ))
57
+ value , err = a .db .Get (configKey ( modify .Key ))
54
58
if err != nil {
55
59
value = nil
56
60
}
@@ -64,7 +68,7 @@ func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error
64
68
} else { // if config item not exist, create a new empty
65
69
item .Key = modify .Key
66
70
item .Addr = modify .Addr
67
- item .Ty = pty .ConfigItemArrayConfig
71
+ item .Ty = mty .ConfigItemArrayConfig
68
72
emptyValue := & types.ArrayConfig {Value : make ([]string , 0 )}
69
73
arr := types.ConfigItem_Arr {Arr : emptyValue }
70
74
item .Value = & arr
@@ -110,15 +114,144 @@ func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error
110
114
111
115
var logs []* types.ReceiptLog
112
116
var kv []* types.KeyValue
113
- key := m . cfg . ManaeKeyWithHeigh (modify .Key , m . height )
117
+ key := a . manageKeyWithHeigh (modify .Key )
114
118
valueSave := types .Encode (& item )
115
- err = m .db .Set ([] byte ( key ) , valueSave )
119
+ err = a .db .Set (key , valueSave )
116
120
if err != nil {
117
121
return nil , err
118
122
}
119
- kv = append (kv , & types.KeyValue {Key : [] byte ( key ) , Value : valueSave })
123
+ kv = append (kv , & types.KeyValue {Key : key , Value : valueSave })
120
124
log := types.ReceiptConfig {Prev : & copyItem , Current : & item }
121
- logs = append (logs , & types.ReceiptLog {Ty : pty .TyLogModifyConfig , Log : types .Encode (& log )})
125
+ logs = append (logs , & types.ReceiptLog {Ty : mty .TyLogModifyConfig , Log : types .Encode (& log )})
122
126
receipt := & types.Receipt {Ty : types .ExecOk , KV : kv , Logs : logs }
123
127
return receipt , nil
124
128
}
129
+
130
+ //ManaeKeyWithHeigh 超级管理员账户key
131
+ func (a * action ) manageKeyWithHeigh (key string ) []byte {
132
+ if a .api .GetConfig ().IsFork (a .height , "ForkExecKey" ) {
133
+ return manageKey (key )
134
+ }
135
+ return configKey (key )
136
+ }
137
+
138
+ func (a * action ) applyConfig (payload * mty.ApplyConfig ) (* types.Receipt , error ) {
139
+ configStatus := & mty.ConfigStatus {
140
+ Id : common .ToHex (a .txhash ),
141
+ Modify : payload .Modify ,
142
+ Status : mty .ManageConfigStatusApply ,
143
+ Proposer : a .fromaddr ,
144
+ Height : a .height ,
145
+ Index : a .index ,
146
+ }
147
+
148
+ return makeApplyReceipt (configStatus ), nil
149
+ }
150
+
151
+ func (a * action ) getConfig (ID string ) (* mty.ConfigStatus , error ) {
152
+ value , err := a .db .Get (manageKey (ID ))
153
+ if err != nil {
154
+ return nil , err
155
+ }
156
+ var status mty.ConfigStatus
157
+ err = types .Decode (value , & status )
158
+ if err != nil {
159
+ return nil , err
160
+ }
161
+ return & status , nil
162
+ }
163
+
164
+ func (a * action ) approveConfig (payload * mty.ApproveConfig ) (* types.Receipt , error ) {
165
+ if len (payload .ApproveId ) <= 0 || len (payload .Id ) <= 0 {
166
+ return nil , errors .Wrapf (types .ErrInvalidParam , "id nil, appoved=%s,id=%s" , payload .ApproveId , payload .Id )
167
+ }
168
+
169
+ s , err := a .getConfig (payload .Id )
170
+ if err != nil {
171
+ return nil , errors .Wrapf (err , "get Config id=%s" , payload .Id )
172
+ }
173
+
174
+ if s .Status != mty .ManageConfigStatusApply {
175
+ return nil , errors .Wrapf (types .ErrNotAllow , "id status =%d" , s .Status )
176
+ }
177
+
178
+ cfg := a .api .GetConfig ()
179
+ confManager := types .ConfSub (cfg , mty .ManageX )
180
+ autonomyExec := confManager .GStr (types .AutonomyCfgKey )
181
+ if len (autonomyExec ) <= 0 {
182
+ return nil , errors .Wrapf (types .ErrNotFound , "manager autonomy key not config" )
183
+ }
184
+
185
+ //去autonomy 合约检验是否id approved, 成功 err返回nil
186
+ _ , err = a .api .QueryChain (& types.ChainExecutor {
187
+ Driver : autonomyExec ,
188
+ FuncName : "IsAutonomyApprovedItem" ,
189
+ Param : types .Encode (& types.ReqStrings {Datas : []string {payload .ApproveId , payload .Id }}),
190
+ })
191
+ if err != nil {
192
+ return nil , errors .Wrapf (err , "query autonomy,approveid=%s,hashId=%s" , payload .ApproveId , payload .Id )
193
+ }
194
+
195
+ copyStat := proto .Clone (s ).(* mty.ConfigStatus )
196
+ s .Status = mty .ForkManageAutonomyApprove
197
+
198
+ r := makeApproveReceipt (copyStat , s )
199
+
200
+ cr , err := a .modifyConfig (s .Modify )
201
+ if err != nil {
202
+ return nil , errors .Wrap (err , "modify config" )
203
+ }
204
+
205
+ return mergeReceipt (r , cr ), nil
206
+
207
+ }
208
+
209
+ func mergeReceipt (receipt1 , receipt2 * types.Receipt ) * types.Receipt {
210
+ if receipt2 != nil {
211
+ receipt1 .KV = append (receipt1 .KV , receipt2 .KV ... )
212
+ receipt1 .Logs = append (receipt1 .Logs , receipt2 .Logs ... )
213
+ }
214
+
215
+ return receipt1
216
+ }
217
+
218
+ func makeApplyReceipt (status * mty.ConfigStatus ) * types.Receipt {
219
+ key := manageKey (status .Id )
220
+ log := & mty.ReceiptApplyConfig {
221
+ Status : status ,
222
+ }
223
+
224
+ return & types.Receipt {
225
+ Ty : types .ExecOk ,
226
+ KV : []* types.KeyValue {
227
+ {Key : key , Value : types .Encode (status )},
228
+ },
229
+ Logs : []* types.ReceiptLog {
230
+ {
231
+ Ty : mty .TyLogApplyConfig ,
232
+ Log : types .Encode (log ),
233
+ },
234
+ },
235
+ }
236
+ }
237
+
238
+ func makeApproveReceipt (pre , cur * mty.ConfigStatus ) * types.Receipt {
239
+ key := manageKey (cur .Id )
240
+ log := & mty.ReceiptApproveConfig {
241
+ Pre : pre ,
242
+ Cur : cur ,
243
+ }
244
+
245
+ return & types.Receipt {
246
+ Ty : types .ExecOk ,
247
+ KV : []* types.KeyValue {
248
+ {Key : key , Value : types .Encode (cur )},
249
+ },
250
+ Logs : []* types.ReceiptLog {
251
+ {
252
+ Ty : mty .TyLogApproveConfig ,
253
+ Log : types .Encode (log ),
254
+ },
255
+ },
256
+ }
257
+ }
0 commit comments