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-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
args := data.([]interface{})
|
|
|
|
notaryDisabled := args[0].(bool)
|
|
|
|
owner := args[1].(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-05-04 14:02:05 +00:00
|
|
|
runtime.Log("reputation contract notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2021-04-29 13:20:22 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-29 13:01:03 +00:00
|
|
|
|
2021-04-29 13:20:22 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
alphabetCall bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
alphabetCall = len(nodeKey) != 0
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
alphabetCall = runtime.CheckWitness(multiaddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !alphabetCall {
|
2021-03-29 13:01:03 +00:00
|
|
|
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)
|
2021-04-29 13:20:22 +00:00
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:01:03 +00:00
|
|
|
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),
|
2021-04-29 13:20:22 +00:00
|
|
|
[]byte(notaryDisabledKey),
|
2021-03-29 13:01:03 +00:00
|
|
|
}
|
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
|
|
|
}
|