Skip to content

Commit

Permalink
container: add alias method, fix #343
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Khimov <roman@nspcc.ru>
  • Loading branch information
roman-khimov committed Sep 25, 2023
1 parent d2f3761 commit e602091
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion container/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "NeoFS Container"
safemethods: ["count", "containersOf", "get", "owner", "list", "eACL", "getContainerSize", "listContainerSizes", "iterateContainerSizes", "iterateAllContainerSizes", "version"]
safemethods: ["alias", "count", "containersOf", "get", "owner", "list", "eACL", "getContainerSize", "listContainerSizes", "iterateContainerSizes", "iterateAllContainerSizes", "version"]
permissions:
- methods: ["update", "addKey", "transferX",
"register", "registerTLD", "addRecord", "deleteRecords"]
Expand Down
13 changes: 13 additions & 0 deletions container/container_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,19 @@ func Owner(containerID []byte) []byte {
return owner
}

// Alias method returns a string with an alias of the container if it's set
// (Null otherwise).
//
// If the container doesn't exist, it panics with NotFoundError.
func Alias(cid []byte) string {
ctx := storage.GetReadOnlyContext()
owner := getOwnerByID(ctx, cid)
if owner == nil {
panic(NotFoundError)
}
return storage.Get(ctx, append([]byte(nnsHasAliasKey), cid...)).(string)
}

// Count method returns the number of registered containers.
func Count() int {
count := 0
Expand Down
5 changes: 5 additions & 0 deletions rpc/container/rpcbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ func New(actor Actor, hash util.Uint160) *Contract {
return &Contract{ContractReader{actor, hash}, actor, hash}
}

// Alias invokes `alias` method of contract.
func (c *ContractReader) Alias(cid []byte) (string, error) {
return unwrap.UTF8String(c.invoker.Call(c.hash, "alias", cid))
}

// ContainersOf invokes `containersOf` method of contract.
func (c *ContractReader) ContainersOf(owner []byte) (uuid.UUID, result.Iterator, error) {
return unwrap.SessionIterator(c.invoker.Call(c.hash, "containersOf", owner))
Expand Down
2 changes: 2 additions & 0 deletions tests/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func TestContainerPut(t *testing.T) {
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "put", putArgs...)

c.Invoke(t, stackitem.Null{}, "put", putArgs...)
c.Invoke(t, stackitem.Null{}, "alias", cnt.id[:])

t.Run("with nice names", func(t *testing.T) {
ctrNNS := neotest.CompileFile(t, c.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
Expand All @@ -196,6 +197,7 @@ func TestContainerPut(t *testing.T) {
})
cNNS := c.CommitteeInvoker(nnsHash)
cNNS.Invoke(t, expected, "resolve", "mycnt.neofs", int64(nns.TXT))
c.Invoke(t, stackitem.NewByteArray([]byte("mycnt.neofs")), "alias", cnt.id[:])

t.Run("name is already taken", func(t *testing.T) {
c.InvokeFail(t, "name is already taken", "putNamed", putArgs...)
Expand Down

0 comments on commit e602091

Please sign in to comment.