Skip to content

Commit

Permalink
add happy path tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kc1116 committed Mar 29, 2024
1 parent 7d09f47 commit ffbd640
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 168 deletions.
8 changes: 4 additions & 4 deletions cmd/bootstrap/cmd/final_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func assembleInternalNodesWithoutWeight() []model.NodeInfo {
common.ValidateAddressFormat(log, internal.Address)

// validate every single internal node
nodeID := ValidateNodeID(internal.NodeID)
nodeID := common.ValidateNodeID(log, internal.NodeID)
node := model.NewPrivateNodeInfo(
nodeID,
internal.Role,
Expand Down Expand Up @@ -279,9 +279,9 @@ func createPublicNodeInfo(nodes []model.NodeInfoPub) []model.NodeInfo {
common.ValidateAddressFormat(log, n.Address)

// validate every single partner node
nodeID := ValidateNodeID(n.NodeID)
networkPubKey := ValidateNetworkPubKey(n.NetworkPubKey)
stakingPubKey := ValidateStakingPubKey(n.StakingPubKey)
nodeID := common.ValidateNodeID(log, n.NodeID)
networkPubKey := common.ValidateNetworkPubKey(log, n.NetworkPubKey)
stakingPubKey := common.ValidateStakingPubKey(log, n.StakingPubKey)

// all nodes should have equal weight
node := model.NewPublicNodeInfo(
Expand Down
31 changes: 1 addition & 30 deletions cmd/bootstrap/cmd/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/onflow/flow-go/fvm"
model "github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/dkg"
"github.com/onflow/flow-go/model/encodable"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/epochs"
"github.com/onflow/flow-go/state/protocol"
Expand All @@ -46,10 +45,7 @@ var (
flagGenesisTokenSupply string
)

// PartnerWeights is the format of the JSON file specifying partner node weights.
type PartnerWeights map[flow.Identifier]uint64

// finalizeCmd represents the finalize command
// finalizeCmd represents the finalize command`
var finalizeCmd = &cobra.Command{
Use: "finalize",
Short: "Finalize the bootstrapping process",
Expand Down Expand Up @@ -357,31 +353,6 @@ func readDKGData() dkg.DKGData {

// Validation utility methods ------------------------------------------------

func ValidateNodeID(nodeID flow.Identifier) flow.Identifier {
if nodeID == flow.ZeroID {
log.Fatal().Msg("NodeID must not be zero")
}
return nodeID
}

func ValidateNetworkPubKey(key encodable.NetworkPubKey) encodable.NetworkPubKey {
if key.PublicKey == nil {
log.Fatal().Msg("NetworkPubKey must not be nil")
}
return key
}

func ValidateStakingPubKey(key encodable.StakingPubKey) encodable.StakingPubKey {
if key.PublicKey == nil {
log.Fatal().Msg("StakingPubKey must not be nil")
}
return key
}

func ValidateWeight(weight uint64) (uint64, bool) {
return weight, weight > 0
}

// loadRootProtocolSnapshot loads the root protocol snapshot from disk
func loadRootProtocolSnapshot(path string) (*inmem.Snapshot, error) {
data, err := io.ReadFile(filepath.Join(flagOutdir, path))
Expand Down
4 changes: 2 additions & 2 deletions cmd/bootstrap/cmd/partner_infos.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func populatePartnerInfosRun(_ *cobra.Command, _ []string) {

flowClient := getFlowClient()

partnerWeights := make(PartnerWeights)
partnerWeights := make(common.PartnerWeights)
skippedNodes := 0
numOfPartnerNodesByRole := map[flow.Role]int{
flow.RoleCollection: 0,
Expand Down Expand Up @@ -210,7 +210,7 @@ func writeNodePubInfoFile(info *bootstrap.NodeInfoPub) {
}

// writePartnerWeightsFile writes the partner weights file
func writePartnerWeightsFile(partnerWeights PartnerWeights) {
func writePartnerWeightsFile(partnerWeights common.PartnerWeights) {
err := common.WriteJSON(bootstrap.FileNamePartnerWeights, flagOutdir, partnerWeights)
if err != nil {
log.Fatal().Err(err).Msg("failed to write json")
Expand Down
41 changes: 40 additions & 1 deletion cmd/util/cmd/common/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package common

import (
"errors"

"fmt"
"github.com/rs/zerolog"

"github.com/onflow/cadence"
"github.com/onflow/flow-go/cmd/bootstrap/run"
"github.com/onflow/flow-go/model/bootstrap"
model "github.com/onflow/flow-go/model/bootstrap"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/onflow/flow-go/model/flow/assignment"
"github.com/onflow/flow-go/model/flow/factory"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/module/signature"
)

// ConstructClusterAssignment random cluster assignment with internal and partner nodes.
Expand Down Expand Up @@ -104,6 +106,43 @@ func ConstructRootQCsForClusters(log zerolog.Logger, clusterList flow.ClusterLis
return qcs
}

// ConvertClusterAssignmentsCdc converts golang cluster assignments type to cadence array of arrays.
func ConvertClusterAssignmentsCdc(assignments flow.AssignmentList) cadence.Array {
assignmentsCdc := make([]cadence.Value, len(assignments))
for i, asmt := range assignments {
fmt.Println(asmt.Len())
vals := make([]cadence.Value, asmt.Len())
for j, k := range asmt {
fmt.Println(k.String())
vals[j] = cadence.String(k.String())
}
assignmentsCdc[i] = cadence.NewArray(vals).WithType(cadence.NewVariableSizedArrayType(cadence.StringType{}))
}

return cadence.NewArray(assignmentsCdc).WithType(cadence.NewVariableSizedArrayType(cadence.NewVariableSizedArrayType(cadence.StringType{})))
}

// ConvertClusterQcsCdc converts golang cluster qcs type to cadence struct.
func ConvertClusterQcsCdc(qcs []*flow.QuorumCertificate, clusterList flow.ClusterList) ([]*flow.ClusterQCVoteData, error) {
voteData := make([]*flow.ClusterQCVoteData, len(qcs))
for i, qc := range qcs {
c, ok := clusterList.ByIndex(uint(i))
if !ok {
return nil, fmt.Errorf("could not get cluster list for cluster index %v", i)
}
voterIds, err := signature.DecodeSignerIndicesToIdentifiers(c.NodeIDs(), qc.SignerIndices)
if err != nil {
return nil, fmt.Errorf("could not decode signer indices: %w", err)
}
voteData[i] = &flow.ClusterQCVoteData{
SigData: qc.SigData,
VoterIDs: voterIds,
}
}

return voteData, nil
}

// Filters a list of nodes to include only nodes that will sign the QC for the
// given cluster. The resulting list of nodes is only nodes that are in the
// given cluster AND are not partner nodes (ie. we have the private keys).
Expand Down
15 changes: 7 additions & 8 deletions cmd/util/cmd/common/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/rs/zerolog"

"github.com/onflow/flow-go/cmd/bootstrap/cmd"
"github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/flow"
)
Expand All @@ -16,7 +15,7 @@ func ReadPartnerNodeInfos(log zerolog.Logger, partnerWeightsPath, partnerNodeInf
partners := ReadPartnerNodes(log, partnerNodeInfoDir)
log.Info().Msgf("read %d partner node configuration files", len(partners))

var weights cmd.PartnerWeights
var weights PartnerWeights
err := ReadJSON(partnerWeightsPath, &weights)
if err != nil {
log.Fatal().Err(err).Msg("failed to read partner weights json")
Expand All @@ -26,10 +25,10 @@ func ReadPartnerNodeInfos(log zerolog.Logger, partnerWeightsPath, partnerNodeInf
var nodes []bootstrap.NodeInfo
for _, partner := range partners {
// validate every single partner node
nodeID := cmd.ValidateNodeID(partner.NodeID)
networkPubKey := cmd.ValidateNetworkPubKey(partner.NetworkPubKey)
stakingPubKey := cmd.ValidateStakingPubKey(partner.StakingPubKey)
weight, valid := cmd.ValidateWeight(weights[partner.NodeID])
nodeID := ValidateNodeID(log, partner.NodeID)
networkPubKey := ValidateNetworkPubKey(log, partner.NetworkPubKey)
stakingPubKey := ValidateStakingPubKey(log, partner.StakingPubKey)
weight, valid := ValidateWeight(weights[partner.NodeID])
if !valid {
log.Error().Msgf("weights: %v", weights)
log.Fatal().Msgf("partner node id %x has no weight", nodeID)
Expand Down Expand Up @@ -91,8 +90,8 @@ func ReadInternalNodeInfos(log zerolog.Logger, internalNodePrivInfoDir, internal
ValidateAddressFormat(log, internal.Address)

// validate every single internal node
nodeID := cmd.ValidateNodeID(internal.NodeID)
weight, valid := cmd.ValidateWeight(weights[internal.Address])
nodeID := ValidateNodeID(log, internal.NodeID)
weight, valid := ValidateWeight(weights[internal.Address])
if !valid {
log.Error().Msgf("weights: %v", weights)
log.Fatal().Msgf("internal node %v has no weight. Did you forget to update the node address?", internal)
Expand Down
31 changes: 30 additions & 1 deletion cmd/util/cmd/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"path/filepath"
"strconv"

"github.com/multiformats/go-multiaddr"
"github.com/rs/zerolog"

"github.com/multiformats/go-multiaddr"
"github.com/onflow/crypto"
"github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/encodable"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/network/p2p/utils"
"github.com/onflow/flow-go/utils/io"
Expand Down Expand Up @@ -128,3 +129,31 @@ func ValidateAddressFormat(log zerolog.Logger, address string) {
_, err = multiaddr.NewMultiaddr(lp2pAddr)
checkErr(err)
}

func ValidateNodeID(lg zerolog.Logger, nodeID flow.Identifier) flow.Identifier {
if nodeID == flow.ZeroID {
lg.Fatal().Msg("NodeID must not be zero")
}
return nodeID
}

func ValidateNetworkPubKey(lg zerolog.Logger, key encodable.NetworkPubKey) encodable.NetworkPubKey {
if key.PublicKey == nil {
lg.Fatal().Msg("NetworkPubKey must not be nil")
}
return key
}

func ValidateStakingPubKey(lg zerolog.Logger, key encodable.StakingPubKey) encodable.StakingPubKey {
if key.PublicKey == nil {
lg.Fatal().Msg("StakingPubKey must not be nil")
}
return key
}

func ValidateWeight(weight uint64) (uint64, bool) {
return weight, weight > 0
}

// PartnerWeights is the format of the JSON file specifying partner node weights.
type PartnerWeights map[flow.Identifier]uint64
Loading

0 comments on commit ffbd640

Please sign in to comment.