-
Notifications
You must be signed in to change notification settings - Fork 322
/
bc.go
174 lines (159 loc) · 5.42 KB
/
bc.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
// Copyright (c) 2022 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package bc
import (
"context"
"strconv"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
_bcCmdShorts = map[config.Language]string{
config.English: "Deal with blockchain of IoTeX blockchain",
config.Chinese: "处理IoTeX区块链上的区块",
}
)
// NewBCCmd represents the bc(block chain) command
func NewBCCmd(client ioctl.Client) *cobra.Command {
bcShorts, _ := client.SelectTranslation(_bcCmdShorts)
bc := &cobra.Command{
Use: "bc",
Short: bcShorts,
}
bc.AddCommand(NewBCInfoCmd(client))
bc.AddCommand(NewBCBlockCmd(client))
client.SetEndpointWithFlag(bc.PersistentFlags().StringVar)
client.SetInsecureWithFlag(bc.PersistentFlags().BoolVar)
return bc
}
// GetChainMeta gets blockchain metadata
func GetChainMeta(client ioctl.Client) (*iotextypes.ChainMeta, error) {
apiServiceClient, err := client.APIServiceClient()
if err != nil {
return nil, err
}
ctx := context.Background()
jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(context.Background())
}
chainMetaResponse, err := apiServiceClient.GetChainMeta(ctx, &iotexapi.GetChainMetaRequest{})
if err != nil {
if sta, ok := status.FromError(err); ok {
if sta.Code() == codes.Unavailable {
return nil, ioctl.ErrInvalidEndpointOrInsecure
}
return nil, errors.New(sta.Message())
}
return nil, errors.Wrap(err, "failed to invoke GetChainMeta api")
}
return chainMetaResponse.ChainMeta, nil
}
// GetEpochMeta gets blockchain epoch meta
func GetEpochMeta(client ioctl.Client, epochNum uint64) (*iotexapi.GetEpochMetaResponse, error) {
apiServiceClient, err := client.APIServiceClient()
if err != nil {
return nil, err
}
ctx := context.Background()
jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}
epochMetaresponse, err := apiServiceClient.GetEpochMeta(ctx, &iotexapi.GetEpochMetaRequest{EpochNumber: epochNum})
if err != nil {
if sta, ok := status.FromError(err); ok {
if sta.Code() == codes.Unavailable {
return nil, ioctl.ErrInvalidEndpointOrInsecure
}
return nil, errors.New(sta.Message())
}
return nil, errors.Wrap(err, "failed to invoke GetEpochMeta api")
}
return epochMetaresponse, nil
}
// GetProbationList gets probation list
func GetProbationList(client ioctl.Client, epochNum uint64, epochStartHeight uint64) (*iotexapi.ReadStateResponse, error) {
apiServiceClient, err := client.APIServiceClient()
if err != nil {
return nil, err
}
request := &iotexapi.ReadStateRequest{
ProtocolID: []byte("poll"),
MethodName: []byte("ProbationListByEpoch"),
Arguments: [][]byte{[]byte(strconv.FormatUint(epochNum, 10))},
Height: strconv.FormatUint(epochStartHeight, 10),
}
ctx := context.Background()
if jwtMD, err := util.JwtAuth(); err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}
response, err := apiServiceClient.ReadState(ctx, request)
if err != nil {
if sta, ok := status.FromError(err); ok {
if sta.Code() == codes.NotFound {
return nil, nil
} else if sta.Code() == codes.Unavailable {
return nil, ioctl.ErrInvalidEndpointOrInsecure
}
return nil, errors.New(sta.Message())
}
return nil, errors.Wrap(err, "failed to invoke ReadState api")
}
return response, nil
}
// GetBucketList get bucket list
func GetBucketList(
client ioctl.Client,
methodName iotexapi.ReadStakingDataMethod_Name,
readStakingDataRequest *iotexapi.ReadStakingDataRequest,
) (*iotextypes.VoteBucketList, error) {
apiServiceClient, err := client.APIServiceClient()
if err != nil {
return nil, err
}
methodData, err := proto.Marshal(&iotexapi.ReadStakingDataMethod{Method: methodName})
if err != nil {
return nil, errors.Wrap(err, "failed to marshal read staking data method")
}
requestData, err := proto.Marshal(readStakingDataRequest)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal read staking data request")
}
request := &iotexapi.ReadStateRequest{
ProtocolID: []byte("staking"),
MethodName: methodData,
Arguments: [][]byte{requestData},
}
ctx := context.Background()
if jwtMD, err := util.JwtAuth(); err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}
response, err := apiServiceClient.ReadState(ctx, request)
if err != nil {
if sta, ok := status.FromError(err); ok {
if sta.Code() == codes.Unavailable {
return nil, ioctl.ErrInvalidEndpointOrInsecure
}
return nil, errors.New(sta.Message())
}
return nil, errors.Wrap(err, "failed to invoke ReadState api")
}
bucketlist := iotextypes.VoteBucketList{}
if err := proto.Unmarshal(response.Data, &bucketlist); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal response")
}
return &bucketlist, nil
}