Skip to content

Commit a8b6471

Browse files
authored
Merge pull request #667 from ava-labs/check-for-elastic-subnet
check for permissioness subnet status for creating elastic subnet, adding validators, adding delegators
2 parents 171edf6 + 869872a commit a8b6471

File tree

1 file changed

+68
-17
lines changed

1 file changed

+68
-17
lines changed

local/blockchain.go

+68-17
Original file line numberDiff line numberDiff line change
@@ -961,15 +961,28 @@ func (ln *localNetwork) addPermissionlessDelegators(
961961
}
962962
platformCli := platformvm.NewClient(clientURI)
963963
// wallet needs txs for all previously created subnets
964-
preloadTXs := make([]ids.ID, len(delegatorSpecs))
964+
subnetIDs := make([]ids.ID, len(delegatorSpecs))
965965
for i, delegatorSpec := range delegatorSpecs {
966966
subnetID, err := ids.FromString(delegatorSpec.SubnetID)
967967
if err != nil {
968968
return err
969969
}
970-
preloadTXs[i] = subnetID
970+
subnetIDs[i] = subnetID
971971
}
972-
w, err := newWallet(ctx, clientURI, preloadTXs)
972+
// check for all subnets to be permissionless
973+
for _, subnetID := range subnetIDs {
974+
b, err := subnetIsPermissionless(ctx, platformCli, subnetID)
975+
if err != nil {
976+
return err
977+
}
978+
if !b {
979+
msg := fmt.Sprintf("subnet %s is not permissionless", subnetID)
980+
ln.log.Info(logging.Red.Wrap(msg))
981+
return errors.New(msg)
982+
}
983+
}
984+
// wallet needs txs for all existent subnets
985+
w, err := newWallet(ctx, clientURI, subnetIDs)
973986
if err != nil {
974987
return err
975988
}
@@ -1049,16 +1062,28 @@ func (ln *localNetwork) addPermissionlessValidators(
10491062
return err
10501063
}
10511064
platformCli := platformvm.NewClient(clientURI)
1052-
// wallet needs txs for all previously created subnets
1053-
preloadTXs := make([]ids.ID, len(validatorSpecs))
1065+
subnetIDs := make([]ids.ID, len(validatorSpecs))
10541066
for i, validatorSpec := range validatorSpecs {
10551067
subnetID, err := ids.FromString(validatorSpec.SubnetID)
10561068
if err != nil {
10571069
return err
10581070
}
1059-
preloadTXs[i] = subnetID
1071+
subnetIDs[i] = subnetID
10601072
}
1061-
w, err := newWallet(ctx, clientURI, preloadTXs)
1073+
// check for all subnets to be permissionless
1074+
for _, subnetID := range subnetIDs {
1075+
b, err := subnetIsPermissionless(ctx, platformCli, subnetID)
1076+
if err != nil {
1077+
return err
1078+
}
1079+
if !b {
1080+
msg := fmt.Sprintf("subnet %s is not permissionless", subnetID)
1081+
ln.log.Info(logging.Red.Wrap(msg))
1082+
return errors.New(msg)
1083+
}
1084+
}
1085+
// wallet needs txs for all existent subnets
1086+
w, err := newWallet(ctx, clientURI, subnetIDs)
10621087
if err != nil {
10631088
return err
10641089
}
@@ -1170,20 +1195,32 @@ func (ln *localNetwork) transformToElasticSubnets(
11701195
if err != nil {
11711196
return nil, nil, err
11721197
}
1173-
// wallet needs txs for all previously created subnets
1174-
var preloadTXs []ids.ID
1175-
for _, elasticSubnetSpec := range elasticSubnetSpecs {
1198+
platformCli := platformvm.NewClient(clientURI)
1199+
subnetIDs := make([]ids.ID, len(elasticSubnetSpecs))
1200+
for i, elasticSubnetSpec := range elasticSubnetSpecs {
11761201
if elasticSubnetSpec.SubnetID == nil {
11771202
return nil, nil, errors.New("elastic subnet spec has no subnet ID")
1178-
} else {
1179-
subnetID, err := ids.FromString(*elasticSubnetSpec.SubnetID)
1180-
if err != nil {
1181-
return nil, nil, err
1182-
}
1183-
preloadTXs = append(preloadTXs, subnetID)
11841203
}
1204+
subnetID, err := ids.FromString(*elasticSubnetSpec.SubnetID)
1205+
if err != nil {
1206+
return nil, nil, err
1207+
}
1208+
subnetIDs[i] = subnetID
11851209
}
1186-
w, err := newWallet(ctx, clientURI, preloadTXs)
1210+
// check for all subnets to be not permissionless
1211+
for _, subnetID := range subnetIDs {
1212+
b, err := subnetIsPermissionless(ctx, platformCli, subnetID)
1213+
if err != nil {
1214+
return nil, nil, err
1215+
}
1216+
if b {
1217+
msg := fmt.Sprintf("subnet %s is already permissionless", subnetID)
1218+
ln.log.Info(logging.Red.Wrap(msg))
1219+
return nil, nil, errors.New(msg)
1220+
}
1221+
}
1222+
// wallet needs txs for all existent subnets
1223+
w, err := newWallet(ctx, clientURI, subnetIDs)
11871224
if err != nil {
11881225
return nil, nil, err
11891226
}
@@ -1634,3 +1671,17 @@ func createDefaultCtx(ctx context.Context) (context.Context, context.CancelFunc)
16341671
}
16351672
return context.WithTimeout(ctx, defaultTimeout)
16361673
}
1674+
1675+
func subnetIsPermissionless(
1676+
ctx context.Context,
1677+
platformCli platformvm.Client,
1678+
subnetID ids.ID,
1679+
) (bool, error) {
1680+
if _, _, err := platformCli.GetCurrentSupply(ctx, subnetID); err != nil {
1681+
if strings.Contains(err.Error(), "fetching current supply failed: not found") {
1682+
return false, nil
1683+
}
1684+
return false, fmt.Errorf("failure checking if subnet %s is permissionles: %w", subnetID, err)
1685+
}
1686+
return true, nil
1687+
}

0 commit comments

Comments
 (0)