-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
122 lines (101 loc) · 2.58 KB
/
client.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
118
119
120
121
122
package main
import (
"fmt"
"strings"
)
type Client interface {
// Get a specific data for a specific slot/block number
GetDataPoint(dataName MetricName, blockSlotNumber uint64) (interface{}, error)
// Get the latest block/slot number for this client
GetLatestBlockSlotNumber() (uint64, error)
// Update the TTD Block information:
// Execution clients will ask for the totalDifficulty and find the TTD block
// Consensus clients this is a no-op
UpdateGetTTDBlockSlot() (*uint64, error)
// Get the client type: Execution or Beacon
ClientLayer() ClientLayer
// Get the client type: Execution or Beacon
ClientType() ClientType
// Get the client version if available
ClientVersion() (string, error)
// Get the client ID
ClientID() int
// Get the client version if available
String() string
// Close the clietn
Close() error
}
type Clients []Client
func (cs *Clients) BeaconClients() []*BeaconClient {
beaconClients := make([]*BeaconClient, 0)
for _, c := range *cs {
if bc, ok := c.(*BeaconClient); ok {
beaconClients = append(beaconClients, bc)
}
}
return beaconClients
}
func (cs *Clients) ExecutionClients() []*ExecutionClient {
executionClients := make([]*ExecutionClient, 0)
for _, c := range *cs {
if ec, ok := c.(*ExecutionClient); ok {
executionClients = append(executionClients, ec)
}
}
return executionClients
}
func (cs *Clients) ContainsID(clientType ClientType, id int) bool {
for _, c := range *cs {
if c.ClientType() == clientType && c.ClientID() == id {
return true
}
}
return false
}
func (cs *Clients) Set(typeUrl string) error {
splitUrl := strings.Split(typeUrl, ",")
if len(splitUrl) != 2 {
return fmt.Errorf("invalid format")
}
clientTypeStr := splitUrl[0]
url := splitUrl[1]
clientType, ok := ParseClientTypeString(clientTypeStr)
if !ok {
return fmt.Errorf("invalid client type: %s", clientTypeStr)
}
ct, ok := ClientTypeToLayer[clientType]
if !ok {
return fmt.Errorf("unknown client type")
}
var clientID int
for {
if !cs.ContainsID(clientType, clientID) {
break
}
clientID += 1
}
switch ct {
case Execution:
el, err := NewExecutionClient(clientType, clientID, url)
if err != nil {
return err
}
*cs = append(*cs, el)
return nil
case Beacon:
bc, err := NewBeaconClient(clientType, clientID, url)
if err != nil {
return err
}
*cs = append(*cs, bc)
return nil
}
return fmt.Errorf("unable to instantiate client %s", clientType)
}
func (cs *Clients) String() string {
str := make([]string, 0)
for _, c := range *cs {
str = append(str, c.String())
}
return strings.Join(str, ",")
}