From 4154899cff47cf3a3fcb3a441d85806c894b74dc Mon Sep 17 00:00:00 2001 From: aarifullin Date: Mon, 26 Feb 2024 18:30:31 +0300 Subject: [PATCH] [#54] morph: Introduce ContractStorageReader * Implement MorphRuleChainStorageReader interface to make possible to read from Policy contract without wallets. Signed-off-by: Airat Arifullin --- pkg/engine/chain_router.go | 6 +-- pkg/morph/policy/policy_contract_storage.go | 46 ++++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/pkg/engine/chain_router.go b/pkg/engine/chain_router.go index 93775b8..830919f 100644 --- a/pkg/engine/chain_router.go +++ b/pkg/engine/chain_router.go @@ -6,18 +6,18 @@ import ( ) type defaultChainRouter struct { - morph MorphRuleChainStorage + morph MorphRuleChainStorageReader local LocalOverrideStorage } -func NewDefaultChainRouter(morph MorphRuleChainStorage) ChainRouter { +func NewDefaultChainRouter(morph MorphRuleChainStorageReader) ChainRouter { return &defaultChainRouter{ morph: morph, } } -func NewDefaultChainRouterWithLocalOverrides(morph MorphRuleChainStorage, local LocalOverrideStorage) ChainRouter { +func NewDefaultChainRouterWithLocalOverrides(morph MorphRuleChainStorageReader, local LocalOverrideStorage) ChainRouter { return &defaultChainRouter{ morph: morph, local: local, diff --git a/pkg/morph/policy/policy_contract_storage.go b/pkg/morph/policy/policy_contract_storage.go index 2eaa099..8dbda8b 100644 --- a/pkg/morph/policy/policy_contract_storage.go +++ b/pkg/morph/policy/policy_contract_storage.go @@ -22,13 +22,20 @@ var ( ErrEngineTargetTypeUnsupported = errors.New("this target type is not supported yet") ) -// ContractStorage is the interface to manage chain rules within the policy contract. +// ContractStorage is the interface to manage chain rules within Policy contract. type ContractStorage struct { contractInterface *client.Contract } var _ engine.MorphRuleChainStorage = (*ContractStorage)(nil) +// ContractStorageReader is the interface to read data from Policy contract. +type ContractStorageReader struct { + contractReaderInterface *client.ContractReader +} + +var _ engine.MorphRuleChainStorageReader = (*ContractStorageReader)(nil) + func NewContractStorage(actor client.Actor, contract util.Uint160) *ContractStorage { return &ContractStorage{ contractInterface: client.New(actor, contract), @@ -112,6 +119,43 @@ func (s *ContractStorage) SetAdmin(addr util.Uint160) (util.Uint256, uint32, err return s.contractInterface.SetAdmin(addr) } +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() +} + func bytesFromStackItem(param stackitem.Item) ([]byte, error) { switch param.Type() { case stackitem.BufferT, stackitem.ByteArrayT, stackitem.IntegerT: