-
Notifications
You must be signed in to change notification settings - Fork 91
/
node.go
176 lines (153 loc) · 5.56 KB
/
node.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
package containertest
import (
"context"
"fmt"
"time"
comethttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cosmos "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
gogogrpc "github.com/cosmos/gogoproto/grpc"
"github.com/cosmos/gogoproto/proto"
"github.com/dydxprotocol/v4-chain/protocol/cmd/dydxprotocold/cmd"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/ory/dockertest/v3"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
// When polling a node, poll every `pollFrequencyNs` and give up after `pollAttempts` attempts.
pollFrequencyNs = time.Second
pollAttempts = 30
cometPort = "26657/tcp"
grpcPort = "9090/tcp"
)
type Node struct {
keyring *keyring.Keyring
cometClient *comethttp.HTTP
grpcConn *grpc.ClientConn
resource *dockertest.Resource
}
func newNode(keyring *keyring.Keyring, resource *dockertest.Resource) (node *Node, err error) {
node = &Node{
keyring: keyring,
resource: resource,
}
cometPort := resource.GetHostPort(cometPort)
node.cometClient, err = comethttp.New("tcp://"+cometPort, "/websocket")
if err != nil {
return nil, err
}
grpcPort := resource.GetHostPort(grpcPort)
node.grpcConn, err = grpc.Dial(grpcPort, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
return node, err
}
// Wait for current block height has advanced by at least `numBlocks`
func (n *Node) Wait(numBlocks int64) error {
currBlock, err := n.LatestBlockHeight()
if err != nil {
return err
}
return n.WaitUntilBlockHeight(currBlock + numBlocks)
}
// Wait for the current block height to reach at least `height`
func (n *Node) WaitUntilBlockHeight(height int64) error {
for i := 0; i < pollAttempts; i++ {
latestHeight, err := n.LatestBlockHeight()
if err != nil {
return err
}
if latestHeight >= height {
return nil
}
time.Sleep(pollFrequencyNs)
}
return fmt.Errorf("timed out when waiting for block height %d", height)
}
func (n *Node) LatestBlockHeight() (int64, error) {
status, err := n.cometClient.Status(context.Background())
if err != nil {
return 0, err
}
return status.SyncInfo.LatestBlockHeight, nil
}
// Get a `Context` to be used for broadcasting tx given a signer address.
// NB: The cosmos client for broadcasting tx seems to be extremely coupled with command, so we have to create a dummy
// command to hijack some of the private functionality to create the context. There may be a better way to get Context,
// such as going a level lower to the tx.Factory level, but this works for now.
func (n *Node) getContextForBroadcastTx(signer string) (*client.Context, *pflag.FlagSet, error) {
initClientCtx := client.Context{}.
WithCodec(constants.TestEncodingCfg.Codec).
WithInterfaceRegistry(constants.TestEncodingCfg.InterfaceRegistry).
WithTxConfig(constants.TestEncodingCfg.TxConfig).
WithAccountRetriever(types.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastSync).
WithViper(cmd.EnvPrefix)
option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option)
flags.AddTxFlagsToCmd(rootCmd)
flags := rootCmd.Flags()
hostPort := n.resource.GetHostPort(cometPort)
if err := flags.Set("node", fmt.Sprintf("tcp://%s", hostPort)); err != nil {
return nil, nil, err
}
if err := flags.Set("from", signer); err != nil {
return nil, nil, err
}
if err := flags.Set("chain-id", "localdydxprotocol"); err != nil {
return nil, nil, err
}
initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
if err != nil {
return nil, nil, err
}
// NB: In `cmd/dydxprotocol/root.go` this step is done before ReadFromClientConfig, but here we choose to
// do it second because ReadPersistentCommandFlags sets the node address we configured in flags.
// If we were to do it in reverse, ReadFromClientConfig would overwrite the node address.
initClientCtx, err = client.ReadPersistentCommandFlags(initClientCtx, flags)
if err != nil {
return nil, nil, err
}
initClientCtx = initClientCtx.WithKeyring(*n.keyring).WithSkipConfirmation(true)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &initClientCtx)
rootCmd.SetContext(ctx)
initClientCtx, err = client.GetClientTxContext(rootCmd)
if err != nil {
return nil, nil, err
}
return &initClientCtx, flags, nil
}
// Broadcast a tx to the node given the message and a signer address.
func BroadcastTx[M cosmos.Msg](n *Node, message M, signer string) (err error) {
clientContext, flags, err := n.getContextForBroadcastTx(signer)
if err != nil {
return err
}
txFactory, err := tx.NewFactoryCLI(*clientContext, flags)
if err != nil {
return err
}
// Use default gas limit and gas fee.
txFactory = txFactory.WithGas(constants.TestGasLimit).WithFees(constants.TestFee)
if err = tx.GenerateOrBroadcastTxWithFactory(*clientContext, txFactory, message); err != nil {
return err
}
return nil
}
// Query the node's grpc endpoint given the client constructor, request method, and request
func Query[Request proto.Message, Response proto.Message, Client interface{}](
n *Node,
clientConstructor func(gogogrpc.ClientConn) Client,
requestFn func(Client, context.Context, Request, ...grpc.CallOption) (Response, error),
request Request) (proto.Message, error) {
client := clientConstructor(n.grpcConn)
return requestFn(client, context.Background(), request)
}