From ffd5750c1108b0731105617a8e57cc7d791baa39 Mon Sep 17 00:00:00 2001 From: Sergey Bushnyak Date: Thu, 8 Apr 2021 17:11:01 +0300 Subject: [PATCH 1/3] add peer discovery stub --- internal/engine/peers.go | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 internal/engine/peers.go diff --git a/internal/engine/peers.go b/internal/engine/peers.go new file mode 100644 index 0000000..d7364bd --- /dev/null +++ b/internal/engine/peers.go @@ -0,0 +1,51 @@ +// MIT License +// +// Copyright 2020-2021 Kompendium, LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +package engine + +import ( + "context" + "fmt" + "os" + "runtime" + "strings" + "sync" + "time" + + "github.com/Factom-Asset-Tokens/factom" + "github.com/Factom-Asset-Tokens/fatd/internal/flag" + _log "github.com/Factom-Asset-Tokens/fatd/internal/log" + "golang.org/x/sync/errgroup" +) + +var log _log.Log + +// Peer represents a connected remote node. +type Peer struct { + addr uint32 + version string +} + +// Get list of all peers available on the Network +func GetPeers() []Peer { + // do network discovery +} \ No newline at end of file From 9f2e895be50f1f8fcfe96f1bcc32f717a644beb3 Mon Sep 17 00:00:00 2001 From: Sergey Bushnyak Date: Thu, 8 Apr 2021 18:04:12 +0300 Subject: [PATCH 2/3] remove unused --- internal/engine/peers.go | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/internal/engine/peers.go b/internal/engine/peers.go index d7364bd..6ed469f 100644 --- a/internal/engine/peers.go +++ b/internal/engine/peers.go @@ -22,30 +22,14 @@ package engine -import ( - "context" - "fmt" - "os" - "runtime" - "strings" - "sync" - "time" - - "github.com/Factom-Asset-Tokens/factom" - "github.com/Factom-Asset-Tokens/fatd/internal/flag" - _log "github.com/Factom-Asset-Tokens/fatd/internal/log" - "golang.org/x/sync/errgroup" -) - -var log _log.Log - // Peer represents a connected remote node. type Peer struct { - addr uint32 + addr uint32 version string } // Get list of all peers available on the Network func GetPeers() []Peer { // do network discovery -} \ No newline at end of file + return []Peer{} +} From 3c2879f1a74d7bbfd1b1d083667d607704b2b295 Mon Sep 17 00:00:00 2001 From: Sergey Bushnyak Date: Thu, 8 Apr 2021 20:03:43 +0300 Subject: [PATCH 3/3] add peer store; management functions --- internal/engine/peers.go | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/internal/engine/peers.go b/internal/engine/peers.go index 6ed469f..f60dcf8 100644 --- a/internal/engine/peers.go +++ b/internal/engine/peers.go @@ -22,14 +22,60 @@ package engine +import ( + "fmt" + "sync" +) + // Peer represents a connected remote node. type Peer struct { addr uint32 + hash string version string } +// PeerStore holds active Peers, managing them in a concurrency safe +// manner and providing lookup via various functions +type PeerStore struct { + mtx sync.RWMutex + peers map[string]*Peer // hash -> peer + connected map[string]int // (ip|ip:port) -> count + current []*Peer + incoming int + outgoing int +} + +// NewPeerStore initializes a new peer store +func NewPeerStore() *PeerStore { + ps := new(PeerStore) + ps.peers = make(map[string]*Peer) + ps.connected = make(map[string]int) + return ps +} + +// Add a peer to be managed. Returns an error if a peer with that hash +// is already tracked +func (ps *PeerStore) Add(p *Peer) error { + if p == nil { + return fmt.Errorf("trying to add nil") + } + ps.mtx.Lock() + defer ps.mtx.Unlock() + + if _, ok := ps.peers[p.hash]; ok { + return fmt.Errorf("peer already exists") + } + ps.current = nil + ps.peers[p.hash] = p + ps.connected[fmt.Sprint(p.addr)]++ + ps.connected[p.version]++ + + return nil +} + // Get list of all peers available on the Network func GetPeers() []Peer { // do network discovery + return []Peer{} }