2021-07-04 11:00:56 +00:00
|
|
|
package reputation
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-07 11:06:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-15 13:12:53 +00:00
|
|
|
reputationValuePrefix = 'r'
|
|
|
|
reputationCountPrefix = 'c'
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2023-11-07 12:18:48 +00:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
2023-11-07 12:18:48 +00:00
|
|
|
args := data.([]any)
|
2021-12-27 08:49:30 +00:00
|
|
|
common.CheckVersion(args[len(args)-1].(int))
|
2021-06-03 07:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("reputation contract initialized")
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Update method updates contract source code and manifest. It can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2023-11-07 12:18:48 +00:00
|
|
|
func Update(script []byte, manifest []byte, data any) {
|
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
|
|
|
}
|
|
|
|
|
2023-06-19 08:17:51 +00:00
|
|
|
management.UpdateWithData(script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("reputation contract updated")
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Put method saves DataAuditResult in contract storage. It can be invoked only by
|
|
|
|
// Inner Ring nodes. It does not require multisignature invocations.
|
2021-07-13 17:55:32 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// Epoch is the epoch number when DataAuditResult structure was generated.
|
|
|
|
// PeerID contains public keys of the Inner Ring node that has produced DataAuditResult.
|
|
|
|
// Value contains a 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
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Get method returns a list of all stable marshaled DataAuditResult structures
|
|
|
|
// produced by the specified Inner Ring node during the specified epoch.
|
2021-03-29 13:01:03 +00:00
|
|
|
func Get(epoch int, peerID []byte) [][]byte {
|
|
|
|
id := storageID(epoch, peerID)
|
|
|
|
return GetByID(id)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// GetByID method returns a list of all stable marshaled DataAuditResult with
|
|
|
|
// the specified id. Use ListByEpoch method to obtain the 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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// ListByEpoch returns a 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
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Version returns the 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 {
|
2023-11-07 12:18:48 +00:00
|
|
|
var buf any = 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
|
|
|
}
|