2021-11-18 11:31:58 +00:00
|
|
|
package subnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-11-21 19:55:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-11-18 11:31:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-11-21 19:55:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
2021-11-18 11:31:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-18 12:21:14 +00:00
|
|
|
// ErrInvalidSubnetID is thrown when subnet id is not a slice of 4 bytes.
|
|
|
|
ErrInvalidSubnetID = "invalid subnet ID"
|
|
|
|
// ErrInvalidOwner is thrown when owner has invalid format.
|
|
|
|
ErrInvalidOwner = "invalid owner"
|
2021-11-21 19:55:39 +00:00
|
|
|
// ErrInvalidAdmin is thrown when admin has invalid format.
|
|
|
|
ErrInvalidAdmin = "invalid administrator"
|
2021-11-18 12:21:14 +00:00
|
|
|
// ErrAlreadyExists is thrown when id already exists.
|
|
|
|
ErrAlreadyExists = "subnet id already exists"
|
2021-11-22 14:20:00 +00:00
|
|
|
// ErrSubNotExist is thrown when id doesn't exist.
|
|
|
|
ErrSubNotExist = "subnet id doesn't exist"
|
|
|
|
// ErrNodeAdmNotExist is thrown when node admin is not found.
|
|
|
|
ErrNodeAdmNotExist = "node admin not found"
|
|
|
|
|
|
|
|
errCheckWitnessFailed = "owner witness check failed"
|
2021-11-18 12:21:14 +00:00
|
|
|
|
|
|
|
ownerPrefix = 'o'
|
2021-11-21 19:55:39 +00:00
|
|
|
nodeAdminPrefix = 'a'
|
|
|
|
clientAdminPrefix = 'm'
|
|
|
|
nodePrefix = 'n'
|
|
|
|
user = 'u'
|
2021-11-18 12:21:14 +00:00
|
|
|
infoPrefix = 'i'
|
2021-11-18 11:31:58 +00:00
|
|
|
notaryDisabledKey = 'z'
|
|
|
|
)
|
|
|
|
|
|
|
|
// _deploy function sets up initial list of inner ring public keys.
|
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
if isUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args := data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, []byte{notaryDisabledKey}, args.notaryDisabled)
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:56:21 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
|
|
|
// only by committee.
|
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
contract.Call(interop.Hash160(management.Hash), "update", contract.All, script, manifest, data)
|
|
|
|
runtime.Log("subnet contract updated")
|
|
|
|
}
|
|
|
|
|
2021-11-18 12:21:14 +00:00
|
|
|
// Put creates new subnet with the specified owner and info.
|
|
|
|
func Put(id []byte, ownerKey interop.PublicKey, info []byte) {
|
|
|
|
if len(id) != 4 {
|
|
|
|
panic("put: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
if len(ownerKey) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("put: " + ErrInvalidOwner)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
stKey := append([]byte{ownerPrefix}, id...)
|
|
|
|
if storage.Get(ctx, stKey) != nil {
|
|
|
|
panic("put: " + ErrAlreadyExists)
|
|
|
|
}
|
|
|
|
|
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet := common.AlphabetNodes()
|
|
|
|
nodeKey := common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
if !runtime.CheckWitness(ownerKey) {
|
|
|
|
panic("put: witness check failed")
|
|
|
|
}
|
2021-11-22 12:19:33 +00:00
|
|
|
runtime.Notify("SubnetPut", id, ownerKey, info)
|
2021-11-18 12:21:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{ownerKey, info}, []byte("put"))
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
} else {
|
|
|
|
if !runtime.CheckWitness(ownerKey) {
|
2021-11-22 14:20:00 +00:00
|
|
|
panic("put: " + errCheckWitnessFailed)
|
2021-11-18 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("put: alphabet witness check failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, stKey, ownerKey)
|
|
|
|
stKey[0] = infoPrefix
|
|
|
|
storage.Put(ctx, stKey, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns info about subnet with the specified id.
|
|
|
|
func Get(id []byte) []byte {
|
2021-11-22 14:20:00 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-11-18 12:21:14 +00:00
|
|
|
key := append([]byte{infoPrefix}, id...)
|
|
|
|
raw := storage.Get(ctx, key)
|
|
|
|
if raw == nil {
|
2021-11-22 14:20:00 +00:00
|
|
|
panic("get: " + ErrSubNotExist)
|
2021-11-18 12:21:14 +00:00
|
|
|
}
|
|
|
|
return raw.([]byte)
|
|
|
|
}
|
|
|
|
|
2021-11-18 12:24:32 +00:00
|
|
|
// Delete deletes subnet with the specified id.
|
|
|
|
func Delete(id []byte) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
key := append([]byte{ownerPrefix}, id...)
|
|
|
|
raw := storage.Get(ctx, key)
|
|
|
|
if raw == nil {
|
2021-11-22 14:20:00 +00:00
|
|
|
panic("delete:" + ErrSubNotExist)
|
2021-11-18 12:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := raw.([]byte)
|
|
|
|
if !runtime.CheckWitness(owner) {
|
2021-11-22 14:20:00 +00:00
|
|
|
panic("delete: " + errCheckWitnessFailed)
|
2021-11-18 12:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
|
|
|
|
key[0] = infoPrefix
|
|
|
|
storage.Delete(ctx, key)
|
2021-11-22 12:19:33 +00:00
|
|
|
|
|
|
|
runtime.Notify("SubnetDelete", id)
|
2021-11-18 12:24:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 19:55:39 +00:00
|
|
|
// AddNodeAdmin adds new node administrator to the specified subnetwork.
|
|
|
|
func AddNodeAdmin(subnetID []byte, adminKey interop.PublicKey) {
|
|
|
|
if len(adminKey) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("addNodeAdmin: " + ErrInvalidAdmin)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-22 14:20:00 +00:00
|
|
|
panic("addNodeAdmin: " + ErrSubNotExist)
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
if !runtime.CheckWitness(owner) {
|
2021-11-22 14:20:00 +00:00
|
|
|
panic("addNodeAdmin: " + errCheckWitnessFailed)
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = nodeAdminPrefix
|
|
|
|
prefixLen := len(stKey)
|
|
|
|
|
|
|
|
iter := storage.Find(ctx, stKey, storage.KeysOnly)
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
key := iterator.Value(iter).([]byte)
|
|
|
|
if util.Equals(string(key[prefixLen:]), string(adminKey)) {
|
|
|
|
panic("addNodeAdmin: node admin has already been added")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, append(stKey, adminKey...), []byte{1})
|
|
|
|
}
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
// RemoveNodeAdmin removes node administrator from the specified subnetwork.
|
|
|
|
// Must be called by subnet owner only.
|
|
|
|
func RemoveNodeAdmin(subnetID []byte, adminKey interop.PublicKey) {
|
|
|
|
if len(adminKey) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("removeNodeAdmin: " + ErrInvalidAdmin)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stOwnerKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stOwnerKey)
|
|
|
|
if rawOwner == nil {
|
|
|
|
panic("removeNodeAdmin: " + ErrSubNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
if !runtime.CheckWitness(owner) {
|
|
|
|
panic("removeNodeAdmin: " + errCheckWitnessFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
stOwnerKey[0] = nodeAdminPrefix
|
|
|
|
stNodeAdmKey := append(stOwnerKey, adminKey...)
|
|
|
|
|
|
|
|
if storage.Get(ctx, stNodeAdmKey) == nil {
|
|
|
|
panic("removeNodeAdmin: " + ErrNodeAdmNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, stNodeAdmKey)
|
|
|
|
}
|
|
|
|
|
2021-11-18 11:31:58 +00:00
|
|
|
// Version returns version of the contract.
|
|
|
|
func Version() int {
|
|
|
|
return common.Version
|
|
|
|
}
|