2021-07-04 11:00:56 +00:00
|
|
|
package reputation
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-07-01 17:41:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-11-15 13:12:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/convert"
|
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"
|
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-11-15 13:12:53 +00:00
|
|
|
notaryDisabledKey = "notary"
|
|
|
|
reputationValuePrefix = 'r'
|
|
|
|
reputationCountPrefix = 'c'
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-11-15 13:12:53 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
2021-12-27 08:49:30 +00:00
|
|
|
args := data.([]interface{})
|
|
|
|
common.CheckVersion(args[len(args)-1].(int))
|
2021-06-03 07:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
})
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, notaryDisabledKey, args.notaryDisabled)
|
|
|
|
if args.notaryDisabled {
|
2021-04-27 10:48:40 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:02:24 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2021-09-21 12:58:37 +00:00
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
2021-09-20 15:41:46 +00:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:55:21 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update",
|
|
|
|
contract.All, script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("reputation contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:55:32 +00:00
|
|
|
// Put method saves DataAuditResult in contract storage. Can be invoked only by
|
|
|
|
// Inner Ring nodes. Does not require multi signature invocations.
|
|
|
|
//
|
|
|
|
// Epoch is an epoch number when DataAuditResult structure was generated.
|
|
|
|
// PeerID contains public keys of Inner Ring node that produced DataAuditResult.
|
|
|
|
// Value contains stable marshaled structure of DataAuditResult.
|
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
|
2022-03-21 11:01:45 +00:00
|
|
|
alphabet []interop.PublicKey
|
2021-04-29 13:20:22 +00:00
|
|
|
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)
|
2021-11-30 08:59:52 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-30 08:59:52 +00:00
|
|
|
key := getReputationKey(reputationCountPrefix, id)
|
2021-11-15 13:12:53 +00:00
|
|
|
rawCnt := storage.Get(ctx, key)
|
|
|
|
cnt := 0
|
|
|
|
if rawCnt != nil {
|
|
|
|
cnt = rawCnt.(int)
|
|
|
|
}
|
|
|
|
cnt++
|
|
|
|
storage.Put(ctx, key, cnt)
|
2021-03-29 13:01:03 +00:00
|
|
|
|
2021-11-15 13:12:53 +00:00
|
|
|
key[0] = reputationValuePrefix
|
|
|
|
key = append(key, convert.ToBytes(cnt)...)
|
|
|
|
storage.Put(ctx, key, value)
|
2021-03-29 13:01:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 17:55:32 +00:00
|
|
|
// Get method returns list of all stable marshaled DataAuditResult structures
|
|
|
|
// produced by specified Inner Ring node in specified epoch.
|
2021-03-29 13:01:03 +00:00
|
|
|
func Get(epoch int, peerID []byte) [][]byte {
|
|
|
|
id := storageID(epoch, peerID)
|
|
|
|
return GetByID(id)
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:55:32 +00:00
|
|
|
// GetByID method returns list of all stable marshaled DataAuditResult with
|
|
|
|
// specified id. Use ListByEpoch method to obtain id.
|
2021-03-29 13:01:03 +00:00
|
|
|
func GetByID(id []byte) [][]byte {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-11-15 13:12:53 +00:00
|
|
|
var data [][]byte
|
|
|
|
|
|
|
|
it := storage.Find(ctx, getReputationKey(reputationValuePrefix, id), storage.ValuesOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
data = append(data, iterator.Value(it).([]byte))
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
2021-11-15 13:12:53 +00:00
|
|
|
return data
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-15 13:12:53 +00:00
|
|
|
func getReputationKey(prefix byte, id []byte) []byte {
|
|
|
|
return append([]byte{prefix}, id...)
|
2021-03-29 13:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListByEpoch returns list of IDs that may be used to get reputation data
|
2021-07-13 17:55:32 +00:00
|
|
|
// with GetByID method.
|
2021-03-29 13:01:03 +00:00
|
|
|
func ListByEpoch(epoch int) [][]byte {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-11-15 13:12:53 +00:00
|
|
|
key := getReputationKey(reputationCountPrefix, convert.ToBytes(epoch))
|
|
|
|
it := storage.Find(ctx, key, 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
|
|
|
for iterator.Next(it) {
|
|
|
|
key := iterator.Value(it).([]byte) // iterator MUST BE `storage.KeysOnly`
|
2021-11-15 13:12:53 +00:00
|
|
|
result = append(result, key[1:])
|
2021-03-29 13:01:03 +00:00
|
|
|
}
|
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-07-13 17:55:32 +00:00
|
|
|
// Version returns version of the contract.
|
2021-03-29 13:01:03 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.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
|
|
|
}
|