This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
/
mesh_service.go
125 lines (102 loc) · 3.74 KB
/
mesh_service.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
package client
import (
"context"
"github.com/spacemeshos/CLIWallet/common"
apitypes "github.com/spacemeshos/api/release/go/spacemesh/v1"
gosmtypes "github.com/spacemeshos/go-spacemesh/common/types"
)
// GetMeshTransactions returns the transactions on the mesh to or from an address.
func (c *gRPCClient) GetMeshTransactions(address gosmtypes.Address, offset uint32, maxResults uint32) ([]*apitypes.Transaction, uint32, error) {
ms := c.getMeshServiceClient()
resp, err := ms.AccountMeshDataQuery(context.Background(), &apitypes.AccountMeshDataQueryRequest{
Filter: &apitypes.AccountMeshDataFilter{
AccountId: &apitypes.AccountId{Address: address.Bytes()},
AccountMeshDataFlags: uint32(apitypes.AccountMeshDataFlag_ACCOUNT_MESH_DATA_FLAG_TRANSACTIONS),
},
MinLayer: &apitypes.LayerNumber{Number: 0},
MaxResults: maxResults,
Offset: offset,
})
if err != nil {
return nil, 0, err
}
txsMap := make(map[string]bool)
txs := make([]*apitypes.Transaction, 0)
for _, data := range resp.Data {
tx := data.GetTransaction()
if tx != nil {
if !txsMap[string(tx.Id.Id)] {
txsMap[string(tx.Id.Id)] = true
txs = append(txs, tx)
}
}
}
// hack alert: for now, we return the number of filtered results and not the results returned from the api
// because they include duplicated transactions in case where a transaction is on more than 1 mesh block
// todo: think about default sorting
return txs, uint32(len(txs)), nil
}
// GetMeshActivations returns activations where the address is the coinbase
func (c *gRPCClient) GetMeshActivations(address gosmtypes.Address, offset uint32, maxResults uint32) ([]*apitypes.Activation, uint32, error) {
ms := c.getMeshServiceClient()
resp, err := ms.AccountMeshDataQuery(context.Background(), &apitypes.AccountMeshDataQueryRequest{
Filter: &apitypes.AccountMeshDataFilter{
AccountId: &apitypes.AccountId{Address: address.Bytes()},
AccountMeshDataFlags: uint32(apitypes.AccountMeshDataFlag_ACCOUNT_MESH_DATA_FLAG_ACTIVATIONS),
},
MinLayer: &apitypes.LayerNumber{Number: 0},
MaxResults: maxResults,
Offset: offset,
})
if err != nil {
return nil, 0, err
}
activations := make([]*apitypes.Activation, 0)
for _, data := range resp.Data {
a := data.GetActivation()
if a != nil {
activations = append(activations, a)
}
}
return activations, resp.TotalResults, nil
}
func (c *gRPCClient) GetMeshInfo() (*common.NetInfo, error) {
netInfo := &common.NetInfo{}
ms := c.getMeshServiceClient()
res, err := ms.GenesisTime(context.Background(), &apitypes.GenesisTimeRequest{})
if err != nil {
return nil, err
}
netInfo.GenesisTime = res.Unixtime.Value
currLayer, err := ms.CurrentLayer(context.Background(), &apitypes.CurrentLayerRequest{})
if err != nil {
return nil, err
}
netInfo.CurrentLayer = currLayer.Layernum.Number
epochNum, err := ms.CurrentEpoch(context.Background(), &apitypes.CurrentEpochRequest{})
if err != nil {
return nil, err
}
netInfo.CurrentEpoch = epochNum.Epochnum.Value
netId, err := ms.NetID(context.Background(), &apitypes.NetIDRequest{})
if err != nil {
return nil, err
}
netInfo.NetId = netId.Netid.Value
layersPerEpoch, err := ms.EpochNumLayers(context.Background(), &apitypes.EpochNumLayersRequest{})
if err != nil {
return nil, err
}
netInfo.LayerPerEpoch = layersPerEpoch.Numlayers.Value
layerDuration, err := ms.LayerDuration(context.Background(), &apitypes.LayerDurationRequest{})
if err != nil {
return nil, err
}
netInfo.LayerDuration = layerDuration.Duration.Value
maxTxsPerSec, err := ms.MaxTransactionsPerSecond(context.Background(), &apitypes.MaxTransactionsPerSecondRequest{})
if err != nil {
return nil, err
}
netInfo.MaxTxsPerSec = maxTxsPerSec.Maxtxpersecond.Value
return netInfo, nil
}