github.com/MetalBlockchain/metalgo@v1.11.9/genesis/generate/validators/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  	"encoding/json"
     9  	"log"
    10  
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  	"github.com/MetalBlockchain/metalgo/utils/constants"
    13  	"github.com/MetalBlockchain/metalgo/utils/perms"
    14  	"github.com/MetalBlockchain/metalgo/utils/set"
    15  	"github.com/MetalBlockchain/metalgo/vms/platformvm"
    16  	"github.com/MetalBlockchain/metalgo/wallet/subnet/primary"
    17  )
    18  
    19  // This fetches the current validator set of both Fuji and Mainnet.
    20  func main() {
    21  	ctx := context.Background()
    22  
    23  	fujiValidators, err := getCurrentValidators(ctx, primary.TahoeAPIURI)
    24  	if err != nil {
    25  		log.Fatalf("failed to fetch Fuji validators: %v", err)
    26  	}
    27  
    28  	mainnetValidators, err := getCurrentValidators(ctx, primary.MainnetAPIURI)
    29  	if err != nil {
    30  		log.Fatalf("failed to fetch Mainnet validators: %v", err)
    31  	}
    32  
    33  	validators := map[string]set.Set[ids.NodeID]{
    34  		constants.TahoeName:   fujiValidators,
    35  		constants.MainnetName: mainnetValidators,
    36  	}
    37  	validatorsJSON, err := json.MarshalIndent(validators, "", "\t")
    38  	if err != nil {
    39  		log.Fatalf("failed to marshal validators: %v", err)
    40  	}
    41  
    42  	if err := perms.WriteFile("validators.json", validatorsJSON, perms.ReadWrite); err != nil {
    43  		log.Fatalf("failed to write validators: %v", err)
    44  	}
    45  }
    46  
    47  func getCurrentValidators(ctx context.Context, uri string) (set.Set[ids.NodeID], error) {
    48  	client := platformvm.NewClient(uri)
    49  	currentValidators, err := client.GetCurrentValidators(
    50  		ctx,
    51  		constants.PrimaryNetworkID,
    52  		nil, // fetch all validators
    53  	)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	var nodeIDs set.Set[ids.NodeID]
    59  	for _, validator := range currentValidators {
    60  		nodeIDs.Add(validator.NodeID)
    61  	}
    62  	return nodeIDs, nil
    63  }