/
rpc_client_cluster.go
179 lines (159 loc) · 4.8 KB
/
rpc_client_cluster.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package sbch
import (
"bytes"
"crypto/sha256"
"fmt"
"reflect"
"sort"
"sync"
"time"
gethcmn "github.com/ethereum/go-ethereum/common"
sbchrpctypes "github.com/smartbch/smartbch/rpc/types"
)
var _ RpcClient = (*ClusterClient)(nil)
type ClusterClient struct {
clients []RpcClient
PublicNodes []NodeInfo
}
func NewClusterRpcClient(nodesGovAddr string, nodes []NodeInfo, privateUrls []string,
reqTimeout time.Duration) (*ClusterClient, error) {
clients := make([]RpcClient, 0, len(nodes))
for _, node := range nodes {
client, err := NewSimpleRpcClient(nodesGovAddr, node.RpcUrl, reqTimeout)
if err != nil {
return nil, fmt.Errorf("dail %s failed: %w", node.RpcUrl, err)
}
pbk, err := client.GetRpcPubkey()
if err != nil {
return nil, fmt.Errorf("get pubkey from %s failed: %w", node.RpcUrl, err)
}
if sha256.Sum256(pbk) != node.PbkHash {
return nil, fmt.Errorf("pubkey not match: %s", node.RpcUrl)
}
clients = append(clients, client)
}
for _, url := range privateUrls {
client, err := NewSimpleRpcClient(nodesGovAddr, url, reqTimeout)
if err != nil {
return nil, fmt.Errorf("dail %s failed: %w", url, err)
}
clients = append(clients, client)
}
return &ClusterClient{
clients: clients,
PublicNodes: nodes,
}, nil
}
func (cluster *ClusterClient) RpcURL() string {
return "clusterRpcClient"
}
func (cluster *ClusterClient) GetRpcPubkey() ([]byte, error) {
return nil, fmt.Errorf("unsupported operation")
}
func (cluster *ClusterClient) GetSbchdNodes() ([]NodeInfo, error) {
result, err := cluster.getFromAllNodes("GetSbchdNodes")
if err != nil {
return nil, err
}
return result.([]NodeInfo), err
}
func (cluster *ClusterClient) GetSbchdNodesSorted() ([]NodeInfo, error) {
nodes, err := cluster.GetSbchdNodes()
if err == nil {
sortNodes(nodes)
}
return nodes, err
}
func sortNodes(nodes []NodeInfo) {
sort.Slice(nodes, func(i, j int) bool {
return bytes.Compare(nodes[i].PbkHash[:], nodes[j].PbkHash[:]) < 0
})
}
func (cluster *ClusterClient) GetRedeemingUtxosForOperators() ([]*sbchrpctypes.UtxoInfo, error) {
result, err := cluster.getFromAllNodes("GetRedeemingUtxosForOperators")
if err != nil {
return nil, err
}
return result.([]*sbchrpctypes.UtxoInfo), err
}
func (cluster *ClusterClient) GetRedeemingUtxosForMonitors() ([]*sbchrpctypes.UtxoInfo, error) {
result, err := cluster.getFromAllNodes("GetRedeemingUtxosForMonitors")
if err != nil {
return nil, err
}
return result.([]*sbchrpctypes.UtxoInfo), err
}
func (cluster *ClusterClient) GetToBeConvertedUtxosForOperators() ([]*sbchrpctypes.UtxoInfo, error) {
result, err := cluster.getFromAllNodes("GetToBeConvertedUtxosForOperators")
if err != nil {
return nil, err
}
return result.([]*sbchrpctypes.UtxoInfo), err
}
func (cluster *ClusterClient) GetToBeConvertedUtxosForMonitors() ([]*sbchrpctypes.UtxoInfo, error) {
result, err := cluster.getFromAllNodes("GetToBeConvertedUtxosForMonitors")
if err != nil {
return nil, err
}
return result.([]*sbchrpctypes.UtxoInfo), err
}
func (cluster *ClusterClient) GetMonitors() ([]gethcmn.Address, error) {
result, err := cluster.getFromAllNodes("GetMonitors")
if err != nil {
return nil, err
}
return result.([]gethcmn.Address), err
}
func (cluster *ClusterClient) getFromAllNodes(methodName string) (any, error) {
if len(cluster.clients) == 0 {
return nil, fmt.Errorf("no clients")
}
nClients := len(cluster.clients)
resps := make([]any, nClients)
errors := make([]error, nClients)
// send post to nodes concurrently
wg := sync.WaitGroup{}
wg.Add(nClients)
for i, client := range cluster.clients {
go func(idx int, client RpcClient) {
resps[idx], errors[idx] = getFromOneNode(client, methodName)
wg.Done()
}(i, client)
}
wg.Wait()
// fail if one of node return error
for idx, err := range errors {
if err != nil {
return nil, fmt.Errorf("failed to call %s: %w",
cluster.clients[idx].RpcURL(), err)
}
}
// all responses should be same
resp0 := resps[0]
for idx, resp := range resps {
if idx > 0 && !reflect.DeepEqual(resp0, resp) {
return nil, fmt.Errorf("response not match between: %s, %s",
cluster.clients[0].RpcURL(), cluster.clients[idx].RpcURL())
}
}
//fmt.Println("resp:", string(resp0))
return resp0, nil
}
func getFromOneNode(client RpcClient, methodName string) (any, error) {
switch methodName {
case "GetSbchdNodes":
return client.GetSbchdNodes()
case "GetRedeemingUtxosForOperators":
return client.GetRedeemingUtxosForOperators()
case "GetRedeemingUtxosForMonitors":
return client.GetRedeemingUtxosForMonitors()
case "GetToBeConvertedUtxosForOperators":
return client.GetToBeConvertedUtxosForOperators()
case "GetToBeConvertedUtxosForMonitors":
return client.GetToBeConvertedUtxosForMonitors()
case "GetMonitors":
return client.GetMonitors()
default:
panic("unknown method") // unreachable
}
}