2023-11-07 18:29:51 +00:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-29 13:36:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2024-02-22 09:09:22 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2023-11-07 18:29:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type inmemoryMorphRuleChainStorage struct {
|
2024-03-07 10:55:41 +00:00
|
|
|
storage engine.LocalOverrideStorage
|
2023-11-07 18:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewInmemoryMorphRuleChainStorage() engine.MorphRuleChainStorage {
|
|
|
|
return &inmemoryMorphRuleChainStorage{
|
2024-03-07 10:55:41 +00:00
|
|
|
storage: NewInmemoryLocalStorage(),
|
2023-11-07 18:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 13:36:54 +00:00
|
|
|
func (s *inmemoryMorphRuleChainStorage) AddMorphRuleChain(name chain.Name, target engine.Target, c *chain.Chain) (_ util.Uint256, _ uint32, err error) {
|
2023-11-07 18:29:51 +00:00
|
|
|
switch target.Type {
|
2024-03-07 10:55:41 +00:00
|
|
|
case engine.Namespace, engine.Container:
|
|
|
|
_, err = s.storage.AddOverride(name, target, c)
|
2023-11-07 18:29:51 +00:00
|
|
|
default:
|
|
|
|
err = engine.ErrUnknownTarget
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-29 13:36:54 +00:00
|
|
|
func (s *inmemoryMorphRuleChainStorage) RemoveMorphRuleChain(name chain.Name, target engine.Target, chainID chain.ID) (_ util.Uint256, _ uint32, err error) {
|
2023-11-07 18:29:51 +00:00
|
|
|
switch target.Type {
|
2024-03-07 10:55:41 +00:00
|
|
|
case engine.Namespace, engine.Container:
|
|
|
|
err = s.storage.RemoveOverride(name, target, chainID)
|
|
|
|
default:
|
|
|
|
err = engine.ErrUnknownTarget
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inmemoryMorphRuleChainStorage) RemoveMorphRuleChainsByTarget(name chain.Name, target engine.Target) (_ util.Uint256, _ uint32, err error) {
|
|
|
|
switch target.Type {
|
|
|
|
case engine.Namespace, engine.Container:
|
|
|
|
err = s.storage.RemoveOverridesByTarget(name, target)
|
2023-11-07 18:29:51 +00:00
|
|
|
default:
|
2023-11-29 13:36:54 +00:00
|
|
|
err = engine.ErrUnknownTarget
|
2023-11-07 18:29:51 +00:00
|
|
|
}
|
2023-11-29 13:36:54 +00:00
|
|
|
return
|
2023-11-07 18:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inmemoryMorphRuleChainStorage) ListMorphRuleChains(name chain.Name, target engine.Target) ([]*chain.Chain, error) {
|
|
|
|
switch target.Type {
|
2024-03-07 10:55:41 +00:00
|
|
|
case engine.Namespace, engine.Container:
|
|
|
|
return s.storage.ListOverrides(name, target)
|
2023-11-07 18:29:51 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil, engine.ErrUnknownTarget
|
|
|
|
}
|
2024-02-26 15:19:37 +00:00
|
|
|
|
|
|
|
func (s *inmemoryMorphRuleChainStorage) GetAdmin() (util.Uint160, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inmemoryMorphRuleChainStorage) SetAdmin(_ util.Uint160) (util.Uint256, uint32, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2024-02-22 09:09:22 +00:00
|
|
|
|
|
|
|
func (s *inmemoryMorphRuleChainStorage) ListTargetsIterator(targetType engine.TargetType) (uuid uuid.UUID, it result.Iterator, err error) {
|
|
|
|
it.Values = make([]stackitem.Item, 0)
|
|
|
|
|
|
|
|
switch targetType {
|
|
|
|
case engine.Namespace:
|
|
|
|
// Listing targets may look bizarre, because inmemory rule chain storage use inmemory local overrides where
|
|
|
|
// targets are listed by chain names.
|
|
|
|
var targets []engine.Target
|
2024-03-07 10:55:41 +00:00
|
|
|
targets, err = s.storage.ListOverrideDefinedTargets(chain.Ingress)
|
2024-02-22 09:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, t := range targets {
|
|
|
|
it.Values = append(it.Values, stackitem.NewByteArray([]byte(t.Name)))
|
|
|
|
}
|
|
|
|
|
2024-03-07 10:55:41 +00:00
|
|
|
targets, err = s.storage.ListOverrideDefinedTargets(chain.S3)
|
2024-02-22 09:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, t := range targets {
|
|
|
|
it.Values = append(it.Values, stackitem.NewByteArray([]byte(t.Name)))
|
|
|
|
}
|
|
|
|
case engine.Container:
|
|
|
|
var targets []engine.Target
|
2024-03-07 10:55:41 +00:00
|
|
|
targets, err = s.storage.ListOverrideDefinedTargets(chain.Ingress)
|
2024-02-22 09:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, t := range targets {
|
|
|
|
it.Values = append(it.Values, stackitem.NewByteArray([]byte(t.Name)))
|
|
|
|
}
|
|
|
|
|
2024-03-07 10:55:41 +00:00
|
|
|
targets, err = s.storage.ListOverrideDefinedTargets(chain.S3)
|
2024-02-22 09:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, t := range targets {
|
|
|
|
it.Values = append(it.Values, stackitem.NewByteArray([]byte(t.Name)))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|