github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/cmd/bootstrap/utils/node_info.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	model "github.com/onflow/flow-go/model/bootstrap"
     9  	"github.com/onflow/flow-go/model/encodable"
    10  	"github.com/onflow/flow-go/model/flow"
    11  	io "github.com/onflow/flow-go/utils/io"
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  // WritePartnerFiles writes the all partner public node info into `bootDir/partners/public-root-information/`
    16  // also writes a map containing each of the nodes weights mapped by NodeID
    17  func WritePartnerFiles(nodeInfos []model.NodeInfo, bootDir string) (string, string, error) {
    18  
    19  	// convert to public nodeInfos and map stkes
    20  	nodePubInfos := make([]model.NodeInfoPub, len(nodeInfos))
    21  	weights := make(map[flow.Identifier]uint64)
    22  	for i, node := range nodeInfos {
    23  		nodePubInfos[i] = node.Public()
    24  		weights[node.NodeID] = node.Weight
    25  	}
    26  
    27  	// write node public infos to partner dir
    28  	partnersDir := filepath.Join(bootDir, "partners")
    29  	err := os.MkdirAll(filepath.Join(bootDir, partnersDir), os.ModePerm)
    30  	if err != nil {
    31  		return "", "", fmt.Errorf("could not create partner node info directory: %w", err)
    32  	}
    33  
    34  	// write each node info into partners dir
    35  	for _, node := range nodePubInfos {
    36  		nodePubInfosPath := filepath.Join(partnersDir, fmt.Sprintf(model.PathNodeInfoPub, node.NodeID.String()))
    37  		err := io.WriteJSON(nodePubInfosPath, node)
    38  		if err != nil {
    39  			return "", "", fmt.Errorf("could not write partner node info: %w", err)
    40  		}
    41  	}
    42  
    43  	// write partner weights
    44  	weightsPath := filepath.Join(bootDir, model.FileNamePartnerWeights)
    45  	err = io.WriteJSON(weightsPath, weights)
    46  	if err != nil {
    47  		return "", "", fmt.Errorf("could not write partner weights info: %w", err)
    48  	}
    49  
    50  	return filepath.Join(partnersDir, model.DirnamePublicBootstrap), weightsPath, nil
    51  }
    52  
    53  // WriteInternalFiles writes the internal private node info into `bootDir/private-root-information/`
    54  // also writes a map containing each of the nodes weights mapped by the node's networking address
    55  func WriteInternalFiles(nodeInfos []model.NodeInfo, bootDir string) (string, string, error) {
    56  
    57  	// convert to private nodeInfos and node configuration map
    58  	nodePrivInfos := make([]model.NodeInfoPriv, len(nodeInfos))
    59  	configs := make([]model.NodeConfig, len(nodeInfos))
    60  	for i, node := range nodeInfos {
    61  
    62  		netPriv := unittest.NetworkingPrivKeyFixture()
    63  
    64  		stakePriv := unittest.StakingPrivKeyFixture()
    65  
    66  		nodePrivInfos[i] = model.NodeInfoPriv{
    67  			Role:    node.Role,
    68  			Address: node.Address,
    69  			NodeID:  node.NodeID,
    70  			NetworkPrivKey: encodable.NetworkPrivKey{
    71  				PrivateKey: netPriv,
    72  			},
    73  			StakingPrivKey: encodable.StakingPrivKey{
    74  				PrivateKey: stakePriv,
    75  			},
    76  		}
    77  
    78  		configs[i] = model.NodeConfig{
    79  			Role:    node.Role,
    80  			Address: node.Address,
    81  			Weight:  node.Weight,
    82  		}
    83  	}
    84  
    85  	// write config
    86  	configPath := filepath.Join(bootDir, "node-internal-infos.pub.json")
    87  	err := io.WriteJSON(configPath, configs)
    88  	if err != nil {
    89  		return "", "", fmt.Errorf("could not write internal node configuration: %w", err)
    90  	}
    91  
    92  	// write node private infos to internal priv dir
    93  	for _, node := range nodePrivInfos {
    94  		internalPrivPath := fmt.Sprintf(model.PathNodeInfoPriv, node.NodeID)
    95  		err = io.WriteJSON(filepath.Join(bootDir, internalPrivPath), node)
    96  		if err != nil {
    97  			return "", "", fmt.Errorf("could not write internal node info: %w", err)
    98  		}
    99  	}
   100  
   101  	return bootDir, configPath, nil
   102  }
   103  
   104  func GenerateNodeInfos(consensus, collection, execution, verification, access int) []model.NodeInfo {
   105  
   106  	nodes := make([]model.NodeInfo, 0)
   107  
   108  	// CONSENSUS = 1
   109  	consensusNodes := unittest.NodeInfosFixture(consensus,
   110  		unittest.WithRole(flow.RoleConsensus),
   111  		unittest.WithInitialWeight(flow.DefaultInitialWeight),
   112  	)
   113  	nodes = append(nodes, consensusNodes...)
   114  
   115  	// COLLECTION = 1
   116  	collectionNodes := unittest.NodeInfosFixture(collection,
   117  		unittest.WithRole(flow.RoleCollection),
   118  		unittest.WithInitialWeight(flow.DefaultInitialWeight),
   119  	)
   120  	nodes = append(nodes, collectionNodes...)
   121  
   122  	// EXECUTION = 1
   123  	executionNodes := unittest.NodeInfosFixture(execution,
   124  		unittest.WithRole(flow.RoleExecution),
   125  		unittest.WithInitialWeight(flow.DefaultInitialWeight),
   126  	)
   127  	nodes = append(nodes, executionNodes...)
   128  
   129  	// VERIFICATION = 1
   130  	verificationNodes := unittest.NodeInfosFixture(verification,
   131  		unittest.WithRole(flow.RoleVerification),
   132  		unittest.WithInitialWeight(flow.DefaultInitialWeight),
   133  	)
   134  	nodes = append(nodes, verificationNodes...)
   135  
   136  	// ACCESS = 1
   137  	accessNodes := unittest.NodeInfosFixture(access,
   138  		unittest.WithRole(flow.RoleAccess),
   139  		unittest.WithInitialWeight(flow.DefaultInitialWeight),
   140  	)
   141  	nodes = append(nodes, accessNodes...)
   142  
   143  	return nodes
   144  }