Skip to content

Commit b3cff32

Browse files
committed
fixup! [GH-659] Basic protocol version change handling.
1 parent b559513 commit b3cff32

File tree

11 files changed

+49
-66
lines changed

11 files changed

+49
-66
lines changed

nil/internal/collate/syncer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *Syncer) WaitComplete() {
8888
s.waitForSync.Wait()
8989
}
9090

91-
func (s *Syncer) readLocalVersion(ctx context.Context) (*NodeVersion, error) {
91+
func (s *Syncer) getLocalVersion(ctx context.Context) (*NodeVersion, error) {
9292
protocolVersion := s.networkManager.ProtocolVersion()
9393

9494
rotx, err := s.db.CreateRoTx(ctx)
@@ -134,7 +134,7 @@ func (s *Syncer) fetchRemoteVersion(ctx context.Context) (NodeVersion, error) {
134134
return NodeVersion{protocolVersion, res}, nil
135135
}
136136
}
137-
return NodeVersion{"", common.EmptyHash}, fmt.Errorf("failed to fetch version from all peers; last error: %w", err)
137+
return NodeVersion{}, fmt.Errorf("failed to fetch version from all peers; last error: %w", err)
138138
}
139139

140140
func (s *Syncer) fetchSnapshot(ctx context.Context) error {
@@ -158,7 +158,7 @@ func (s *Syncer) Init(ctx context.Context, allowDbDrop bool) error {
158158
return nil
159159
}
160160

161-
version, err := s.readLocalVersion(ctx)
161+
version, err := s.getLocalVersion(ctx)
162162
if err != nil {
163163
return err
164164
}

nil/internal/network/config.go

+16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type Config struct {
3333
Reachability network.Reachability `yaml:"-"`
3434
}
3535

36+
type Option func(cfg *Config) error
37+
3638
func NewDefaultConfig() *Config {
3739
return &Config{
3840
KeysPath: "network-keys.yaml",
@@ -45,3 +47,17 @@ func NewDefaultConfig() *Config {
4547
func (c *Config) Enabled() bool {
4648
return c.TcpPort != 0 || c.QuicPort != 0
4749
}
50+
51+
// Apply applies the given options to the config, returning the first error
52+
// encountered (if any).
53+
func (c *Config) Apply(opts ...Option) error {
54+
for _, opt := range opts {
55+
if opt == nil {
56+
continue
57+
}
58+
if err := opt(c); err != nil {
59+
return err
60+
}
61+
}
62+
return nil
63+
}

nil/internal/network/testaide.go

-30
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,6 @@ func topLevelTestName(t *testing.T) string {
9393
return strings.Split(t.Name(), "/")[0]
9494
}
9595

96-
// ConfigOverrides is a helper structure for configuring the config in tests.
97-
//
98-
// Why was it needed at all?
99-
// To understand this, let's consider how the config is filled when the application is running.
100-
// We have several layers of overrides:
101-
// - Default values set in the code
102-
// - They are merged with the values from the yaml file (overwriting in favor of the file)
103-
// - And they can be overridden by command line options (again with overwriting)
104-
//
105-
// What do the yaml file and command line options have in common?
106-
// Both of these sources have the semantics of absence of value.
107-
// That is, we can clearly distinguish, for example, false from the absence of a value,
108-
// and if a true was set at an earlier layer and there was no override at a later layer,
109-
// we should not turn true into false.
110-
//
111-
// It is important that the semantics of the absence of a value are not represented at the level
112-
// of the config structure itself in Go, so the task of merging two configs presented in this way becomes non-trivial.
113-
// We can easily accidentally overwrite the value from the base config with the default value from the override config.
114-
//
115-
// This is exactly why a separate structure was introduced, having an explicit semantics of absence of value.
116-
type ConfigOverrides struct {
117-
ProtocolVersion *string
118-
}
119-
120-
func (c *ConfigOverrides) ApplyToConfig(cfg *Config) {
121-
if c.ProtocolVersion != nil {
122-
cfg.ProtocolVersion = *c.ProtocolVersion
123-
}
124-
}
125-
12696
func GenerateConfig(t *testing.T, port int) (*Config, AddrInfo) {
12797
t.Helper()
12898

nil/tests/archive_node/archive_node_test.go renamed to nil/tests/archive_node/suite_archive_node.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build test
2+
13
package tests
24

35
import (
@@ -16,6 +18,13 @@ var (
1618
newProtocolVersion = "new"
1719
)
1820

21+
func protocolVersionOption(protocolVersion string) network.Option {
22+
return func(c *network.Config) error {
23+
c.ProtocolVersion = protocolVersion
24+
return nil
25+
}
26+
}
27+
1928
type SuiteArchiveNode struct {
2029
tests.ShardedSuite
2130

@@ -43,12 +52,12 @@ func (s *SuiteArchiveNode) runArchiveNode(database db.DB) (*nilservice.Config, n
4352
ctx, cancel := context.WithCancel(s.Context)
4453
s.cancel = cancel
4554
return s.RunArchiveNode(&tests.ArchiveNodeConfig{
46-
Ctx: ctx,
47-
Wg: &s.wg,
48-
AllowDbDrop: true,
49-
Port: s.port + int(s.nShards),
50-
WithBootstrapPeers: s.withBootstrapPeers,
51-
NetworkConfigOverrides: network.ConfigOverrides{ProtocolVersion: &oldProtocolVersion},
55+
Ctx: ctx,
56+
Wg: &s.wg,
57+
AllowDbDrop: true,
58+
Port: s.port + int(s.nShards),
59+
WithBootstrapPeers: s.withBootstrapPeers,
60+
NetworkOptions: []network.Option{protocolVersionOption(oldProtocolVersion)},
5261
})
5362
}
5463

@@ -69,10 +78,8 @@ func (s *SuiteArchiveNode) startCluster(protocolVersion string) {
6978
NShards: s.nShards,
7079
CollatorTickPeriodMs: 200,
7180
},
72-
network.ConfigOverrides{
73-
ProtocolVersion: &protocolVersion,
74-
},
75-
s.port)
81+
s.port,
82+
protocolVersionOption(protocolVersion))
7683
}
7784

7885
func (s *SuiteArchiveNode) checkBlocksGeneration() {

nil/tests/async_await/async_await_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ func (s *SuiteAsyncAwait) SetupSuite() {
8383
ZeroState: zeroState,
8484
DisableConsensus: disableConsensus,
8585
},
86-
network.ConfigOverrides{},
8786
port)
8887

8988
_, archiveNodeAddr := s.StartArchiveNode(&tests.ArchiveNodeConfig{

nil/tests/cli/cli_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/NilFoundation/nil/nil/internal/contracts"
1515
nilcrypto "github.com/NilFoundation/nil/nil/internal/crypto"
1616
"github.com/NilFoundation/nil/nil/internal/execution"
17-
"github.com/NilFoundation/nil/nil/internal/network"
1817
"github.com/NilFoundation/nil/nil/internal/types"
1918
"github.com/NilFoundation/nil/nil/services/cliservice"
2019
"github.com/NilFoundation/nil/nil/services/cometa"
@@ -52,7 +51,6 @@ func (s *SuiteCliBase) SetupTest() {
5251
NShards: 3,
5352
CollatorTickPeriodMs: 200,
5453
},
55-
network.ConfigOverrides{},
5654
s.basePort)
5755

5856
s.DefaultClient, s.endpoint = s.StartRPCNode(tests.WithDhtBootstrapByValidators, nil)

nil/tests/cli_call/cli_call_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/NilFoundation/nil/nil/internal/contracts"
1010
nilcrypto "github.com/NilFoundation/nil/nil/internal/crypto"
1111
"github.com/NilFoundation/nil/nil/internal/execution"
12-
"github.com/NilFoundation/nil/nil/internal/network"
1312
"github.com/NilFoundation/nil/nil/internal/types"
1413
"github.com/NilFoundation/nil/nil/services/nilservice"
1514
"github.com/NilFoundation/nil/nil/tests"
@@ -50,7 +49,6 @@ func (s *SuiteCliTestCall) SetupTest() {
5049
CollatorTickPeriodMs: 200,
5150
ZeroState: zeroState,
5251
},
53-
network.ConfigOverrides{},
5452
10525)
5553

5654
s.client, s.endpoint = s.StartRPCNode(tests.WithDhtBootstrapByValidators, nil)

nil/tests/faucet/faucet_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/NilFoundation/nil/nil/common"
99
"github.com/NilFoundation/nil/nil/common/logging"
1010
"github.com/NilFoundation/nil/nil/internal/contracts"
11-
"github.com/NilFoundation/nil/nil/internal/network"
1211
"github.com/NilFoundation/nil/nil/internal/types"
1312
"github.com/NilFoundation/nil/nil/services/faucet"
1413
"github.com/NilFoundation/nil/nil/services/nilservice"
@@ -30,7 +29,6 @@ func (s *SuiteFaucet) SetupSuite() {
3029
NShards: 3,
3130
CollatorTickPeriodMs: 200,
3231
},
33-
network.ConfigOverrides{},
3432
10225)
3533

3634
s.DefaultClient, _ = s.StartRPCNode(tests.WithDhtBootstrapByValidators, nil)

nil/tests/rpc_node/rpc_node_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func (s *SuiteRpcNode) SetupTest() {
2424
NShards: nShards,
2525
RunMode: nilservice.NormalRunMode,
2626
},
27-
network.ConfigOverrides{},
2827
port)
2928

3029
_, archiveNodeAddr := s.StartArchiveNode(&tests.ArchiveNodeConfig{

nil/tests/shard/shard_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"testing"
55

6-
"github.com/NilFoundation/nil/nil/internal/network"
76
"github.com/NilFoundation/nil/nil/internal/types"
87
"github.com/NilFoundation/nil/nil/services/nilservice"
98
"github.com/NilFoundation/nil/nil/services/rpc/transport"
@@ -21,7 +20,6 @@ func (s *BasicShardSuite) SetupSuite() {
2120
NShards: 3,
2221
CollatorTickPeriodMs: 1000,
2322
},
24-
network.ConfigOverrides{},
2523
10000)
2624
}
2725

nil/tests/sharded_suite.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func createAllShardsAllValidatorsCfg(
172172

173173
func (s *ShardedSuite) start(
174174
cfg *nilservice.Config,
175-
networkConfigOverrides network.ConfigOverrides,
176175
port int,
177176
shardCfgGen func(
178177
*ShardedSuite,
@@ -181,6 +180,7 @@ func (s *ShardedSuite) start(
181180
*network.Config,
182181
map[InstanceId]*keys.ValidatorKeysManager,
183182
) *nilservice.Config,
183+
options ...network.Option,
184184
) {
185185
s.T().Helper()
186186
s.Context, s.ctxCancel = context.WithCancel(context.Background())
@@ -196,7 +196,7 @@ func (s *ShardedSuite) start(
196196
instanceCount := cfg.NShards - 1
197197
networkConfigs, p2pAddresses := network.GenerateConfigs(s.T(), instanceCount, port)
198198
for _, networkConfig := range networkConfigs {
199-
networkConfigOverrides.ApplyToConfig(networkConfig)
199+
s.Require().NoError(networkConfig.Apply(options...))
200200
}
201201
keysManagers := make(map[InstanceId]*keys.ValidatorKeysManager)
202202
s.Instances = make([]Instance, instanceCount)
@@ -255,16 +255,16 @@ func (s *ShardedSuite) start(
255255
s.waitShardsTick(cfg.NShards)
256256
}
257257

258-
func (s *ShardedSuite) Start(cfg *nilservice.Config, networkConfigOverrides network.ConfigOverrides, port int) {
258+
func (s *ShardedSuite) Start(cfg *nilservice.Config, port int, options ...network.Option) {
259259
s.T().Helper()
260260

261-
s.start(cfg, networkConfigOverrides, port, createOneShardOneValidatorCfg)
261+
s.start(cfg, port, createOneShardOneValidatorCfg, options...)
262262
}
263263

264264
func (s *ShardedSuite) StartShardAllValidators(cfg *nilservice.Config, port int) {
265265
s.T().Helper()
266266

267-
s.start(cfg, network.ConfigOverrides{}, port, createAllShardsAllValidatorsCfg)
267+
s.start(cfg, port, createAllShardsAllValidatorsCfg)
268268
}
269269

270270
func (s *ShardedSuite) connectToInstances(nm *network.Manager) {
@@ -288,13 +288,13 @@ func (s *ShardedSuite) GetNShards() uint32 {
288288
}
289289

290290
type ArchiveNodeConfig struct {
291-
Ctx context.Context
292-
Wg *sync.WaitGroup
293-
AllowDbDrop bool
294-
Port int
295-
WithBootstrapPeers bool
296-
DisableConsensus bool
297-
NetworkConfigOverrides network.ConfigOverrides
291+
Ctx context.Context
292+
Wg *sync.WaitGroup
293+
AllowDbDrop bool
294+
Port int
295+
WithBootstrapPeers bool
296+
DisableConsensus bool
297+
NetworkOptions []network.Option
298298
}
299299

300300
func (s *ShardedSuite) RunArchiveNode(params *ArchiveNodeConfig) (*nilservice.Config, network.AddrInfo, chan error) {
@@ -311,7 +311,7 @@ func (s *ShardedSuite) RunArchiveNode(params *ArchiveNodeConfig) (*nilservice.Co
311311

312312
s.Require().NotEmpty(s.Instances)
313313
netCfg, addr := network.GenerateConfig(s.T(), params.Port)
314-
params.NetworkConfigOverrides.ApplyToConfig(netCfg)
314+
s.Require().NoError(netCfg.Apply(params.NetworkOptions...))
315315
serviceName := fmt.Sprintf("archive-%d", params.Port)
316316

317317
cfg := &nilservice.Config{

0 commit comments

Comments
 (0)