[#983] node: Configure subnets

Add `subnet` sub-section to `node` section of storage node config. Add
`entries` value which allows to enumerate subnets for entrance. Add
`exit_zero` value which allows to not enter zero subnet.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-11-28 15:06:07 +03:00 committed by LeL
parent b27c72c02a
commit 0fb838b169
6 changed files with 98 additions and 0 deletions

View file

@ -145,3 +145,30 @@ func (p PersistentStateConfig) Path() string {
return PersistentStatePathDefault
}
// SubnetConfig represents node configuration related to subnets.
type SubnetConfig config.Config
// Init initializes SubnetConfig from "subnet" sub-section of "node" section
// of the root config.
func (x *SubnetConfig) Init(root config.Config) {
*x = SubnetConfig(*root.Sub(subsection).Sub("subnet"))
}
// ExitZero returns value of "exit_zero" config parameter as bool.
// Returns false if value can not be cast.
func (x SubnetConfig) ExitZero() bool {
return config.BoolSafe((*config.Config)(&x), "exit_zero")
}
// IterateSubnets casts value of "entries" config parameter to string slice,
// iterates over all of its elements and passes them to f.
//
// Does nothing if value can not be cast to string slice.
func (x SubnetConfig) IterateSubnets(f func(string)) {
ids := config.StringSliceSafe((*config.Config)(&x), "entries")
for i := range ids {
f(ids[i])
}
}

View file

@ -35,6 +35,20 @@ func TestNodeSection(t *testing.T) {
require.Empty(t, attribute)
require.Equal(t, false, relay)
require.Equal(t, PersistentStatePathDefault, persistatePath)
var subnetCfg SubnetConfig
subnetCfg.Init(*empty)
require.False(t, subnetCfg.ExitZero())
called := false
subnetCfg.IterateSubnets(func(string) {
called = true
})
require.False(t, called)
})
const path = "../../../../config/example/node"
@ -99,6 +113,20 @@ func TestNodeSection(t *testing.T) {
address.Uint160ToString(wKey.GetScriptHash()))
require.Equal(t, "/state", persistatePath)
var subnetCfg SubnetConfig
subnetCfg.Init(*c)
require.True(t, subnetCfg.ExitZero())
var ids []string
subnetCfg.IterateSubnets(func(id string) {
ids = append(ids, id)
})
require.Equal(t, []string{"123", "456", "789"}, ids)
}
configtest.ForEachFileType(path, fileConfigTest)