-
Notifications
You must be signed in to change notification settings - Fork 665
/
permissionless_subnets.go
249 lines (224 loc) · 8.22 KB
/
permissionless_subnets.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package p
import (
"context"
"fmt"
"time"
ginkgo "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/e2e"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)
var _ = e2e.DescribePChain("[Permissionless Subnets]", func() {
ginkgo.It("subnets operations",
// use this for filtering tests by labels
// ref. https://onsi.github.io/ginkgo/#spec-labels
ginkgo.Label(
"require-network-runner",
"xp",
"permissionless-subnets",
),
func() {
ginkgo.By("reload initial snapshot for test independence", func() {
err := e2e.Env.RestoreInitialState(true /*switchOffNetworkFirst*/)
gomega.Expect(err).Should(gomega.BeNil())
})
rpcEps := e2e.Env.GetURIs()
gomega.Expect(rpcEps).ShouldNot(gomega.BeEmpty())
nodeURI := rpcEps[0]
tests.Outf("{{blue}} setting up keys {{/}}\n")
testKey := genesis.EWOQKey
keyChain := secp256k1fx.NewKeychain(testKey)
var baseWallet primary.Wallet
ginkgo.By("setup wallet", func() {
var err error
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultWalletCreationTimeout)
baseWallet, err = primary.NewWalletFromURI(ctx, nodeURI, keyChain)
cancel()
gomega.Expect(err).Should(gomega.BeNil())
})
pWallet := baseWallet.P()
pChainClient := platformvm.NewClient(nodeURI)
xWallet := baseWallet.X()
xChainClient := avm.NewClient(nodeURI, xWallet.BlockchainID().String())
xChainID := xWallet.BlockchainID()
owner := &secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
testKey.PublicKey().Address(),
},
}
var subnetID ids.ID
ginkgo.By("create a permissioned subnet", func() {
var err error
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
subnetID, err = pWallet.IssueCreateSubnetTx(
owner,
common.WithContext(ctx),
)
cancel()
gomega.Expect(subnetID, err).Should(gomega.Not(gomega.Equal(constants.PrimaryNetworkID)))
ctx, cancel = context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
txStatus, err := pChainClient.GetTxStatus(ctx, subnetID)
cancel()
gomega.Expect(txStatus.Status, err).To(gomega.Equal(status.Committed))
})
var subnetAssetID ids.ID
ginkgo.By("create a custom asset for the permissionless subnet", func() {
var err error
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultWalletCreationTimeout)
subnetAssetID, err = xWallet.IssueCreateAssetTx(
"RnM",
"RNM",
9,
map[uint32][]verify.State{
0: {
&secp256k1fx.TransferOutput{
Amt: 100 * units.MegaAvax,
OutputOwners: *owner,
},
},
},
common.WithContext(ctx),
)
cancel()
gomega.Expect(err).Should(gomega.BeNil())
ctx, cancel = context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
txStatus, err := xChainClient.GetTxStatus(ctx, subnetAssetID)
cancel()
gomega.Expect(txStatus, err).To(gomega.Equal(choices.Accepted))
})
ginkgo.By(fmt.Sprintf("Send 100 MegaAvax of asset %s to the P-chain", subnetAssetID), func() {
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultWalletCreationTimeout)
exportTxID, err := xWallet.IssueExportTx(
constants.PlatformChainID,
[]*avax.TransferableOutput{
{
Asset: avax.Asset{
ID: subnetAssetID,
},
Out: &secp256k1fx.TransferOutput{
Amt: 100 * units.MegaAvax,
OutputOwners: *owner,
},
},
},
common.WithContext(ctx),
)
cancel()
gomega.Expect(err).Should(gomega.BeNil())
ctx, cancel = context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
txStatus, err := xChainClient.GetTxStatus(ctx, exportTxID)
cancel()
gomega.Expect(txStatus, err).To(gomega.Equal(choices.Accepted))
})
ginkgo.By(fmt.Sprintf("Import the 100 MegaAvax of asset %s from the X-chain into the P-chain", subnetAssetID), func() {
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultWalletCreationTimeout)
importTxID, err := pWallet.IssueImportTx(
xChainID,
owner,
common.WithContext(ctx),
)
cancel()
gomega.Expect(err).Should(gomega.BeNil())
ctx, cancel = context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
txStatus, err := pChainClient.GetTxStatus(ctx, importTxID)
cancel()
gomega.Expect(txStatus.Status, err).To(gomega.Equal(status.Committed))
})
ginkgo.By("make subnet permissionless", func() {
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
transformSubnetTxID, err := pWallet.IssueTransformSubnetTx(
subnetID,
subnetAssetID,
50*units.MegaAvax,
100*units.MegaAvax,
reward.PercentDenominator,
reward.PercentDenominator,
1,
100*units.MegaAvax,
time.Second,
365*24*time.Hour,
0,
1,
5,
.80*reward.PercentDenominator,
common.WithContext(ctx),
)
cancel()
gomega.Expect(err).Should(gomega.BeNil())
ctx, cancel = context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
txStatus, err := pChainClient.GetTxStatus(ctx, transformSubnetTxID)
cancel()
gomega.Expect(txStatus.Status, err).To(gomega.Equal(status.Committed))
})
validatorStartTime := time.Now().Add(time.Minute)
ginkgo.By("add permissionless validator", func() {
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
addSubnetValidatorTxID, err := pWallet.IssueAddPermissionlessValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: genesis.LocalConfig.InitialStakers[0].NodeID,
Start: uint64(validatorStartTime.Unix()),
End: uint64(validatorStartTime.Add(5 * time.Second).Unix()),
Wght: 25 * units.MegaAvax,
},
Subnet: subnetID,
},
&signer.Empty{},
subnetAssetID,
&secp256k1fx.OutputOwners{},
&secp256k1fx.OutputOwners{},
reward.PercentDenominator,
common.WithContext(ctx),
)
cancel()
gomega.Expect(err).Should(gomega.BeNil())
ctx, cancel = context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
txStatus, err := pChainClient.GetTxStatus(ctx, addSubnetValidatorTxID)
cancel()
gomega.Expect(txStatus.Status, err).To(gomega.Equal(status.Committed))
})
delegatorStartTime := validatorStartTime
ginkgo.By("add permissionless delegator", func() {
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
addSubnetDelegatorTxID, err := pWallet.IssueAddPermissionlessDelegatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: genesis.LocalConfig.InitialStakers[0].NodeID,
Start: uint64(delegatorStartTime.Unix()),
End: uint64(delegatorStartTime.Add(5 * time.Second).Unix()),
Wght: 25 * units.MegaAvax,
},
Subnet: subnetID,
},
subnetAssetID,
&secp256k1fx.OutputOwners{},
common.WithContext(ctx),
)
cancel()
gomega.Expect(err).Should(gomega.BeNil())
ctx, cancel = context.WithTimeout(context.Background(), e2e.DefaultConfirmTxTimeout)
txStatus, err := pChainClient.GetTxStatus(ctx, addSubnetDelegatorTxID)
cancel()
gomega.Expect(txStatus.Status, err).To(gomega.Equal(status.Committed))
})
})
})