Skip to content

Commit

Permalink
ONT-559 modify ontid method outputs (#173)
Browse files Browse the repository at this point in the history
* add attribute paths in add attribute event
* query functions return nil when not found
  • Loading branch information
AlverLyu authored and laizy committed May 17, 2018
1 parent e906600 commit ddbe38d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
18 changes: 11 additions & 7 deletions smartcontract/service/native/ontid/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,28 @@ func findAttr(srvc *native.NativeService, encID, item []byte) (*utils.Linkedlist
return utils.LinkedlistGetItem(srvc, key, item)
}

func batchInsertAttr(srvc *native.NativeService, encID, data []byte) error {
func batchInsertAttr(srvc *native.NativeService, encID, data []byte) ([][]byte, error) {
// parse attributes
buf := bytes.NewBuffer(data)
attr := make([]*attribute, 0)
for buf.Len() > 0 {
t := new(attribute)
err := t.Deserialize(buf)
if err != nil {
return errors.New("parse attribute error: " + err.Error())
return nil, errors.New("parse attribute error: " + err.Error())
}
attr = append(attr, t)
}
for _, v := range attr {
res := make([][]byte, len(attr))
for i, v := range attr {
err := insertOrUpdateAttr(srvc, encID, v)
if err != nil {
return errors.New("store attributes error: " + err.Error())
return nil, errors.New("store attributes error: " + err.Error())
}
res[i] = v.key
}
return nil

return res, nil
}

func getAllAttr(srvc *native.NativeService, encID []byte) ([]byte, error) {
Expand All @@ -142,7 +145,8 @@ func getAllAttr(srvc *native.NativeService, encID []byte) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("get list head error, %s", err)
} else if len(item) == 0 {
return nil, errors.New("cannot get list head")
// not exists
return nil, nil
}

var res bytes.Buffer
Expand All @@ -152,7 +156,7 @@ func getAllAttr(srvc *native.NativeService, encID []byte) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("get storage item error, %s", err)
} else if node == nil {
return nil, fmt.Errorf("storage item not exists")
return nil, fmt.Errorf("storage item not exists, %v", item)
}

var attr attribute
Expand Down
14 changes: 12 additions & 2 deletions smartcontract/service/native/ontid/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ func triggerPublicEvent(srvc *native.NativeService, op string, id, pub []byte, k
newEvent(srvc, st)
}

func triggerAttributeEvent(srvc *native.NativeService, op string, id, path []byte) {
st := []string{"Attribute", op, string(id), string(path)}
func triggerAttributeEvent(srvc *native.NativeService, op string, id []byte, path [][]byte) {
var attr interface{}
if op == "remove" {
attr = hex.EncodeToString(path[0])
} else {
t := make([]string, len(path))
for i, v := range path {
t[i] = hex.EncodeToString(v)
}
attr = t
}
st := []interface{}{"Attribute", op, string(id), attr}
newEvent(srvc, st)
}

Expand Down
8 changes: 4 additions & 4 deletions smartcontract/service/native/ontid/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func regIdWithAttributes(srvc *native.NativeService) ([]byte, error) {
return utils.BYTE_FALSE, errors.New("register ID with attributes error: store pubic key error: " + err.Error())
}

err = batchInsertAttr(srvc, key, arg2)
_, err = batchInsertAttr(srvc, key, arg2)
if err != nil {
return utils.BYTE_FALSE, errors.New("register ID with attributes error: insert attribute error: " + err.Error())
}
Expand Down Expand Up @@ -372,12 +372,12 @@ func addAttributes(srvc *native.NativeService) ([]byte, error) {
return utils.BYTE_FALSE, fmt.Errorf("add attributes failed, %s", err)
}

err = batchInsertAttr(srvc, key, arg1)
paths, err := batchInsertAttr(srvc, key, arg1)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("add attributes failed, %s", err)
}

triggerAttributeEvent(srvc, "add", arg0, []byte("multiple attributes"))
triggerAttributeEvent(srvc, "add", arg0, paths)
return utils.BYTE_TRUE, nil
}

Expand Down Expand Up @@ -422,7 +422,7 @@ func removeAttribute(srvc *native.NativeService) ([]byte, error) {
return utils.BYTE_FALSE, errors.New("remove attribute failed: attribute not exist")
}

triggerAttributeEvent(srvc, "remove", arg0, arg1)
triggerAttributeEvent(srvc, "remove", arg0, [][]byte{arg1})
return utils.BYTE_TRUE, nil
}

Expand Down
3 changes: 1 addition & 2 deletions smartcontract/service/native/ontid/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package ontid

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -67,7 +66,7 @@ func getAllPk(srvc *native.NativeService, key []byte) ([]*owner, error) {
return nil, fmt.Errorf("get storage error, %s", err)
}
if val == nil {
return nil, fmt.Errorf("empty storage item, key = %s", hex.EncodeToString(key))
return nil, nil
}
buf := bytes.NewBuffer(val.Value)
owners := make([]*owner, 0)
Expand Down
5 changes: 5 additions & 0 deletions smartcontract/service/native/ontid/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func GetDDO(srvc *native.NativeService) ([]byte, error) {
var0, err := GetPublicKeys(srvc)
if err != nil {
return nil, fmt.Errorf("get DDO error: %s", err)
} else if var0 == nil {
log.Debug("DDO: null")
return nil, nil
}
var buf bytes.Buffer
serialization.WriteVarBytes(&buf, var0)
Expand Down Expand Up @@ -99,6 +102,8 @@ func GetPublicKeys(srvc *native.NativeService) ([]byte, error) {
list, err := getAllPk(srvc, key)
if err != nil {
return nil, fmt.Errorf("get public keys error: %s", err)
} else if list == nil {
return nil, nil
}

var res bytes.Buffer
Expand Down
17 changes: 4 additions & 13 deletions smartcontract/service/native/ontid/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ontio/ontology/core/store/common"
"github.com/ontio/ontology/core/types"
"github.com/ontio/ontology/smartcontract/service/native"
"github.com/ontio/ontology/smartcontract/service/native/utils"
)

const flag_exist = 0x01
Expand Down Expand Up @@ -78,25 +79,15 @@ func setRecovery(srvc *native.NativeService, encID, recovery []byte) error {

func getRecovery(srvc *native.NativeService, encID []byte) ([]byte, error) {
key := append(encID, FIELD_RECOVERY)
item, err := getStorageItem(srvc, key)
item, err := utils.GetStorageItem(srvc, key)
if err != nil {
return nil, errors.New("get recovery error: " + err.Error())
} else if item == nil {
return nil, nil
}
return item.Value, nil
}

func getStorageItem(srvc *native.NativeService, key []byte) (*states.StorageItem, error) {
val, err := srvc.CloneCache.Get(common.ST_STORAGE, key)
if err != nil {
return nil, err
}
t, ok := val.(*states.StorageItem)
if !ok {
return nil, errors.New("invalid value type")
}
return t, nil
}

func checkWitness(srvc *native.NativeService, key []byte) error {
// try as if key is a public key
pk, err := keypair.DeserializePublicKey(key)
Expand Down

0 comments on commit ddbe38d

Please sign in to comment.