From a2c2791146d65887c14fb38a531c01a318fe84cc Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 Nov 2024 15:51:45 +0300 Subject: [PATCH] [#129] frostfsid: Return subject by an additional address Signed-off-by: Evgenii Stratonikov --- frostfsid/frostfsid_contract.go | 31 ++++++++++++++++++++----------- tests/frostfsid_test.go | 7 +++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/frostfsid/frostfsid_contract.go b/frostfsid/frostfsid_contract.go index fd2d446..15099ab 100644 --- a/frostfsid/frostfsid_contract.go +++ b/frostfsid/frostfsid_contract.go @@ -417,7 +417,12 @@ func GetSubject(addr interop.Hash160) Subject { sKey := subjectKeyFromAddr(addr) data := storage.Get(ctx, sKey).([]byte) if data == nil { - panic("subject not found") + a := getPrimaryAddr(ctx, addr) + sKey = subjectKeyFromAddr(a) + data = storage.Get(ctx, sKey).([]byte) + if data == nil { + panic("subject not found") + } } return std.Deserialize(data).(Subject) @@ -469,21 +474,25 @@ func GetSubjectByKey(key interop.PublicKey) Subject { return std.Deserialize(data).(Subject) } - saPrefix := subjectAdditionalPrefix(key) - it := storage.Find(ctx, saPrefix, storage.KeysOnly|storage.RemovePrefix) - for iterator.Next(it) { - addr := iterator.Value(it).([]byte) - sKey = subjectKeyFromAddr(addr) - data = storage.Get(ctx, sKey).([]byte) - if data != nil { - return std.Deserialize(data).(Subject) - } - break + addr := getPrimaryAddr(ctx, contract.CreateStandardAccount(key)) + sKey = subjectKeyFromAddr(addr) + data = storage.Get(ctx, sKey).([]byte) + if data != nil { + return std.Deserialize(data).(Subject) } panic("subject not found") } +func getPrimaryAddr(ctx storage.Context, addr interop.Hash160) interop.Hash160 { + saPrefix := append([]byte{additionalKeysPrefix}, addr...) + it := storage.Find(ctx, saPrefix, storage.KeysOnly|storage.RemovePrefix) + if iterator.Next(it) { + return iterator.Value(it).([]byte) + } + panic("subject not found") +} + // GetSubjectByName retrieves the subject with the specified name within the given namespace. func GetSubjectByName(ns, name string) Subject { key := GetSubjectKeyByName(ns, name) diff --git a/tests/frostfsid_test.go b/tests/frostfsid_test.go index 419fb9f..2f0865a 100644 --- a/tests/frostfsid_test.go +++ b/tests/frostfsid_test.go @@ -238,6 +238,13 @@ func TestFrostFSID_SubjectManagement(t *testing.T) { subj = parseSubject(t, s) require.True(t, subjKey.PublicKey().Equal(&subj.PrimaryKey), "keys must be the same") + t.Run("with GetSubject", func(t *testing.T) { + s, err = anonInvoker.TestInvoke(t, getSubjectMethod, subjKey.PublicKey().GetScriptHash()) + require.NoError(t, err) + subj = parseSubject(t, s) + require.True(t, subjKey.PublicKey().Equal(&subj.PrimaryKey), "keys must be the same") + }) + t.Run("remove subject key", func(t *testing.T) { anonInvoker.InvokeFail(t, notWitnessedError, removeSubjectKeyMethod, subjKeyAddr, subjNewKey.PublicKey().Bytes()) invoker.Invoke(t, stackitem.Null{}, removeSubjectKeyMethod, subjKeyAddr, subjNewKey.PublicKey().Bytes())