-
Notifications
You must be signed in to change notification settings - Fork 22
/
local_client.go
182 lines (155 loc) · 5.41 KB
/
local_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
// 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 abci
import (
"context"
"time"
tmquery "github.com/cometbft/cometbft/libs/pubsub/query"
"github.com/cometbft/cometbft/libs/service"
nm "github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/rpc/client/local"
tmctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
)
type LocalClient struct {
node *local.Local
genesisDoc *cachedGenesisDoc
}
func newLocalClient(node service.Service) (*LocalClient, error) {
localNode := local.New(node.(*nm.Node))
return &LocalClient{
node: localNode,
genesisDoc: newCachedGenesisDoc(),
}, nil
}
func (c *LocalClient) SendTransactionAsync(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTx, error) {
// Fire off the transaction for consensus
res, err := c.node.BroadcastTxAsync(ctx, bytes)
if err != nil {
return nil, err
}
return res, nil
}
func (c *LocalClient) SendTransactionSync(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTx, error) {
// Fire off the transaction for consensus
return c.node.BroadcastTxSync(ctx, bytes)
}
func (c *LocalClient) SendTransactionCommit(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTxCommit, error) {
// Fire off the transaction for consensus
return c.node.BroadcastTxCommit(ctx, bytes)
}
func (c *LocalClient) CheckTransaction(ctx context.Context, bytes []byte) (*tmctypes.ResultCheckTx, error) {
return c.node.CheckTx(ctx, bytes)
}
// GetGenesisTime retrieves the genesis time from the blockchain.
func (c *LocalClient) GetGenesisTime(ctx context.Context) (genesisTime time.Time, err error) {
res, err := c.genesisDoc.Get(ctx, c.node)
if err != nil {
return time.Time{}, err
}
return res.GenesisTime.UTC(), nil
}
// GetChainID retrieves the chainID from the blockchain.
func (c *LocalClient) GetChainID(ctx context.Context) (chainID string, err error) {
res, err := c.genesisDoc.Get(ctx, c.node)
if err != nil {
return "", err
}
return res.ChainID, nil
}
// GetStatus returns the current status of the chain.
func (c *LocalClient) GetStatus(ctx context.Context) (status *tmctypes.ResultStatus, err error) {
return c.node.Status(ctx)
}
// GetNetworkInfo return information of the current network.
func (c *LocalClient) GetNetworkInfo(ctx context.Context) (netInfo *tmctypes.ResultNetInfo, err error) {
return c.node.NetInfo(ctx)
}
// GetUnconfirmedTxCount return the current count of unconfirmed transactions.
func (c *LocalClient) GetUnconfirmedTxCount(ctx context.Context) (count int, err error) {
res, err := c.node.NumUnconfirmedTxs(ctx)
if err != nil {
return 0, err
}
return res.Count, err
}
// Health returns the result of the health endpoint of the chain.
func (c *LocalClient) Health(ctx context.Context) (*tmctypes.ResultHealth, error) {
return c.node.Health(ctx)
}
func (c *LocalClient) Validators(ctx context.Context, height *int64) ([]*tmtypes.Validator, error) {
res, err := c.node.Validators(ctx, height, nil, nil)
if err != nil {
return nil, err
}
return res.Validators, nil
}
func (c *LocalClient) Genesis(ctx context.Context) (*tmtypes.GenesisDoc, error) {
res, err := c.genesisDoc.Get(ctx, c.node)
if err != nil {
return nil, err
}
return res, nil
}
func (c *LocalClient) GenesisValidators(ctx context.Context) ([]*tmtypes.Validator, error) {
gen, err := c.Genesis(ctx)
if err != nil {
return nil, err
}
validators := make([]*tmtypes.Validator, 0, len(gen.Validators))
for _, v := range gen.Validators {
validators = append(validators, &tmtypes.Validator{
Address: v.Address,
PubKey: v.PubKey,
VotingPower: v.Power,
})
}
return validators, nil
}
// Subscribe subscribes to any event matching query (https://godoc.org/github.com/cometbft/cometbft/types#pkg-constants).
// Subscribe will call fn each time it receives an event from the node.
// The function returns nil when the context is canceled or when fn returns an error.
func (c *LocalClient) Subscribe(ctx context.Context, fn func(tmctypes.ResultEvent) error, queries ...string) error {
if err := c.node.Start(); err != nil {
return err
}
defer c.node.Stop()
errCh := make(chan error)
for _, query := range queries {
q, err := tmquery.New(query)
if err != nil {
return err
}
// For subscription we use "vega" as the client name but it's ignored by the implementation.
// 10 is the channel capacity which is absolutely arbitraty.
out, err := c.node.Subscribe(ctx, "vega", q.String(), 10)
if err != nil {
return err
}
go func() {
for res := range out {
if err := fn(res); err != nil {
errCh <- err
return
}
}
}()
}
defer c.node.UnsubscribeAll(context.Background(), "vega")
return <-errCh
}
func (c *LocalClient) Start() error {
return nil // Nothing to do for this client type.
}