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