forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 3
/
interchain_workflow.go
219 lines (191 loc) · 6.53 KB
/
interchain_workflow.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package p
import (
"math/big"
"time"
"github.com/MetalBlockchain/coreth/plugin/evm"
"github.com/spf13/cast"
"github.com/stretchr/testify/require"
"github.com/MetalBlockchain/metalgo/api/info"
"github.com/MetalBlockchain/metalgo/config"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/tests/fixture/e2e"
"github.com/MetalBlockchain/metalgo/tests/fixture/tmpnet"
"github.com/MetalBlockchain/metalgo/utils/constants"
"github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1"
"github.com/MetalBlockchain/metalgo/utils/set"
"github.com/MetalBlockchain/metalgo/utils/units"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/platformvm/reward"
"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
"github.com/MetalBlockchain/metalgo/wallet/subnet/primary/common"
ginkgo "github.com/onsi/ginkgo/v2"
)
var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainLabel), func() {
require := require.New(ginkgo.GinkgoT())
const (
transferAmount = 10 * units.Avax
weight = 2_000 * units.Avax // Used for both validation and delegation
)
ginkgo.It("should ensure that funds can be transferred from the P-Chain to the X-Chain and the C-Chain", func() {
network := e2e.Env.GetNetwork()
ginkgo.By("checking that the network has a compatible minimum stake duration", func() {
minStakeDuration := cast.ToDuration(network.DefaultFlags[config.MinStakeDurationKey])
require.Equal(tmpnet.DefaultMinStakeDuration, minStakeDuration)
})
ginkgo.By("creating wallet with a funded key to send from and recipient key to deliver to")
recipientKey, err := secp256k1.NewPrivateKey()
require.NoError(err)
keychain := e2e.Env.NewKeychain(1)
keychain.Add(recipientKey)
nodeURI := e2e.Env.GetRandomNodeURI()
baseWallet := e2e.NewWallet(keychain, nodeURI)
xWallet := baseWallet.X()
cWallet := baseWallet.C()
pWallet := baseWallet.P()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
pBuilder := pWallet.Builder()
pContext := pBuilder.Context()
ginkgo.By("defining common configuration")
recipientEthAddress := evm.GetEthAddress(recipientKey)
avaxAssetID := xContext.AVAXAssetID
// Use the same owner for sending to X-Chain and importing funds to P-Chain
recipientOwner := secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
recipientKey.Address(),
},
}
// Use the same outputs for both X-Chain and C-Chain exports
exportOutputs := []*avax.TransferableOutput{
{
Asset: avax.Asset{
ID: avaxAssetID,
},
Out: &secp256k1fx.TransferOutput{
Amt: transferAmount,
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
keychain.Keys[0].Address(),
},
},
},
},
}
ginkgo.By("adding new node and waiting for it to report healthy")
node := e2e.AddEphemeralNode(network, tmpnet.FlagsMap{})
e2e.WaitForHealthy(node)
ginkgo.By("retrieving new node's id and pop")
infoClient := info.NewClient(node.URI)
nodeID, nodePOP, err := infoClient.GetNodeID(e2e.DefaultContext())
require.NoError(err)
// Adding a validator should not break interchain transfer.
endTime := time.Now().Add(30 * time.Second)
ginkgo.By("adding the new node as a validator", func() {
rewardKey, err := secp256k1.NewPrivateKey()
require.NoError(err)
const (
delegationPercent = 0.10 // 10%
delegationShare = reward.PercentDenominator * delegationPercent
)
_, err = pWallet.IssueAddPermissionlessValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: nodeID,
End: uint64(endTime.Unix()),
Wght: weight,
},
Subnet: constants.PrimaryNetworkID,
},
nodePOP,
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{rewardKey.Address()},
},
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{rewardKey.Address()},
},
delegationShare,
e2e.WithDefaultContext(),
)
require.NoError(err)
})
// Adding a delegator should not break interchain transfer.
ginkgo.By("adding a delegator to the new node", func() {
rewardKey, err := secp256k1.NewPrivateKey()
require.NoError(err)
_, err = pWallet.IssueAddPermissionlessDelegatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: nodeID,
End: uint64(endTime.Unix()),
Wght: weight,
},
Subnet: constants.PrimaryNetworkID,
},
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{rewardKey.Address()},
},
e2e.WithDefaultContext(),
)
require.NoError(err)
})
ginkgo.By("exporting AVAX from the P-Chain to the X-Chain", func() {
_, err := pWallet.IssueExportTx(
xContext.BlockchainID,
exportOutputs,
e2e.WithDefaultContext(),
)
require.NoError(err)
})
ginkgo.By("importing AVAX from the P-Chain to the X-Chain", func() {
_, err := xWallet.IssueImportTx(
constants.PlatformChainID,
&recipientOwner,
e2e.WithDefaultContext(),
)
require.NoError(err)
})
ginkgo.By("checking that the recipient address has received imported funds on the X-Chain", func() {
balances, err := xWallet.Builder().GetFTBalance(common.WithCustomAddresses(set.Of(
recipientKey.Address(),
)))
require.NoError(err)
require.Positive(balances[avaxAssetID])
})
ginkgo.By("exporting AVAX from the P-Chain to the C-Chain", func() {
_, err := pWallet.IssueExportTx(
cWallet.BlockchainID(),
exportOutputs,
e2e.WithDefaultContext(),
)
require.NoError(err)
})
ginkgo.By("initializing a new eth client")
ethClient := e2e.NewEthClient(nodeURI)
ginkgo.By("importing AVAX from the P-Chain to the C-Chain", func() {
_, err := cWallet.IssueImportTx(
constants.PlatformChainID,
recipientEthAddress,
e2e.WithDefaultContext(),
e2e.WithSuggestedGasPrice(ethClient),
)
require.NoError(err)
})
ginkgo.By("checking that the recipient address has received imported funds on the C-Chain")
balance, err := ethClient.BalanceAt(e2e.DefaultContext(), recipientEthAddress, nil)
require.NoError(err)
require.Positive(balance.Cmp(big.NewInt(0)))
ginkgo.By("stopping validator node to free up resources for a bootstrap check")
require.NoError(node.Stop(e2e.DefaultContext()))
e2e.CheckBootstrapIsPossible(network)
})
})