2023-11-28 16:40:30 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
|
|
|
|
client "git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/policy"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
2024-02-22 09:09:22 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2023-11-28 16:40:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrEmptyChainID = errors.New("chain id is not set")
|
|
|
|
|
|
|
|
ErrEngineTargetTypeUnsupported = errors.New("this target type is not supported yet")
|
|
|
|
)
|
|
|
|
|
2024-02-26 15:30:31 +00:00
|
|
|
// ContractStorage is the interface to manage chain rules within Policy contract.
|
2023-11-28 16:40:30 +00:00
|
|
|
type ContractStorage struct {
|
|
|
|
contractInterface *client.Contract
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ engine.MorphRuleChainStorage = (*ContractStorage)(nil)
|
|
|
|
|
2024-02-26 15:30:31 +00:00
|
|
|
// ContractStorageReader is the interface to read data from Policy contract.
|
|
|
|
type ContractStorageReader struct {
|
|
|
|
contractReaderInterface *client.ContractReader
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ engine.MorphRuleChainStorageReader = (*ContractStorageReader)(nil)
|
|
|
|
|
2023-11-28 16:40:30 +00:00
|
|
|
func NewContractStorage(actor client.Actor, contract util.Uint160) *ContractStorage {
|
|
|
|
return &ContractStorage{
|
|
|
|
contractInterface: client.New(actor, contract),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContractStorageWithSimpleActor(rpcActor actor.RPCActor, acc *wallet.Account, contract util.Uint160) (*ContractStorage, error) {
|
|
|
|
act, err := actor.NewSimple(rpcActor, acc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create simple actor: %w", err)
|
|
|
|
}
|
|
|
|
return NewContractStorage(act, contract), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ContractStorage) AddMorphRuleChain(name chain.Name, target engine.Target, c *chain.Chain) (txHash util.Uint256, vub uint32, err error) {
|
2024-01-18 08:01:39 +00:00
|
|
|
if len(c.ID) == 0 {
|
2023-11-28 16:40:30 +00:00
|
|
|
err = ErrEmptyChainID
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var kind policy.Kind
|
|
|
|
kind, err = policyKind(target.Type)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fullName := prefixedChainName(name, c.ID)
|
|
|
|
|
2023-12-12 15:45:59 +00:00
|
|
|
txHash, vub, err = s.contractInterface.AddChain(big.NewInt(int64(kind)), target.Name, fullName, c.Bytes())
|
2023-11-28 16:40:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ContractStorage) RemoveMorphRuleChain(name chain.Name, target engine.Target, chainID chain.ID) (txHash util.Uint256, vub uint32, err error) {
|
2024-01-18 08:01:39 +00:00
|
|
|
if len(chainID) == 0 {
|
2023-11-28 16:40:30 +00:00
|
|
|
err = ErrEmptyChainID
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var kind policy.Kind
|
|
|
|
kind, err = policyKind(target.Type)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fullName := prefixedChainName(name, chainID)
|
|
|
|
|
2023-12-12 15:45:59 +00:00
|
|
|
txHash, vub, err = s.contractInterface.RemoveChain(big.NewInt(int64(kind)), target.Name, fullName)
|
2023-11-28 16:40:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-07 10:55:41 +00:00
|
|
|
func (s *ContractStorage) RemoveMorphRuleChainsByTarget(name chain.Name, target engine.Target) (txHash util.Uint256, vub uint32, err error) {
|
|
|
|
var kind policy.Kind
|
|
|
|
kind, err = policyKind(target.Type)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fullName := prefixedChainName(name, nil)
|
|
|
|
|
|
|
|
txHash, vub, err = s.contractInterface.RemoveChainsByPrefix(big.NewInt(int64(kind)), target.Name, fullName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:40:30 +00:00
|
|
|
func (s *ContractStorage) ListMorphRuleChains(name chain.Name, target engine.Target) ([]*chain.Chain, error) {
|
|
|
|
kind, err := policyKind(target.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-12 15:45:59 +00:00
|
|
|
items, err := s.contractInterface.ListChainsByPrefix(big.NewInt(int64(kind)), target.Name, []byte(name))
|
2023-11-28 16:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chains []*chain.Chain
|
|
|
|
for _, item := range items {
|
|
|
|
serialized, err := bytesFromStackItem(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c := new(chain.Chain)
|
|
|
|
if err := c.DecodeBytes(serialized); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chains = append(chains, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chains, nil
|
|
|
|
}
|
|
|
|
|
2024-02-22 09:09:22 +00:00
|
|
|
func (s *ContractStorage) ListTargetsIterator(targetType engine.TargetType) (uuid.UUID, result.Iterator, error) {
|
|
|
|
kind, err := policyKind(targetType)
|
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
return s.contractInterface.ListTargets(big.NewInt(int64(kind)))
|
|
|
|
}
|
|
|
|
|
2023-12-19 13:18:48 +00:00
|
|
|
func (s *ContractStorage) GetAdmin() (util.Uint160, error) {
|
|
|
|
return s.contractInterface.GetAdmin()
|
|
|
|
}
|
|
|
|
|
2023-12-21 04:54:42 +00:00
|
|
|
func (s *ContractStorage) SetAdmin(addr util.Uint160) (util.Uint256, uint32, error) {
|
|
|
|
return s.contractInterface.SetAdmin(addr)
|
2023-12-19 13:18:48 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 15:30:31 +00:00
|
|
|
func NewContractStorageReader(inv client.Invoker, contract util.Uint160) *ContractStorageReader {
|
|
|
|
return &ContractStorageReader{
|
|
|
|
contractReaderInterface: client.NewReader(inv, contract),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ContractStorageReader) ListMorphRuleChains(name chain.Name, target engine.Target) ([]*chain.Chain, error) {
|
|
|
|
kind, err := policyKind(target.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := s.contractReaderInterface.ListChainsByPrefix(big.NewInt(int64(kind)), target.Name, []byte(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chains []*chain.Chain
|
|
|
|
for _, item := range items {
|
|
|
|
serialized, err := bytesFromStackItem(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c := new(chain.Chain)
|
|
|
|
if err := c.DecodeBytes(serialized); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chains = append(chains, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chains, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ContractStorageReader) GetAdmin() (util.Uint160, error) {
|
|
|
|
return s.contractReaderInterface.GetAdmin()
|
|
|
|
}
|
|
|
|
|
2024-02-22 09:09:22 +00:00
|
|
|
func (s *ContractStorageReader) ListTargetsIterator(targetType engine.TargetType) (uuid.UUID, result.Iterator, error) {
|
|
|
|
kind, err := policyKind(targetType)
|
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
return s.contractReaderInterface.ListTargets(big.NewInt(int64(kind)))
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:40:30 +00:00
|
|
|
func bytesFromStackItem(param stackitem.Item) ([]byte, error) {
|
|
|
|
switch param.Type() {
|
|
|
|
case stackitem.BufferT, stackitem.ByteArrayT, stackitem.IntegerT:
|
|
|
|
return param.TryBytes()
|
|
|
|
case stackitem.AnyT:
|
|
|
|
if param.Value() == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("chain/client: %s is not a byte array type", param.Type())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prefixedChainName(name chain.Name, chainID chain.ID) []byte {
|
|
|
|
return []byte(strings.ToLower(fmt.Sprintf("%s:%s", name, chainID)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func policyKind(typ engine.TargetType) (policy.Kind, error) {
|
2024-04-09 15:10:56 +00:00
|
|
|
switch typ {
|
|
|
|
case engine.Namespace:
|
2023-11-28 16:40:30 +00:00
|
|
|
return policy.Namespace, nil
|
2024-04-09 15:10:56 +00:00
|
|
|
case engine.Container:
|
2023-11-28 16:40:30 +00:00
|
|
|
return policy.Container, nil
|
2024-04-09 15:10:56 +00:00
|
|
|
case engine.User:
|
|
|
|
return policy.Kind(engine.User), nil
|
|
|
|
case engine.Group:
|
|
|
|
return policy.Kind(engine.Group), nil
|
|
|
|
default:
|
|
|
|
return policy.Kind(0), ErrEngineTargetTypeUnsupported
|
2023-11-28 16:40:30 +00:00
|
|
|
}
|
|
|
|
}
|