2023-10-19 16:33:02 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
2023-11-20 13:10:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-10-19 16:33:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2023-11-20 13:10:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
2023-10-19 16:33:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Kind represents the object the chain is attached to.
|
|
|
|
// Currently only namespace and container are supported.
|
|
|
|
type Kind byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
Namespace = 'n'
|
|
|
|
Container = 'c'
|
|
|
|
IAM = 'i'
|
|
|
|
)
|
|
|
|
|
2023-11-20 13:10:52 +00:00
|
|
|
const (
|
|
|
|
ownerKeyPrefix = 'o'
|
|
|
|
)
|
|
|
|
|
2024-01-18 07:46:19 +00:00
|
|
|
const (
|
|
|
|
mappingKeyPrefix = 'm'
|
2024-02-20 08:58:33 +00:00
|
|
|
counterKey = "Counter"
|
2024-01-18 07:46:19 +00:00
|
|
|
)
|
|
|
|
|
2023-11-20 13:10:52 +00:00
|
|
|
const (
|
2023-11-28 10:48:44 +00:00
|
|
|
// ErrNotAuthorized is returned when the none of the transaction signers
|
2023-11-20 13:10:52 +00:00
|
|
|
// belongs to the list of autorized keys.
|
2023-11-28 10:48:44 +00:00
|
|
|
ErrNotAuthorized = "none of the signers is authorized to change the contract"
|
2023-11-20 13:10:52 +00:00
|
|
|
)
|
|
|
|
|
2023-10-19 16:33:02 +00:00
|
|
|
// _deploy function sets up initial list of inner ring public keys.
|
2023-11-07 12:18:48 +00:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2023-11-20 13:10:52 +00:00
|
|
|
if isUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args := data.(struct {
|
|
|
|
Admin interop.Hash160
|
|
|
|
})
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
if args.Admin != nil {
|
|
|
|
if len(args.Admin) != 20 {
|
|
|
|
panic("invaliad admin hash length")
|
|
|
|
}
|
2023-11-28 08:50:33 +00:00
|
|
|
storage.Put(ctx, []byte{ownerKeyPrefix}, args.Admin)
|
2023-11-20 13:10:52 +00:00
|
|
|
}
|
2024-01-18 07:46:19 +00:00
|
|
|
storage.Put(ctx, counterKey, 0)
|
2023-11-20 13:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkAuthorization(ctx storage.Context) {
|
|
|
|
admin := getAdmin(ctx)
|
|
|
|
if admin != nil && runtime.CheckWitness(admin) {
|
|
|
|
return
|
|
|
|
}
|
2023-11-28 08:51:24 +00:00
|
|
|
if runtime.CheckWitness(common.AlphabetAddress()) {
|
|
|
|
return
|
|
|
|
}
|
2023-11-20 13:10:52 +00:00
|
|
|
|
2023-11-28 10:48:44 +00:00
|
|
|
panic(ErrNotAuthorized)
|
2023-11-20 13:10:52 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 12:36:55 +00:00
|
|
|
// Version returns the version of the contract.
|
|
|
|
func Version() int {
|
|
|
|
return common.Version
|
|
|
|
}
|
|
|
|
|
2023-11-20 13:10:52 +00:00
|
|
|
func SetAdmin(addr interop.Hash160) {
|
|
|
|
common.CheckAlphabetWitness()
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, []byte{ownerKeyPrefix}, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAdmin() interop.Hash160 {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getAdmin(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAdmin(ctx storage.Context) interop.Hash160 {
|
|
|
|
return storage.Get(ctx, []byte{ownerKeyPrefix}).(interop.Hash160)
|
2023-10-19 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 07:46:19 +00:00
|
|
|
func storageKey(prefix Kind, counter int, name []byte) []byte {
|
|
|
|
key := append([]byte{byte(prefix)}, common.ToFixedWidth64(counter)...)
|
2023-10-19 16:33:02 +00:00
|
|
|
return append(key, name...)
|
|
|
|
}
|
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
func mapKey(kind Kind, name []byte) []byte {
|
|
|
|
return append([]byte{mappingKeyPrefix, byte(kind)}, name...)
|
|
|
|
}
|
|
|
|
|
2024-01-18 07:46:19 +00:00
|
|
|
// mapToNumeric maps a name to a number. That allows to keep more space in
|
|
|
|
// a storage key shortening long names. Short entity
|
|
|
|
// names are also mapped to prevent collisions in the map.
|
2024-02-09 14:28:42 +00:00
|
|
|
func mapToNumeric(ctx storage.Context, kind Kind, name []byte) (mapped int, mappingExists bool) {
|
|
|
|
mKey := mapKey(kind, name)
|
2024-01-18 07:46:19 +00:00
|
|
|
numericID := storage.Get(ctx, mKey)
|
|
|
|
if numericID == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mapped = numericID.(int)
|
|
|
|
mappingExists = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapToNumericCreateIfNotExists maps a name to a number. That allows to keep
|
|
|
|
// more space in a storage key shortening long names. Short entity
|
|
|
|
// names are also mapped to prevent collisions in the map.
|
|
|
|
// If a mapping cannot be found, then the method creates and returns it.
|
|
|
|
// mapToNumericCreateIfNotExists is NOT applicable for a read-only context.
|
2024-02-09 14:28:42 +00:00
|
|
|
func mapToNumericCreateIfNotExists(ctx storage.Context, kind Kind, name []byte) int {
|
|
|
|
mKey := mapKey(kind, name)
|
2024-01-18 07:46:19 +00:00
|
|
|
numericID := storage.Get(ctx, mKey)
|
|
|
|
if numericID == nil {
|
|
|
|
counter := storage.Get(ctx, counterKey).(int)
|
|
|
|
counter++
|
|
|
|
storage.Put(ctx, counterKey, counter)
|
|
|
|
storage.Put(ctx, mKey, counter)
|
|
|
|
return counter
|
|
|
|
}
|
|
|
|
return numericID.(int)
|
|
|
|
}
|
|
|
|
|
2023-11-28 09:30:04 +00:00
|
|
|
func AddChain(entity Kind, entityName string, name []byte, chain []byte) {
|
2023-10-19 16:33:02 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-11-20 13:10:52 +00:00
|
|
|
checkAuthorization(ctx)
|
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
entityNameBytes := mapToNumericCreateIfNotExists(ctx, entity, []byte(entityName))
|
2024-01-18 07:46:19 +00:00
|
|
|
key := storageKey(entity, entityNameBytes, name)
|
2023-10-19 16:33:02 +00:00
|
|
|
storage.Put(ctx, key, chain)
|
|
|
|
}
|
|
|
|
|
2023-11-28 09:30:04 +00:00
|
|
|
func GetChain(entity Kind, entityName string, name []byte) []byte {
|
2023-11-17 08:06:08 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2024-01-18 07:46:19 +00:00
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
entityNameBytes, exists := mapToNumeric(ctx, entity, []byte(entityName))
|
2024-01-18 07:46:19 +00:00
|
|
|
if !exists {
|
|
|
|
panic("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := storageKey(entity, entityNameBytes, name)
|
2023-11-17 08:06:08 +00:00
|
|
|
data := storage.Get(ctx, key).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2023-11-28 09:30:04 +00:00
|
|
|
func RemoveChain(entity Kind, entityName string, name []byte) {
|
2023-10-19 16:33:02 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-11-20 13:10:52 +00:00
|
|
|
checkAuthorization(ctx)
|
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
entityNameNum, exists := mapToNumeric(ctx, entity, []byte(entityName))
|
2024-01-18 07:46:19 +00:00
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
key := storageKey(entity, entityNameNum, name)
|
2023-10-19 16:33:02 +00:00
|
|
|
storage.Delete(ctx, key)
|
2024-02-09 14:28:42 +00:00
|
|
|
|
|
|
|
// If no chains are left for the target, then remove the mapping.
|
|
|
|
prefix := append([]byte{byte(entity)}, common.ToFixedWidth64(entityNameNum)...)
|
|
|
|
it := storage.Find(ctx, prefix, storage.KeysOnly)
|
|
|
|
if !iterator.Next(it) {
|
|
|
|
storage.Delete(ctx, mapKey(entity, []byte(entityName)))
|
|
|
|
}
|
2023-10-19 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 09:30:04 +00:00
|
|
|
func RemoveChainsByPrefix(entity Kind, entityName string, name []byte) {
|
2023-10-19 16:33:02 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-11-20 13:10:52 +00:00
|
|
|
checkAuthorization(ctx)
|
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
entityNameNum, exists := mapToNumeric(ctx, entity, []byte(entityName))
|
2024-01-18 07:46:19 +00:00
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
key := storageKey(entity, entityNameNum, name)
|
2023-10-19 16:33:02 +00:00
|
|
|
it := storage.Find(ctx, key, storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
storage.Delete(ctx, iterator.Value(it).([]byte))
|
|
|
|
}
|
2024-02-09 14:28:42 +00:00
|
|
|
|
|
|
|
// If no chains are left for the target, then remove the mapping.
|
|
|
|
prefix := append([]byte{byte(entity)}, common.ToFixedWidth64(entityNameNum)...)
|
|
|
|
it = storage.Find(ctx, prefix, storage.KeysOnly)
|
|
|
|
if !iterator.Next(it) {
|
|
|
|
storage.Delete(ctx, mapKey(entity, []byte(entityName)))
|
|
|
|
}
|
2023-10-19 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListChains lists all chains for the namespace by prefix.
|
|
|
|
// container may be empty.
|
2023-11-28 09:30:04 +00:00
|
|
|
func ListChains(namespace, container string, name []byte) [][]byte {
|
2023-11-17 12:02:13 +00:00
|
|
|
result := ListChainsByPrefix(Namespace, namespace, name)
|
2023-10-19 16:33:02 +00:00
|
|
|
|
|
|
|
if container != "" {
|
2023-11-17 12:02:13 +00:00
|
|
|
result = append(result, ListChainsByPrefix(Container, container, name)...)
|
2023-10-19 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2023-11-17 08:06:08 +00:00
|
|
|
|
|
|
|
// ListChainsByPrefix list all chains for the provided kind and entity by prefix.
|
2023-11-28 09:30:04 +00:00
|
|
|
func ListChainsByPrefix(entity Kind, entityName string, prefix []byte) [][]byte {
|
2023-11-17 08:06:08 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
result := [][]byte{}
|
|
|
|
|
2024-02-09 14:28:42 +00:00
|
|
|
entityNameBytes, exists := mapToNumeric(ctx, entity, []byte(entityName))
|
2024-01-18 07:46:19 +00:00
|
|
|
if !exists {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPrefix := storageKey(entity, entityNameBytes, prefix)
|
2023-11-17 08:06:08 +00:00
|
|
|
it := storage.Find(ctx, keyPrefix, storage.ValuesOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
result = append(result, iterator.Value(it).([]byte))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2024-01-26 14:39:33 +00:00
|
|
|
|
|
|
|
func IteratorChainsByPrefix(entity Kind, entityName string, prefix []byte) iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
2024-02-09 14:28:42 +00:00
|
|
|
id, _ := mapToNumeric(ctx, entity, []byte(entityName))
|
2024-01-30 13:34:40 +00:00
|
|
|
keyPrefix := storageKey(entity, id, prefix)
|
2024-01-26 14:39:33 +00:00
|
|
|
return storage.Find(ctx, keyPrefix, storage.ValuesOnly)
|
|
|
|
}
|
2024-02-09 14:28:42 +00:00
|
|
|
|
|
|
|
// ListTargets iterates over targets for which rules are defined.
|
|
|
|
func ListTargets(entity Kind) iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
mKey := mapKey(entity, []byte{})
|
|
|
|
return storage.Find(ctx, mKey, storage.KeysOnly|storage.RemovePrefix)
|
|
|
|
}
|