github.com/ava-labs/avalanchego@v1.11.11/wallet/subnet/primary/examples/add-permissioned-subnet-validator/main.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"log"
     9  	"time"
    10  
    11  	"github.com/ava-labs/avalanchego/api/info"
    12  	"github.com/ava-labs/avalanchego/genesis"
    13  	"github.com/ava-labs/avalanchego/ids"
    14  	"github.com/ava-labs/avalanchego/utils/units"
    15  	"github.com/ava-labs/avalanchego/vms/platformvm/txs"
    16  	"github.com/ava-labs/avalanchego/vms/secp256k1fx"
    17  	"github.com/ava-labs/avalanchego/wallet/subnet/primary"
    18  )
    19  
    20  func main() {
    21  	key := genesis.EWOQKey
    22  	uri := primary.LocalAPIURI
    23  	kc := secp256k1fx.NewKeychain(key)
    24  	subnetIDStr := "29uVeLPJB1eQJkzRemU8g8wZDw5uJRqpab5U2mX9euieVwiEbL"
    25  	startTime := time.Now().Add(time.Minute)
    26  	duration := 2 * 7 * 24 * time.Hour // 2 weeks
    27  	weight := units.Schmeckle
    28  
    29  	subnetID, err := ids.FromString(subnetIDStr)
    30  	if err != nil {
    31  		log.Fatalf("failed to parse subnet ID: %s\n", err)
    32  	}
    33  
    34  	ctx := context.Background()
    35  	infoClient := info.NewClient(uri)
    36  
    37  	nodeInfoStartTime := time.Now()
    38  	nodeID, _, err := infoClient.GetNodeID(ctx)
    39  	if err != nil {
    40  		log.Fatalf("failed to fetch node IDs: %s\n", err)
    41  	}
    42  	log.Printf("fetched node ID %s in %s\n", nodeID, time.Since(nodeInfoStartTime))
    43  
    44  	// MakeWallet fetches the available UTXOs owned by [kc] on the network that
    45  	// [uri] is hosting and registers [subnetID].
    46  	walletSyncStartTime := time.Now()
    47  	wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
    48  		URI:          uri,
    49  		AVAXKeychain: kc,
    50  		EthKeychain:  kc,
    51  		SubnetIDs:    []ids.ID{subnetID},
    52  	})
    53  	if err != nil {
    54  		log.Fatalf("failed to initialize wallet: %s\n", err)
    55  	}
    56  	log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
    57  
    58  	// Get the P-chain wallet
    59  	pWallet := wallet.P()
    60  
    61  	addValidatorStartTime := time.Now()
    62  	addValidatorTx, err := pWallet.IssueAddSubnetValidatorTx(&txs.SubnetValidator{
    63  		Validator: txs.Validator{
    64  			NodeID: nodeID,
    65  			Start:  uint64(startTime.Unix()),
    66  			End:    uint64(startTime.Add(duration).Unix()),
    67  			Wght:   weight,
    68  		},
    69  		Subnet: subnetID,
    70  	})
    71  	if err != nil {
    72  		log.Fatalf("failed to issue add subnet validator transaction: %s\n", err)
    73  	}
    74  	log.Printf("added new subnet validator %s to %s with %s in %s\n", nodeID, subnetID, addValidatorTx.ID(), time.Since(addValidatorStartTime))
    75  }