Skip to content

Commit 6ab042c

Browse files
mdj3333cn
mdj33
authored andcommitted
add manager exec apply process
1 parent 4c27291 commit 6ab042c

File tree

12 files changed

+281
-62
lines changed

12 files changed

+281
-62
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ webhook_auto_ci: clean fmt_proto fmt_shell protobuf mock
310310
fi;
311311

312312
addupstream:
313-
git remote add upstream https://github.com/33cn/chain33.git
313+
git remote add upstream git@github.com:33cn/chain33.git
314314
git remote -v
315315

316316
sync:

cmd/chain33/chain33.toml

+2
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ superManager=[
319319
"12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv",
320320
"1Q8hGLfoGe63efeWa8fJ4Pnukhkngt6poK"
321321
]
322+
#自治合约执行器可配置
323+
autonomyExec="autonomy"
322324

323325
[exec.sub.autonomy]
324326
total="16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp"

system/dapp/manage/executor/exec.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,40 @@ func (c *Manage) Exec_Modify(manageAction *types.ModifyConfig, tx *types.Transac
2828
// 兼容在区块上没有To地址检查的交易数据
2929
types.AssertConfig(c.GetAPI())
3030
cfg := c.GetAPI().GetConfig()
31-
if cfg.IsDappFork(c.GetHeight(), mty.ManageX, "ForkManageExec") {
31+
32+
if cfg.IsDappFork(c.GetHeight(), mty.ManageX, mty.ForkManageAutonomyApprove) {
33+
return nil, types.ErrNotAllow
34+
}
35+
36+
if cfg.IsDappFork(c.GetHeight(), mty.ManageX, mty.ForkManageExec) {
3237
if err := c.checkTxToAddress(tx, index); err != nil {
3338
return nil, err
3439
}
3540
}
36-
action := NewAction(c, tx)
41+
action := newAction(c, tx, int32(index))
42+
if !IsSuperManager(cfg, action.fromaddr) {
43+
return nil, mty.ErrNoPrivilege
44+
}
3745
return action.modifyConfig(manageAction)
3846

3947
}
48+
49+
func (c *Manage) Exec_ApplyConfig(payload *mty.ApplyConfig, tx *types.Transaction, index int) (*types.Receipt, error) {
50+
cfg := c.GetAPI().GetConfig()
51+
if !cfg.IsDappFork(c.GetHeight(), mty.ManageX, mty.ForkManageAutonomyApprove) {
52+
return nil, types.ErrNotAllow
53+
}
54+
55+
action := newAction(c, tx, int32(index))
56+
return action.applyConfig(payload)
57+
}
58+
59+
func (c *Manage) Exec_ApproveConfig(payload *mty.ApproveConfig, tx *types.Transaction, index int) (*types.Receipt, error) {
60+
cfg := c.GetAPI().GetConfig()
61+
if !cfg.IsDappFork(c.GetHeight(), mty.ManageX, mty.ForkManageAutonomyApprove) {
62+
return nil, types.ErrNotAllow
63+
}
64+
65+
action := newAction(c, tx, int32(index))
66+
return action.approveConfig(payload)
67+
}

system/dapp/manage/executor/kv.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package executor
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
// ConfigPrefix 配置前缀key
12+
var configPrefix = "mavl-config-"
13+
14+
// ConfigKey 原来实现有bug, 但生成的key在状态树里, 不可修改
15+
// mavl-config–{key} key 前面两个-
16+
func configKey(key string) []byte {
17+
return []byte(fmt.Sprintf("%s-%s", configPrefix, key))
18+
}
19+
20+
// ManagePrefix 超级管理员账户配置前缀key
21+
var managePrefix = "mavl-manage"
22+
23+
//ManageKey 超级管理员账户key
24+
func manageKey(key string) []byte {
25+
return []byte(fmt.Sprintf("%s-%s", managePrefix, key))
26+
}

system/dapp/manage/executor/managedb.go

+158-25
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,34 @@
55
package executor
66

77
import (
8+
"github.com/33cn/chain33/account"
9+
"github.com/33cn/chain33/client"
10+
"github.com/33cn/chain33/common"
811
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"
1014
"github.com/33cn/chain33/types"
15+
"github.com/golang/protobuf/proto"
16+
"github.com/pkg/errors"
1117
)
1218

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
1928
}
2029

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))}
2633
}
2734

28-
func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error) {
35+
func (a *action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error) {
2936

3037
//if modify.Key == "Manager-managers" && !wallet.IsSuperManager(modify.GetAddr()) {
3138
// return nil, types.ErrNoPrivilege
@@ -34,23 +41,20 @@ func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error
3441
// return nil, types.ErrNoPrivilege
3542
//}
3643

37-
if !IsSuperManager(m.cfg, m.fromaddr) {
38-
return nil, pty.ErrNoPrivilege
39-
}
4044
if len(modify.Key) == 0 {
41-
return nil, pty.ErrBadConfigKey
45+
return nil, mty.ErrBadConfigKey
4246
}
4347
if modify.Op != "add" && modify.Op != "delete" {
44-
return nil, pty.ErrBadConfigOp
48+
return nil, mty.ErrBadConfigOp
4549
}
4650

4751
var item types.ConfigItem
48-
value, err := m.db.Get([]byte(types.ManageKey(modify.Key)))
52+
value, err := a.db.Get(manageKey(modify.Key))
4953
if err != nil {
5054
value = nil
5155
}
5256
if value == nil {
53-
value, err = m.db.Get([]byte(types.ConfigKey(modify.Key)))
57+
value, err = a.db.Get(configKey(modify.Key))
5458
if err != nil {
5559
value = nil
5660
}
@@ -64,7 +68,7 @@ func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error
6468
} else { // if config item not exist, create a new empty
6569
item.Key = modify.Key
6670
item.Addr = modify.Addr
67-
item.Ty = pty.ConfigItemArrayConfig
71+
item.Ty = mty.ConfigItemArrayConfig
6872
emptyValue := &types.ArrayConfig{Value: make([]string, 0)}
6973
arr := types.ConfigItem_Arr{Arr: emptyValue}
7074
item.Value = &arr
@@ -110,15 +114,144 @@ func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error
110114

111115
var logs []*types.ReceiptLog
112116
var kv []*types.KeyValue
113-
key := m.cfg.ManaeKeyWithHeigh(modify.Key, m.height)
117+
key := a.manageKeyWithHeigh(modify.Key)
114118
valueSave := types.Encode(&item)
115-
err = m.db.Set([]byte(key), valueSave)
119+
err = a.db.Set(key, valueSave)
116120
if err != nil {
117121
return nil, err
118122
}
119-
kv = append(kv, &types.KeyValue{Key: []byte(key), Value: valueSave})
123+
kv = append(kv, &types.KeyValue{Key: key, Value: valueSave})
120124
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)})
122126
receipt := &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
123127
return receipt, nil
124128
}
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+
}

system/dapp/manage/executor/query.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
// Query_GetConfigItem get config item
1414
func (c *Manage) Query_GetConfigItem(in *types.ReqString) (types.Message, error) {
1515
// Load config from state db
16-
value, err := c.GetStateDB().Get([]byte(types.ManageKey(in.Data)))
16+
value, err := c.GetStateDB().Get(manageKey(in.Data))
1717
if err != nil {
1818
clog.Info("modifyConfig", "get db key", "not found")
1919
value = nil
2020
}
2121
if value == nil {
22-
value, err = c.GetStateDB().Get([]byte(types.ConfigKey(in.Data)))
22+
value, err = c.GetStateDB().Get(configKey(in.Data))
2323
if err != nil {
2424
clog.Info("modifyConfig", "get db key", "not found")
2525
value = nil

system/dapp/manage/proto/manage.proto

+36-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,44 @@ syntax = "proto3";
33
import "executor.proto";
44

55
package types;
6+
option go_package = "../types";
7+
8+
//申请修改配置项
9+
message ApplyConfig {
10+
ModifyConfig modify = 1;
11+
}
12+
13+
//批准配置项
14+
message ApproveConfig {
15+
string id = 1; //修改配置项申请ID
16+
string approveId = 2; // approve 合约批准的ID
17+
}
618

719
message ManageAction {
820
oneof value {
9-
ModifyConfig modify = 1;
21+
ModifyConfig modify = 1;
22+
ApplyConfig apply = 3;
23+
ApproveConfig approve = 4;
1024
}
1125
int32 Ty = 2;
12-
}
26+
}
27+
28+
message ConfigStatus {
29+
string id = 1; //申请ID
30+
ModifyConfig modify = 2;
31+
int32 status = 3;
32+
string proposer = 4;
33+
34+
// 状态
35+
int64 height = 8;
36+
int32 index = 9;
37+
}
38+
39+
message ReceiptApplyConfig {
40+
ConfigStatus status = 1;
41+
}
42+
43+
message ReceiptApproveConfig {
44+
ConfigStatus pre = 1;
45+
ConfigStatus cur = 2;
46+
}

0 commit comments

Comments
 (0)