Skip to content
Snippets Groups Projects
Commit 65a00c4e authored by Hans Moog's avatar Hans Moog
Browse files

Feat: settings and accountability get lazy initialized

parent 4316a398
No related branches found
No related tags found
No related merge requests found
...@@ -4,9 +4,22 @@ import ( ...@@ -4,9 +4,22 @@ import (
"github.com/dgraph-io/badger" "github.com/dgraph-io/badger"
"github.com/iotaledger/goshimmer/packages/settings" "github.com/iotaledger/goshimmer/packages/settings"
"github.com/iotaledger/goshimmer/packages/identity" "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 { func generateNewIdentity() *identity.Identity {
newIdentity := identity.GenerateRandomIdentity() newIdentity := identity.GenerateRandomIdentity()
......
package settings package settings
import "github.com/iotaledger/goshimmer/packages/database" import (
"github.com/iotaledger/goshimmer/packages/database"
"sync"
)
var settingsDatabase database.Database var settingsDatabase database.Database
func init() { var lazyInit sync.Once
if db, err := database.Get("settings"); err != nil {
panic(err)
} else {
settingsDatabase = db
}
}
func Get(key []byte) ([]byte, error) { func Get(key []byte) ([]byte, error) {
return settingsDatabase.Get(key) lazyInit.Do(initDb)
return settingsDatabase.Get(key)
} }
func Set(key []byte, value []byte) error { func Set(key []byte, value []byte) error {
return settingsDatabase.Set(key, value) lazyInit.Do(initDb)
}
\ No newline at end of file return settingsDatabase.Set(key, value)
}
func initDb() {
if db, err := database.Get("settings"); err != nil {
panic(err)
} else {
settingsDatabase = db
}
}
...@@ -62,7 +62,7 @@ func getEventDispatchers(conn *network.ManagedConnection) *EventDispatchers { ...@@ -62,7 +62,7 @@ func getEventDispatchers(conn *network.ManagedConnection) *EventDispatchers {
} }
func reportCurrentStatus(eventDispatchers *EventDispatchers) { func reportCurrentStatus(eventDispatchers *EventDispatchers) {
eventDispatchers.AddNode(accountability.OWN_ID.Identifier) eventDispatchers.AddNode(accountability.GetOwnId().Identifier)
reportChosenNeighbors(eventDispatchers) reportChosenNeighbors(eventDispatchers)
} }
...@@ -75,19 +75,19 @@ func setupHooks(conn *network.ManagedConnection, eventDispatchers *EventDispatch ...@@ -75,19 +75,19 @@ func setupHooks(conn *network.ManagedConnection, eventDispatchers *EventDispatch
}) })
onAddAcceptedNeighbor := events.NewClosure(func(p *peer.Peer) { 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) { 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) { 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) { 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 ///////////////////////////////////////////////////////////////////////////////////////////////////// // setup hooks /////////////////////////////////////////////////////////////////////////////////////////////////////
...@@ -118,7 +118,7 @@ func reportChosenNeighbors(dispatchers *EventDispatchers) { ...@@ -118,7 +118,7 @@ func reportChosenNeighbors(dispatchers *EventDispatchers) {
dispatchers.AddNode(chosenNeighbor.Identity.Identifier) dispatchers.AddNode(chosenNeighbor.Identity.Identifier)
} }
for _, chosenNeighbor := range chosenneighbors.INSTANCE.Peers { for _, chosenNeighbor := range chosenneighbors.INSTANCE.Peers {
dispatchers.ConnectNodes(accountability.OWN_ID.Identifier, chosenNeighbor.Identity.Identifier) dispatchers.ConnectNodes(accountability.GetOwnId().Identifier, chosenNeighbor.Identity.Identifier)
} }
} }
......
...@@ -14,7 +14,7 @@ var INSTANCE *peer.Peer ...@@ -14,7 +14,7 @@ var INSTANCE *peer.Peer
func Configure(plugin *node.Plugin) { func Configure(plugin *node.Plugin) {
INSTANCE = &peer.Peer{ INSTANCE = &peer.Peer{
Identity: accountability.OWN_ID, Identity: accountability.GetOwnId(),
PeeringPort: uint16(*parameters.PORT.Value), PeeringPort: uint16(*parameters.PORT.Value),
GossipPort: uint16(*gossip.PORT.Value), GossipPort: uint16(*gossip.PORT.Value),
Address: net.IPv4(0, 0, 0, 0), Address: net.IPv4(0, 0, 0, 0),
......
...@@ -64,7 +64,7 @@ func pingPeers(plugin *node.Plugin, outgoingPing *ping.Ping) { ...@@ -64,7 +64,7 @@ func pingPeers(plugin *node.Plugin, outgoingPing *ping.Ping) {
for i := 0; i < constants.PING_CONTACT_COUNT_PER_CYCLE; i++ { for i := 0; i < constants.PING_CONTACT_COUNT_PER_CYCLE; i++ {
randomNeighborHoodPeer := neighborhood.LIST_INSTANCE[rand.Intn(len(neighborhood.LIST_INSTANCE))] 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 chosenPeers[randomNeighborHoodPeer.Identity.StringIdentifier] = randomNeighborHoodPeer
} }
} }
......
...@@ -60,7 +60,7 @@ func candidateShouldBeContacted(candidate *peer.Peer) bool { ...@@ -60,7 +60,7 @@ func candidateShouldBeContacted(candidate *peer.Peer) bool {
nodeId := candidate.Identity.StringIdentifier nodeId := candidate.Identity.StringIdentifier
return (!acceptedneighbors.INSTANCE.Contains(nodeId) &&!chosenneighbors.INSTANCE.Contains(nodeId) && 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 || len(chosenneighbors.INSTANCE.Peers) < constants.NEIGHBOR_COUNT / 2 ||
chosenneighbors.OWN_DISTANCE(candidate) < chosenneighbors.FURTHEST_NEIGHBOR_DISTANCE) chosenneighbors.OWN_DISTANCE(candidate) < chosenneighbors.FURTHEST_NEIGHBOR_DISTANCE)
} }
...@@ -33,7 +33,7 @@ func (this *PeerRegister) AddOrUpdate(peer *peer.Peer, lock... bool) bool { ...@@ -33,7 +33,7 @@ func (this *PeerRegister) AddOrUpdate(peer *peer.Peer, lock... bool) bool {
defer this.Lock()() 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 return false
} }
......
...@@ -156,7 +156,7 @@ func (neighbor *Neighbor) Connect() (*protocol, bool, errors.IdentifiableError) ...@@ -156,7 +156,7 @@ func (neighbor *Neighbor) Connect() (*protocol, bool, errors.IdentifiableError)
// drop the "secondary" connection upon successful handshake // drop the "secondary" connection upon successful handshake
neighbor.InitiatedProtocol.Events.HandshakeCompleted.Attach(events.NewClosure(func() { 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() neighbor.acceptedProtocolMutex.Lock()
var acceptedProtocolConn *network.ManagedConnection var acceptedProtocolConn *network.ManagedConnection
if neighbor.AcceptedProtocol != nil { if neighbor.AcceptedProtocol != nil {
......
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
// region protocolV1 /////////////////////////////////////////////////////////////////////////////////////////////////// // region protocolV1 ///////////////////////////////////////////////////////////////////////////////////////////////////
func protocolV1(protocol *protocol) errors.IdentifiableError { 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 return err
} }
......
...@@ -43,7 +43,7 @@ func configureServer(plugin *node.Plugin) { ...@@ -43,7 +43,7 @@ func configureServer(plugin *node.Plugin) {
// drop the "secondary" connection upon successful handshake // drop the "secondary" connection upon successful handshake
protocol.Events.HandshakeCompleted.Attach(events.NewClosure(func() { 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() protocol.Neighbor.initiatedProtocolMutex.Lock()
var initiatedProtocolConn *network.ManagedConnection var initiatedProtocolConn *network.ManagedConnection
if protocol.Neighbor.InitiatedProtocol != nil { if protocol.Neighbor.InitiatedProtocol != nil {
......
...@@ -65,7 +65,7 @@ func (headerBar *UIHeaderBar) Update() { ...@@ -65,7 +65,7 @@ func (headerBar *UIHeaderBar) Update() {
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintln(headerBar.InfoContainer) 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.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.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) fmt.Fprintln(headerBar.InfoContainer)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment