From f9304fb2cbdc7269333be4ac840fea0931f56dc1 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 23 Mar 2021 13:54:48 +0300 Subject: [PATCH] [#446] innerring: Remove inner ring list method Signed-off-by: Alex Vanin --- pkg/innerring/innerring.go | 2 +- pkg/innerring/invoke/chain.go | 42 ++++++++++++++++++++++++++++++ pkg/innerring/invoke/neofs.go | 49 +---------------------------------- 3 files changed, 44 insertions(+), 49 deletions(-) create mode 100644 pkg/innerring/invoke/chain.go diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 2c68da924..9cbb6a9e9 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -714,7 +714,7 @@ func (s *Server) initConfigFromBlockchain() error { key := &s.key.PublicKey // check if node inside inner ring list and what index it has - index, size, err := invoke.InnerRingIndex(s.mainnetClient, s.contracts.neofs, key) + index, size, err := invoke.InnerRingIndex(s.mainnetClient, key) if err != nil { return errors.Wrap(err, "can't read inner ring list") } diff --git a/pkg/innerring/invoke/chain.go b/pkg/innerring/invoke/chain.go new file mode 100644 index 000000000..dd794f60c --- /dev/null +++ b/pkg/innerring/invoke/chain.go @@ -0,0 +1,42 @@ +package invoke + +import ( + "bytes" + "crypto/ecdsa" + + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + crypto "github.com/nspcc-dev/neofs-crypto" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" +) + +// InnerRingIndex returns index of the `key` in the inner ring list from sidechain +// along with total size of inner ring list. If key is not in the inner ring list, +// then returns `-1` as index. +func InnerRingIndex(cli *client.Client, key *ecdsa.PublicKey) (int32, int32, error) { + if cli == nil { + return 0, 0, client.ErrNilClient + } + + innerRing, err := cli.NeoFSAlphabetList() + if err != nil { + return 0, 0, err + } + + return keyPosition(key, innerRing), int32(len(innerRing)), nil +} + +// keyPosition returns "-1" if key is not found in the list, otherwise returns +// index of the key. +func keyPosition(key *ecdsa.PublicKey, list keys.PublicKeys) (result int32) { + result = -1 + rawBytes := crypto.MarshalPublicKey(key) + + for i := range list { + if bytes.Equal(list[i].Bytes(), rawBytes) { + result = int32(i) + break + } + } + + return result +} diff --git a/pkg/innerring/invoke/neofs.go b/pkg/innerring/invoke/neofs.go index 6c8876b31..efbae57a3 100644 --- a/pkg/innerring/invoke/neofs.go +++ b/pkg/innerring/invoke/neofs.go @@ -1,11 +1,7 @@ package invoke import ( - "bytes" - "crypto/ecdsa" - "github.com/nspcc-dev/neo-go/pkg/util" - crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/morph/client" ) @@ -29,8 +25,7 @@ const ( // execution stage. Otherwise invocation will fail due to gas limit. extraFee = 2_0000_0000 // 2.0 Fixed8 gas - innerRingListMethod = "innerRingList" - chequeMethod = "cheque" + chequeMethod = "cheque" ) // CashOutCheque invokes Cheque method. @@ -46,45 +41,3 @@ func CashOutCheque(cli *client.Client, con util.Uint160, p *ChequeParams) error p.LockAccount.BytesBE(), ) } - -// InnerRingIndex returns index of the `key` in the inner ring list from sidechain -// along with total size of inner ring list. If key is not in the inner ring list, -// then returns `-1` as index. -func InnerRingIndex(cli *client.Client, con util.Uint160, key *ecdsa.PublicKey) (int32, int32, error) { - if cli == nil { - return 0, 0, client.ErrNilClient - } - - nodePublicKey := crypto.MarshalPublicKey(key) - - data, err := cli.TestInvoke(con, innerRingListMethod) - if err != nil { - return 0, 0, err - } - - irNodes, err := client.ArrayFromStackItem(data[0]) - if err != nil { - return 0, 0, err - } - - var result int32 = -1 - - for i := range irNodes { - key, err := client.ArrayFromStackItem(irNodes[i]) - if err != nil { - return 0, 0, err - } - - keyValue, err := client.BytesFromStackItem(key[0]) - if err != nil { - return 0, 0, err - } - - if bytes.Equal(keyValue, nodePublicKey) { - result = int32(i) - break - } - } - - return result, int32(len(irNodes)), nil -}