/
storage_client.go
151 lines (125 loc) · 3.98 KB
/
storage_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
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
package byzantine
import (
"context"
"crypto/tls"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc"
"github.com/oasisprotocol/oasis-core/go/common/identity"
"github.com/oasisprotocol/oasis-core/go/common/node"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
storage "github.com/oasisprotocol/oasis-core/go/storage/api"
)
var _ storage.Backend = (*storageClient)(nil)
type storageClient struct {
storage.Backend
nodeID signature.PublicKey
initCh chan struct{}
}
func dialOptionForNode(ourCerts []tls.Certificate, node *node.Node) (grpc.DialOption, error) {
tlsKeys := make(map[signature.PublicKey]bool)
for _, addr := range node.TLS.Addresses {
tlsKeys[addr.PubKey] = true
}
creds, err := cmnGrpc.NewClientCreds(&cmnGrpc.ClientOptions{
CommonName: identity.CommonName,
ServerPubKeys: tlsKeys,
Certificates: ourCerts,
})
if err != nil {
return nil, err
}
return grpc.WithTransportCredentials(creds), nil
}
func dialNode(node *node.Node, opts grpc.DialOption) (*grpc.ClientConn, error) {
manualResolver := manual.NewBuilderWithScheme("oasis-core-resolver")
conn, err := cmnGrpc.Dial("oasis-core-resolver:///", opts,
grpc.WithBalancerName(roundrobin.Name), // nolint: staticcheck
grpc.WithResolvers(manualResolver),
)
if err != nil {
return nil, fmt.Errorf("failed dialing node: %w", err)
}
var resolverState resolver.State
for _, addr := range node.TLS.Addresses {
resolverState.Addresses = append(resolverState.Addresses, resolver.Address{Addr: addr.String()})
}
manualResolver.UpdateState(resolverState)
return conn, nil
}
func newHonestNodeStorage(id *identity.Identity, node *node.Node) (*storageClient, error) {
opts, err := dialOptionForNode([]tls.Certificate{*id.GetTLSCertificate()}, node)
if err != nil {
return nil, fmt.Errorf("storage client DialOptionForNode: %w", err)
}
conn, err := dialNode(node, opts)
if err != nil {
return nil, fmt.Errorf("storage client DialNode: %w", err)
}
initCh := make(chan struct{})
close(initCh)
return &storageClient{
Backend: storage.NewStorageClient(conn),
nodeID: node.ID,
initCh: initCh,
}, nil
}
func (sc *storageClient) Initialized() <-chan struct{} {
return sc.initCh
}
func storageConnectToCommittee(ht *honestTendermint, height int64, committee *scheduler.Committee, role scheduler.Role, id *identity.Identity) ([]*storageClient, error) {
var clients []*storageClient
if err := schedulerForRoleInCommittee(ht, height, committee, role, func(n *node.Node) error {
client, err := newHonestNodeStorage(id, n)
if err != nil {
return fmt.Errorf("new honest node storage %s: %w", n.ID, err)
}
clients = append(clients, client)
return nil
}); err != nil {
return nil, err
}
return clients, nil
}
func storageBroadcastCleanup(clients []*storageClient) {
for _, sc := range clients {
sc.Cleanup()
}
}
func storageBroadcastApplyBatch(
ctx context.Context,
clients []*storageClient,
ns common.Namespace,
dstRound uint64,
ops []storage.ApplyOp,
) ([]*storage.Receipt, error) {
var receipts []*storage.Receipt
for _, sc := range clients {
r, err := sc.ApplyBatch(ctx, &storage.ApplyBatchRequest{Namespace: ns, DstRound: dstRound, Ops: ops})
if err != nil {
return receipts, fmt.Errorf("honest node storage ApplyBatch %s: %w", sc.nodeID, err)
}
receipts = append(receipts, r...)
}
return receipts, nil
}
func storageBroadcastApply(
ctx context.Context,
clients []*storageClient,
req *storage.ApplyRequest,
) ([]*storage.Receipt, error) {
var receipts []*storage.Receipt
for _, sc := range clients {
r, err := sc.Apply(ctx, req)
if err != nil {
return receipts, fmt.Errorf("honest node storage Apply %s: %w", sc.nodeID, err)
}
receipts = append(receipts, r...)
}
return receipts, nil
}