-
Notifications
You must be signed in to change notification settings - Fork 568
/
chain.go
222 lines (190 loc) · 7.5 KB
/
chain.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package chain
import (
"fmt"
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
appparams "github.com/osmosis-labs/osmosis/v12/app/params"
"github.com/osmosis-labs/osmosis/v12/tests/e2e/configurer/config"
"github.com/osmosis-labs/osmosis/v12/tests/e2e/containers"
"github.com/osmosis-labs/osmosis/v12/tests/e2e/initialization"
)
type Config struct {
initialization.ChainMeta
ValidatorInitConfigs []*initialization.NodeConfig
// voting period is number of blocks it takes to deposit, 1.2 seconds per validator to vote on the prop, and a buffer.
VotingPeriod float32
ExpeditedVotingPeriod float32
// upgrade proposal height for chain.
UpgradePropHeight int64
LatestProposalNumber int
LatestLockNumber int
NodeConfigs []*NodeConfig
LatestCodeId int
t *testing.T
containerManager *containers.Manager
}
const (
// defaultNodeIndex to use for querying and executing transactions.
// It is used when we are indifferent about the node we are working with.
defaultNodeIndex = 0
// waitUntilRepeatPauseTime is the time to wait between each check of the node status.
waitUntilRepeatPauseTime = 2 * time.Second
// waitUntilrepeatMax is the maximum number of times to repeat the wait until condition.
waitUntilrepeatMax = 60
)
func New(t *testing.T, containerManager *containers.Manager, id string, initValidatorConfigs []*initialization.NodeConfig) *Config {
numVal := float32(len(initValidatorConfigs))
return &Config{
ChainMeta: initialization.ChainMeta{
Id: id,
},
ValidatorInitConfigs: initValidatorConfigs,
VotingPeriod: config.PropDepositBlocks + numVal*config.PropVoteBlocks + config.PropBufferBlocks,
ExpeditedVotingPeriod: config.PropDepositBlocks + numVal*config.PropVoteBlocks + config.PropBufferBlocks - 2,
t: t,
containerManager: containerManager,
}
}
// CreateNode returns new initialized NodeConfig.
func (c *Config) CreateNode(initNode *initialization.Node) *NodeConfig {
nodeConfig := &NodeConfig{
Node: *initNode,
chainId: c.Id,
containerManager: c.containerManager,
t: c.t,
}
c.NodeConfigs = append(c.NodeConfigs, nodeConfig)
return nodeConfig
}
// RemoveNode removes node and stops it from running.
func (c *Config) RemoveNode(nodeName string) error {
for i, node := range c.NodeConfigs {
if node.Name == nodeName {
c.NodeConfigs = append(c.NodeConfigs[:i], c.NodeConfigs[i+1:]...)
return node.Stop()
}
}
return fmt.Errorf("node %s not found", nodeName)
}
// WaitUntilEpoch waits for the chain to reach the specified epoch.
func (c *Config) WaitUntilEpoch(epoch int64, epochIdentifier string) {
node, err := c.GetDefaultNode()
require.NoError(c.t, err)
node.WaitUntil(func(_ coretypes.SyncInfo) bool {
newEpochNumber := node.QueryCurrentEpoch(epochIdentifier)
c.t.Logf("current epoch number is (%d), waiting to reach (%d)", newEpochNumber, epoch)
return newEpochNumber >= epoch
})
}
// WaitForNumEpochs waits for the chain to to go through a given number of epochs.
func (c *Config) WaitForNumEpochs(epochsToWait int64, epochIdentifier string) {
node, err := c.GetDefaultNode()
require.NoError(c.t, err)
oldEpochNumber := node.QueryCurrentEpoch(epochIdentifier)
c.WaitUntilEpoch(oldEpochNumber+epochsToWait, epochIdentifier)
}
// WaitUntilHeight waits for all validators to reach the specified height at the minimum.
// returns error, if any.
func (c *Config) WaitUntilHeight(height int64) {
// Ensure the nodes are making progress.
doneCondition := func(syncInfo coretypes.SyncInfo) bool {
curHeight := syncInfo.LatestBlockHeight
if curHeight < height {
c.t.Logf("current block height is %d, waiting to reach: %d", curHeight, height)
return false
}
return !syncInfo.CatchingUp
}
for _, node := range c.NodeConfigs {
c.t.Logf("node container: %s, waiting to reach height %d", node.Name, height)
node.WaitUntil(doneCondition)
}
}
// WaitForNumHeights waits for all nodes to go through a given number of heights.
func (c *Config) WaitForNumHeights(heightsToWait int64) {
node, err := c.GetDefaultNode()
require.NoError(c.t, err)
currentHeight, err := node.QueryCurrentHeight()
require.NoError(c.t, err)
c.WaitUntilHeight(currentHeight + heightsToWait)
}
func (c *Config) SendIBC(dstChain *Config, recipient string, token sdk.Coin) {
c.t.Logf("IBC sending %s from %s to %s (%s)", token, c.Id, dstChain.Id, recipient)
dstNode, err := dstChain.GetDefaultNode()
require.NoError(c.t, err)
balancesDstPre, err := dstNode.QueryBalances(recipient)
require.NoError(c.t, err)
cmd := []string{"hermes", "tx", "raw", "ft-transfer", dstChain.Id, c.Id, "transfer", "channel-0", token.Amount.String(), fmt.Sprintf("--denom=%s", token.Denom), fmt.Sprintf("--receiver=%s", recipient), "--timeout-height-offset=1000"}
_, _, err = c.containerManager.ExecHermesCmd(c.t, cmd, "Success")
require.NoError(c.t, err)
require.Eventually(
c.t,
func() bool {
balancesDstPost, err := dstNode.QueryBalances(recipient)
require.NoError(c.t, err)
ibcCoin := balancesDstPost.Sub(balancesDstPre)
if ibcCoin.Len() == 1 {
tokenPre := balancesDstPre.AmountOfNoDenomValidation(ibcCoin[0].Denom)
tokenPost := balancesDstPost.AmountOfNoDenomValidation(ibcCoin[0].Denom)
resPre := token.Amount
resPost := tokenPost.Sub(tokenPre)
return resPost.Uint64() == resPre.Uint64()
} else {
return false
}
},
5*time.Minute,
time.Second,
"tx not received on destination chain",
)
c.t.Log("successfully sent IBC tokens")
}
func (c *Config) EnableSuperfluidAsset(denom string) {
chain, err := c.GetDefaultNode()
require.NoError(c.t, err)
chain.SubmitSuperfluidProposal(denom, sdk.NewCoin(appparams.BaseCoinUnit, sdk.NewInt(config.InitialMinDeposit)))
c.LatestProposalNumber += 1
chain.DepositProposal(c.LatestProposalNumber, false)
for _, node := range c.NodeConfigs {
node.VoteYesProposal(initialization.ValidatorWalletName, c.LatestProposalNumber)
}
}
func (c *Config) LockAndAddToExistingLock(amount sdk.Int, denom, lockupWalletAddr, lockupWalletSuperfluidAddr string) {
chain, err := c.GetDefaultNode()
require.NoError(c.t, err)
// lock tokens
chain.LockTokens(fmt.Sprintf("%v%s", amount, denom), "240s", lockupWalletAddr)
c.LatestLockNumber += 1
// add to existing lock
chain.AddToExistingLock(amount, denom, "240s", lockupWalletAddr)
// superfluid lock tokens
chain.LockTokens(fmt.Sprintf("%v%s", amount, denom), "240s", lockupWalletSuperfluidAddr)
c.LatestLockNumber += 1
chain.SuperfluidDelegate(c.LatestLockNumber, c.NodeConfigs[1].OperatorAddress, lockupWalletSuperfluidAddr)
// add to existing lock
chain.AddToExistingLock(amount, denom, "240s", lockupWalletSuperfluidAddr)
}
// GetDefaultNode returns the default node of the chain.
// The default node is the first one created. Returns error if no
// ndoes created.
func (c *Config) GetDefaultNode() (*NodeConfig, error) {
return c.getNodeAtIndex(defaultNodeIndex)
}
// GetPersistentPeers returns persistent peers from every node
// associated with a chain.
func (c *Config) GetPersistentPeers() []string {
peers := make([]string, len(c.NodeConfigs))
for i, node := range c.NodeConfigs {
peers[i] = node.PeerId
}
return peers
}
func (c *Config) getNodeAtIndex(nodeIndex int) (*NodeConfig, error) {
if nodeIndex > len(c.NodeConfigs) {
return nil, fmt.Errorf("node index (%d) is greter than the number of nodes available (%d)", nodeIndex, len(c.NodeConfigs))
}
return c.NodeConfigs[nodeIndex], nil
}