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"
|
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-24 10:33:24 +00:00
|
|
|
// ErrInvalidSubnetID is thrown when subnet id is not a slice of 5 bytes.
|
2021-11-18 12:21:14 +00:00
|
|
|
ErrInvalidSubnetID = "invalid subnet ID"
|
2021-11-24 10:33:24 +00:00
|
|
|
// ErrInvalidGroupID is thrown when group id is not a slice of 5 bytes.
|
|
|
|
ErrInvalidGroupID = "invalid group ID"
|
2021-11-18 12:21:14 +00:00
|
|
|
// 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-23 15:36:45 +00:00
|
|
|
// ErrNotExist is thrown when id doesn't exist.
|
|
|
|
ErrNotExist = "subnet id doesn't exist"
|
2021-11-23 10:56:56 +00:00
|
|
|
// ErrInvalidUser is thrown when user has invalid format.
|
|
|
|
ErrInvalidUser = "invalid user"
|
2021-11-23 09:54:32 +00:00
|
|
|
// ErrInvalidNode is thrown when node has invalid format.
|
|
|
|
ErrInvalidNode = "invalid node key"
|
2021-11-22 14:20:00 +00:00
|
|
|
// ErrNodeAdmNotExist is thrown when node admin is not found.
|
|
|
|
ErrNodeAdmNotExist = "node admin not found"
|
2021-11-23 10:20:07 +00:00
|
|
|
// ErrClientAdmNotExist is thrown when client admin is not found.
|
|
|
|
ErrClientAdmNotExist = "client admin not found"
|
2021-11-22 18:35:07 +00:00
|
|
|
// ErrNodeNotExist is thrown when node is not found.
|
|
|
|
ErrNodeNotExist = "node not found"
|
2021-11-23 11:15:16 +00:00
|
|
|
// ErrUserNotExist is thrown when user is not found.
|
|
|
|
ErrUserNotExist = "user not found"
|
2021-11-22 16:55:07 +00:00
|
|
|
// ErrAccessDenied is thrown when operation is denied for caller.
|
|
|
|
ErrAccessDenied = "access denied"
|
2021-11-22 14:20:00 +00:00
|
|
|
|
|
|
|
errCheckWitnessFailed = "owner witness check failed"
|
2021-11-24 10:03:02 +00:00
|
|
|
)
|
2021-11-18 12:21:14 +00:00
|
|
|
|
2021-11-24 10:03:02 +00:00
|
|
|
const (
|
2021-11-21 19:55:39 +00:00
|
|
|
nodeAdminPrefix = 'a'
|
2021-11-23 10:56:56 +00:00
|
|
|
infoPrefix = 'i'
|
2021-11-21 19:55:39 +00:00
|
|
|
clientAdminPrefix = 'm'
|
|
|
|
nodePrefix = 'n'
|
2021-11-23 10:56:56 +00:00
|
|
|
ownerPrefix = 'o'
|
|
|
|
userPrefix = 'u'
|
2021-11-18 11:31:58 +00:00
|
|
|
notaryDisabledKey = 'z'
|
|
|
|
)
|
|
|
|
|
2021-11-23 10:56:56 +00:00
|
|
|
const (
|
2021-11-24 17:26:33 +00:00
|
|
|
userIDSize = 27
|
2021-11-24 10:03:02 +00:00
|
|
|
subnetIDSize = 5
|
|
|
|
groupIDSize = 5
|
2021-11-23 10:56:56 +00:00
|
|
|
)
|
|
|
|
|
2021-11-18 11:31:58 +00:00
|
|
|
// _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) {
|
2021-11-24 10:03:02 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(id) != subnetIDSize {
|
2021-11-18 12:21:14 +00:00
|
|
|
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-24 17:26:33 +00:00
|
|
|
runtime.Notify("Put", 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-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(id) != subnetIDSize {
|
|
|
|
panic("get: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
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-23 15:36:45 +00:00
|
|
|
panic("get: " + ErrNotExist)
|
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) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(id) != subnetIDSize {
|
|
|
|
panic("delete: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
2021-11-18 12:24:32 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
key := append([]byte{ownerPrefix}, id...)
|
|
|
|
raw := storage.Get(ctx, key)
|
|
|
|
if raw == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("delete:" + ErrNotExist)
|
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
|
|
|
|
2021-11-24 18:09:13 +00:00
|
|
|
key[0] = nodeAdminPrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
|
|
|
key[0] = nodePrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
|
|
|
key[0] = clientAdminPrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
|
|
|
key[0] = userPrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
2021-11-24 17:26:33 +00:00
|
|
|
runtime.Notify("Delete", 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) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("addNodeAdmin: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
2021-11-21 19:55:39 +00:00
|
|
|
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-23 15:36:45 +00:00
|
|
|
panic("addNodeAdmin: " + ErrNotExist)
|
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
|
|
|
|
|
2021-11-22 16:55:07 +00:00
|
|
|
if keyInList(ctx, adminKey, stKey) {
|
|
|
|
panic("addNodeAdmin: node admin has already been added")
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
putKeyInList(ctx, adminKey, stKey)
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("removeNodeAdmin: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
if len(adminKey) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("removeNodeAdmin: " + ErrInvalidAdmin)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
2021-11-22 14:20:00 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
2021-11-22 14:20:00 +00:00
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("removeNodeAdmin: " + ErrNotExist)
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
if !runtime.CheckWitness(owner) {
|
|
|
|
panic("removeNodeAdmin: " + errCheckWitnessFailed)
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
stKey[0] = nodeAdminPrefix
|
2021-11-22 14:20:00 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
if !keyInList(ctx, adminKey, stKey) {
|
2021-11-22 14:20:00 +00:00
|
|
|
panic("removeNodeAdmin: " + ErrNodeAdmNotExist)
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
deleteKeyFromList(ctx, adminKey, stKey)
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 16:55:07 +00:00
|
|
|
// AddNode adds node to the specified subnetwork.
|
|
|
|
// Must be called by subnet's owner or node administrator
|
|
|
|
// only.
|
|
|
|
func AddNode(subnetID []byte, node interop.PublicKey) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("addNode: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:55:07 +00:00
|
|
|
if len(node) != interop.PublicKeyCompressedLen {
|
2021-11-22 18:59:49 +00:00
|
|
|
panic("addNode: " + ErrInvalidNode)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("addNode: " + ErrNotExist)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
stKey[0] = nodeAdminPrefix
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
owner := rawOwner.([]byte)
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
|
|
|
panic("addNode: " + ErrAccessDenied)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = nodePrefix
|
|
|
|
|
|
|
|
if keyInList(ctx, node, stKey) {
|
|
|
|
panic("addNode: node has already been added")
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
putKeyInList(ctx, node, stKey)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:35:07 +00:00
|
|
|
// RemoveNode removes node from the specified subnetwork.
|
|
|
|
// Must be called by subnet's owner or node administrator
|
|
|
|
// only.
|
|
|
|
func RemoveNode(subnetID []byte, node interop.PublicKey) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("removeNode: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
2021-11-22 18:35:07 +00:00
|
|
|
if len(node) != interop.PublicKeyCompressedLen {
|
2021-11-22 18:59:49 +00:00
|
|
|
panic("removeNode: " + ErrInvalidNode)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("removeNode: " + ErrNotExist)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
stKey[0] = nodeAdminPrefix
|
2021-11-22 18:35:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
owner := rawOwner.([]byte)
|
2021-11-22 18:35:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
|
|
|
panic("removeNode: " + ErrAccessDenied)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = nodePrefix
|
|
|
|
|
|
|
|
if !keyInList(ctx, node, stKey) {
|
|
|
|
panic("removeNode: " + ErrNodeNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, append(stKey, node...))
|
|
|
|
|
2021-11-24 17:26:33 +00:00
|
|
|
runtime.Notify("RemoveNode", subnetID, node)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:59:49 +00:00
|
|
|
// NodeAllowed checks if node is included in the
|
|
|
|
// specified subnet or not.
|
|
|
|
func NodeAllowed(subnetID []byte, node interop.PublicKey) bool {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("nodeAllowed: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
2021-11-22 18:59:49 +00:00
|
|
|
if len(node) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("nodeAllowed: " + ErrInvalidNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("nodeAllowed: " + ErrNotExist)
|
2021-11-22 18:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = nodePrefix
|
|
|
|
|
|
|
|
return storage.Get(ctx, append(stKey, node...)) != nil
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
// AddClientAdmin adds new client administrator of the specified group in the specified subnetwork.
|
|
|
|
// Must be called by owner only.
|
|
|
|
func AddClientAdmin(subnetID []byte, groupID []byte, adminPublicKey interop.PublicKey) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("addClientAdmin: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// V2 format check
|
|
|
|
if len(groupID) != groupIDSize {
|
|
|
|
panic("addClientAdmin: " + ErrInvalidGroupID)
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
if len(adminPublicKey) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("addClientAdmin: " + ErrInvalidAdmin)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("addClientAdmin: " + ErrNotExist)
|
2021-11-23 09:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
if !runtime.CheckWitness(owner) {
|
|
|
|
panic("addClientAdmin: " + errCheckWitnessFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = clientAdminPrefix
|
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
if keyInList(ctx, adminPublicKey, stKey) {
|
|
|
|
panic("addClientAdmin: client admin has already been added")
|
|
|
|
}
|
|
|
|
|
|
|
|
putKeyInList(ctx, adminPublicKey, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:20:07 +00:00
|
|
|
// RemoveClientAdmin removes client administrator from the
|
|
|
|
// specified group in the specified subnetwork.
|
|
|
|
// Must be called by owner only.
|
|
|
|
func RemoveClientAdmin(subnetID []byte, groupID []byte, adminPublicKey interop.PublicKey) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("removeClientAdmin: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// V2 format check
|
|
|
|
if len(groupID) != groupIDSize {
|
|
|
|
panic("removeClientAdmin: " + ErrInvalidGroupID)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:20:07 +00:00
|
|
|
if len(adminPublicKey) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("removeClientAdmin: " + ErrInvalidAdmin)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("removeClientAdmin: " + ErrNotExist)
|
2021-11-23 10:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
if !runtime.CheckWitness(owner) {
|
|
|
|
panic("removeClientAdmin: " + errCheckWitnessFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = clientAdminPrefix
|
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
if !keyInList(ctx, adminPublicKey, stKey) {
|
|
|
|
panic("removeClientAdmin: " + ErrClientAdmNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteKeyFromList(ctx, adminPublicKey, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:56:56 +00:00
|
|
|
// AddUser adds user to the specified subnetwork and group.
|
|
|
|
// Must be called by the owner or the group's admin only.
|
|
|
|
func AddUser(subnetID []byte, groupID []byte, userID []byte) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("addUser: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// V2 format check
|
2021-11-23 10:56:56 +00:00
|
|
|
if len(userID) != userIDSize {
|
|
|
|
panic("addUser: " + ErrInvalidUser)
|
|
|
|
}
|
|
|
|
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(groupID) != groupIDSize {
|
|
|
|
panic("addUser: " + ErrInvalidGroupID)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:56:56 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("addUser: " + ErrNotExist)
|
2021-11-23 10:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
stKey[0] = clientAdminPrefix
|
2021-11-23 10:56:56 +00:00
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
|
|
|
panic("addUser: " + ErrAccessDenied)
|
2021-11-23 10:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = userPrefix
|
|
|
|
|
|
|
|
if keyInList(ctx, userID, stKey) {
|
|
|
|
panic("addUser: user has already been added")
|
|
|
|
}
|
|
|
|
|
|
|
|
putKeyInList(ctx, userID, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
// RemoveUser removes user from the specified subnetwork and group.
|
|
|
|
// Must be called by the owner or the group's admin only.
|
|
|
|
func RemoveUser(subnetID []byte, groupID []byte, userID []byte) {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("removeUser: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// V2 format check
|
|
|
|
if len(groupID) != groupIDSize {
|
|
|
|
panic("removeUser: " + ErrInvalidGroupID)
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(userID) != userIDSize {
|
|
|
|
panic("addUser: " + ErrInvalidUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("removeUser: " + ErrNotExist)
|
2021-11-23 11:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = clientAdminPrefix
|
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
|
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
|
|
|
panic("removeUser: " + ErrAccessDenied)
|
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = userPrefix
|
|
|
|
|
|
|
|
if !keyInList(ctx, userID, stKey) {
|
|
|
|
panic("removeUser: " + ErrUserNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteKeyFromList(ctx, userID, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:19:58 +00:00
|
|
|
// UserAllowed returns bool that indicates if node is included in the
|
|
|
|
// specified subnet or not.
|
|
|
|
func UserAllowed(subnetID []byte, user []byte) bool {
|
2021-11-24 10:33:24 +00:00
|
|
|
// V2 format check
|
|
|
|
if len(subnetID) != subnetIDSize {
|
|
|
|
panic("userAllowed: " + ErrInvalidSubnetID)
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:19:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
if storage.Get(ctx, stKey) == nil {
|
2021-11-23 15:36:45 +00:00
|
|
|
panic("userAllowed: " + ErrNotExist)
|
2021-11-23 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = userPrefix
|
|
|
|
prefixLen := len(stKey) + groupIDSize
|
|
|
|
|
|
|
|
iter := storage.Find(ctx, stKey, storage.KeysOnly)
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
key := iterator.Value(iter).([]byte)
|
|
|
|
if common.BytesEqual(user, key[prefixLen:]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-18 11:31:58 +00:00
|
|
|
// Version returns version of the contract.
|
|
|
|
func Version() int {
|
|
|
|
return common.Version
|
|
|
|
}
|
2021-11-22 16:55:07 +00:00
|
|
|
|
|
|
|
func keyInList(ctx storage.Context, searchedKey interop.PublicKey, prefix []byte) bool {
|
2021-11-23 09:54:32 +00:00
|
|
|
return storage.Get(ctx, append(prefix, searchedKey...)) != nil
|
|
|
|
}
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
func putKeyInList(ctx storage.Context, keyToPut interop.PublicKey, prefix []byte) {
|
|
|
|
storage.Put(ctx, append(prefix, keyToPut...), []byte{1})
|
|
|
|
}
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
func deleteKeyFromList(ctx storage.Context, keyToDelete interop.PublicKey, prefix []byte) {
|
|
|
|
storage.Delete(ctx, append(prefix, keyToDelete...))
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
2021-11-23 11:15:16 +00:00
|
|
|
|
2021-11-24 18:09:13 +00:00
|
|
|
func deleteByPrefix(ctx storage.Context, prefix []byte) {
|
|
|
|
iter := storage.Find(ctx, prefix, storage.KeysOnly)
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
k := iterator.Value(iter).([]byte)
|
|
|
|
storage.Delete(ctx, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
func calledByOwnerOrAdmin(ctx storage.Context, owner []byte, adminPrefix []byte) bool {
|
|
|
|
if runtime.CheckWitness(owner) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixLen := len(adminPrefix)
|
|
|
|
|
|
|
|
iter := storage.Find(ctx, adminPrefix, storage.KeysOnly)
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
key := iterator.Value(iter).([]byte)
|
|
|
|
if runtime.CheckWitness(key[prefixLen:]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|