Skip to content

Commit

Permalink
snapshot working with etna devnet
Browse files Browse the repository at this point in the history
  • Loading branch information
arturrez committed Sep 30, 2024
1 parent 22b9fbf commit ca86bb4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
20 changes: 13 additions & 7 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func NewNetwork(
redirectStderr bool,
walletPrivateKey string,
) (network.Network, error) {
beaconSet, err := utils.BeaconMapToSet(networkConfig.BeaconConfig)
if err != nil {
return nil, err
}
net, err := newNetwork(
log,
api.NewAPIClient,
Expand All @@ -192,7 +196,7 @@ func NewNetwork(
redirectStdout,
redirectStderr,
walletPrivateKey,
networkConfig.BeaconConfig,
beaconSet,
)
if err != nil {
return net, err
Expand Down Expand Up @@ -393,10 +397,6 @@ func NewDefaultConfigNNodes(
if int(numNodes) == 1 && !utils.IsPublicNetwork(networkID) && len(beaconConfig) == 0 {
flags[config.SybilProtectionEnabledKey] = false
}
beaconSet, err := utils.BeaconMapToSet(beaconConfig)
if err != nil {
return network.Config{}, err
}
cfg := network.Config{
NetworkID: networkID,
Flags: flags,
Expand All @@ -405,7 +405,7 @@ func NewDefaultConfigNNodes(
ChainConfigFiles: map[string]string{},
UpgradeConfigFiles: map[string]string{},
SubnetConfigFiles: map[string]string{},
BeaconConfig: beaconSet,
BeaconConfig: beaconConfig,
}
if len(upgradePath) != 0 {
upgrade, err := os.ReadFile(upgradePath)
Expand Down Expand Up @@ -485,6 +485,12 @@ func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Co
ln.flags = networkConfig.Flags
ln.binaryPath = networkConfig.BinaryPath
ln.chainConfigFiles = networkConfig.ChainConfigFiles

beaconConf, err := utils.BeaconMapToSet(networkConfig.BeaconConfig)
if err != nil {
return err
}
ln.bootstraps = beaconConf
if ln.chainConfigFiles == nil {
ln.chainConfigFiles = map[string]string{}
}
Expand Down Expand Up @@ -660,7 +666,7 @@ func (ln *localNetwork) addNode(nodeConfig node.Config) (node.Node, error) {
if err != nil {
return nil, err
}
if !isPausedNode && nodeConfig.IsBeacon {
if nodeConfig.IsBeacon && ln.bootstraps.Len() == 0 && !isPausedNode {
if err := ln.bootstraps.Add(beacon.New(nodeID, netip.AddrPortFrom(
ip,
nodeData.p2pPort,
Expand Down
6 changes: 5 additions & 1 deletion local/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (ln *localNetwork) persistNetwork() error {
nodeConfigs = append(nodeConfigs, nodeConfig)
}
// save network conf
beaconConf, err := utils.BeaconMapFromSet(ln.bootstraps)
if err != nil {
return err
}
networkConfig := network.Config{
NetworkID: ln.networkID,
Genesis: string(ln.genesisData),
Expand All @@ -168,7 +172,7 @@ func (ln *localNetwork) persistNetwork() error {
ChainConfigFiles: ln.chainConfigFiles,
UpgradeConfigFiles: ln.upgradeConfigFiles,
SubnetConfigFiles: ln.subnetConfigFiles,
BeaconConfig: ln.bootstraps,
BeaconConfig: beaconConf,
}
networkConfigJSON, err := json.MarshalIndent(networkConfig, "", " ")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"errors"
"fmt"
"math/big"
"net/netip"
"strconv"
"time"

"github.com/ava-labs/avalanche-network-runner/network/node"
"github.com/ava-labs/avalanche-network-runner/utils"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/beacon"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/units"
Expand Down Expand Up @@ -57,7 +57,7 @@ type Config struct {
// Subnet config files to use per default, if not specified in node config
SubnetConfigFiles map[string]string `json:"subnetConfigFiles"`
// Beacon config used for all nodes, can be empty
BeaconConfig beacon.Set `json:"beaconConfig"`
BeaconConfig map[ids.NodeID]netip.AddrPort `json:"beaconConfig"`
// Upgrade file used for all nodes, can be empty
Upgrade string `json:"upgrade"`
}
Expand Down
24 changes: 24 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"net/netip"
"os"
"strings"
"time"

rpcb "github.com/ava-labs/avalanche-network-runner/rpcpb"
Expand Down Expand Up @@ -184,3 +185,26 @@ func BeaconMapToSet(beaconMap map[ids.NodeID]netip.AddrPort) (beacon.Set, error)
}
return set, nil
}

func BeaconMapFromSet(beaconSet beacon.Set) (map[ids.NodeID]netip.AddrPort, error) {
beaconMap := make(map[ids.NodeID]netip.AddrPort)
beaconIDs := strings.Split(beaconSet.IDsArg(), ",")
beaconIPs := strings.Split(beaconSet.IPsArg(), ",")

if len(beaconIDs) != len(beaconIPs) {
return nil, fmt.Errorf("beacon IDs and IPs do not match")
}

for i := 0; i < len(beaconIDs); i++ {
beaconID, err := ids.NodeIDFromString(beaconIDs[i])
if err != nil {
return nil, fmt.Errorf("failed to parse beacon ID: %w", err)
}
beaconIP, err := netip.ParseAddrPort(beaconIPs[i])
if err != nil {
return nil, fmt.Errorf("failed to parse beacon IP: %w", err)
}
beaconMap[beaconID] = beaconIP
}
return beaconMap, nil
}

0 comments on commit ca86bb4

Please sign in to comment.