From 8547c8c7b8fe6d3b832863bf89c11b1ea4dce04a Mon Sep 17 00:00:00 2001 From: Konstantin <355847+Frozen@users.noreply.github.com> Date: Tue, 13 Dec 2022 00:36:59 +0700 Subject: [PATCH] Removed legacy syncing peer provider. (#4260) * Removed legacy syncing peer provider. * Fix localnet. * Fix migrate version. * Rebased on main. * Fix formatting. --- cmd/harmony/config.go | 7 +- cmd/harmony/config_migrations.go | 6 ++ cmd/harmony/default.go | 2 +- cmd/harmony/flags.go | 4 - cmd/harmony/flags_test.go | 35 ++------- cmd/harmony/main.go | 2 - internal/configs/harmony/harmony.go | 11 ++- node/node.go | 34 +------- node/node_syncing.go | 30 ------- node/node_test.go | 118 ---------------------------- rosetta/infra/harmony-mainnet.conf | 3 +- rosetta/infra/harmony-pstn.conf | 3 +- 12 files changed, 28 insertions(+), 227 deletions(-) diff --git a/cmd/harmony/config.go b/cmd/harmony/config.go index 7299a74a9..dea5fb2e3 100644 --- a/cmd/harmony/config.go +++ b/cmd/harmony/config.go @@ -81,10 +81,9 @@ func getDefaultDNSSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.DnsSync { zone := nodeconfig.GetDefaultDNSZone(nt) port := nodeconfig.GetDefaultDNSPort(nt) dnsSync := harmonyconfig.DnsSync{ - Port: port, - Zone: zone, - LegacySyncing: false, - ServerPort: nodeconfig.DefaultDNSPort, + Port: port, + Zone: zone, + ServerPort: nodeconfig.DefaultDNSPort, } switch nt { case nodeconfig.Mainnet: diff --git a/cmd/harmony/config_migrations.go b/cmd/harmony/config_migrations.go index f8e7e6111..813c9045e 100644 --- a/cmd/harmony/config_migrations.go +++ b/cmd/harmony/config_migrations.go @@ -288,6 +288,12 @@ func init() { confTree.Set("Version", "2.5.7") return confTree } + migrations["2.5.7"] = func(confTree *toml.Tree) *toml.Tree { + confTree.Delete("DNSSync.LegacySyncing") + + confTree.Set("Version", "2.5.8") + return confTree + } // check that the latest version here is the same as in default.go largestKey := getNextVersion(migrations) diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index 58db92594..64c60365d 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -5,7 +5,7 @@ import ( nodeconfig "github.com/harmony-one/harmony/internal/configs/node" ) -const tomlConfigVersion = "2.5.7" +const tomlConfigVersion = "2.5.8" const ( defNetworkType = nodeconfig.Mainnet diff --git a/cmd/harmony/flags.go b/cmd/harmony/flags.go index 43c0b6103..02e8989da 100644 --- a/cmd/harmony/flags.go +++ b/cmd/harmony/flags.go @@ -497,10 +497,6 @@ func applyDNSSyncFlags(cmd *cobra.Command, cfg *harmonyconfig.HarmonyConfig) { } else if cli.IsFlagChanged(cmd, legacyDNSZoneFlag) { cfg.DNSSync.Zone = cli.GetStringFlagValue(cmd, legacyDNSZoneFlag) } else if cli.IsFlagChanged(cmd, legacyDNSFlag) { - val := cli.GetBoolFlagValue(cmd, legacyDNSFlag) - if !val { - cfg.DNSSync.LegacySyncing = true - } } if cli.IsFlagChanged(cmd, dnsPortFlag) { diff --git a/cmd/harmony/flags_test.go b/cmd/harmony/flags_test.go index 59fa817d4..2f69ccdc2 100644 --- a/cmd/harmony/flags_test.go +++ b/cmd/harmony/flags_test.go @@ -295,11 +295,10 @@ func TestNetworkFlags(t *testing.T) { BootNodes: []string{"1", "2", "3", "4"}, }, DNSSync: harmonyconfig.DnsSync{ - Port: 9001, - Zone: "8.8.8.8", - LegacySyncing: false, - Server: true, - ServerPort: 9002, + Port: 9001, + Zone: "8.8.8.8", + Server: true, + ServerPort: 9002, }, }, }, @@ -312,28 +311,10 @@ func TestNetworkFlags(t *testing.T) { BootNodes: []string{"1", "2", "3", "4"}, }, DNSSync: harmonyconfig.DnsSync{ - Port: 9001, - Zone: "8.8.8.8", - LegacySyncing: false, - Server: true, - ServerPort: nodeconfig.GetDefaultDNSPort(nodeconfig.Pangaea), - }, - }, - }, - { - args: []string{"--dns=false"}, - expConfig: harmonyconfig.HarmonyConfig{ - Network: harmonyconfig.NetworkConfig{ - NetworkType: defNetworkType, - BootNodes: nodeconfig.GetDefaultBootNodes(defNetworkType), - }, - DNSSync: harmonyconfig.DnsSync{ - Port: nodeconfig.GetDefaultDNSPort(defNetworkType), - Zone: nodeconfig.GetDefaultDNSZone(defNetworkType), - LegacySyncing: true, - Client: true, - Server: true, - ServerPort: nodeconfig.GetDefaultDNSPort(nodeconfig.Pangaea), + Port: 9001, + Zone: "8.8.8.8", + Server: true, + ServerPort: nodeconfig.GetDefaultDNSPort(nodeconfig.Pangaea), }, }, }, diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 279e6c396..c4508c965 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -730,8 +730,6 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi selfPort := hc.P2P.Port currentNode.SyncingPeerProvider = node.NewLocalSyncingPeerProvider( 6000, uint16(selfPort), epochConfig.NumShards(), uint32(epochConfig.NumNodesPerShard())) - } else if hc.DNSSync.LegacySyncing { - currentNode.SyncingPeerProvider = node.NewLegacySyncingPeerProvider(currentNode) } else { currentNode.SyncingPeerProvider = node.NewDNSSyncingPeerProvider(hc.DNSSync.Zone, strconv.Itoa(hc.DNSSync.Port)) } diff --git a/internal/configs/harmony/harmony.go b/internal/configs/harmony/harmony.go index c611e9422..da37011e0 100644 --- a/internal/configs/harmony/harmony.go +++ b/internal/configs/harmony/harmony.go @@ -33,12 +33,11 @@ type HarmonyConfig struct { } type DnsSync struct { - Port int // replaces: Network.DNSSyncPort - Zone string // replaces: Network.DNSZone - LegacySyncing bool // replaces: Network.LegacySyncing - Client bool // replaces: Sync.LegacyClient - Server bool // replaces: Sync.LegacyServer - ServerPort int + Port int // replaces: Network.DNSSyncPort + Zone string // replaces: Network.DNSZone + Client bool // replaces: Sync.LegacyClient + Server bool // replaces: Sync.LegacyServer + ServerPort int } type NetworkConfig struct { diff --git a/node/node.go b/node/node.go index 8b0c8a835..d78c225ec 100644 --- a/node/node.go +++ b/node/node.go @@ -91,13 +91,9 @@ type Node struct { pendingCXMutex sync.Mutex crosslinks *crosslinks.Crosslinks // Memory storage for crosslink processing. // Shard databases - shardChains shardchain.Collection - SelfPeer p2p.Peer - // TODO: Neighbors should store only neighbor nodes in the same shard - Neighbors sync.Map // All the neighbor nodes, key is the sha256 of Peer IP/Port, value is the p2p.Peer - stateMutex sync.Mutex // mutex for change node state - // BeaconNeighbors store only neighbor nodes in the beacon chain shard - BeaconNeighbors sync.Map // All the neighbor nodes, key is the sha256 of Peer IP/Port, value is the p2p.Peer + shardChains shardchain.Collection + SelfPeer p2p.Peer + stateMutex sync.Mutex // mutex for change node state TxPool *core.TxPool CxPool *core.CxPool // pool for missing cross shard receipts resend Worker *worker.Worker @@ -1228,30 +1224,6 @@ func (node *Node) InitConsensusWithValidators() (err error) { return nil } -// AddPeers adds neighbors nodes -func (node *Node) AddPeers(peers []*p2p.Peer) int { - for _, p := range peers { - key := fmt.Sprintf("%s:%s:%s", p.IP, p.Port, p.PeerID) - _, ok := node.Neighbors.LoadOrStore(key, *p) - if !ok { - // !ok means new peer is stored - node.host.AddPeer(p) - continue - } - } - - return node.host.GetPeerCount() -} - -// AddBeaconPeer adds beacon chain neighbors nodes -// Return false means new neighbor peer was added -// Return true means redundant neighbor peer wasn't added -func (node *Node) AddBeaconPeer(p *p2p.Peer) bool { - key := fmt.Sprintf("%s:%s:%s", p.IP, p.Port, p.PeerID) - _, ok := node.BeaconNeighbors.LoadOrStore(key, *p) - return ok -} - func (node *Node) initNodeConfiguration() (service.NodeConfig, chan p2p.Peer, error) { chanPeer := make(chan p2p.Peer) nodeConfig := service.NodeConfig{ diff --git a/node/node_syncing.go b/node/node_syncing.go index 58186dffa..f10128385 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -115,36 +115,6 @@ type SyncingPeerProvider interface { SyncingPeers(shardID uint32) (peers []p2p.Peer, err error) } -// LegacySyncingPeerProvider uses neighbor lists stored in a Node to serve -// syncing peer list query. -type LegacySyncingPeerProvider struct { - node *Node - shardID func() uint32 -} - -// NewLegacySyncingPeerProvider creates and returns a new node-based syncing -// peer provider. -func NewLegacySyncingPeerProvider(node *Node) *LegacySyncingPeerProvider { - var shardID func() uint32 - if node.shardChains != nil { - shardID = node.Blockchain().ShardID - } - return &LegacySyncingPeerProvider{node: node, shardID: shardID} -} - -// SyncingPeers returns peers stored in neighbor maps in the node structure. -func (p *LegacySyncingPeerProvider) SyncingPeers(shardID uint32) (peers []p2p.Peer, err error) { - switch shardID { - case p.shardID(): - peers = getNeighborPeers(&p.node.Neighbors) - case 0: - peers = getNeighborPeers(&p.node.BeaconNeighbors) - default: - return nil, errors.Errorf("unsupported shard ID %v", shardID) - } - return peers, nil -} - // DNSSyncingPeerProvider uses the given DNS zone to resolve syncing peers. type DNSSyncingPeerProvider struct { zone, port string diff --git a/node/node_test.go b/node/node_test.go index c2a972257..bbcef4e2c 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -2,7 +2,6 @@ package node import ( "errors" - "sync" "testing" "github.com/harmony-one/harmony/consensus" @@ -61,59 +60,6 @@ func TestNewNode(t *testing.T) { } } -func TestLegacySyncingPeerProvider(t *testing.T) { - t.Run("ShardChain", func(t *testing.T) { - p := makeLegacySyncingPeerProvider() - expectedPeers := []p2p.Peer{ - {IP: "127.0.0.1", Port: "6001"}, - {IP: "127.0.0.1", Port: "6003"}, - } - actualPeers, err := p.SyncingPeers(1) - if assert.NoError(t, err) { - assert.ElementsMatch(t, actualPeers, expectedPeers) - } - }) - t.Run("BeaconChain", func(t *testing.T) { - p := makeLegacySyncingPeerProvider() - expectedPeers := []p2p.Peer{ - {IP: "127.0.0.1", Port: "6000"}, - {IP: "127.0.0.1", Port: "6002"}, - } - actualPeers, err := p.SyncingPeers(0) - if assert.NoError(t, err) { - assert.ElementsMatch(t, actualPeers, expectedPeers) - } - }) - t.Run("NoMatch", func(t *testing.T) { - p := makeLegacySyncingPeerProvider() - _, err := p.SyncingPeers(999) - assert.Error(t, err) - }) -} - -func makeLegacySyncingPeerProvider() *LegacySyncingPeerProvider { - node := makeSyncOnlyNode() - p := NewLegacySyncingPeerProvider(node) - p.shardID = func() uint32 { return 1 } - return p -} - -func makeSyncOnlyNode() *Node { - node := &Node{ - Neighbors: sync.Map{}, - BeaconNeighbors: sync.Map{}, - } - node.Neighbors.Store( - "127.0.0.1:9001:omg", p2p.Peer{IP: "127.0.0.1", Port: "9001"}) - node.Neighbors.Store( - "127.0.0.1:9003:wtf", p2p.Peer{IP: "127.0.0.1", Port: "9003"}) - node.BeaconNeighbors.Store( - "127.0.0.1:9000:bbq", p2p.Peer{IP: "127.0.0.1", Port: "9000"}) - node.BeaconNeighbors.Store( - "127.0.0.1:9002:cakes", p2p.Peer{IP: "127.0.0.1", Port: "9002"}) - return node -} - func TestDNSSyncingPeerProvider(t *testing.T) { t.Run("Happy", func(t *testing.T) { p := NewDNSSyncingPeerProvider("example.com", "1234") @@ -181,67 +127,3 @@ func TestLocalSyncingPeerProvider(t *testing.T) { func makeLocalSyncingPeerProvider() *LocalSyncingPeerProvider { return NewLocalSyncingPeerProvider(6000, 6001, 2, 3) } - -func TestAddBeaconPeer(t *testing.T) { - pubKey1 := bls.RandPrivateKey().GetPublicKey() - pubKey2 := bls.RandPrivateKey().GetPublicKey() - - peers1 := []*p2p.Peer{ - { - IP: "127.0.0.1", - Port: "8888", - ConsensusPubKey: pubKey1, - PeerID: "1234", - }, - { - IP: "127.0.0.1", - Port: "9999", - ConsensusPubKey: pubKey2, - PeerID: "4567", - }, - } - blsKey := bls.RandPrivateKey() - pubKey := blsKey.GetPublicKey() - leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey} - priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") - host, err := p2p.NewHost(p2p.HostConfig{ - Self: &leader, - BLSKey: priKey, - }) - if err != nil { - t.Fatalf("newhost failure: %v", err) - } - engine := chain.NewEngine() - chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig() - collection := shardchain.NewCollection( - nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, engine, &chainconfig, - ) - blockchain, err := collection.ShardChain(shard.BeaconChainShardID) - if err != nil { - t.Fatalf("Cannot craeate consensus: %v", err) - } - decider := quorum.NewDecider( - quorum.SuperMajorityVote, shard.BeaconChainShardID, - ) - - consensus, err := consensus.New( - host, shard.BeaconChainShardID, multibls.GetPrivateKeys(blsKey), blockchain, decider, 3, false, - ) - - archiveMode := make(map[uint32]bool) - archiveMode[0] = true - archiveMode[1] = false - node := New(host, consensus, engine, collection, nil, nil, nil, archiveMode, nil) - for _, p := range peers1 { - ret := node.AddBeaconPeer(p) - if ret { - t.Errorf("AddBeaconPeer Failed, expecting false, got %v, peer %v", ret, p) - } - } - for _, p := range peers1 { - ret := node.AddBeaconPeer(p) - if !ret { - t.Errorf("AddBeaconPeer Failed, expecting true, got %v, peer %v", ret, p) - } - } -} diff --git a/rosetta/infra/harmony-mainnet.conf b/rosetta/infra/harmony-mainnet.conf index cdbce636a..b2000fd4d 100644 --- a/rosetta/infra/harmony-mainnet.conf +++ b/rosetta/infra/harmony-mainnet.conf @@ -1,4 +1,4 @@ -Version = "2.5.7" +Version = "2.5.8" [BLSKeys] KMSConfigFile = "" @@ -18,7 +18,6 @@ Version = "2.5.7" [DNSSync] Client = true - LegacySyncing = false Port = 6000 Server = true ServerPort = 6000 diff --git a/rosetta/infra/harmony-pstn.conf b/rosetta/infra/harmony-pstn.conf index 2e22b491a..18d083052 100644 --- a/rosetta/infra/harmony-pstn.conf +++ b/rosetta/infra/harmony-pstn.conf @@ -1,4 +1,4 @@ -Version = "2.5.7" +Version = "2.5.8" [BLSKeys] KMSConfigFile = "" @@ -18,7 +18,6 @@ Version = "2.5.7" [DNSSync] Client = true - LegacySyncing = false Port = 6000 Server = true ServerPort = 6000