-
Notifications
You must be signed in to change notification settings - Fork 19
/
grpc_adapter.go
169 lines (146 loc) · 5.44 KB
/
grpc_adapter.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package adapters
import (
"context"
"sort"
"strings"
apipb "code.vegaprotocol.io/vega/protos/vega/api/v1"
nodetypes "code.vegaprotocol.io/vega/wallet/api/node/types"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
type GRPCAdapter struct {
client apipb.CoreServiceClient
connection *grpc.ClientConn
host string
}
func (c *GRPCAdapter) Host() string {
return c.host
}
func (c *GRPCAdapter) SpamStatistics(ctx context.Context, party string) (nodetypes.SpamStatistics, error) {
r, err := c.client.GetSpamStatistics(ctx,
&apipb.GetSpamStatisticsRequest{
PartyId: party,
})
if err != nil {
return nodetypes.SpamStatistics{}, err
}
proposals := map[string]uint64{}
for _, st := range r.Statistics.Votes.Statistics {
proposals[st.Proposal] = st.CountForEpoch
}
blockStates := []nodetypes.PoWBlockState{}
for _, b := range r.Statistics.Pow.BlockStates {
blockStates = append(blockStates, nodetypes.PoWBlockState{
BlockHeight: b.BlockHeight,
BlockHash: b.BlockHash,
TransactionsSeen: b.TransactionsSeen,
ExpectedDifficulty: b.ExpectedDifficulty,
HashFunction: b.HashFunction,
Difficulty: b.Difficulty,
TxPerBlock: b.TxPerBlock,
IncreasingDifficulty: b.IncreasingDifficulty,
})
}
// sort by block-height so latest block is first
sort.Slice(blockStates, func(i int, j int) bool {
return blockStates[i].BlockHeight > blockStates[j].BlockHeight
})
var lastBlockHeight uint64
if len(blockStates) > 0 {
lastBlockHeight = blockStates[0].BlockHeight
}
return nodetypes.SpamStatistics{
Proposals: toSpamStatistic(r.Statistics.Proposals),
Delegations: toSpamStatistic(r.Statistics.Delegations),
Transfers: toSpamStatistic(r.Statistics.Transfers),
NodeAnnouncements: toSpamStatistic(r.Statistics.NodeAnnouncements),
IssuesSignatures: toSpamStatistic(r.Statistics.IssueSignatures),
CreateReferralSet: toSpamStatistic(r.Statistics.CreateReferralSet),
UpdateReferralSet: toSpamStatistic(r.Statistics.UpdateReferralSet),
ApplyReferralCode: toSpamStatistic(r.Statistics.ApplyReferralCode),
Votes: &nodetypes.VoteSpamStatistics{
Proposals: proposals,
MaxForEpoch: r.Statistics.Votes.MaxForEpoch,
BannedUntil: r.Statistics.Votes.BannedUntil,
},
PoW: &nodetypes.PoWStatistics{
PowBlockStates: blockStates,
BannedUntil: r.Statistics.Pow.BannedUntil,
PastBlocks: r.Statistics.Pow.NumberOfPastBlocks,
},
ChainID: r.ChainId,
EpochSeq: r.Statistics.EpochSeq,
LastBlockHeight: lastBlockHeight,
}, nil
}
func (c *GRPCAdapter) Statistics(ctx context.Context) (nodetypes.Statistics, error) {
statistics, err := c.client.Statistics(ctx, &apipb.StatisticsRequest{})
if err != nil {
return nodetypes.Statistics{}, err
}
return nodetypes.Statistics{
BlockHeight: statistics.Statistics.BlockHeight,
BlockHash: statistics.Statistics.BlockHash,
ChainID: statistics.Statistics.ChainId,
VegaTime: statistics.Statistics.VegaTime,
}, nil
}
func (c *GRPCAdapter) CheckTransaction(ctx context.Context, req *apipb.CheckTransactionRequest) (*apipb.CheckTransactionResponse, error) {
return c.client.CheckTransaction(ctx, req)
}
func (c *GRPCAdapter) SubmitTransaction(ctx context.Context, req *apipb.SubmitTransactionRequest) (*apipb.SubmitTransactionResponse, error) {
return c.client.SubmitTransaction(ctx, req)
}
func (c *GRPCAdapter) LastBlock(ctx context.Context) (nodetypes.LastBlock, error) {
lastBlock, err := c.client.LastBlockHeight(ctx, &apipb.LastBlockHeightRequest{})
if err != nil {
return nodetypes.LastBlock{}, err
}
return nodetypes.LastBlock{
ChainID: lastBlock.ChainId,
BlockHeight: lastBlock.Height,
BlockHash: lastBlock.Hash,
ProofOfWorkHashFunction: lastBlock.SpamPowHashFunction,
ProofOfWorkDifficulty: lastBlock.SpamPowDifficulty,
ProofOfWorkPastBlocks: lastBlock.SpamPowNumberOfPastBlocks,
ProofOfWorkIncreasingDifficulty: lastBlock.SpamPowIncreasingDifficulty,
ProofOfWorkTxPerBlock: lastBlock.SpamPowNumberOfTxPerBlock,
}, nil
}
func (c *GRPCAdapter) Stop() error {
return c.connection.Close()
}
func NewGRPCAdapter(host string) (*GRPCAdapter, error) {
useTLS := strings.HasPrefix(host, "tls://")
var creds credentials.TransportCredentials
if useTLS {
host = host[6:]
creds = credentials.NewClientTLSFromCert(nil, "")
} else {
creds = insecure.NewCredentials()
}
connection, err := grpc.Dial(host, grpc.WithTransportCredentials(creds))
if err != nil {
return nil, err
}
return &GRPCAdapter{
client: apipb.NewCoreServiceClient(connection),
connection: connection,
host: host,
}, nil
}