Files
netrunner/utils/utils.go
T

131 lines
3.4 KiB
Go
Raw Normal View History

package utils
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
2022-06-14 14:41:45 -04:00
"time"
"github.com/luxfi/ids"
"github.com/luxfi/log"
2025-07-17 16:29:44 -05:00
rpcb "github.com/luxfi/netrunner/rpcpb"
"github.com/luxfi/netrunner/ux"
"github.com/luxfi/node/staking"
)
2022-06-27 12:21:32 -03:00
const (
genesisNetworkIDKey = "networkID"
dirTimestampFormat = "20060102_150405"
)
2022-05-27 15:07:04 -03:00
func ToNodeID(stakingKey, stakingCert []byte) (ids.NodeID, error) {
2022-04-17 18:33:08 -04:00
cert, err := staking.LoadTLSCertFromBytes(stakingKey, stakingCert)
if err != nil {
2022-05-27 15:07:04 -03:00
return ids.EmptyNodeID, err
}
2025-08-06 04:56:44 +00:00
stakingCertificate := &ids.Certificate{
2025-07-18 06:49:13 +00:00
Raw: cert.Leaf.Raw,
PublicKey: cert.Leaf.PublicKey,
}
nodeID := ids.NodeIDFromCert(stakingCertificate)
2022-04-17 18:33:08 -04:00
return nodeID, nil
}
// Returns the network ID in the given genesis
func NetworkIDFromGenesis(genesis []byte) (uint32, error) {
genesisMap := map[string]interface{}{}
if err := json.Unmarshal(genesis, &genesisMap); err != nil {
return 0, fmt.Errorf("couldn't unmarshal genesis: %w", err)
}
networkIDIntf, ok := genesisMap[genesisNetworkIDKey]
if !ok {
return 0, fmt.Errorf("couldn't find key %q in genesis", genesisNetworkIDKey)
}
networkID, ok := networkIDIntf.(float64)
if !ok {
return 0, fmt.Errorf("expected float64 but got %T", networkIDIntf)
}
return uint32(networkID), nil
}
2021-12-20 16:16:13 -05:00
var (
2024-01-02 20:55:14 +01:00
ErrInvalidExecPath = errors.New("lux exec is invalid")
ErrNotExists = errors.New("lux exec not exists")
2023-04-27 12:17:05 -03:00
ErrNotExistsPlugin = errors.New("plugin exec not exists")
)
2022-06-02 21:55:45 -03:00
func CheckExecPath(exec string) error {
if exec == "" {
return ErrInvalidExecPath
}
_, err := os.Stat(exec)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return ErrNotExists
}
return fmt.Errorf("failed to stat exec %q (%w)", exec, err)
}
2022-06-02 21:55:45 -03:00
return nil
}
2023-04-27 12:17:05 -03:00
func CheckPluginPath(pluginExec string) error {
2022-06-02 21:55:45 -03:00
var err error
if _, err = os.Stat(pluginExec); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return ErrNotExistsPlugin
}
return fmt.Errorf("failed to stat plugin exec %q (%w)", pluginExec, err)
}
return nil
}
func VMID(vmName string) (ids.ID, error) {
if len(vmName) > 32 {
return ids.Empty, fmt.Errorf("VM name must be <= 32 bytes, found %d", len(vmName))
}
b := make([]byte, 32)
copy(b, []byte(vmName))
return ids.ToID(b)
}
2022-06-14 14:41:45 -04:00
func MkDirWithTimestamp(dirPrefix string) (string, error) {
2022-06-27 12:21:32 -03:00
currentTime := time.Now().Format(dirTimestampFormat)
dirName := dirPrefix + "_" + currentTime
2022-06-14 14:41:45 -04:00
return dirName, os.MkdirAll(dirName, os.ModePerm)
}
2023-04-10 11:59:50 -04:00
func VerifyChainHasCorrectParticipants(
logger log.Logger,
chainParticipants []string,
2023-04-10 12:04:37 -04:00
cluster *rpcb.ClusterInfo,
chainID string,
2023-04-10 12:04:37 -04:00
) bool {
2023-04-10 11:59:50 -04:00
if cluster != nil {
participatingNodeNames := cluster.Chains[chainID].GetChainParticipants().GetNodeNames()
2023-04-13 23:12:14 -04:00
2023-04-10 11:59:50 -04:00
var nodeIsInList bool
// Check that all chain validators are equal to the node IDs added as participant in chain creation
for _, node := range chainParticipants {
2023-04-10 11:59:50 -04:00
nodeIsInList = false
for _, chainValidator := range participatingNodeNames {
if chainValidator == node {
2023-04-10 11:59:50 -04:00
nodeIsInList = true
break
}
}
if !nodeIsInList {
ux.Print(logger, "%s", log.Red.Wrap(fmt.Sprintf("VerifyChainHasCorrectParticipants: %#v", cluster)))
ux.Print(logger, "%s", log.Red.Wrap(fmt.Sprintf("VerifyChainHasCorrectParticipants: node not in list chain %q node %q %v %v", chainID, node, chainParticipants, participatingNodeNames)))
2023-04-10 11:59:50 -04:00
return false
}
}
return true
} else {
ux.Print(logger, "%s", log.Red.Wrap("VerifyChainHasCorrectParticipants: cluster is nil"))
2023-04-10 11:59:50 -04:00
}
return false
}