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"
|
|
|
|
"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 version = 1
|
|
|
|
|
|
|
|
const (
|
|
|
|
peerIDSize = 46 // NeoFS PeerIDSize
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
func Init(owner interop.Hash160) {
|
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
panic("only owner can reinitialize contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
|
|
|
|
|
|
|
runtime.Log("reputation contract initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Migrate(script []byte, manifest []byte) bool {
|
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
runtime.Log("only owner can update contract")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
management.Update(script, manifest)
|
|
|
|
runtime.Log("reputation contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Put(manager, epoch, typ []byte, newTrustList [][]byte) bool {
|
|
|
|
if !runtime.CheckWitness(manager) {
|
|
|
|
panic("put: incorrect manager key")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(newTrustList); i++ {
|
|
|
|
trustData := newTrustList[i]
|
|
|
|
|
|
|
|
if len(trustData) != trustStructSize {
|
|
|
|
panic("list: invalid trust value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: consider using notification for inner ring node
|
|
|
|
|
|
|
|
// 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...)...)
|
|
|
|
|
2021-02-02 18:34:17 +00:00
|
|
|
trustList := common.GetList(ctx, key)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
// 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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, key, trustList)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
runtime.Log("trust list was successfully updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func List(epoch, typ []byte) [][]byte {
|
|
|
|
key := append(trustJournalPrefix, append(epoch, typ...)...)
|
|
|
|
|
2021-02-02 18:34:17 +00:00
|
|
|
return common.GetList(ctx, key)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|