github.com/turingchain2020/turingchain@v1.1.21/system/dapp/manage/executor/managedb.go (about) 1 // Copyright Turing 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 dbm "github.com/turingchain2020/turingchain/common/db" 9 pty "github.com/turingchain2020/turingchain/system/dapp/manage/types" 10 "github.com/turingchain2020/turingchain/types" 11 ) 12 13 // Action attribute 14 type Action struct { 15 db dbm.KV 16 fromaddr string 17 height int64 18 cfg *types.TuringchainConfig 19 } 20 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 26 } 27 28 func (m *Action) modifyConfig(modify *types.ModifyConfig) (*types.Receipt, error) { 29 30 //if modify.Key == "Manager-managers" && !wallet.IsSuperManager(modify.GetAddr()) { 31 // return nil, types.ErrNoPrivilege 32 //} 33 //if !wallet.IsSuperManager(m.fromaddr) && ! IsManager(m.fromaddr) { 34 // return nil, types.ErrNoPrivilege 35 //} 36 37 if !IsSuperManager(m.cfg, m.fromaddr) { 38 return nil, pty.ErrNoPrivilege 39 } 40 if len(modify.Key) == 0 { 41 return nil, pty.ErrBadConfigKey 42 } 43 if modify.Op != "add" && modify.Op != "delete" { 44 return nil, pty.ErrBadConfigOp 45 } 46 47 var item types.ConfigItem 48 value, err := m.db.Get([]byte(types.ManageKey(modify.Key))) 49 if err != nil { 50 value = nil 51 } 52 if value == nil { 53 value, err = m.db.Get([]byte(types.ConfigKey(modify.Key))) 54 if err != nil { 55 value = nil 56 } 57 } 58 if value != nil { 59 err = types.Decode(value, &item) 60 if err != nil { 61 clog.Error("modifyConfig", "decode db key", modify.Key) 62 return nil, err // types.ErrBadConfigValue 63 } 64 } else { // if config item not exist, create a new empty 65 item.Key = modify.Key 66 item.Addr = modify.Addr 67 item.Ty = pty.ConfigItemArrayConfig 68 emptyValue := &types.ArrayConfig{Value: make([]string, 0)} 69 arr := types.ConfigItem_Arr{Arr: emptyValue} 70 item.Value = &arr 71 } 72 copyValue := *item.GetArr() 73 copyItem := item 74 copyItem.Value = &types.ConfigItem_Arr{Arr: ©Value} 75 76 switch modify.Op { 77 case "add": 78 item.GetArr().Value = append(item.GetArr().Value, modify.Value) 79 item.Addr = modify.Addr 80 clog.Info("modifyConfig", "add key", modify.Key, "from", copyItem.GetArr().Value, "to", item.GetArr().Value) 81 82 case "delete": 83 item.Addr = modify.Addr 84 item.GetArr().Value = make([]string, 0) 85 for _, value := range copyItem.GetArr().Value { 86 clog.Info("modifyConfig", "key delete", modify.Key, "current", value) 87 if value != modify.Value { 88 item.GetArr().Value = append(item.GetArr().Value, value) 89 } 90 } 91 clog.Info("modifyConfig", "delete key", modify.Key, "from", copyItem.GetArr().Value, "to", item.GetArr().Value) 92 /* 93 case "assign": 94 if item.Ty == types.ConfigItemIntConfig { 95 intvalue, err := strconv.Atoi(modify.Value) 96 if err != nil { 97 clog.Error("modifyConfig", "key", modify.Key, "strconv.Atoi", err) 98 } 99 item.GetInt().Value = int32(intvalue) 100 clog.Info("modifyConfig", "key", modify.Key, "from", copyItem.GetInt().Value, "to", item.GetInt().Value) 101 } else { 102 item.GetStr().Value = modify.Value 103 clog.Info("modifyConfig", "key", modify.Key, "from", copyItem.GetStr().Value, "to", item.GetStr().Value) 104 }*/ 105 } 106 107 var logs []*types.ReceiptLog 108 var kv []*types.KeyValue 109 key := m.cfg.ManaeKeyWithHeigh(modify.Key, m.height) 110 valueSave := types.Encode(&item) 111 err = m.db.Set([]byte(key), valueSave) 112 if err != nil { 113 return nil, err 114 } 115 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: valueSave}) 116 log := types.ReceiptConfig{Prev: ©Item, Current: &item} 117 logs = append(logs, &types.ReceiptLog{Ty: pty.TyLogModifyConfig, Log: types.Encode(&log)}) 118 receipt := &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs} 119 return receipt, nil 120 }