Skip to content
Merged
3 changes: 3 additions & 0 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ func main() {
BLSKey: privKey,
BootNodes: nil, // Boot nodes have no boot nodes :) Will be connected when other nodes joined
TrustedNodes: nil,
TrustedMinPeers: 0,
TrustedBootstrapEnabled: false,
DNSStaticNodes: nil,
DataStoreFile: &dataStorePath,
MaxConnPerIP: *maxConnPerIP,
ForceReachabilityPublic: *forceReachabilityPublic,
Expand Down
13 changes: 10 additions & 3 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ func GetDefaultLocalnetConfig() harmonyconfig.LocalnetConfig {
func GetDefaultNetworkConfig(nt nodeconfig.NetworkType) harmonyconfig.NetworkConfig {
bn := nodeconfig.GetDefaultBootNodes(nt)
return harmonyconfig.NetworkConfig{
NetworkType: string(nt),
BootNodes: bn,
TrustedNodes: []string{},
NetworkType: string(nt),
BootNodes: bn,
}
}

Expand Down Expand Up @@ -275,6 +274,14 @@ func loadHarmonyConfig(file string) (harmonyconfig.HarmonyConfig, string, error)
return harmonyconfig.HarmonyConfig{}, "", err
}

// Normalize nil slices to empty slices for consistency
if config.Sync.TrustedNodes == nil {
config.Sync.TrustedNodes = []string{}
}
if config.Sync.DNSStaticNodes == nil {
config.Sync.DNSStaticNodes = []string{}
}

return config, migratedVer, nil
}

Expand Down
19 changes: 18 additions & 1 deletion cmd/config/config_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func init() {

migrations["2.6.2"] = func(confTree *toml.Tree) *toml.Tree {
if confTree.Get("Network.TrustedNodes") == nil {
confTree.Set("Network.TrustedNodes", defaultConfig.Network.TrustedNodes)
confTree.Set("Network.TrustedNodes", defaultConfig.Sync.TrustedNodes)
}
// upgrade minor version because of `Cache` network introduction
confTree.Set("Version", "2.6.3")
Expand Down Expand Up @@ -505,6 +505,23 @@ func init() {
return confTree
}

migrations["2.6.6"] = func(confTree *toml.Tree) *toml.Tree {
// Rename Downloader to Client in Sync configs
if trustedNodes := confTree.Get("Network.TrustedNodes"); trustedNodes != nil {
// If old Downloader field exists, copy its value to Client
confTree.Set("Sync.TrustedNodes", trustedNodes)
confTree.Delete("Network.TrustedNodes")
} else if confTree.Get("Sync.TrustedNodes") == nil {
// If neither field exists, set default
confTree.Set("Sync.TrustedNodes", defaultConfig.Sync.TrustedNodes)
}
if confTree.Get("Sync.DNSStaticNodes") == nil {
confTree.Set("Sync.DNSStaticNodes", defaultConfig.Sync.DNSStaticNodes)
}
confTree.Set("Version", "2.6.7")
return confTree
}

// check that the latest version here is the same as in default.go
largestKey := getNextVersion(migrations)
if largestKey != tomlConfigVersion {
Expand Down
12 changes: 11 additions & 1 deletion cmd/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
)

const tomlConfigVersion = "2.6.6"
const tomlConfigVersion = "2.6.7"

const (
defNetworkType = nodeconfig.Mainnet
Expand Down Expand Up @@ -219,6 +219,8 @@ var (
DiscHardLowCap: 6,
DiscHighCap: 128,
DiscBatch: 8,
TrustedNodes: []string{},
DNSStaticNodes: []string{},
}

defaultTestNetSyncConfig = harmonyconfig.SyncConfig{
Expand All @@ -234,6 +236,8 @@ var (
DiscHardLowCap: 3,
DiscHighCap: 1024,
DiscBatch: 3,
TrustedNodes: []string{},
DNSStaticNodes: []string{},
}

defaultLocalNetSyncConfig = harmonyconfig.SyncConfig{
Expand All @@ -249,6 +253,8 @@ var (
DiscHardLowCap: 4,
DiscHighCap: 1024,
DiscBatch: 8,
TrustedNodes: []string{},
DNSStaticNodes: []string{},
}

defaultPartnerSyncConfig = harmonyconfig.SyncConfig{
Expand All @@ -264,6 +270,8 @@ var (
DiscHardLowCap: 3,
DiscHighCap: 1024,
DiscBatch: 5,
TrustedNodes: []string{},
DNSStaticNodes: []string{},
}

defaultElseSyncConfig = harmonyconfig.SyncConfig{
Expand All @@ -279,6 +287,8 @@ var (
DiscHardLowCap: 4,
DiscHighCap: 1024,
DiscBatch: 8,
TrustedNodes: []string{},
DNSStaticNodes: []string{},
}
)

Expand Down
21 changes: 8 additions & 13 deletions cmd/config/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestHarmonyFlags(t *testing.T) {
"/ip4/13.113.101.219/tcp/12019/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX",
"/ip4/99.81.170.167/tcp/12019/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj",
},
TrustedNodes: []string{},
},
Localnet: harmonyconfig.LocalnetConfig{
BlocksPerEpoch: 64,
Expand Down Expand Up @@ -370,19 +369,17 @@ func TestNetworkFlags(t *testing.T) {
args: []string{},
expConfig: harmonyconfig.HarmonyConfig{
Network: harmonyconfig.NetworkConfig{
NetworkType: defNetworkType,
BootNodes: nodeconfig.GetDefaultBootNodes(defNetworkType),
TrustedNodes: []string{},
NetworkType: defNetworkType,
BootNodes: nodeconfig.GetDefaultBootNodes(defNetworkType),
},
DNSSync: GetDefaultDNSSyncConfig(defNetworkType)},
},
{
args: []string{"-n", "stn"},
expConfig: harmonyconfig.HarmonyConfig{
Network: harmonyconfig.NetworkConfig{
NetworkType: nodeconfig.Stressnet,
BootNodes: nodeconfig.GetDefaultBootNodes(nodeconfig.Stressnet),
TrustedNodes: []string{},
NetworkType: nodeconfig.Stressnet,
BootNodes: nodeconfig.GetDefaultBootNodes(nodeconfig.Stressnet),
},
DNSSync: GetDefaultDNSSyncConfig(nodeconfig.Stressnet),
},
Expand All @@ -392,9 +389,8 @@ func TestNetworkFlags(t *testing.T) {
"--dns.port", "9001", "--dns.server-port", "9002"},
expConfig: harmonyconfig.HarmonyConfig{
Network: harmonyconfig.NetworkConfig{
NetworkType: "pangaea",
BootNodes: []string{"1", "2", "3", "4"},
TrustedNodes: []string{},
NetworkType: "pangaea",
BootNodes: []string{"1", "2", "3", "4"},
},
DNSSync: harmonyconfig.DnsSync{
Port: 9001,
Expand All @@ -409,9 +405,8 @@ func TestNetworkFlags(t *testing.T) {
"--dns_port", "9001"},
expConfig: harmonyconfig.HarmonyConfig{
Network: harmonyconfig.NetworkConfig{
NetworkType: "pangaea",
BootNodes: []string{"1", "2", "3", "4"},
TrustedNodes: []string{},
NetworkType: "pangaea",
BootNodes: []string{"1", "2", "3", "4"},
},
DNSSync: harmonyconfig.DnsSync{
Port: 9001,
Expand Down
13 changes: 12 additions & 1 deletion cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,22 @@ func createGlobalConfig(hc harmonyconfig.HarmonyConfig) (*nodeconfig.ConfigType,
forceReachabilityPublic = true
}

// Disable trusted bootstrap if both TrustedNodes and DNSStaticNodes are empty or nil
trustedBootstrapEnabled := hc.Sync.Client
trustedNodesEmpty := hc.Sync.TrustedNodes == nil || len(hc.Sync.TrustedNodes) == 0
dnsStaticNodesEmpty := hc.Sync.DNSStaticNodes == nil || len(hc.Sync.DNSStaticNodes) == 0
if trustedNodesEmpty && dnsStaticNodesEmpty {
trustedBootstrapEnabled = false
}

myHost, err = p2p.NewHost(p2p.HostConfig{
Self: &selfPeer,
BLSKey: nodeConfig.P2PPriKey,
BootNodes: hc.Network.BootNodes,
TrustedNodes: hc.Network.TrustedNodes,
TrustedNodes: hc.Sync.TrustedNodes,
TrustedMinPeers: hc.Sync.MinPeers,
TrustedBootstrapEnabled: trustedBootstrapEnabled,
DNSStaticNodes: hc.Sync.DNSStaticNodes,
DataStoreFile: hc.P2P.DHTDataStore,
DiscConcurrency: hc.P2P.DiscConcurrency,
MaxConnPerIP: hc.P2P.MaxConnsPerIP,
Expand Down
7 changes: 4 additions & 3 deletions internal/configs/harmony/harmony.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ type DnsSync struct {
}

type NetworkConfig struct {
NetworkType string
BootNodes []string
TrustedNodes []string
NetworkType string
BootNodes []string
}

type P2pConfig struct {
Expand Down Expand Up @@ -369,6 +368,8 @@ type SyncConfig struct {
Enabled bool // enable the stream sync protocol
SyncMode uint32 // sync mode (default:Full sync, 1: Fast Sync, 2: Snap Sync(not implemented yet))
Client bool // start the sync downloader client
TrustedNodes []string `toml:",omitempty"` // trusted nodes for the sync client
DNSStaticNodes []string `toml:",omitempty"` // DNS static nodes dnsaddr records (e.g., "/dnsaddr/_dnsaddr.trusted.s0.ps.hmny.io")
StagedSyncCfg StagedSyncConfig // staged sync configurations
Concurrency int // concurrency used for stream sync protocol
MinPeers int // minimum streams to start a sync task.
Expand Down
27 changes: 23 additions & 4 deletions node/harmony/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"testing"
"time"

ffi_bls "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/consensus"
Expand Down Expand Up @@ -92,18 +93,36 @@ func TestTrustedNodes(t *testing.T) {
trustedNodes := []string{addr1, addr2, addr3}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2p.NewHost(p2p.HostConfig{
Self: leader,
BLSKey: priKey,
TrustedNodes: trustedNodes,
Self: leader,
BLSKey: priKey,
TrustedNodes: trustedNodes,
TrustedMinPeers: len(trustedNodes),
TrustedBootstrapEnabled: true,
DNSStaticNodes: nil,
})
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
host.Start()

// Wait for trusted peers initialization to complete
// This ensures AddTrustedNodes has finished before checking peer count
timeout := 10 * time.Second
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if host.TrustedPeersInitiated() {
break
}
time.Sleep(100 * time.Millisecond)
}

if !host.TrustedPeersInitiated() {
t.Fatalf("trusted peers initialization did not complete within %v", timeout)
}

connectedPeers := host.GetPeerCount()
if connectedPeers != len(trustedNodes)+1 {
t.Fatalf("host adding trusted nodes failed, expected:%d, got:%d", len(trustedNodes), connectedPeers)
t.Fatalf("host adding trusted nodes failed, expected:%d, got:%d", len(trustedNodes)+1, connectedPeers)
}
}
func TestNewNode(t *testing.T) {
Expand Down
Loading