[#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 <leonard@nspcc.ru>
enable-notary-in-public-chains
Leonard Lyubich 2021-02-02 21:34:17 +03:00 committed by Alex Vanin
parent 3395a886fc
commit 68882b5b3c
4 changed files with 29 additions and 33 deletions

21
common/storage.go 100644
View File

@ -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)
}

View File

@ -96,9 +96,3 @@ func getBallots(ctx storage.Context) []Ballot {
func BytesEqual(a []byte, b []byte) bool { func BytesEqual(a []byte, b []byte) bool {
return util.Equals(string(a), string(b)) 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)
}

View File

@ -203,14 +203,14 @@ func List(owner []byte) [][]byte {
var list [][]byte var list [][]byte
owners := getList(ctx, ownersKey) owners := common.GetList(ctx, ownersKey)
for i := 0; i < len(owners); i++ { for i := 0; i < len(owners); i++ {
ownerID := owners[i] ownerID := owners[i]
if len(owner) != 0 && !common.BytesEqual(owner, ownerID) { if len(owner) != 0 && !common.BytesEqual(owner, ownerID) {
continue continue
} }
containers := getList(ctx, ownerID) containers := common.GetList(ctx, ownerID)
for j := 0; j < len(containers); j++ { for j := 0; j < len(containers); j++ {
container := 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) { 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++ { for i := 0; i < len(list); i++ {
if common.BytesEqual(list[i], value) { if common.BytesEqual(list[i], value) {
return return
@ -448,7 +448,7 @@ func addOrAppend(ctx storage.Context, key interface{}, value []byte) {
// remove returns amount of left elements in the list // remove returns amount of left elements in the list
func remove(ctx storage.Context, key interface{}, value []byte) int { func remove(ctx storage.Context, key interface{}, value []byte) int {
var ( var (
list = getList(ctx, key) list = common.GetList(ctx, key)
newList = [][]byte{} newList = [][]byte{}
) )
@ -468,15 +468,6 @@ func remove(ctx storage.Context, key interface{}, value []byte) int {
return ln 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 { func getAllContainers(ctx storage.Context) [][]byte {
var list [][]byte var list [][]byte
@ -517,10 +508,10 @@ func verifySignature(msg, sig []byte, keys [][]byte) bool {
} }
func getOwnerByID(ctx storage.Context, id []byte) []byte { func getOwnerByID(ctx storage.Context, id []byte) []byte {
owners := getList(ctx, ownersKey) owners := common.GetList(ctx, ownersKey)
for i := 0; i < len(owners); i++ { for i := 0; i < len(owners); i++ {
ownerID := owners[i] ownerID := owners[i]
containers := getList(ctx, ownerID) containers := common.GetList(ctx, ownerID)
for j := 0; j < len(containers); j++ { for j := 0; j < len(containers); j++ {
container := containers[j] container := containers[j]

View File

@ -1,7 +1,6 @@
package reputationcontract package reputationcontract
import ( 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/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/nspcc-dev/neofs-contract/common" "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 // contract storage will be used as a cache if needed
key := append(trustJournalPrefix, append(epoch, typ...)...) key := append(trustJournalPrefix, append(epoch, typ...)...)
trustList := getList(ctx, key) trustList := common.GetList(ctx, key)
// fixme: with neo3.0 it is kinda unnecessary // fixme: with neo3.0 it is kinda unnecessary
if len(trustList) == 0 { if len(trustList) == 0 {
@ -75,14 +74,5 @@ func Put(manager, epoch, typ []byte, newTrustList [][]byte) bool {
func List(epoch, typ []byte) [][]byte { func List(epoch, typ []byte) [][]byte {
key := append(trustJournalPrefix, append(epoch, typ...)...) key := append(trustJournalPrefix, append(epoch, typ...)...)
return getList(ctx, key) return common.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{}
} }