WIP: Morph: Add unit tests #2

Closed
dstepanov-yadro wants to merge 233 commits from TrueCloudLab/frostfs-node:master into object-3608-morph-unit-tests
5 changed files with 136 additions and 322 deletions
Showing only changes of commit 656fd7f376 - Show all commits

View file

@ -71,7 +71,6 @@ func (s *Server) initNetmapProcessor(cfg *viper.Viper,
addrvalidator.New(),
locodeValidator,
),
NotaryDisabled: s.sideNotaryConfig.disabled,
NodeStateSettings: netSettings,
})

View file

@ -31,7 +31,6 @@ func TestNewEpochTick(t *testing.T) {
nc := &testNetmapClient{}
proc, err := newTestProc(t, func(p *Params) {
p.NotaryDisabled = true
p.CleanupEnabled = true
p.EpochState = es
p.NetmapClient = nc
@ -80,7 +79,6 @@ func TestNewEpoch(t *testing.T) {
eh := &testEventHandler{}
proc, err := newTestProc(t, func(p *Params) {
p.NotaryDisabled = true
p.NotaryDepositHandler = eh.Handle
p.AlphabetSyncHandler = eh.Handle
p.NetmapClient = nc
@ -119,276 +117,139 @@ func TestNewEpoch(t *testing.T) {
func TestAddPeer(t *testing.T) {
t.Parallel()
t.Run("with notary", func(t *testing.T) {
t.Parallel()
nc := &testNetmapClient{
contractAddress: util.Uint160{47},
}
nc := &testNetmapClient{
contractAddress: util.Uint160{47},
}
proc, err := newTestProc(t, func(p *Params) {
p.NotaryDisabled = true
p.NetmapClient = nc
})
require.NoError(t, err, "failed to create processor")
var node netmap.NodeInfo
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
require.NoError(t, err, "failed to parse key1")
node.SetPublicKey(key.Bytes())
ev := netmapEvent.AddPeer{
NodeBytes: node.Marshal(),
Request: &payload.P2PNotaryRequest{
MainTransaction: &transaction.Transaction{
Nonce: 100,
},
},
}
proc.handleAddPeer(ev)
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
require.EqualValues(t, []notaryInvoke{
{
contract: nc.contractAddress,
fee: 0,
nonce: ev.Request.MainTransaction.Nonce,
vub: nil,
method: "addPeerIR",
args: []any{ev.Node()},
},
}, nc.notaryInvokes, "invalid notary invokes")
proc, err := newTestProc(t, func(p *Params) {
p.NetmapClient = nc
})
t.Run("without notary", func(t *testing.T) {
t.Parallel()
require.NoError(t, err, "failed to create processor")
nc := &testNetmapClient{
contractAddress: util.Uint160{47},
}
var node netmap.NodeInfo
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
require.NoError(t, err, "failed to parse key")
node.SetPublicKey(key.Bytes())
proc, err := newTestProc(t, func(p *Params) {
p.NotaryDisabled = true
p.NetmapClient = nc
})
ev := netmapEvent.AddPeer{
NodeBytes: node.Marshal(),
Request: &payload.P2PNotaryRequest{
MainTransaction: &transaction.Transaction{},
},
}
proc.handleAddPeer(ev)
require.NoError(t, err, "failed to create processor")
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
var node netmap.NodeInfo
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
require.NoError(t, err, "failed to parse key")
node.SetPublicKey(key.Bytes())
ev := netmapEvent.AddPeer{
NodeBytes: node.Marshal(),
}
proc.handleAddPeer(ev)
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
var addPeerExp netmapclient.AddPeerPrm
addPeerExp.SetNodeInfo(node)
require.EqualValues(t, []netmapclient.AddPeerPrm{addPeerExp}, nc.addPeers, "invalid peers")
})
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")
}
func TestUpdateState(t *testing.T) {
t.Parallel()
t.Run("with notary", func(t *testing.T) {
t.Parallel()
ns := &testNodeStateSettings{
maintAllowed: true,
}
nc := &testNetmapClient{}
ns := &testNodeStateSettings{
maintAllowed: true,
}
nc := &testNetmapClient{}
proc, err := newTestProc(t, func(p *Params) {
p.NotaryDisabled = true
p.NodeStateSettings = ns
p.NetmapClient = nc
})
require.NoError(t, err, "failed to create processor")
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
require.NoError(t, err, "failed to parse key")
ev := netmapEvent.UpdatePeer{
State: netmapContract.NodeStateOnline,
PubKey: key,
Request: &payload.P2PNotaryRequest{
MainTransaction: &transaction.Transaction{
Nonce: 100,
},
},
}
proc.handleUpdateState(ev)
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
require.EqualValues(t, []*transaction.Transaction{
ev.Request.MainTransaction,
}, nc.invokedTxs, "invalid invoked transactions")
proc, err := newTestProc(t, func(p *Params) {
p.NetmapClient = nc
p.NodeStateSettings = ns
})
t.Run("without notary", func(t *testing.T) {
t.Parallel()
ns := &testNodeStateSettings{
maintAllowed: true,
}
nc := &testNetmapClient{}
require.NoError(t, err, "failed to create processor")
proc, err := newTestProc(t, func(p *Params) {
p.NetmapClient = nc
p.NodeStateSettings = ns
})
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
require.NoError(t, err, "failed to parse key")
require.NoError(t, err, "failed to create processor")
ev := netmapEvent.UpdatePeer{
State: netmapContract.NodeStateOnline,
PubKey: key,
Request: &payload.P2PNotaryRequest{
MainTransaction: &transaction.Transaction{},
},
}
proc.handleUpdateState(ev)
key, err := keys.NewPublicKeyFromString("038c862959e56b43e20f79187c4fe9e0bc7c8c66c1603e6cf0ec7f87ab6b08dc35")
require.NoError(t, err, "failed to parse key")
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
ev := netmapEvent.UpdatePeer{
State: netmapContract.NodeStateOnline,
PubKey: key,
}
proc.handleUpdateState(ev)
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
var expUpdPeer netmapclient.UpdatePeerPrm
expUpdPeer.SetMaintenance()
expUpdPeer.SetOnline()
expUpdPeer.SetKey(ev.PubKey.Bytes())
require.EqualValues(t, []netmapclient.UpdatePeerPrm{expUpdPeer}, nc.peerStateUpdates, "invalid peer state updates")
})
require.EqualValues(t, []*transaction.Transaction{ev.Request.MainTransaction}, nc.invokedTxs, "invalid transactions")
}
func TestCleanupTick(t *testing.T) {
t.Parallel()
t.Run("notary disabled", func(t *testing.T) {
t.Parallel()
nc := &testNetmapClient{}
proc, err := newTestProc(t, func(p *Params) {
nc := &testNetmapClient{
contractAddress: util.Uint160{111},
}
proc, err := newTestProc(t,
func(p *Params) {
p.NetmapClient = nc
p.NotaryDisabled = true
p.CleanupEnabled = true
})
},
)
require.NoError(t, err, "failed to create processor")
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,
},
}
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},
}
ev := netmapCleanupTick{
epoch: 100,
txHash: util.Uint256{123},
}
proc.handleCleanupTick(ev)
proc.handleCleanupTick(ev)
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
for proc.pool.Running() > 0 {
time.Sleep(10 * time.Millisecond)
}
keyExp, err := keys.NewPublicKeyFromString(key1Str)
require.NoError(t, err, "failed to parse expired key")
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())
updExp := netmapclient.UpdatePeerPrm{}
updExp.SetKey(keyExp.Bytes())
updExp.SetHash(ev.TxHash())
require.EqualValues(t, []netmapclient.UpdatePeerPrm{updExp}, nc.peerStateUpdates, "invalid peer updates")
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")
})
t.Run("notary enabled", func(t *testing.T) {
t.Parallel()
nc := &testNetmapClient{
contractAddress: util.Uint160{111},
}
proc, err := newTestProc(t,
func(p *Params) {
p.NetmapClient = nc
p.CleanupEnabled = true
},
)
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")
})
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")
}
func newTestProc(t *testing.T, nonDefault func(p *Params)) (*Processor, error) {
@ -407,7 +268,6 @@ func newTestProc(t *testing.T, nonDefault func(p *Params)) (*Processor, error) {
PoolSize: 1,
CleanupEnabled: false,
CleanupThreshold: 3,
NotaryDisabled: false,
NodeStateSettings: ns,
NodeValidator: &testValidator{},
EpochState: es,
@ -500,17 +360,11 @@ type testNetmapClient struct {
netmap *netmap.NetMap
txHeights map[util.Uint256]uint32
peerStateUpdates []netmapclient.UpdatePeerPrm
notaryInvokes []notaryInvoke
newEpochs []uint64
addPeers []netmapclient.AddPeerPrm
invokedTxs []*transaction.Transaction
notaryInvokes []notaryInvoke
newEpochs []uint64
invokedTxs []*transaction.Transaction
}
func (c *testNetmapClient) UpdatePeerState(p netmapclient.UpdatePeerPrm) error {
c.peerStateUpdates = append(c.peerStateUpdates, p)
return nil
}
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,
@ -522,32 +376,35 @@ func (c *testNetmapClient) MorphNotaryInvoke(contract util.Uint160, fee fixedn.F
})
return nil
}
func (c *testNetmapClient) ContractAddress() util.Uint160 {
return c.contractAddress
}
func (c *testNetmapClient) EpochDuration() (uint64, error) {
return c.epochDuration, nil
}
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")
}
func (c *testNetmapClient) NetMap() (*netmap.NetMap, error) {
return c.netmap, nil
}
func (c *testNetmapClient) NewEpoch(epoch uint64, force bool) error {
c.newEpochs = append(c.newEpochs, epoch)
return nil
}
func (c *testNetmapClient) MorphIsValidScript(script []byte, signers []transaction.Signer) (valid bool, err error) {
return true, nil
}
func (c *testNetmapClient) AddPeer(p netmapclient.AddPeerPrm) error {
c.addPeers = append(c.addPeers, p)
return nil
}
func (c *testNetmapClient) MorphNotarySignAndInvokeTX(mainTx *transaction.Transaction) error {
c.invokedTxs = append(c.invokedTxs, mainTx)
return nil

View file

@ -3,7 +3,6 @@ package netmap
import (
v2netmap "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
netmapclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"go.uber.org/zap"
)
@ -31,23 +30,14 @@ func (np *Processor) processNetmapCleanupTick(ev netmapCleanupTick) {
// See https://github.com/nspcc-dev/frostfs-contract/issues/225
const methodUpdateStateNotary = "updateStateIR"
if np.notaryDisabled {
prm := netmapclient.UpdatePeerPrm{}
prm.SetKey(key.Bytes())
prm.SetHash(ev.TxHash())
err = np.netmapClient.UpdatePeerState(prm)
} else {
err = np.netmapClient.MorphNotaryInvoke(
np.netmapClient.ContractAddress(),
0,
uint32(ev.epoch),
nil,
methodUpdateStateNotary,
int64(v2netmap.Offline), key.Bytes(),
)
}
err = np.netmapClient.MorphNotaryInvoke(
np.netmapClient.ContractAddress(),
0,
uint32(ev.epoch),
nil,
methodUpdateStateNotary,
int64(v2netmap.Offline), key.Bytes(),
)
if err != nil {
np.log.Error(logs.NetmapCantInvokeNetmapUpdateState, zap.Error(err))
}

View file

@ -19,16 +19,14 @@ func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
}
// check if notary transaction is valid, see #976
if originalRequest := ev.NotaryRequest(); originalRequest != nil {
tx := originalRequest.MainTransaction
ok, err := np.netmapClient.MorphIsValidScript(tx.Script, tx.Signers)
if err != nil || !ok {
np.log.Warn(logs.NetmapNonhaltNotaryTransaction,
zap.String("method", "netmap.AddPeer"),
zap.String("hash", tx.Hash().StringLE()),
zap.Error(err))
return
}
tx := ev.NotaryRequest().MainTransaction
ok, err := np.netmapClient.MorphIsValidScript(tx.Script, tx.Signers)
if err != nil || !ok {
np.log.Warn(logs.NetmapNonhaltNotaryTransaction,
zap.String("method", "netmap.AddPeer"),
zap.String("hash", tx.Hash().StringLE()),
zap.Error(err))
return
}
// unmarshal node info
@ -40,7 +38,7 @@ func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
}
// validate and update node info
err := np.nodeValidator.VerifyAndUpdate(&nodeInfo)
err = np.nodeValidator.VerifyAndUpdate(&nodeInfo)
if err != nil {
np.log.Warn(logs.NetmapCouldNotVerifyAndUpdateInformationAboutNetworkMapCandidate,
zap.String("error", err.Error()),
@ -71,20 +69,15 @@ func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
// See https://github.com/nspcc-dev/frostfs-contract/issues/154.
const methodAddPeerNotary = "addPeerIR"
if nr := ev.NotaryRequest(); nr != nil {
// create new notary request with the original nonce
err = np.netmapClient.MorphNotaryInvoke(
np.netmapClient.ContractAddress(),
0,
nr.MainTransaction.Nonce,
nil,
methodAddPeerNotary,
nodeInfoBinary,
)
} else {
// notification event case
err = np.netmapClient.AddPeer(prm)
}
// create new notary request with the original nonce
err = np.netmapClient.MorphNotaryInvoke(
np.netmapClient.ContractAddress(),
0,
ev.NotaryRequest().MainTransaction.Nonce,
nil,
methodAddPeerNotary,
nodeInfoBinary,
)
if err != nil {
np.log.Error(logs.NetmapCantInvokeNetmapAddPeer, zap.Error(err))
@ -116,23 +109,7 @@ func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) {
}
}
if nr := ev.NotaryRequest(); nr != nil {
err = np.netmapClient.MorphNotarySignAndInvokeTX(nr.MainTransaction)
} else {
prm := netmapclient.UpdatePeerPrm{}
switch {
case ev.Online():
prm.SetOnline()
case ev.Maintenance():
prm.SetMaintenance()
}
prm.SetKey(ev.PublicKey().Bytes())
err = np.netmapClient.UpdatePeerState(prm)
}
if err != nil {
if err = np.netmapClient.MorphNotarySignAndInvokeTX(ev.NotaryRequest().MainTransaction); err != nil {
np.log.Error(logs.NetmapCantInvokeNetmapUpdatePeer, zap.Error(err))
}
}

View file

@ -7,7 +7,6 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/netmap/nodevalidation/state"
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"
@ -55,7 +54,6 @@ type (
}
Client interface {
UpdatePeerState(p netmapclient.UpdatePeerPrm) error
MorphNotaryInvoke(contract util.Uint160, fee fixedn.Fixed8, nonce uint32, vub *uint32, method string, args ...any) error
ContractAddress() util.Uint160
EpochDuration() (uint64, error)
@ -63,7 +61,6 @@ type (
NetMap() (*netmap.NetMap, error)
NewEpoch(epoch uint64, force bool) error
MorphIsValidScript(script []byte, signers []transaction.Signer) (valid bool, err error)
AddPeer(p netmapclient.AddPeerPrm) error
MorphNotarySignAndInvokeTX(mainTx *transaction.Transaction) error
}
@ -90,8 +87,6 @@ type (
nodeValidator NodeValidator
notaryDisabled bool
nodeStateSettings state.NetworkSettings
}
@ -112,8 +107,6 @@ type (
NodeValidator NodeValidator
NotaryDisabled bool
NodeStateSettings state.NetworkSettings
}
)
@ -168,8 +161,6 @@ func New(p *Params) (*Processor, error) {
nodeValidator: p.NodeValidator,
notaryDisabled: p.NotaryDisabled,
nodeStateSettings: p.NodeStateSettings,
}, nil
}