-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
201 lines (177 loc) · 5.41 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2019 The go-ultiledger Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package client defines the APIs to communicate with the node.
package client
import (
"context"
"errors"
"fmt"
"time"
b58 "github.com/mr-tron/base58/base58"
"google.golang.org/grpc"
"github.com/ultiledger/go-ultiledger/client/types"
"github.com/ultiledger/go-ultiledger/crypto"
"github.com/ultiledger/go-ultiledger/rpc/rpcpb"
"github.com/ultiledger/go-ultiledger/ultpb"
)
// GrpcClient manages the gRPC connections to ult servers and
// works as a load balancer to the backend ult servers.
type GrpcClient struct {
networkID string
coreEndpoints string
client rpcpb.NodeClient
}
// New creates a GrpcClient to the given target servers.
func New(networkID, coreEndpoints string) (*GrpcClient, error) {
// Connect to node servers.
r := NewResolver()
b := grpc.RoundRobin(r)
conn, err := grpc.Dial(coreEndpoints, grpc.WithInsecure(), grpc.WithBalancer(b), grpc.WithBlock(), grpc.WithTimeout(3*time.Second))
if err != nil {
return nil, fmt.Errorf("connect to core servers failed: %v", err)
}
// Compute the hash of network id.
netID := crypto.SHA256HashBytes([]byte(networkID))
netIDStr := b58.Encode(netID[:])
client := rpcpb.NewNodeClient(conn)
gc := &GrpcClient{
networkID: netIDStr,
coreEndpoints: coreEndpoints,
client: client,
}
return gc, nil
}
// SummitTx summits the tx to ult servers and return appropriate
// response messages to the client.
func (c *GrpcClient) SubmitTx(txKey string, signature string, data []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
req := &rpcpb.SubmitTxRequest{
NetworkID: c.networkID,
TxKey: txKey,
Signature: signature,
Data: data,
}
_, err := c.client.SubmitTx(ctx, req)
if err != nil {
return err
}
return nil
}
// QueryTx queries the tx status from ult servers and return current
// tx status.
func (c *GrpcClient) QueryTx(txKey string) (*types.TxStatus, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
req := &rpcpb.QueryTxRequest{
NetworkID: c.networkID,
TxKey: txKey,
}
resp, err := c.client.QueryTx(ctx, req)
if err != nil {
return nil, err
}
status := &types.TxStatus{
ErrorMessage: resp.TxStatus.ErrorMessage,
}
switch resp.TxStatus.StatusCode {
case rpcpb.TxStatusCode_NOTEXIST:
status.StatusCode = types.NotExist
case rpcpb.TxStatusCode_REJECTED:
status.StatusCode = types.Rejected
case rpcpb.TxStatusCode_ACCEPTED:
status.StatusCode = types.Accepted
case rpcpb.TxStatusCode_CONFIRMED:
status.StatusCode = types.Confirmed
case rpcpb.TxStatusCode_FAILED:
status.StatusCode = types.Failed
default:
status.StatusCode = types.Unknown
}
if len(resp.TxStatus.Data) > 0 {
tx, err := ultpb.DecodeTx(resp.TxStatus.Data)
if err != nil {
return nil, fmt.Errorf("decode tx failed: %v", err)
}
status.Tx = tx
}
return status, nil
}
// GetAccount gets the account with the requested account id.
func (c *GrpcClient) GetAccount(accountID string) (*ultpb.Account, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
req := &rpcpb.GetAccountRequest{
NetworkID: c.networkID,
AccountID: accountID,
}
resp, err := c.client.GetAccount(ctx, req)
if err != nil {
return nil, err
}
acc, err := ultpb.DecodeAccount(resp.Data)
if err != nil {
return nil, fmt.Errorf("decode account failed: %v", err)
}
return acc, nil
}
// CreateTestAccount creates a test account for the
// purpose of testing in testnet.
func (c *GrpcClient) CreateTestAccount(accountID string) (*ultpb.Account, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
req := &rpcpb.CreateTestAccountRequest{
NetworkID: c.networkID,
AccountID: accountID,
}
resp, err := c.client.CreateTestAccount(ctx, req)
if err != nil {
return nil, err
}
// Wait until the tx be confirmed.
ticker := time.NewTicker(2 * time.Second)
timer := time.NewTimer(30 * time.Second)
for {
select {
case <-ticker.C:
// Check the tx.
status, err := c.QueryTx(resp.TxKey)
if err != nil {
return nil, fmt.Errorf("query tx failed: %v", err)
}
switch status.StatusCode {
case types.NotExist:
return nil, errors.New("tx not found")
case types.Rejected:
return nil, fmt.Errorf("tx rejected: %v", status.ErrorMessage)
case types.Accepted:
continue
case types.Confirmed:
// Get the account
account, err := c.GetAccount(accountID)
if err != nil {
return nil, fmt.Errorf("get account failed: %v", err)
}
return account, nil
case types.Failed:
return nil, fmt.Errorf("tx failed: %v", status.ErrorMessage)
default:
return nil, errors.New("tx status unknown")
}
case <-timer.C:
return nil, errors.New("query result takes too long")
}
}
return nil, nil
}