Skip to content

Commit

Permalink
generalized all lists and set types
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Nguyen authored and iajrz committed Jan 22, 2019
1 parent 55bc4ef commit aa23c1d
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 76 deletions.
14 changes: 7 additions & 7 deletions dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ func Find(sessionKey string) []node.Node {
bigSessionKey := new(big.Int)
bigSessionKey.SetString(sessionKey, 16)
peerList := node.GetPeerList()
peerList.Lock() // TODO currently locking the peerlist, however this will all change when p2p is integerated
defer peerList.Unlock()
peerList.Mux.Lock() // TODO currently locking the peerlist, however this will all change when p2p is integerated
defer peerList.Mux.Unlock()
// map the nodes to the corresponding difference
m := make(map[uint64]node.Node)
// store the keys (to easily sort)
keys := make([]uint64, len(peerList.Map))
keys := make([]uint64, len(peerList.M))
// resulting array that holds the sorted nodes ordered by difference
sortedNodes := make([]node.Node, len(peerList.Map))
sortedNodes := make([]node.Node, len(peerList.M))
var i = 0
for gid, n := range peerList.Map {
for gid, n := range peerList.M {
// setup a new big integer to hold the converted ID
id := new(big.Int)
// convert the hex GID into a bigInteger for comparison
id.SetString(gid, 16)
id.SetString(gid.(string), 16)
difference := big.NewInt(0).Sub(bigSessionKey, id)
// take absolute of the difference for comparison
difference.Abs(difference)
m[difference.Uint64()] = n
m[difference.Uint64()] = n.(node.Node)
keys[i] = difference.Uint64()
i++
}
Expand Down
2 changes: 1 addition & 1 deletion message/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ func NewMessage(pLoad Payload) Message {
}

// "NewSessionMessage" creates a new message with a payload that can derive a session
func NewSessionMessage(devID string, peers []session.SessionPeer) Message {
func NewSessionMessage(devID string, peers []session.Peer) Message {
return NewMessage(Payload{ID: 1, Data: SessionPL{devID, peers}})
}
16 changes: 7 additions & 9 deletions message/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package message

import (
"net"
"sync"

"github.com/pokt-network/pocket-core/session"
)
Expand All @@ -17,16 +16,15 @@ func HandleMSG(message *Message, addr *net.UDPAddr) {

// "sessionMSG" handles an incoming message by deriving a new session from the payload.
func sessionMSG(message *Message) {
sList := session.GetSessionList()
sl := session.GetSessionList()
// extract the SessionPL
nSPL := message.Payload.Data.(SessionPL)
spl := message.Payload.Data.(SessionPL)
// create a session using developerID from payload
s := session.Session{DevID: nSPL.DevID,
Peers: session.PeerList{List: make(map[string]session.SessionPeer)},
Mutex: sync.Mutex{}}
s := session.Session{DevID: spl.DevID,
Peers: session.NewPeerList()}
// TODO create new connections with each peer
s.NewPeers(nSPL.Peers)
s.AddPeers(spl.Peers)
// adds new session to sessionList and adds peers to the peerList
sList.AddSession(s)
session.AddSPeers(nSPL.Peers)
sl.Add(s)
session.AddPeer(spl.Peers)
}
4 changes: 2 additions & 2 deletions message/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ type Message struct {
}

type SessionPL struct {
DevID string `json:"devid"` // the devID of the session
Peers []session.SessionPeer `json:"peers"` // the list of peers
DevID string `json:"devid"` // the devID of the session
Peers []session.Peer `json:"peers"` // the list of peers
}
12 changes: 6 additions & 6 deletions node/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (dp DispatchPeers) GetPeers(bc Blockchain) map[string]Node {
return getPeers(dp, bc)
}

// "Delete" deletes a peer from DispatchPeers.
// "Remove" deletes a peer from DispatchPeers.
func (dp *DispatchPeers) Delete(n Node) {
dp.Lock()
defer dp.Unlock()
Expand Down Expand Up @@ -87,12 +87,12 @@ func (dp *DispatchPeers) Print() {
// "Check" checks each service node's liveness.
func (dp *DispatchPeers) Check() {
pl := GetPeerList()
for _, p := range pl.Map {
if !isAlive(p) {
for _, p := range pl.M {
if !isAlive(p.(Node)) {
// try again
if !isAlive(p) {
pl.Remove(p)
dp.Delete(p)
if !isAlive(p.(Node)) {
pl.Remove(p.(Node))
dp.Delete(p.(Node))
}
}
}
Expand Down
82 changes: 82 additions & 0 deletions node/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package node

import (
"encoding/json"
"io/ioutil"
"sync"

"github.com/pokt-network/pocket-core/types"
)

type List types.List

var (
o sync.Once
pl *List
)

// "GetPeerList" returns the global map of nodes.
func GetPeerList() *List {
o.Do(func() {
pl = (*List)(types.NewList())
})
return pl
}

// "Add" adds a peer object to the global map.
func (pl *List) Add(node Node) {
(*types.List)(pl).Add(node.GID, node)
}

// "Remove" removes a peer object from the global map.
func (pl *List) Remove(node Node) {
(*types.List)(pl).Remove(node.GID)
}

// "Contains" returns true if node is within peerlist.
func (pl *List) Contains(gid string) bool {
return (*types.List)(pl).Contains(gid)
}

// "Count" returns the count of peers within the map.
func (pl *List) Count() int {
return (*types.List)(pl).Count()
}

// "Print" prints the peerlist to the CLI
func (pl *List) Print() {
(*types.List)(pl).Print()
}

// "ManualPeersFile" adds Map from a peers.json to the peerlist
func ManualPeersFile(filepath string) error {
file, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
return manualPeersJSON(file)
}

// "manualPeersJSON" adds Map from a json []byte to the peerlist
func manualPeersJSON(b []byte) error {
var nSlice []Node
if err := json.Unmarshal(b, &nSlice); err != nil {
return err
}
for _, n := range nSlice {
pList := GetPeerList()
pList.Add(n)
}
return nil
}

// DISCLAIMER: the code below is for pocket core mvp centralized dispatcher
// may remove for production

func (pl *List) CopyToDP() {
pl.Mux.Lock()
defer pl.Mux.Unlock()
for _, peer := range pl.M {
GetDispatchPeers().Add(peer.(Node))
}
}
2 changes: 1 addition & 1 deletion node/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Register() {
if _, err := util.RPCRequest("http://"+_const.DISPATCHIP+":"+_const.DISPATCHCLIENTPORT+"/v1/register", GetSelf(), util.POST); err != nil {
util.ExitGracefully("Error, unable to register node at Pocket Incorporated's Dispatcher")
}
fmt.Println("Node registered successfully " , GetPeerList().Map)
fmt.Println("Node registered successfully " , GetPeerList().M)
}

func UnRegister(count int) {
Expand Down
65 changes: 28 additions & 37 deletions node/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,70 @@ import (
"fmt"
"io/ioutil"
"sync"

"github.com/pokt-network/pocket-core/config"
"github.com/pokt-network/pocket-core/types"
)

type Whitelist struct {
Map map[string]struct{}
mux sync.Mutex
}
type Whitelist types.Set

var (
SNWL Whitelist
DWL Whitelist
SNWL *Whitelist
DWL *Whitelist
wlOnce sync.Once
)

// "WhiteListInit()" initializes both whitelist structures.
func WhiteListInit() {
wlOnce.Do(func() {
SNWL.Map = make(map[string]struct{})
DWL.Map = make(map[string]struct{})
SNWL = (*Whitelist)(types.NewSet())
DWL = (*Whitelist)(types.NewSet())
})
}

// "GetSWL" returns service node white list.
func GetSWL() Whitelist {
if SNWL.Map == nil { // just in case
func GetSWL() *Whitelist {
if SNWL == nil { // just in case
WhiteListInit()
}
return SNWL
}

// "GetDWL" returns developer.
func GetDWL() Whitelist {
if DWL.Map == nil { // just in case
// "GetDWL" returns developer white list.
func GetDWL() *Whitelist {
if DWL == nil { // just in case
WhiteListInit()
}
return DWL
}

// "Contains" returns if within whitelist.
func (w Whitelist) Contains(s string) bool {
w.mux.Lock()
defer w.mux.Unlock()
_, ok := w.Map[s]
return ok
func (w *Whitelist) Contains(s string) bool {
return (*types.Set)(w).Contains(s)
}

// "Delete" removes item from whitelist.
func (w Whitelist) Delete(s string) {
w.mux.Lock()
defer w.mux.Unlock()
delete(w.Map, s)
// "Remove" removes item from whitelist.
func (w *Whitelist) Remove(s string) {
(*types.Set)(w).Remove(s)
}

// "Add" appends item to whitelist.
func (w Whitelist) Add(s string) {
w.mux.Lock()
defer w.mux.Unlock()
w.Map[s] = struct{}{}
func (w *Whitelist) Add(s string) {
(*types.Set)(w).Add(s)
}

// "AddMulti" appends multiple items to whitelist
func (w Whitelist) AddMulti(list []string) {
func (w *Whitelist) AddMulti(list []string) {
w.Mux.Lock()
defer w.Mux.Unlock()
for _, v := range list {
w.Add(v)
w.M[v] = struct{}{}
}
}

// "Size" returns the length of the whitelist.
func (w Whitelist) Size() int {
w.mux.Lock()
defer w.mux.Unlock()
return len(w.Map)
// "Count" returns the length of the whitelist.
func (w *Whitelist) Count() int {
return (*types.Set)(w).Count()
}

// "SWLFile" builds the service white list from a file.
Expand All @@ -91,7 +82,7 @@ func DWLFile() error {
}

// "wlFile" builds a whitelist structure from a file.
func (w Whitelist) wlFile(filePath string) error {
func (w *Whitelist) wlFile(filePath string) error {
f, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println(err.Error())
Expand All @@ -109,7 +100,7 @@ func (w Whitelist) wlFile(filePath string) error {
}

// "EnsureWL" cross checks the whitelist for
func EnsureWL(whiteList Whitelist, query string) bool {
func EnsureWL(whiteList *Whitelist, query string) bool {
if !whiteList.Contains(query) {
fmt.Println("Node: ", query, "rejected because it is not within whitelist")
return false
Expand Down
10 changes: 5 additions & 5 deletions session/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
type List types.List

var (
sesList *List
sListO sync.Once
sessionList *List
o sync.Once
)

func GetSessionList() *List {
sListO.Do(func() {
sesList = (*List)(types.NewList())
o.Do(func() {
sessionList = (*List)(types.NewList())
})
return sesList
return sessionList
}

// "AddSession" adds a session object to the global list
Expand Down
12 changes: 6 additions & 6 deletions session/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const (
DISPATCHER
)

type SessionPeer struct {
type Peer struct {
Role Role `json:"role"`
node.Node `json:"node"`
}

// "AddSPeers" adds sessionPeers from a slice to the peerlist
func AddSPeers(spl []SessionPeer) {
pl := node.GetPeerList() // get the peerlist
for _, sp := range spl { // for each SessionPeer
pl.Add(sp.Node) // add to the list
// "AddPeer" adds sessionPeers from a slice to the peerlist
func AddPeer(spl []Peer) {
pl := node.GetPeerList()
for _, sp := range spl {
pl.Add(sp.Node)
}
}
21 changes: 21 additions & 0 deletions session/peerList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package session

import "github.com/pokt-network/pocket-core/types"

type PeerList types.List

func NewPeerList() PeerList {
return *(*PeerList)(types.NewList())
}

func (pl *PeerList) Get(gid string) Peer {
return (*types.List)(pl).Get(gid).(Peer)
}

func (pl *PeerList) Set(gid string, sp Peer) {
(*types.List)(pl).Add(gid, sp)
}

func (pl *PeerList) Count() int {
return (*types.List)(pl).Count()
}
Loading

0 comments on commit aa23c1d

Please sign in to comment.