Skip to content

Commit

Permalink
netmap: Do not allow double new epoch subscribing
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Nov 27, 2023
1 parent 57acf79 commit 66cffb4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
7 changes: 6 additions & 1 deletion netmap/netmap_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,13 @@ func SubscribeForNewEpoch(contract interop.Hash160) {

ctx := storage.GetContext()
var num byte
it := storage.Find(ctx, []byte(newEpochSubscribersPrefix), storage.None)
it := storage.Find(ctx, []byte(newEpochSubscribersPrefix), storage.KeysOnly|storage.RemovePrefix)
for iterator.Next(it) {
raw := iterator.Value(it).([]byte)[1:] // 1 byte is an index
if contract.Equals(raw) {
return
}

num += 1
}

Expand Down
35 changes: 33 additions & 2 deletions tests/netmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func TestSubscribeForNewEpoch(t *testing.T) {
ctrNetmap := neotest.CompileFile(t, e.CommitteeHash, netmapPath, path.Join(netmapPath, "config.yml"))
ctrBalance := neotest.CompileFile(t, e.CommitteeHash, balancePath, path.Join(balancePath, "config.yml"))
ctrContainer := neotest.CompileFile(t, e.CommitteeHash, containerPath, path.Join(containerPath, "config.yml"))
netmapInvoker := e.CommitteeInvoker(ctrNetmap.Hash)

nnsInvoker := deployNNSWithTLDs(t, e, "neofs")
deployNetmapContract(t, e, nnsInvoker.Hash)
Expand All @@ -185,8 +186,38 @@ func TestSubscribeForNewEpoch(t *testing.T) {
deployContainerContract(t, e, ctrNetmap.Hash, ctrBalance.Hash, nnsInvoker.Hash)
deployBalanceContract(t, e, ctrNetmap.Hash, ctrContainer.Hash)

netmapInvoker := e.CommitteeInvoker(ctrNetmap.Hash)
netmapInvoker.Invoke(t, stackitem.Null{}, "newEpoch", 1) // no panic so registrations and calls are OK
t.Run("new epoch", func(t *testing.T) {
netmapInvoker.Invoke(t, stackitem.Null{}, "newEpoch", 1) // no panic so registrations and calls are OK
})

t.Run("double subscription", func(t *testing.T) {
netmapInvoker.Invoke(t, stackitem.Null{}, "subscribeForNewEpoch", ctrBalance.Hash)
netmapInvoker.Invoke(t, stackitem.Null{}, "subscribeForNewEpoch", ctrBalance.Hash)

containerID := netmapInvoker.Executor.Chain.GetContractState(netmapInvoker.Hash).ID

const subscribersPrefix = "e"
var foundThirdSubscriber bool
var balanceSubscribers int
var containerSubscribers int

netmapInvoker.Chain.SeekStorage(containerID, append([]byte(subscribersPrefix), 0), func(k, v []byte) bool {
balanceSubscribers++
return false
})
netmapInvoker.Chain.SeekStorage(containerID, append([]byte(subscribersPrefix), 1), func(k, v []byte) bool {
containerSubscribers++
return false
})
netmapInvoker.Chain.SeekStorage(containerID, append([]byte(subscribersPrefix), 2), func(k, v []byte) bool {
foundThirdSubscriber = true
return false
})

require.Equal(t, 1, balanceSubscribers)
require.Equal(t, 1, containerSubscribers)
require.False(t, foundThirdSubscriber)
})
}

func TestUpdateSnapshotCount(t *testing.T) {
Expand Down

0 comments on commit 66cffb4

Please sign in to comment.