2020-10-27 12:14:06 +00:00
|
|
|
package reputationcontract
|
|
|
|
|
|
|
|
import (
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-03-29 13:01:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-03-29 13:01:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-02-02 17:36:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
version = 1
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
func Init(notaryDisabled bool, owner interop.Hash160) {
|
2021-03-29 13:01:03 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
panic("only owner can reinitialize contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
|
|
|
storage.Put(ctx, notaryDisabledKey, notaryDisabled)
|
|
|
|
if notaryDisabled {
|
|
|
|
common.InitVote(ctx)
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("reputation contract initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Migrate(script []byte, manifest []byte) bool {
|
2021-03-29 13:01:03 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
runtime.Log("only owner can update contract")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
management.Update(script, manifest)
|
|
|
|
runtime.Log("reputation contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
func Put(epoch int, peerID []byte, value []byte) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
runtime.Notify("reputationPut", epoch, peerID, value)
|
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
id := storageID(epoch, peerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
reputationValues := GetByID(id)
|
|
|
|
reputationValues = append(reputationValues, value)
|
|
|
|
|
|
|
|
rawValues := std.Serialize(reputationValues)
|
|
|
|
storage.Put(ctx, id, rawValues)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Get(epoch int, peerID []byte) [][]byte {
|
|
|
|
id := storageID(epoch, peerID)
|
|
|
|
return GetByID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetByID(id []byte) [][]byte {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
data := storage.Get(ctx, id)
|
|
|
|
if data == nil {
|
|
|
|
return [][]byte{}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
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()
|
2021-04-01 06:36:29 +00:00
|
|
|
var buf interface{} = epoch
|
|
|
|
it := storage.Find(ctx, buf.([]byte), storage.KeysOnly)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
var result [][]byte
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
ignore := [][]byte{
|
|
|
|
[]byte(common.OwnerKey),
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
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
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
result = append(result, key)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
return result
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
func Version() int {
|
|
|
|
return version
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
func storageID(epoch int, peerID []byte) []byte {
|
|
|
|
var buf interface{} = epoch
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
return append(buf.([]byte), peerID...)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|