Skip to content

Commit ab03adc

Browse files
Improve Trusted Nodes Handling; Auto-populate from DNS Static Nodes
1 parent c2d0f2f commit ab03adc

File tree

10 files changed

+302
-83
lines changed

10 files changed

+302
-83
lines changed

cmd/bootnode/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ func main() {
163163
BLSKey: privKey,
164164
BootNodes: nil, // Boot nodes have no boot nodes :) Will be connected when other nodes joined
165165
TrustedNodes: nil,
166+
TrustedMinPeers: 0,
167+
TrustedBootstrapEnabled: false,
168+
DNSStaticNodes: nil,
166169
DataStoreFile: &dataStorePath,
167170
MaxConnPerIP: *maxConnPerIP,
168171
ForceReachabilityPublic: *forceReachabilityPublic,

cmd/config/config.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ func GetDefaultLocalnetConfig() harmonyconfig.LocalnetConfig {
113113
func GetDefaultNetworkConfig(nt nodeconfig.NetworkType) harmonyconfig.NetworkConfig {
114114
bn := nodeconfig.GetDefaultBootNodes(nt)
115115
return harmonyconfig.NetworkConfig{
116-
NetworkType: string(nt),
117-
BootNodes: bn,
118-
TrustedNodes: []string{},
116+
NetworkType: string(nt),
117+
BootNodes: bn,
119118
}
120119
}
121120

@@ -275,6 +274,14 @@ func loadHarmonyConfig(file string) (harmonyconfig.HarmonyConfig, string, error)
275274
return harmonyconfig.HarmonyConfig{}, "", err
276275
}
277276

277+
// Normalize nil slices to empty slices for consistency
278+
if config.Sync.TrustedNodes == nil {
279+
config.Sync.TrustedNodes = []string{}
280+
}
281+
if config.Sync.DNSStaticNodes == nil {
282+
config.Sync.DNSStaticNodes = []string{}
283+
}
284+
278285
return config, migratedVer, nil
279286
}
280287

cmd/config/config_migrations.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func init() {
449449

450450
migrations["2.6.2"] = func(confTree *toml.Tree) *toml.Tree {
451451
if confTree.Get("Network.TrustedNodes") == nil {
452-
confTree.Set("Network.TrustedNodes", defaultConfig.Network.TrustedNodes)
452+
confTree.Set("Network.TrustedNodes", defaultConfig.Sync.TrustedNodes)
453453
}
454454
// upgrade minor version because of `Cache` network introduction
455455
confTree.Set("Version", "2.6.3")
@@ -505,6 +505,23 @@ func init() {
505505
return confTree
506506
}
507507

508+
migrations["2.6.6"] = func(confTree *toml.Tree) *toml.Tree {
509+
// Rename Downloader to Client in Sync configs
510+
if trustedNodes := confTree.Get("Network.TrustedNodes"); trustedNodes != nil {
511+
// If old Downloader field exists, copy its value to Client
512+
confTree.Set("Sync.TrustedNodes", trustedNodes)
513+
confTree.Delete("Network.TrustedNodes")
514+
} else if confTree.Get("Sync.TrustedNodes") == nil {
515+
// If neither field exists, set default
516+
confTree.Set("Sync.TrustedNodes", defaultConfig.Sync.TrustedNodes)
517+
}
518+
if confTree.Get("Sync.DNSStaticNodes") == nil {
519+
confTree.Set("Sync.DNSStaticNodes", defaultConfig.Sync.DNSStaticNodes)
520+
}
521+
confTree.Set("Version", "2.6.7")
522+
return confTree
523+
}
524+
508525
// check that the latest version here is the same as in default.go
509526
largestKey := getNextVersion(migrations)
510527
if largestKey != tomlConfigVersion {

cmd/config/default.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
1010
)
1111

12-
const tomlConfigVersion = "2.6.6"
12+
const tomlConfigVersion = "2.6.7"
1313

1414
const (
1515
defNetworkType = nodeconfig.Mainnet
@@ -219,6 +219,8 @@ var (
219219
DiscHardLowCap: 6,
220220
DiscHighCap: 128,
221221
DiscBatch: 8,
222+
TrustedNodes: []string{},
223+
DNSStaticNodes: []string{},
222224
}
223225

224226
defaultTestNetSyncConfig = harmonyconfig.SyncConfig{
@@ -234,6 +236,8 @@ var (
234236
DiscHardLowCap: 3,
235237
DiscHighCap: 1024,
236238
DiscBatch: 3,
239+
TrustedNodes: []string{},
240+
DNSStaticNodes: []string{},
237241
}
238242

239243
defaultLocalNetSyncConfig = harmonyconfig.SyncConfig{
@@ -249,6 +253,8 @@ var (
249253
DiscHardLowCap: 4,
250254
DiscHighCap: 1024,
251255
DiscBatch: 8,
256+
TrustedNodes: []string{},
257+
DNSStaticNodes: []string{},
252258
}
253259

254260
defaultPartnerSyncConfig = harmonyconfig.SyncConfig{
@@ -264,6 +270,8 @@ var (
264270
DiscHardLowCap: 3,
265271
DiscHighCap: 1024,
266272
DiscBatch: 5,
273+
TrustedNodes: []string{},
274+
DNSStaticNodes: []string{},
267275
}
268276

269277
defaultElseSyncConfig = harmonyconfig.SyncConfig{
@@ -279,6 +287,8 @@ var (
279287
DiscHardLowCap: 4,
280288
DiscHighCap: 1024,
281289
DiscBatch: 8,
290+
TrustedNodes: []string{},
291+
DNSStaticNodes: []string{},
282292
}
283293
)
284294

cmd/config/flags_test.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func TestHarmonyFlags(t *testing.T) {
5252
"/ip4/13.113.101.219/tcp/12019/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX",
5353
"/ip4/99.81.170.167/tcp/12019/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj",
5454
},
55-
TrustedNodes: []string{},
5655
},
5756
Localnet: harmonyconfig.LocalnetConfig{
5857
BlocksPerEpoch: 64,
@@ -370,19 +369,17 @@ func TestNetworkFlags(t *testing.T) {
370369
args: []string{},
371370
expConfig: harmonyconfig.HarmonyConfig{
372371
Network: harmonyconfig.NetworkConfig{
373-
NetworkType: defNetworkType,
374-
BootNodes: nodeconfig.GetDefaultBootNodes(defNetworkType),
375-
TrustedNodes: []string{},
372+
NetworkType: defNetworkType,
373+
BootNodes: nodeconfig.GetDefaultBootNodes(defNetworkType),
376374
},
377375
DNSSync: GetDefaultDNSSyncConfig(defNetworkType)},
378376
},
379377
{
380378
args: []string{"-n", "stn"},
381379
expConfig: harmonyconfig.HarmonyConfig{
382380
Network: harmonyconfig.NetworkConfig{
383-
NetworkType: nodeconfig.Stressnet,
384-
BootNodes: nodeconfig.GetDefaultBootNodes(nodeconfig.Stressnet),
385-
TrustedNodes: []string{},
381+
NetworkType: nodeconfig.Stressnet,
382+
BootNodes: nodeconfig.GetDefaultBootNodes(nodeconfig.Stressnet),
386383
},
387384
DNSSync: GetDefaultDNSSyncConfig(nodeconfig.Stressnet),
388385
},
@@ -392,9 +389,8 @@ func TestNetworkFlags(t *testing.T) {
392389
"--dns.port", "9001", "--dns.server-port", "9002"},
393390
expConfig: harmonyconfig.HarmonyConfig{
394391
Network: harmonyconfig.NetworkConfig{
395-
NetworkType: "pangaea",
396-
BootNodes: []string{"1", "2", "3", "4"},
397-
TrustedNodes: []string{},
392+
NetworkType: "pangaea",
393+
BootNodes: []string{"1", "2", "3", "4"},
398394
},
399395
DNSSync: harmonyconfig.DnsSync{
400396
Port: 9001,
@@ -409,9 +405,8 @@ func TestNetworkFlags(t *testing.T) {
409405
"--dns_port", "9001"},
410406
expConfig: harmonyconfig.HarmonyConfig{
411407
Network: harmonyconfig.NetworkConfig{
412-
NetworkType: "pangaea",
413-
BootNodes: []string{"1", "2", "3", "4"},
414-
TrustedNodes: []string{},
408+
NetworkType: "pangaea",
409+
BootNodes: []string{"1", "2", "3", "4"},
415410
},
416411
DNSSync: harmonyconfig.DnsSync{
417412
Port: 9001,

cmd/harmony/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,22 @@ func createGlobalConfig(hc harmonyconfig.HarmonyConfig) (*nodeconfig.ConfigType,
590590
forceReachabilityPublic = true
591591
}
592592

593+
// Disable trusted bootstrap if both TrustedNodes and DNSStaticNodes are empty or nil
594+
trustedBootstrapEnabled := hc.Sync.Client
595+
trustedNodesEmpty := hc.Sync.TrustedNodes == nil || len(hc.Sync.TrustedNodes) == 0
596+
dnsStaticNodesEmpty := hc.Sync.DNSStaticNodes == nil || len(hc.Sync.DNSStaticNodes) == 0
597+
if trustedNodesEmpty && dnsStaticNodesEmpty {
598+
trustedBootstrapEnabled = false
599+
}
600+
593601
myHost, err = p2p.NewHost(p2p.HostConfig{
594602
Self: &selfPeer,
595603
BLSKey: nodeConfig.P2PPriKey,
596604
BootNodes: hc.Network.BootNodes,
597-
TrustedNodes: hc.Network.TrustedNodes,
605+
TrustedNodes: hc.Sync.TrustedNodes,
606+
TrustedMinPeers: hc.Sync.MinPeers,
607+
TrustedBootstrapEnabled: trustedBootstrapEnabled,
608+
DNSStaticNodes: hc.Sync.DNSStaticNodes,
598609
DataStoreFile: hc.P2P.DHTDataStore,
599610
DiscConcurrency: hc.P2P.DiscConcurrency,
600611
MaxConnPerIP: hc.P2P.MaxConnsPerIP,

internal/configs/harmony/harmony.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ type DnsSync struct {
109109
}
110110

111111
type NetworkConfig struct {
112-
NetworkType string
113-
BootNodes []string
114-
TrustedNodes []string
112+
NetworkType string
113+
BootNodes []string
115114
}
116115

117116
type P2pConfig struct {
@@ -369,6 +368,8 @@ type SyncConfig struct {
369368
Enabled bool // enable the stream sync protocol
370369
SyncMode uint32 // sync mode (default:Full sync, 1: Fast Sync, 2: Snap Sync(not implemented yet))
371370
Client bool // start the sync downloader client
371+
TrustedNodes []string `toml:",omitempty"` // trusted nodes for the sync client
372+
DNSStaticNodes []string `toml:",omitempty"` // DNS static nodes dnsaddr records (e.g., "/dnsaddr/_dnsaddr.trusted.s0.ps.hmny.io")
372373
StagedSyncCfg StagedSyncConfig // staged sync configurations
373374
Concurrency int // concurrency used for stream sync protocol
374375
MinPeers int // minimum streams to start a sync task.

node/harmony/node_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ func TestTrustedNodes(t *testing.T) {
9292
trustedNodes := []string{addr1, addr2, addr3}
9393
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
9494
host, err := p2p.NewHost(p2p.HostConfig{
95-
Self: leader,
96-
BLSKey: priKey,
97-
TrustedNodes: trustedNodes,
95+
Self: leader,
96+
BLSKey: priKey,
97+
TrustedNodes: trustedNodes,
98+
DNSStaticNodes: nil,
99+
TrustedBootstrapEnabled: true,
98100
})
99101
if err != nil {
100102
t.Fatalf("newhost failure: %v", err)

0 commit comments

Comments
 (0)