forked from joltgeorge/tss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer_status.go
117 lines (103 loc) · 2.75 KB
/
peer_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package p2p
import (
"errors"
"sync"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/joltify-finance/tss/messages"
)
type PeerStatus struct {
peersResponse map[peer.ID]bool
peerStatusLock *sync.RWMutex
notify chan bool
newFound chan bool
leaderResponse *messages.JoinPartyLeaderComm
leaderResponseLock *sync.RWMutex
leader string
leaderSetLock *sync.RWMutex
threshold int
reqCount int
streams *sync.Map
}
func (ps *PeerStatus) getLeaderResponse() *messages.JoinPartyLeaderComm {
ps.leaderResponseLock.RLock()
defer ps.leaderResponseLock.RUnlock()
return ps.leaderResponse
}
func (ps *PeerStatus) setLeaderResponse(resp *messages.JoinPartyLeaderComm) {
ps.leaderResponseLock.Lock()
defer ps.leaderResponseLock.Unlock()
ps.leaderResponse = resp
}
func NewPeerStatus(peerNodes []peer.ID, myPeerID peer.ID, leader string, threshold int) *PeerStatus {
dat := make(map[peer.ID]bool)
for _, el := range peerNodes {
if el == myPeerID {
continue
}
dat[el] = false
}
peerStatus := &PeerStatus{
peersResponse: dat,
peerStatusLock: &sync.RWMutex{},
notify: make(chan bool, len(peerNodes)),
newFound: make(chan bool, len(peerNodes)),
leader: leader,
threshold: threshold,
reqCount: 0,
leaderResponseLock: &sync.RWMutex{},
streams: &sync.Map{},
leaderSetLock: &sync.RWMutex{},
}
return peerStatus
}
func (ps *PeerStatus) getCoordinationStatus() bool {
_, offline := ps.getPeersStatus()
return len(offline) == 0
}
func (ps *PeerStatus) getPeersStatus() ([]peer.ID, []peer.ID) {
var online []peer.ID
var offline []peer.ID
ps.peerStatusLock.RLock()
defer ps.peerStatusLock.RUnlock()
for peerNode, val := range ps.peersResponse {
if val {
online = append(online, peerNode)
} else {
offline = append(offline, peerNode)
}
}
return online, offline
}
func (ps *PeerStatus) updatePeer(peerNode peer.ID, stream network.Stream) (bool, error) {
ps.peerStatusLock.Lock()
defer ps.peerStatusLock.Unlock()
val, ok := ps.peersResponse[peerNode]
if !ok {
return false, errors.New("key not found")
}
ps.leaderSetLock.RLock()
leader := ps.leader
ps.leaderSetLock.RUnlock()
if leader == "NONE" {
if !val {
ps.peersResponse[peerNode] = true
return true, nil
}
return false, nil
}
// we already have enough participants
if ps.reqCount >= ps.threshold {
return false, nil
}
if !val {
ps.peersResponse[peerNode] = true
// we store the stream for the peer to send the response back to peers
ps.streams.Store(peerNode, stream)
ps.reqCount++
if ps.reqCount >= ps.threshold {
return true, nil
}
}
return false, nil
}