2023-04-27 14:57:27 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
v2netmap "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
|
|
|
|
netmapContract "git.frostfs.info/TrueCloudLab/frostfs-contract/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/governance"
|
|
|
|
timerEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/timers"
|
|
|
|
cntClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/container"
|
|
|
|
netmapclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
netmapEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger/test"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewEpochTick(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
es := &testEpochState{
|
|
|
|
counter: 100,
|
|
|
|
}
|
|
|
|
nc := &testNetmapClient{}
|
|
|
|
|
|
|
|
proc, err := newTestProc(t, func(p *Params) {
|
|
|
|
p.CleanupEnabled = true
|
|
|
|
p.EpochState = es
|
|
|
|
p.NetmapClient = nc
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err, "failed to create processor")
|
|
|
|
|
|
|
|
ev := timerEvent.NewEpochTick{}
|
|
|
|
proc.HandleNewEpochTick(ev)
|
|
|
|
|
|
|
|
for proc.pool.Running() > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.EqualValues(t, []uint64{101}, nc.newEpochs, "invalid epochs")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewEpoch(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var node1 netmap.NodeInfo
|
|
|
|
key1, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
|
|
|
|
require.NoError(t, err, "failed to parse key1")
|
|
|
|
node1.SetPublicKey(key1.Bytes())
|
|
|
|
|
|
|
|
var node2 netmap.NodeInfo
|
|
|
|
key2, err := keys.NewPublicKeyFromString("02ac920cd7df0b61b289072e6b946e2da4e1a31b9ab1c621bb475e30fa4ab102c3")
|
|
|
|
require.NoError(t, err, "failed to parse key2")
|
|
|
|
node2.SetPublicKey(key2.Bytes())
|
|
|
|
|
|
|
|
network := &netmap.NetMap{}
|
|
|
|
network.SetNodes([]netmap.NodeInfo{node1, node2})
|
|
|
|
|
|
|
|
es := &testEpochState{
|
|
|
|
counter: 100,
|
|
|
|
duration: 10,
|
|
|
|
}
|
|
|
|
r := &testEpochResetter{}
|
|
|
|
cc := &testContainerClient{}
|
|
|
|
nc := &testNetmapClient{
|
|
|
|
epochDuration: 20,
|
|
|
|
txHeights: map[util.Uint256]uint32{
|
|
|
|
{101}: 10_000,
|
|
|
|
},
|
|
|
|
netmap: network,
|
|
|
|
}
|
|
|
|
eh := &testEventHandler{}
|
|
|
|
|
|
|
|
proc, err := newTestProc(t, func(p *Params) {
|
|
|
|
p.NotaryDepositHandler = eh.Handle
|
|
|
|
p.AlphabetSyncHandler = eh.Handle
|
|
|
|
p.NetmapClient = nc
|
|
|
|
p.ContainerWrapper = cc
|
|
|
|
p.EpochTimer = r
|
|
|
|
p.EpochState = es
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err, "failed to create processor")
|
|
|
|
|
|
|
|
ev := netmapEvent.NewEpoch{
|
|
|
|
Num: 101,
|
|
|
|
Hash: util.Uint256{101},
|
|
|
|
}
|
|
|
|
proc.handleNewEpoch(ev)
|
|
|
|
|
|
|
|
for proc.pool.Running() > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, nc.epochDuration, es.duration, "invalid epoch duration")
|
|
|
|
require.Equal(t, ev.Num, es.counter, "invalid epoch counter")
|
|
|
|
require.EqualValues(t, []uint32{nc.txHeights[ev.Hash]}, r.timers, "invalid epoch timer resets")
|
|
|
|
|
|
|
|
var expEstimation cntClient.StartEstimationPrm
|
|
|
|
expEstimation.SetEpoch(ev.Num - 1)
|
|
|
|
expEstimation.SetHash(ev.Hash)
|
|
|
|
require.EqualValues(t, []cntClient.StartEstimationPrm{expEstimation}, cc.estimations, "invalid estimations")
|
|
|
|
|
|
|
|
require.EqualValues(t, []event.Event{
|
|
|
|
governance.NewSyncEvent(ev.TxHash()),
|
|
|
|
ev,
|
|
|
|
}, eh.handledEvents, "invalid handled events")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddPeer(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
nc := &testNetmapClient{
|
|
|
|
contractAddress: util.Uint160{47},
|
|
|
|
}
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
proc, err := newTestProc(t, func(p *Params) {
|
|
|
|
p.NetmapClient = nc
|
2023-04-27 14:57:27 +00:00
|
|
|
})
|
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
require.NoError(t, err, "failed to create processor")
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
var node netmap.NodeInfo
|
|
|
|
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
|
|
|
|
require.NoError(t, err, "failed to parse key")
|
|
|
|
node.SetPublicKey(key.Bytes())
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
ev := netmapEvent.AddPeer{
|
|
|
|
NodeBytes: node.Marshal(),
|
|
|
|
Request: &payload.P2PNotaryRequest{
|
|
|
|
MainTransaction: &transaction.Transaction{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
proc.handleAddPeer(ev)
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
for proc.pool.Running() > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
require.EqualValues(t, []notaryInvoke{
|
|
|
|
{
|
|
|
|
contract: nc.contractAddress,
|
|
|
|
fee: 0,
|
|
|
|
nonce: ev.NotaryRequest().MainTransaction.Nonce,
|
|
|
|
vub: nil,
|
|
|
|
method: "addPeerIR",
|
|
|
|
args: []any{node.Marshal()},
|
|
|
|
},
|
|
|
|
}, nc.notaryInvokes, "invalid notary invokes")
|
2023-04-27 14:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateState(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
ns := &testNodeStateSettings{
|
|
|
|
maintAllowed: true,
|
|
|
|
}
|
|
|
|
nc := &testNetmapClient{}
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
proc, err := newTestProc(t, func(p *Params) {
|
|
|
|
p.NetmapClient = nc
|
|
|
|
p.NodeStateSettings = ns
|
2023-04-27 14:57:27 +00:00
|
|
|
})
|
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
require.NoError(t, err, "failed to create processor")
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
|
|
|
|
require.NoError(t, err, "failed to parse key")
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
ev := netmapEvent.UpdatePeer{
|
|
|
|
State: netmapContract.NodeStateOnline,
|
|
|
|
PubKey: key,
|
|
|
|
Request: &payload.P2PNotaryRequest{
|
|
|
|
MainTransaction: &transaction.Transaction{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
proc.handleUpdateState(ev)
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
for proc.pool.Running() > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
require.EqualValues(t, []*transaction.Transaction{ev.Request.MainTransaction}, nc.invokedTxs, "invalid transactions")
|
2023-04-27 14:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupTick(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
nc := &testNetmapClient{
|
|
|
|
contractAddress: util.Uint160{111},
|
|
|
|
}
|
|
|
|
proc, err := newTestProc(t,
|
|
|
|
func(p *Params) {
|
2023-04-27 14:57:27 +00:00
|
|
|
p.NetmapClient = nc
|
|
|
|
p.CleanupEnabled = true
|
2023-05-17 13:56:47 +00:00
|
|
|
},
|
|
|
|
)
|
2023-04-27 14:57:27 +00:00
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
require.NoError(t, err, "failed to create processor")
|
|
|
|
|
|
|
|
key1Str := "038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35"
|
|
|
|
proc.netmapSnapshot.lastAccess[key1Str] = epochStampWithNodeInfo{
|
|
|
|
epochStamp: epochStamp{
|
|
|
|
epoch: 95,
|
|
|
|
removeFlag: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
key2Str := "02ac920cd7df0b61b289072e6b946e2da4e1a31b9ab1c621bb475e30fa4ab102c3"
|
|
|
|
proc.netmapSnapshot.lastAccess[key2Str] = epochStampWithNodeInfo{
|
|
|
|
epochStamp: epochStamp{
|
|
|
|
epoch: 98,
|
|
|
|
removeFlag: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := netmapCleanupTick{
|
|
|
|
epoch: 100,
|
|
|
|
txHash: util.Uint256{123},
|
|
|
|
}
|
|
|
|
|
|
|
|
proc.handleCleanupTick(ev)
|
|
|
|
|
|
|
|
for proc.pool.Running() > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyExp, err := keys.NewPublicKeyFromString(key1Str)
|
|
|
|
require.NoError(t, err, "failed to parse expired key")
|
|
|
|
|
|
|
|
updExp := netmapclient.UpdatePeerPrm{}
|
|
|
|
updExp.SetKey(keyExp.Bytes())
|
|
|
|
updExp.SetHash(ev.TxHash())
|
|
|
|
|
|
|
|
require.EqualValues(t, []notaryInvoke{
|
|
|
|
{
|
|
|
|
contract: nc.contractAddress,
|
|
|
|
fee: 0,
|
|
|
|
nonce: uint32(ev.epoch),
|
|
|
|
vub: nil,
|
|
|
|
method: "updateStateIR",
|
|
|
|
args: []any{int64(v2netmap.Offline), keyExp.Bytes()},
|
|
|
|
},
|
|
|
|
}, nc.notaryInvokes, "invalid notary invokes")
|
|
|
|
require.True(t, proc.netmapSnapshot.lastAccess[key1Str].removeFlag, "invalid expired removed flag")
|
|
|
|
require.False(t, proc.netmapSnapshot.lastAccess[key2Str].removeFlag, "invalid non expired removed flag")
|
2023-04-27 14:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestProc(t *testing.T, nonDefault func(p *Params)) (*Processor, error) {
|
|
|
|
ns := &testNodeStateSettings{}
|
|
|
|
es := &testEpochState{}
|
|
|
|
r := &testEpochResetter{}
|
|
|
|
as := &testAlphabetState{
|
|
|
|
isAlphabet: true,
|
|
|
|
}
|
|
|
|
cc := &testContainerClient{}
|
|
|
|
nc := &testNetmapClient{}
|
|
|
|
eh := &testEventHandler{}
|
|
|
|
|
|
|
|
p := &Params{
|
2023-05-02 08:51:30 +00:00
|
|
|
Log: test.NewLogger(t, true),
|
|
|
|
PoolSize: 1,
|
|
|
|
CleanupEnabled: false,
|
|
|
|
CleanupThreshold: 3,
|
|
|
|
NodeStateSettings: ns,
|
|
|
|
NodeValidator: &testValidator{},
|
|
|
|
EpochState: es,
|
|
|
|
EpochTimer: r,
|
|
|
|
AlphabetState: as,
|
|
|
|
ContainerWrapper: cc,
|
|
|
|
NetmapClient: nc,
|
|
|
|
NotaryDepositHandler: eh.Handle,
|
|
|
|
AlphabetSyncHandler: eh.Handle,
|
2023-04-27 14:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nonDefault(p)
|
|
|
|
|
|
|
|
return New(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
type testNodeStateSettings struct {
|
|
|
|
maintAllowed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testNodeStateSettings) MaintenanceModeAllowed() error {
|
|
|
|
if s.maintAllowed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("maintenance mode not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
type testValidator struct{}
|
|
|
|
|
|
|
|
func (v *testValidator) VerifyAndUpdate(*netmap.NodeInfo) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testEpochState struct {
|
|
|
|
counter uint64
|
|
|
|
duration uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testEpochState) SetEpochCounter(c uint64) {
|
|
|
|
s.counter = c
|
|
|
|
}
|
2023-10-31 11:56:55 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (s *testEpochState) EpochCounter() uint64 {
|
|
|
|
return s.counter
|
|
|
|
}
|
2023-10-31 11:56:55 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (s *testEpochState) SetEpochDuration(d uint64) {
|
|
|
|
s.duration = d
|
|
|
|
}
|
2023-10-31 11:56:55 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (s *testEpochState) EpochDuration() uint64 {
|
|
|
|
return s.duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type testEpochResetter struct {
|
|
|
|
timers []uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testEpochResetter) ResetEpochTimer(t uint32) error {
|
|
|
|
r.timers = append(r.timers, t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testAlphabetState struct {
|
|
|
|
isAlphabet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testAlphabetState) IsAlphabet() bool {
|
|
|
|
return s.isAlphabet
|
|
|
|
}
|
|
|
|
|
|
|
|
type testContainerClient struct {
|
|
|
|
estimations []cntClient.StartEstimationPrm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testContainerClient) StartEstimation(p cntClient.StartEstimationPrm) error {
|
|
|
|
c.estimations = append(c.estimations, p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type notaryInvoke struct {
|
|
|
|
contract util.Uint160
|
|
|
|
fee fixedn.Fixed8
|
|
|
|
nonce uint32
|
|
|
|
vub *uint32
|
|
|
|
method string
|
|
|
|
args []any
|
|
|
|
}
|
|
|
|
|
|
|
|
type testNetmapClient struct {
|
|
|
|
contractAddress util.Uint160
|
|
|
|
epochDuration uint64
|
|
|
|
netmap *netmap.NetMap
|
|
|
|
txHeights map[util.Uint256]uint32
|
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
notaryInvokes []notaryInvoke
|
|
|
|
newEpochs []uint64
|
|
|
|
invokedTxs []*transaction.Transaction
|
2023-04-27 14:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testNetmapClient) MorphNotaryInvoke(contract util.Uint160, fee fixedn.Fixed8, nonce uint32, vub *uint32, method string, args ...any) error {
|
|
|
|
c.notaryInvokes = append(c.notaryInvokes, notaryInvoke{
|
|
|
|
contract: contract,
|
|
|
|
fee: fee,
|
|
|
|
nonce: nonce,
|
|
|
|
vub: vub,
|
|
|
|
method: method,
|
|
|
|
args: args,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-17 13:56:47 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (c *testNetmapClient) ContractAddress() util.Uint160 {
|
|
|
|
return c.contractAddress
|
|
|
|
}
|
2023-05-17 13:56:47 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (c *testNetmapClient) EpochDuration() (uint64, error) {
|
|
|
|
return c.epochDuration, nil
|
|
|
|
}
|
2023-05-17 13:56:47 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (c *testNetmapClient) MorphTxHeight(h util.Uint256) (uint32, error) {
|
|
|
|
if res, found := c.txHeights[h]; found {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("not found")
|
|
|
|
}
|
2023-05-17 13:56:47 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (c *testNetmapClient) NetMap() (*netmap.NetMap, error) {
|
|
|
|
return c.netmap, nil
|
|
|
|
}
|
2023-05-17 13:56:47 +00:00
|
|
|
|
2023-11-08 09:05:03 +00:00
|
|
|
func (c *testNetmapClient) NewEpoch(epoch uint64) error {
|
2023-04-27 14:57:27 +00:00
|
|
|
c.newEpochs = append(c.newEpochs, epoch)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-17 13:56:47 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (c *testNetmapClient) MorphIsValidScript(script []byte, signers []transaction.Signer) (valid bool, err error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-05-17 13:56:47 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
func (c *testNetmapClient) MorphNotarySignAndInvokeTX(mainTx *transaction.Transaction) error {
|
|
|
|
c.invokedTxs = append(c.invokedTxs, mainTx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testEventHandler struct {
|
|
|
|
handledEvents []event.Event
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *testEventHandler) Handle(e event.Event) {
|
|
|
|
h.handledEvents = append(h.handledEvents, e)
|
|
|
|
}
|