From 68882b5b3c3e55c8e17b3654249f9c9e8b44c7bb Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 2 Feb 2021 21:34:17 +0300 Subject: [PATCH] [#42] Share GetList function between contracts Replace getList function to common package and export it. Reuse the function in Container and Reputation contracts. Signed-off-by: Leonard Lyubich --- common/storage.go | 21 +++++++++++++++++++++ common/vote.go | 6 ------ container/container_contract.go | 21 ++++++--------------- reputation/reputation_contract.go | 14 ++------------ 4 files changed, 29 insertions(+), 33 deletions(-) create mode 100644 common/storage.go diff --git a/common/storage.go b/common/storage.go new file mode 100644 index 0000000..085ff07 --- /dev/null +++ b/common/storage.go @@ -0,0 +1,21 @@ +package common + +import ( + "github.com/nspcc-dev/neo-go/pkg/interop/binary" + "github.com/nspcc-dev/neo-go/pkg/interop/storage" +) + +func GetList(ctx storage.Context, key interface{}) [][]byte { + data := storage.Get(ctx, key) + if data != nil { + return binary.Deserialize(data.([]byte)).([][]byte) + } + + return [][]byte{} +} + +// SetSerialized serializes data and puts it into contract storage. +func SetSerialized(ctx storage.Context, key interface{}, value interface{}) { + data := binary.Serialize(value) + storage.Put(ctx, key, data) +} diff --git a/common/vote.go b/common/vote.go index 722ff1e..fa2b9a3 100644 --- a/common/vote.go +++ b/common/vote.go @@ -96,9 +96,3 @@ func getBallots(ctx storage.Context) []Ballot { func BytesEqual(a []byte, b []byte) bool { return util.Equals(string(a), string(b)) } - -// SetSerialized serializes data and puts it into contract storage. -func SetSerialized(ctx storage.Context, key interface{}, value interface{}) { - data := binary.Serialize(value) - storage.Put(ctx, key, data) -} diff --git a/container/container_contract.go b/container/container_contract.go index 58abaa5..f2b9f87 100644 --- a/container/container_contract.go +++ b/container/container_contract.go @@ -203,14 +203,14 @@ func List(owner []byte) [][]byte { var list [][]byte - owners := getList(ctx, ownersKey) + owners := common.GetList(ctx, ownersKey) for i := 0; i < len(owners); i++ { ownerID := owners[i] if len(owner) != 0 && !common.BytesEqual(owner, ownerID) { continue } - containers := getList(ctx, ownerID) + containers := common.GetList(ctx, ownerID) for j := 0; j < len(containers); j++ { container := containers[j] @@ -429,7 +429,7 @@ func removeContainer(ctx storage.Context, id []byte, owner []byte) { } func addOrAppend(ctx storage.Context, key interface{}, value []byte) { - list := getList(ctx, key) + list := common.GetList(ctx, key) for i := 0; i < len(list); i++ { if common.BytesEqual(list[i], value) { return @@ -448,7 +448,7 @@ func addOrAppend(ctx storage.Context, key interface{}, value []byte) { // remove returns amount of left elements in the list func remove(ctx storage.Context, key interface{}, value []byte) int { var ( - list = getList(ctx, key) + list = common.GetList(ctx, key) newList = [][]byte{} ) @@ -468,15 +468,6 @@ func remove(ctx storage.Context, key interface{}, value []byte) int { return ln } -func getList(ctx storage.Context, key interface{}) [][]byte { - data := storage.Get(ctx, key) - if data != nil { - return binary.Deserialize(data.([]byte)).([][]byte) - } - - return [][]byte{} -} - func getAllContainers(ctx storage.Context) [][]byte { var list [][]byte @@ -517,10 +508,10 @@ func verifySignature(msg, sig []byte, keys [][]byte) bool { } func getOwnerByID(ctx storage.Context, id []byte) []byte { - owners := getList(ctx, ownersKey) + owners := common.GetList(ctx, ownersKey) for i := 0; i < len(owners); i++ { ownerID := owners[i] - containers := getList(ctx, ownerID) + containers := common.GetList(ctx, ownerID) for j := 0; j < len(containers); j++ { container := containers[j] diff --git a/reputation/reputation_contract.go b/reputation/reputation_contract.go index 5f5242c..043d1a0 100644 --- a/reputation/reputation_contract.go +++ b/reputation/reputation_contract.go @@ -1,7 +1,6 @@ package reputationcontract import ( - "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neofs-contract/common" @@ -53,7 +52,7 @@ func Put(manager, epoch, typ []byte, newTrustList [][]byte) bool { // contract storage will be used as a cache if needed key := append(trustJournalPrefix, append(epoch, typ...)...) - trustList := getList(ctx, key) + trustList := common.GetList(ctx, key) // fixme: with neo3.0 it is kinda unnecessary if len(trustList) == 0 { @@ -75,14 +74,5 @@ func Put(manager, epoch, typ []byte, newTrustList [][]byte) bool { func List(epoch, typ []byte) [][]byte { key := append(trustJournalPrefix, append(epoch, typ...)...) - return getList(ctx, key) -} - -func getList(ctx storage.Context, key interface{}) [][]byte { - data := storage.Get(ctx, key) - if data != nil { - return binary.Deserialize(data.([]byte)).([][]byte) - } - - return [][]byte{} + return common.GetList(ctx, key) }