From 65a00c4e4c3bb6a40d5e960d311b98ea93654b82 Mon Sep 17 00:00:00 2001 From: Hans Moog <hm@mkjc.net> Date: Mon, 3 Jun 2019 11:55:00 +0200 Subject: [PATCH] Feat: settings and accountability get lazy initialized --- packages/accountability/accountability.go | 15 ++++++++- packages/settings/settings.go | 31 ++++++++++++------- plugins/analysis/client/plugin.go | 12 +++---- .../autopeering/instances/ownpeer/instance.go | 2 +- .../protocol/outgoing_ping_processor.go | 2 +- .../protocol/outgoing_request_processor.go | 2 +- .../types/peerregister/peer_register.go | 2 +- plugins/gossip/neighbors.go | 2 +- plugins/gossip/protocol_v1.go | 2 +- plugins/gossip/server.go | 2 +- plugins/statusscreen/ui_header_bar.go | 2 +- 11 files changed, 48 insertions(+), 26 deletions(-) diff --git a/packages/accountability/accountability.go b/packages/accountability/accountability.go index 0a24e9b8..eef9bcbc 100644 --- a/packages/accountability/accountability.go +++ b/packages/accountability/accountability.go @@ -4,9 +4,22 @@ import ( "github.com/dgraph-io/badger" "github.com/iotaledger/goshimmer/packages/settings" "github.com/iotaledger/goshimmer/packages/identity" + "sync" ) -var OWN_ID = getIdentity() +var ownId *identity.Identity + +var lazyInit sync.Once + +func GetOwnId() *identity.Identity { + lazyInit.Do(initOwnId) + + return ownId +} + +func initOwnId() { + ownId = getIdentity() +} func generateNewIdentity() *identity.Identity { newIdentity := identity.GenerateRandomIdentity() diff --git a/packages/settings/settings.go b/packages/settings/settings.go index d2a0862f..b23c7dfa 100644 --- a/packages/settings/settings.go +++ b/packages/settings/settings.go @@ -1,21 +1,30 @@ package settings -import "github.com/iotaledger/goshimmer/packages/database" +import ( + "github.com/iotaledger/goshimmer/packages/database" + "sync" +) var settingsDatabase database.Database -func init() { - if db, err := database.Get("settings"); err != nil { - panic(err) - } else { - settingsDatabase = db - } -} +var lazyInit sync.Once func Get(key []byte) ([]byte, error) { - return settingsDatabase.Get(key) + lazyInit.Do(initDb) + + return settingsDatabase.Get(key) } func Set(key []byte, value []byte) error { - return settingsDatabase.Set(key, value) -} \ No newline at end of file + lazyInit.Do(initDb) + + return settingsDatabase.Set(key, value) +} + +func initDb() { + if db, err := database.Get("settings"); err != nil { + panic(err) + } else { + settingsDatabase = db + } +} diff --git a/plugins/analysis/client/plugin.go b/plugins/analysis/client/plugin.go index 663bf6cb..ae494ea8 100644 --- a/plugins/analysis/client/plugin.go +++ b/plugins/analysis/client/plugin.go @@ -62,7 +62,7 @@ func getEventDispatchers(conn *network.ManagedConnection) *EventDispatchers { } func reportCurrentStatus(eventDispatchers *EventDispatchers) { - eventDispatchers.AddNode(accountability.OWN_ID.Identifier) + eventDispatchers.AddNode(accountability.GetOwnId().Identifier) reportChosenNeighbors(eventDispatchers) } @@ -75,19 +75,19 @@ func setupHooks(conn *network.ManagedConnection, eventDispatchers *EventDispatch }) onAddAcceptedNeighbor := events.NewClosure(func(p *peer.Peer) { - eventDispatchers.ConnectNodes(p.Identity.Identifier, accountability.OWN_ID.Identifier) + eventDispatchers.ConnectNodes(p.Identity.Identifier, accountability.GetOwnId().Identifier) }) onRemoveAcceptedNeighbor := events.NewClosure(func(p *peer.Peer) { - eventDispatchers.DisconnectNodes(p.Identity.Identifier, accountability.OWN_ID.Identifier) + eventDispatchers.DisconnectNodes(p.Identity.Identifier, accountability.GetOwnId().Identifier) }) onAddChosenNeighbor := events.NewClosure(func(p *peer.Peer) { - eventDispatchers.ConnectNodes(accountability.OWN_ID.Identifier, p.Identity.Identifier) + eventDispatchers.ConnectNodes(accountability.GetOwnId().Identifier, p.Identity.Identifier) }) onRemoveChosenNeighbor := events.NewClosure(func(p *peer.Peer) { - eventDispatchers.DisconnectNodes(accountability.OWN_ID.Identifier, p.Identity.Identifier) + eventDispatchers.DisconnectNodes(accountability.GetOwnId().Identifier, p.Identity.Identifier) }) // setup hooks ///////////////////////////////////////////////////////////////////////////////////////////////////// @@ -118,7 +118,7 @@ func reportChosenNeighbors(dispatchers *EventDispatchers) { dispatchers.AddNode(chosenNeighbor.Identity.Identifier) } for _, chosenNeighbor := range chosenneighbors.INSTANCE.Peers { - dispatchers.ConnectNodes(accountability.OWN_ID.Identifier, chosenNeighbor.Identity.Identifier) + dispatchers.ConnectNodes(accountability.GetOwnId().Identifier, chosenNeighbor.Identity.Identifier) } } diff --git a/plugins/autopeering/instances/ownpeer/instance.go b/plugins/autopeering/instances/ownpeer/instance.go index 9e3c0cef..e5af993c 100644 --- a/plugins/autopeering/instances/ownpeer/instance.go +++ b/plugins/autopeering/instances/ownpeer/instance.go @@ -14,7 +14,7 @@ var INSTANCE *peer.Peer func Configure(plugin *node.Plugin) { INSTANCE = &peer.Peer{ - Identity: accountability.OWN_ID, + Identity: accountability.GetOwnId(), PeeringPort: uint16(*parameters.PORT.Value), GossipPort: uint16(*gossip.PORT.Value), Address: net.IPv4(0, 0, 0, 0), diff --git a/plugins/autopeering/protocol/outgoing_ping_processor.go b/plugins/autopeering/protocol/outgoing_ping_processor.go index e94a78cf..c10ac98e 100644 --- a/plugins/autopeering/protocol/outgoing_ping_processor.go +++ b/plugins/autopeering/protocol/outgoing_ping_processor.go @@ -64,7 +64,7 @@ func pingPeers(plugin *node.Plugin, outgoingPing *ping.Ping) { for i := 0; i < constants.PING_CONTACT_COUNT_PER_CYCLE; i++ { randomNeighborHoodPeer := neighborhood.LIST_INSTANCE[rand.Intn(len(neighborhood.LIST_INSTANCE))] - if randomNeighborHoodPeer.Identity.StringIdentifier != accountability.OWN_ID.StringIdentifier { + if randomNeighborHoodPeer.Identity.StringIdentifier != accountability.GetOwnId().StringIdentifier { chosenPeers[randomNeighborHoodPeer.Identity.StringIdentifier] = randomNeighborHoodPeer } } diff --git a/plugins/autopeering/protocol/outgoing_request_processor.go b/plugins/autopeering/protocol/outgoing_request_processor.go index 617bfaa9..75961075 100644 --- a/plugins/autopeering/protocol/outgoing_request_processor.go +++ b/plugins/autopeering/protocol/outgoing_request_processor.go @@ -60,7 +60,7 @@ func candidateShouldBeContacted(candidate *peer.Peer) bool { nodeId := candidate.Identity.StringIdentifier return (!acceptedneighbors.INSTANCE.Contains(nodeId) &&!chosenneighbors.INSTANCE.Contains(nodeId) && - accountability.OWN_ID.StringIdentifier != nodeId) && ( + accountability.GetOwnId().StringIdentifier != nodeId) && ( len(chosenneighbors.INSTANCE.Peers) < constants.NEIGHBOR_COUNT / 2 || chosenneighbors.OWN_DISTANCE(candidate) < chosenneighbors.FURTHEST_NEIGHBOR_DISTANCE) } diff --git a/plugins/autopeering/types/peerregister/peer_register.go b/plugins/autopeering/types/peerregister/peer_register.go index 6502155e..ea4221fd 100644 --- a/plugins/autopeering/types/peerregister/peer_register.go +++ b/plugins/autopeering/types/peerregister/peer_register.go @@ -33,7 +33,7 @@ func (this *PeerRegister) AddOrUpdate(peer *peer.Peer, lock... bool) bool { defer this.Lock()() } - if peer.Identity == nil || bytes.Equal(peer.Identity.Identifier, accountability.OWN_ID.Identifier) { + if peer.Identity == nil || bytes.Equal(peer.Identity.Identifier, accountability.GetOwnId().Identifier) { return false } diff --git a/plugins/gossip/neighbors.go b/plugins/gossip/neighbors.go index 47f0028c..1d4a1d31 100644 --- a/plugins/gossip/neighbors.go +++ b/plugins/gossip/neighbors.go @@ -156,7 +156,7 @@ func (neighbor *Neighbor) Connect() (*protocol, bool, errors.IdentifiableError) // drop the "secondary" connection upon successful handshake neighbor.InitiatedProtocol.Events.HandshakeCompleted.Attach(events.NewClosure(func() { - if accountability.OWN_ID.StringIdentifier <= neighbor.Identity.StringIdentifier { + if accountability.GetOwnId().StringIdentifier <= neighbor.Identity.StringIdentifier { neighbor.acceptedProtocolMutex.Lock() var acceptedProtocolConn *network.ManagedConnection if neighbor.AcceptedProtocol != nil { diff --git a/plugins/gossip/protocol_v1.go b/plugins/gossip/protocol_v1.go index 7e4bc5e4..52dce40c 100644 --- a/plugins/gossip/protocol_v1.go +++ b/plugins/gossip/protocol_v1.go @@ -15,7 +15,7 @@ import ( // region protocolV1 /////////////////////////////////////////////////////////////////////////////////////////////////// func protocolV1(protocol *protocol) errors.IdentifiableError { - if err := protocol.Send(accountability.OWN_ID); err != nil { + if err := protocol.Send(accountability.GetOwnId()); err != nil { return err } diff --git a/plugins/gossip/server.go b/plugins/gossip/server.go index 264fd0b0..3fa56b64 100644 --- a/plugins/gossip/server.go +++ b/plugins/gossip/server.go @@ -43,7 +43,7 @@ func configureServer(plugin *node.Plugin) { // drop the "secondary" connection upon successful handshake protocol.Events.HandshakeCompleted.Attach(events.NewClosure(func() { - if protocol.Neighbor.Identity.StringIdentifier <= accountability.OWN_ID.StringIdentifier { + if protocol.Neighbor.Identity.StringIdentifier <= accountability.GetOwnId().StringIdentifier { protocol.Neighbor.initiatedProtocolMutex.Lock() var initiatedProtocolConn *network.ManagedConnection if protocol.Neighbor.InitiatedProtocol != nil { diff --git a/plugins/statusscreen/ui_header_bar.go b/plugins/statusscreen/ui_header_bar.go index 60394449..33315322 100644 --- a/plugins/statusscreen/ui_header_bar.go +++ b/plugins/statusscreen/ui_header_bar.go @@ -65,7 +65,7 @@ func (headerBar *UIHeaderBar) Update() { fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer) - fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", accountability.OWN_ID.StringIdentifier) + fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", accountability.GetOwnId().StringIdentifier) fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintf(headerBar.InfoContainer, "[::b]Neighbors: [::d]%40v ", strconv.Itoa(len(chosenneighbors.INSTANCE.Peers)) + " chosen / " + strconv.Itoa(len(acceptedneighbors.INSTANCE.Peers)) + " accepted") fmt.Fprintln(headerBar.InfoContainer) -- GitLab