forked from TrueCloudLab/frostfs-contract
[#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>
This commit is contained in:
parent
3395a886fc
commit
68882b5b3c
4 changed files with 29 additions and 33 deletions
21
common/storage.go
Normal file
21
common/storage.go
Normal 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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue