From bf83ed9a4f170d61f14db04d3fdfd48ab3f0ff0e Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 30 Nov 2021 11:59:52 +0300 Subject: [PATCH] [#101] *: update storage after voting Signed-off-by: Evgenii Stratonikov --- neofsid/neofsid_contract.go | 73 +++++++++++++++---------------- reputation/reputation_contract.go | 23 +++++----- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/neofsid/neofsid_contract.go b/neofsid/neofsid_contract.go index 667d009..8fc1e2f 100644 --- a/neofsid/neofsid_contract.go +++ b/neofsid/neofsid_contract.go @@ -95,6 +95,12 @@ func AddKey(owner []byte, keys []interop.PublicKey) { panic("incorrect owner") } + for i := range keys { + if len(keys[i]) != interop.PublicKeyCompressedLen { + panic("incorrect public key") + } + } + ctx := storage.GetContext() notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool) @@ -114,37 +120,30 @@ func AddKey(owner []byte, keys []interop.PublicKey) { indirectCall = common.FromKnownContract( ctx, runtime.GetCallingScriptHash(), - containerContractKey, - ) + containerContractKey) + + if indirectCall { + threshold := len(alphabet)*2/3 + 1 + id := invokeIDKeys(owner, keys, []byte("add")) + + n := common.Vote(ctx, id, nodeKey) + if n < threshold { + return + } + + common.RemoveVotes(ctx, id) + } } else { multiaddr := common.AlphabetAddress() common.CheckAlphabetWitness(multiaddr) } - for i := range keys { - if len(keys[i]) != interop.PublicKeyCompressedLen { - panic("incorrect public key") - } - } - ownerKey := append([]byte{ownerKeysPrefix}, owner...) for i := range keys { stKey := append(ownerKey, keys[i]...) storage.Put(ctx, stKey, []byte{1}) } - if notaryDisabled && !indirectCall { - threshold := len(alphabet)*2/3 + 1 - id := invokeIDKeys(owner, keys, []byte("add")) - - n := common.Vote(ctx, id, nodeKey) - if n < threshold { - return - } - - common.RemoveVotes(ctx, id) - } - runtime.Log("key bound to the owner") } @@ -159,6 +158,12 @@ func RemoveKey(owner []byte, keys []interop.PublicKey) { panic("incorrect owner") } + for i := range keys { + if len(keys[i]) != interop.PublicKeyCompressedLen { + panic("incorrect public key") + } + } + ctx := storage.GetContext() notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool) @@ -173,24 +178,7 @@ func RemoveKey(owner []byte, keys []interop.PublicKey) { if len(nodeKey) == 0 { panic("invocation from non inner ring node") } - } else { - multiaddr := common.AlphabetAddress() - common.CheckAlphabetWitness(multiaddr) - } - for i := range keys { - if len(keys[i]) != interop.PublicKeyCompressedLen { - panic("incorrect public key") - } - } - - ownerKey := append([]byte{ownerKeysPrefix}, owner...) - for i := range keys { - stKey := append(ownerKey, keys[i]...) - storage.Delete(ctx, stKey) - } - - if notaryDisabled { threshold := len(alphabet)*2/3 + 1 id := invokeIDKeys(owner, keys, []byte("remove")) @@ -200,6 +188,17 @@ func RemoveKey(owner []byte, keys []interop.PublicKey) { } common.RemoveVotes(ctx, id) + } else { + multiaddr := common.AlphabetAddress() + if !runtime.CheckWitness(multiaddr) { + panic("invocation from non inner ring node") + } + } + + ownerKey := append([]byte{ownerKeysPrefix}, owner...) + for i := range keys { + stKey := append(ownerKey, keys[i]...) + storage.Delete(ctx, stKey) } } diff --git a/reputation/reputation_contract.go b/reputation/reputation_contract.go index fac0641..946d352 100644 --- a/reputation/reputation_contract.go +++ b/reputation/reputation_contract.go @@ -100,8 +100,18 @@ func Put(epoch int, peerID []byte, value []byte) { } id := storageID(epoch, peerID) - key := getReputationKey(reputationCountPrefix, id) + if notaryDisabled { + threshold := len(alphabet)*2/3 + 1 + n := common.Vote(ctx, id, nodeKey) + if n < threshold { + return + } + + common.RemoveVotes(ctx, id) + } + + key := getReputationKey(reputationCountPrefix, id) rawCnt := storage.Get(ctx, key) cnt := 0 if rawCnt != nil { @@ -113,17 +123,6 @@ func Put(epoch int, peerID []byte, value []byte) { key[0] = reputationValuePrefix key = append(key, convert.ToBytes(cnt)...) storage.Put(ctx, key, value) - - if notaryDisabled { - threshold := len(alphabet)*2/3 + 1 - - n := common.Vote(ctx, id, nodeKey) - if n < threshold { - return - } - - common.RemoveVotes(ctx, id) - } } // Get method returns list of all stable marshaled DataAuditResult structures