[#446] innerring: Remove inner ring list method

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-03-23 13:54:48 +03:00 committed by Alex Vanin
parent 43e367f52c
commit f9304fb2cb
3 changed files with 44 additions and 49 deletions

View file

@ -714,7 +714,7 @@ func (s *Server) initConfigFromBlockchain() error {
key := &s.key.PublicKey key := &s.key.PublicKey
// check if node inside inner ring list and what index it has // 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 { if err != nil {
return errors.Wrap(err, "can't read inner ring list") return errors.Wrap(err, "can't read inner ring list")
} }

View file

@ -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
}

View file

@ -1,11 +1,7 @@
package invoke package invoke
import ( import (
"bytes"
"crypto/ecdsa"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
) )
@ -29,8 +25,7 @@ const (
// execution stage. Otherwise invocation will fail due to gas limit. // execution stage. Otherwise invocation will fail due to gas limit.
extraFee = 2_0000_0000 // 2.0 Fixed8 gas extraFee = 2_0000_0000 // 2.0 Fixed8 gas
innerRingListMethod = "innerRingList" chequeMethod = "cheque"
chequeMethod = "cheque"
) )
// CashOutCheque invokes Cheque method. // CashOutCheque invokes Cheque method.
@ -46,45 +41,3 @@ func CashOutCheque(cli *client.Client, con util.Uint160, p *ChequeParams) error
p.LockAccount.BytesBE(), 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
}