[#33] Add IterateContainers method

Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
This commit is contained in:
Vladimir Domnich 2023-08-02 11:29:50 +04:00
parent 36f3d39c40
commit 12d203ff93
3 changed files with 35 additions and 1 deletions

View file

@ -1,5 +1,5 @@
name: "Container"
safemethods: ["count", "containersOf", "get", "owner", "list", "eACL", "getContainerSize", "listContainerSizes", "iterateContainerSizes", "version"]
safemethods: ["count", "containersOf", "get", "owner", "list", "iterateContainers", "eACL", "getContainerSize", "listContainerSizes", "iterateContainerSizes", "version"]
permissions:
- methods: ["update", "addKey", "transferX",
"register", "addRecord", "deleteRecords"]

View file

@ -404,6 +404,16 @@ func List(owner []byte) [][]byte {
return list
}
// IterateContainers iterates over all containers. Iterator values are structures that contain a
// stable marshaled Container structure, the signature, the public key of the container creator and
// a stable marshaled SessionToken structure if it was provided.
func IterateContainers() iterator.Iterator {
ctx := storage.GetReadOnlyContext()
key := []byte{containerKeyPrefix}
return storage.Find(ctx, key, storage.ValuesOnly|storage.DeserializeValues)
}
// SetEACL method sets a new extended ACL table related to the contract
// if it was invoked by Alphabet nodes of the Inner Ring.
//

View file

@ -142,6 +142,7 @@ func checkContainerList(t *testing.T, c *neotest.ContractInvoker, expected [][]b
}
require.ElementsMatch(t, expected, actual)
})
t.Run("check with `containersOf`", func(t *testing.T) {
s, err := c.TestInvoke(t, "containersOf", nil)
require.NoError(t, err)
@ -159,6 +160,29 @@ func checkContainerList(t *testing.T, c *neotest.ContractInvoker, expected [][]b
require.ElementsMatch(t, expected, actual)
})
t.Run("check with `iterateContainers`", func(t *testing.T) {
s, err := c.TestInvoke(t, "iterateContainers")
require.NoError(t, err)
require.Equal(t, 1, s.Len())
iter, ok := s.Top().Value().(*storage.Iterator)
require.True(t, ok)
// Iterator contains pairs: key + estimation (as stack item), we extract estimations only
actualIds := make([][]byte, 0, len(expected))
for iter.Next() {
// Parse iterator value into container fields and extract container value which is 1st field
containerFields, ok := iter.Value().Value().([]stackitem.Item)
require.True(t, ok)
require.Len(t, containerFields, 4)
containerValue, err := containerFields[0].TryBytes()
require.NoError(t, err)
containerId := sha256.Sum256(containerValue)
actualIds = append(actualIds, containerId[:])
}
require.ElementsMatch(t, expected, actualIds)
})
}
func TestContainerPut(t *testing.T) {