forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 3
/
permissionless_subnets.go
177 lines (158 loc) · 4.6 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package p
import (
"fmt"
"time"
"github.com/stretchr/testify/require"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/tests/fixture/e2e"
"github.com/MetalBlockchain/metalgo/utils/constants"
"github.com/MetalBlockchain/metalgo/utils/units"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/components/verify"
"github.com/MetalBlockchain/metalgo/vms/platformvm"
"github.com/MetalBlockchain/metalgo/vms/platformvm/reward"
"github.com/MetalBlockchain/metalgo/vms/platformvm/signer"
"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
ginkgo "github.com/onsi/ginkgo/v2"
)
var _ = e2e.DescribePChain("[Permissionless Subnets]", func() {
require := require.New(ginkgo.GinkgoT())
ginkgo.It("subnets operations",
func() {
nodeURI := e2e.Env.GetRandomNodeURI()
keychain := e2e.Env.NewKeychain(1)
baseWallet := e2e.NewWallet(keychain, nodeURI)
pWallet := baseWallet.P()
xWallet := baseWallet.X()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
xChainID := xContext.BlockchainID
var validatorID ids.NodeID
ginkgo.By("retrieving the node ID of a primary network validator", func() {
pChainClient := platformvm.NewClient(nodeURI.URI)
validatorIDs, err := pChainClient.SampleValidators(e2e.DefaultContext(), constants.PrimaryNetworkID, 1)
require.NoError(err)
validatorID = validatorIDs[0]
})
owner := &secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
keychain.Keys[0].Address(),
},
}
var subnetID ids.ID
ginkgo.By("create a permissioned subnet", func() {
subnetTx, err := pWallet.IssueCreateSubnetTx(
owner,
e2e.WithDefaultContext(),
)
subnetID = subnetTx.ID()
require.NoError(err)
require.NotEqual(subnetID, constants.PrimaryNetworkID)
})
var subnetAssetID ids.ID
ginkgo.By("create a custom asset for the permissionless subnet", func() {
subnetAssetTx, err := xWallet.IssueCreateAssetTx(
"RnM",
"RNM",
9,
map[uint32][]verify.State{
0: {
&secp256k1fx.TransferOutput{
Amt: 100 * units.MegaAvax,
OutputOwners: *owner,
},
},
},
e2e.WithDefaultContext(),
)
require.NoError(err)
subnetAssetID = subnetAssetTx.ID()
})
ginkgo.By(fmt.Sprintf("Send 100 MegaAvax of asset %s to the P-chain", subnetAssetID), func() {
_, err := xWallet.IssueExportTx(
constants.PlatformChainID,
[]*avax.TransferableOutput{
{
Asset: avax.Asset{
ID: subnetAssetID,
},
Out: &secp256k1fx.TransferOutput{
Amt: 100 * units.MegaAvax,
OutputOwners: *owner,
},
},
},
e2e.WithDefaultContext(),
)
require.NoError(err)
})
ginkgo.By(fmt.Sprintf("Import the 100 MegaAvax of asset %s from the X-chain into the P-chain", subnetAssetID), func() {
_, err := pWallet.IssueImportTx(
xChainID,
owner,
e2e.WithDefaultContext(),
)
require.NoError(err)
})
ginkgo.By("make subnet permissionless", func() {
_, 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,
e2e.WithDefaultContext(),
)
require.NoError(err)
})
endTime := time.Now().Add(time.Minute)
ginkgo.By("add permissionless validator", func() {
_, err := pWallet.IssueAddPermissionlessValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: validatorID,
End: uint64(endTime.Unix()),
Wght: 25 * units.MegaAvax,
},
Subnet: subnetID,
},
&signer.Empty{},
subnetAssetID,
&secp256k1fx.OutputOwners{},
&secp256k1fx.OutputOwners{},
reward.PercentDenominator,
e2e.WithDefaultContext(),
)
require.NoError(err)
})
ginkgo.By("add permissionless delegator", func() {
_, err := pWallet.IssueAddPermissionlessDelegatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: validatorID,
End: uint64(endTime.Unix()),
Wght: 25 * units.MegaAvax,
},
Subnet: subnetID,
},
subnetAssetID,
&secp256k1fx.OutputOwners{},
e2e.WithDefaultContext(),
)
require.NoError(err)
})
})
})