2020-10-27 12:14:06 +00:00
|
|
|
package reputationcontract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|