[#65] reputation: Update reputation contract

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-03-29 16:01:03 +03:00 committed by Alex Vanin
parent 23f5f1e0e7
commit 88a82820b8
2 changed files with 80 additions and 57 deletions

View file

@ -1,2 +1,11 @@
name: "NeoFS Reputation" name: "NeoFS Reputation"
safemethods: ["list"] safemethods: ["get, getByID, listByEpoch"]
events:
- name: reputationPut
parameters:
- name: epoch
type: Integer
- name: peerID
type: ByteArray
- name: value
type: ByteArray

View file

@ -2,35 +2,21 @@ package reputationcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/interop/native/management" "github.com/nspcc-dev/neo-go/pkg/interop/native/management"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"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"
) )
const version = 1
const ( const (
peerIDSize = 46 // NeoFS PeerIDSize version = 1
trustValSize = 8 // float64
trustStructSize = 0 +
peerIDSize + // manager ID
peerIDSize + // trusted ID
trustValSize // trust value
) )
var (
trustJournalPrefix = []byte("trustJournal")
ctx storage.Context
)
func init() {
ctx = storage.GetContext()
}
func Init(owner interop.Hash160) { func Init(owner interop.Hash160) {
ctx := storage.GetContext()
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract") panic("only owner can reinitialize contract")
} }
@ -41,6 +27,8 @@ func Init(owner interop.Hash160) {
} }
func Migrate(script []byte, manifest []byte) bool { func Migrate(script []byte, manifest []byte) bool {
ctx := storage.GetReadOnlyContext()
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
runtime.Log("only owner can update contract") runtime.Log("only owner can update contract")
return false return false
@ -52,47 +40,73 @@ func Migrate(script []byte, manifest []byte) bool {
return true return true
} }
func Put(manager, epoch, typ []byte, newTrustList [][]byte) bool { func Put(epoch int, peerID []byte, value []byte) {
if !runtime.CheckWitness(manager) { ctx := storage.GetContext()
panic("put: incorrect manager key")
multiaddr := common.AlphabetAddress()
if !runtime.CheckWitness(multiaddr) {
runtime.Notify("reputationPut", epoch, peerID, value)
return
} }
for i := 0; i < len(newTrustList); i++ { id := storageID(epoch, peerID)
trustData := newTrustList[i]
if len(trustData) != trustStructSize { reputationValues := GetByID(id)
panic("list: invalid trust value") reputationValues = append(reputationValues, value)
}
}
// todo: consider using notification for inner ring node rawValues := std.Serialize(reputationValues)
storage.Put(ctx, id, rawValues)
// todo: limit size of the trust journal:
// history will be stored in chain (args or notifies)
// contract storage will be used as a cache if needed
key := append(trustJournalPrefix, append(epoch, typ...)...)
trustList := common.GetList(ctx, key)
// fixme: with neo3.0 it is kinda unnecessary
if len(trustList) == 0 {
// if journal slice is not initialized, then `append` will throw
trustList = newTrustList
} else {
for i := 0; i < len(newTrustList); i++ {
trustList = append(trustList, newTrustList[i])
}
}
common.SetSerialized(ctx, key, trustList)
runtime.Log("trust list was successfully updated")
return true
} }
func List(epoch, typ []byte) [][]byte { func Get(epoch int, peerID []byte) [][]byte {
key := append(trustJournalPrefix, append(epoch, typ...)...) id := storageID(epoch, peerID)
return GetByID(id)
return common.GetList(ctx, key) }
func GetByID(id []byte) [][]byte {
ctx := storage.GetReadOnlyContext()
data := storage.Get(ctx, id)
if data == nil {
return [][]byte{}
}
return std.Deserialize(data.([]byte)).([][]byte)
}
// ListByEpoch returns list of IDs that may be used to get reputation data
// via GetByID method.
func ListByEpoch(epoch int) [][]byte {
ctx := storage.GetReadOnlyContext()
it := storage.Find(ctx, epoch, storage.KeysOnly)
var result [][]byte
ignore := [][]byte{
[]byte(common.OwnerKey),
}
loop:
for iterator.Next(it) {
key := iterator.Value(it).([]byte) // iterator MUST BE `storage.KeysOnly`
for _, ignoreKey := range ignore {
if common.BytesEqual(key, ignoreKey) {
continue loop
}
}
result = append(result, key)
}
return result
}
func Version() int {
return version
}
func storageID(epoch int, peerID []byte) []byte {
var buf interface{} = epoch
return append(buf.([]byte), peerID...)
} }