[#132] Use SDK defined netmap structure in cleanup table

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-30 14:26:57 +03:00 committed by Alex Vanin
parent 976ba06300
commit c7975dbe87
3 changed files with 42 additions and 29 deletions

View file

@ -4,7 +4,9 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
netmapv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap"
netmapgrpc "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
@ -108,7 +110,8 @@ func UpdateInnerRing(cli *client.Client, con util.Uint160, list []*keys.PublicKe
}
// NetmapSnapshot returns current netmap node infos.
func NetmapSnapshot(cli *client.Client, con util.Uint160) ([]netmap.NodeInfo, error) {
// Consider using pkg/morph/client/netmap for this.
func NetmapSnapshot(cli *client.Client, con util.Uint160) (*netmap.Netmap, error) {
if cli == nil {
return nil, client.ErrNilClient
}
@ -127,7 +130,7 @@ func NetmapSnapshot(cli *client.Client, con util.Uint160) ([]netmap.NodeInfo, er
return nil, err
}
result := make([]netmap.NodeInfo, 0, len(rawNodeInfos))
result := make([]netmapv2.NodeInfo, 0, len(rawNodeInfos))
for i := range rawNodeInfos {
nodeInfo, err := peerInfoFromStackItem(rawNodeInfos[i])
@ -138,11 +141,11 @@ func NetmapSnapshot(cli *client.Client, con util.Uint160) ([]netmap.NodeInfo, er
result = append(result, *nodeInfo)
}
return result, nil
return netmap.NewNetmap(netmap.NodesFromV2(result))
}
func peerInfoFromStackItem(prm stackitem.Item) (*netmap.NodeInfo, error) {
node := new(netmap.NodeInfo)
func peerInfoFromStackItem(prm stackitem.Item) (*netmapv2.NodeInfo, error) {
node := new(netmapgrpc.NodeInfo)
subItems, err := client.ArrayFromStackItem(prm)
if err != nil {
@ -155,5 +158,5 @@ func peerInfoFromStackItem(prm stackitem.Item) (*netmap.NodeInfo, error) {
return nil, err
}
return node, nil
return netmapv2.NodeInfoFromGRPCMessage(node), nil
}

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"sync"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
)
type (
@ -31,15 +31,15 @@ func newCleanupTable(enabled bool, threshold uint64) cleanupTable {
}
// Update cleanup table based on on-chain information about netmap.
func (c *cleanupTable) update(snapshot []netmap.NodeInfo, now uint64) {
func (c *cleanupTable) update(snapshot *netmap.Netmap, now uint64) {
c.Lock()
defer c.Unlock()
// replacing map is less memory efficient but faster
newMap := make(map[string]epochStamp, len(snapshot))
newMap := make(map[string]epochStamp, len(snapshot.Nodes))
for i := range snapshot {
keyString := hex.EncodeToString(snapshot[i].PublicKey)
for i := range snapshot.Nodes {
keyString := hex.EncodeToString(snapshot.Nodes[i].PublicKey())
if access, ok := c.lastAccess[keyString]; ok {
access.removeFlag = false // reset remove Flag on each Update
newMap[keyString] = access

View file

@ -1,31 +1,36 @@
package netmap
import (
"crypto/ecdsa"
"encoding/hex"
"testing"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
netmapv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap"
crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/pkg/util/test"
"github.com/stretchr/testify/require"
)
func TestCleanupTable(t *testing.T) {
infos := []netmap.NodeInfo{
{PublicKey: crypto.MarshalPublicKey(&test.DecodeKey(1).PublicKey)},
{PublicKey: crypto.MarshalPublicKey(&test.DecodeKey(2).PublicKey)},
{PublicKey: crypto.MarshalPublicKey(&test.DecodeKey(3).PublicKey)},
infos := []netmapv2.NodeInfo{
newNodeInfo(&test.DecodeKey(1).PublicKey),
newNodeInfo(&test.DecodeKey(2).PublicKey),
newNodeInfo(&test.DecodeKey(3).PublicKey),
}
networkMap, err := netmap.NewNetmap(netmap.NodesFromV2(infos))
require.NoError(t, err)
mapInfos := map[string]struct{}{
hex.EncodeToString(infos[0].PublicKey): {},
hex.EncodeToString(infos[1].PublicKey): {},
hex.EncodeToString(infos[2].PublicKey): {},
hex.EncodeToString(infos[0].GetPublicKey()): {},
hex.EncodeToString(infos[1].GetPublicKey()): {},
hex.EncodeToString(infos[2].GetPublicKey()): {},
}
t.Run("update", func(t *testing.T) {
c := newCleanupTable(true, 1)
c.update(infos, 1)
c.update(networkMap, 1)
require.Len(t, c.lastAccess, len(infos))
for k, v := range c.lastAccess {
@ -37,10 +42,10 @@ func TestCleanupTable(t *testing.T) {
}
t.Run("update with flagged", func(t *testing.T) {
key := hex.EncodeToString(infos[0].PublicKey)
key := hex.EncodeToString(infos[0].GetPublicKey())
c.flag(key)
c.update(infos, 2)
c.update(networkMap, 2)
require.EqualValues(t, 1, c.lastAccess[key].epoch)
require.False(t, c.lastAccess[key].removeFlag)
})
@ -48,9 +53,9 @@ func TestCleanupTable(t *testing.T) {
t.Run("touch", func(t *testing.T) {
c := newCleanupTable(true, 1)
c.update(infos, 1)
c.update(networkMap, 1)
key := hex.EncodeToString(infos[1].PublicKey)
key := hex.EncodeToString(infos[1].GetPublicKey())
require.True(t, c.touch(key, 11))
require.EqualValues(t, 11, c.lastAccess[key].epoch)
@ -60,9 +65,9 @@ func TestCleanupTable(t *testing.T) {
t.Run("flag", func(t *testing.T) {
c := newCleanupTable(true, 1)
c.update(infos, 1)
c.update(networkMap, 1)
key := hex.EncodeToString(infos[1].PublicKey)
key := hex.EncodeToString(infos[1].GetPublicKey())
c.flag(key)
require.True(t, c.lastAccess[key].removeFlag)
@ -72,7 +77,7 @@ func TestCleanupTable(t *testing.T) {
t.Run("iterator", func(t *testing.T) {
c := newCleanupTable(true, 2)
c.update(infos, 1)
c.update(networkMap, 1)
t.Run("no nodes to remove", func(t *testing.T) {
cnt := 0
@ -98,7 +103,7 @@ func TestCleanupTable(t *testing.T) {
t.Run("some nodes to remove", func(t *testing.T) {
cnt := 0
key := hex.EncodeToString(infos[1].PublicKey)
key := hex.EncodeToString(infos[1].GetPublicKey())
require.False(t, c.touch(key, 4)) // one node was updated
@ -112,3 +117,8 @@ func TestCleanupTable(t *testing.T) {
})
})
}
func newNodeInfo(key *ecdsa.PublicKey) (n netmapv2.NodeInfo) {
n.SetPublicKey(crypto.MarshalPublicKey(key))
return n
}