-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer.go
104 lines (84 loc) · 2.64 KB
/
peer.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
package peer
import (
"crypto/ecdsa"
"fmt"
"github.com/multiformats/go-multiaddr"
"github.com/statechannels/go-nitro-testground/config"
p2pms "github.com/statechannels/go-nitro/client/engine/messageservice/p2p-message-service"
)
// START_PORT is the start of the port range we'll use to issue unique ports.
const START_PORT = 49000
type Role = uint
const (
Hub Role = iota
Payer
Payee
PayerPayee
)
// PeerInfo represents a peer testground instance.
// It contains information about the peers address and role that instance is playing.
type PeerInfo struct {
p2pms.PeerInfo
Role Role
Seq int64
}
// IsPayer returns true if the peer's role is a Payer or PayeePayer
func (p PeerInfo) IsPayer() bool {
return p.Role == Payer || p.Role == PayerPayee
}
// IsPayee returns true if the peer's role is a Payee or PayeePayer
func (p PeerInfo) IsPayee() bool {
return p.Role == Payee || p.Role == PayerPayee
}
// MultiAddress returns the multiaddress of the peer based on their port and Id
func (p PeerInfo) MultiAddress() multiaddr.Multiaddr {
a, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", p.IpAddress, p.Port, p.Id))
if err != nil {
panic(err)
}
return a
}
// MyInfo contains an instance's private information.
type MyInfo struct {
PeerInfo
PrivateKey ecdsa.PrivateKey
}
// IsGraphRecorder returns true if an instance is responsible for recording the graph to file.
// We use the first payee instance as they are not too busy.
func IsGraphRecorder(seq int64, c config.RunConfig) bool {
return int64(c.NumHubs+c.NumPayers+c.NumPayees) == seq
}
// GetRole determines the role an instance will play based on the run config.
func GetRole(seq int64, c config.RunConfig) Role {
switch {
case seq <= int64(c.NumHubs):
return Hub
case seq <= int64(c.NumHubs+c.NumPayers):
return Payer
case seq <= int64(c.NumHubs+c.NumPayers+c.NumPayees):
return Payee
case seq <= int64(c.NumHubs+c.NumPayers+c.NumPayees+c.NumPayeePayers):
return PayerPayee
default:
panic("sequence number is larger than the amount of roles we expect")
}
}
// FilterByRole filters a slice of PeerInfos by the given role.
// It returns a slice containing peers with the given role.
func FilterByRole(peers []PeerInfo, role Role) []PeerInfo {
filtered := []PeerInfo{}
for _, peer := range peers {
if peer.Role == role {
filtered = append(filtered, peer)
}
}
return filtered
}
// GetMessageServicePeers takes in our PeerInfos and returns a slice of p2pms.PeerInfos
func GetMessageServicePeers(peers []PeerInfo) []p2pms.PeerInfo {
peerInfos := []p2pms.PeerInfo{}
for _, p := range peers {
peerInfos = append(peerInfos, p.PeerInfo)
}
return peerInfos
}