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

Feat: intermediary commit for the tangle.Approvers

parent 65a00c4e
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,7 @@ var ownId *identity.Identity ...@@ -11,7 +11,7 @@ var ownId *identity.Identity
var lazyInit sync.Once var lazyInit sync.Once
func GetOwnId() *identity.Identity { func OwnId() *identity.Identity {
lazyInit.Do(initOwnId) lazyInit.Do(initOwnId)
return ownId return ownId
......
...@@ -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.GetOwnId().Identifier) eventDispatchers.AddNode(accountability.OwnId().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.GetOwnId().Identifier) eventDispatchers.ConnectNodes(p.Identity.Identifier, accountability.OwnId().Identifier)
}) })
onRemoveAcceptedNeighbor := events.NewClosure(func(p *peer.Peer) { onRemoveAcceptedNeighbor := events.NewClosure(func(p *peer.Peer) {
eventDispatchers.DisconnectNodes(p.Identity.Identifier, accountability.GetOwnId().Identifier) eventDispatchers.DisconnectNodes(p.Identity.Identifier, accountability.OwnId().Identifier)
}) })
onAddChosenNeighbor := events.NewClosure(func(p *peer.Peer) { onAddChosenNeighbor := events.NewClosure(func(p *peer.Peer) {
eventDispatchers.ConnectNodes(accountability.GetOwnId().Identifier, p.Identity.Identifier) eventDispatchers.ConnectNodes(accountability.OwnId().Identifier, p.Identity.Identifier)
}) })
onRemoveChosenNeighbor := events.NewClosure(func(p *peer.Peer) { onRemoveChosenNeighbor := events.NewClosure(func(p *peer.Peer) {
eventDispatchers.DisconnectNodes(accountability.GetOwnId().Identifier, p.Identity.Identifier) eventDispatchers.DisconnectNodes(accountability.OwnId().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.GetOwnId().Identifier, chosenNeighbor.Identity.Identifier) dispatchers.ConnectNodes(accountability.OwnId().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.GetOwnId(), Identity: accountability.OwnId(),
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.GetOwnId().StringIdentifier { if randomNeighborHoodPeer.Identity.StringIdentifier != accountability.OwnId().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.GetOwnId().StringIdentifier != nodeId) && ( accountability.OwnId().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.GetOwnId().Identifier) { if peer.Identity == nil || bytes.Equal(peer.Identity.Identifier, accountability.OwnId().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.GetOwnId().StringIdentifier <= neighbor.Identity.StringIdentifier { if accountability.OwnId().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.GetOwnId()); err != nil { if err := protocol.Send(accountability.OwnId()); 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.GetOwnId().StringIdentifier { if protocol.Neighbor.Identity.StringIdentifier <= accountability.OwnId().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.GetOwnId().StringIdentifier) fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", accountability.OwnId().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)
......
package tangle
import (
"github.com/iotaledger/goshimmer/packages/ternary"
"sync"
)
type Approvers struct {
hash ternary.Trinary
hashes map[ternary.Trinary]bool
hashesMutex sync.RWMutex
modified bool
}
func NewApprovers(hash ternary.Trinary) *Approvers {
return &Approvers{
hash: hash,
hashes: make(map[ternary.Trinary]bool),
modified: false,
}
}
// region public method with locking ///////////////////////////////////////////////////////////////////////////////////
func (approvers *Approvers) Add(transactionHash ternary.Trinary) {
approvers.hashesMutex.Lock()
defer approvers.hashesMutex.Unlock()
approvers.add(transactionHash)
}
func (approvers *Approvers) Remove(approverHash ternary.Trinary) {
approvers.hashesMutex.Lock()
defer approvers.hashesMutex.Unlock()
approvers.remove(approverHash)
}
func (approvers *Approvers) GetHashes() []ternary.Trinary {
approvers.hashesMutex.RLock()
defer approvers.hashesMutex.RUnlock()
return approvers.getHashes()
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region private methods without locking //////////////////////////////////////////////////////////////////////////////
func (approvers *Approvers) add(transactionHash ternary.Trinary) {
if _, exists := approvers.hashes[transactionHash]; !exists {
approvers.hashes[transactionHash] = true
approvers.modified = true
}
}
func (approvers *Approvers) remove(approverHash ternary.Trinary) {
if _, exists := approvers.hashes[approverHash]; exists {
delete(approvers.hashes, approverHash)
approvers.modified = true
}
}
func (approvers *Approvers) getHashes() []ternary.Trinary {
hashes := make([]ternary.Trinary, len(approvers.hashes))
counter := 0
for hash, _ := range approvers.hashes {
hashes[counter] = hash
counter++
}
return hashes
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
func (approvers *Approvers) Store(approverHash ternary.Trinary) {
approvers.hashesMutex.Lock()
approvers.hashesMutex.RUnlock()
approvers.modified = false
}
func (approvers *Approvers) Marshal() []byte {
approvers.hashesMutex.RLock()
defer approvers.hashesMutex.RUnlock()
return make([]byte, 0)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment