Skip to content

Commit

Permalink
Merge pull request #736 from ava-labs/avago-upgrade
Browse files Browse the repository at this point in the history
Avago upgrade
  • Loading branch information
arturrez authored Sep 25, 2024
2 parents 375480f + cf6ee4e commit 5df7f9e
Show file tree
Hide file tree
Showing 11 changed files with 829 additions and 738 deletions.
10 changes: 10 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ func (c *client) Start(ctx context.Context, execPath string, opts ...OpOption) (
if len(ret.customNetworkBootstrapNodeIDs) > 0 {
req.CustomNetworkBootstrapNodeIds = ret.customNetworkBootstrapNodeIDs
}
if ret.upgradePath != "" {
req.UpgradePath = ret.upgradePath
}
req.ReassignPortsIfUsed = &ret.reassignPortsIfUsed
req.DynamicPorts = &ret.dynamicPorts

Expand Down Expand Up @@ -503,6 +506,7 @@ type Op struct {
customNetworkGenesisPath string
customNetworkBootstrapNodeIDs []string
customNetworkBootstrapNodeIPPortPairs []string
upgradePath string
}

type OpOption func(*Op)
Expand Down Expand Up @@ -644,6 +648,12 @@ func WithCustomNetworkBootstrapNodeIPPortPairs(customNetworkBootstrapNodeIPPortP
}
}

func WithUpgradePath(upgradePath string) OpOption {
return func(op *Op) {
op.upgradePath = upgradePath
}
}

func isClientCanceled(ctxErr error, err error) bool {
if ctxErr != nil {
return true
Expand Down
11 changes: 11 additions & 0 deletions cmd/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ var (
customNetworkBootstrapNodeIPPortPairs []string
walletPrivateKey string
walletPrivateKeyPath string
upgradePath string
)

func setLogs() error {
Expand Down Expand Up @@ -308,6 +309,12 @@ func newStartCommand() *cobra.Command {
"",
"[optional] funding wallet private key path",
)
cmd.PersistentFlags().StringVar(
&upgradePath,
"upgrade-path",
"",
"[optional] avalanchego upgrade path",
)
return cmd
}

Expand Down Expand Up @@ -384,6 +391,10 @@ func startFunc(*cobra.Command, []string) error {
client.WithDynamicPorts(dynamicPorts),
}

if upgradePath != "" {
opts = append(opts, client.WithUpgradePath(upgradePath))
}

if customNetworkGenesisPath != "" {
genesisBytes, err := os.ReadFile(customNetworkGenesisPath)
if err != nil {
Expand Down
19 changes: 16 additions & 3 deletions local/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func getFreePort() (uint16, error) {

// writeFiles writes the files a node needs on startup.
// It returns flags used to point to those files.
func writeFiles(genesis []byte, nodeRootDir string, nodeConfig *node.Config) (map[string]string, error) {
func writeFiles(
genesisData []byte,
upgradeData []byte,
nodeRootDir string,
nodeConfig *node.Config,
) (map[string]string, error) {
type file struct {
pathKey string
flagValue string
Expand Down Expand Up @@ -67,12 +72,20 @@ func writeFiles(genesis []byte, nodeRootDir string, nodeConfig *node.Config) (ma
contents: decodedStakingSigningKey,
},
}
if len(genesis) > 0 {
if len(genesisData) > 0 {
files = append(files, file{
flagValue: filepath.Join(nodeRootDir, configsPath, genesisFileName),
path: filepath.Join(nodeRootDir, configsPath, genesisFileName),
pathKey: config.GenesisFileKey,
contents: genesis,
contents: genesisData,
})
}
if len(upgradeData) > 0 {
files = append(files, file{
flagValue: filepath.Join(nodeRootDir, configsPath, upgradeFileName),
path: filepath.Join(nodeRootDir, configsPath, upgradeFileName),
pathKey: config.UpgradeFileKey,
contents: upgradeData,
})
}
flags := map[string]string{}
Expand Down
54 changes: 44 additions & 10 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
stakingCertFileName = "staker.crt"
stakingSigningKeyFileName = "signer.key"
genesisFileName = "genesis.json"
upgradeFileName = "upgrade.json"
stopTimeout = 30 * time.Second
healthCheckFreq = 3 * time.Second
snapshotPrefix = "anr-snapshot-"
Expand Down Expand Up @@ -80,7 +81,10 @@ type localNetwork struct {
networkID uint32
// This network's genesis file.
// Must not be nil.
genesis []byte
genesisData []byte
// This network's upgrade file.
// May be nil
upgradeData []byte
// Used to create a new API client
newAPIClientF api.NewAPIClientF
// Used to create new node processes
Expand Down Expand Up @@ -286,7 +290,7 @@ func NewDefaultNetwork(
redirectStdout bool,
redirectStderr bool,
) (network.Network, error) {
config, err := NewDefaultConfig(binaryPath, constants.DefaultNetworkID, "")
config, err := NewDefaultConfig(binaryPath, constants.DefaultNetworkID, "", "", nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -348,7 +352,14 @@ func loadDefaultNetworkFiles() (map[string]interface{}, []byte, []*utils.NodeKey
}

// NewDefaultConfigNNodes creates a new default network config, with an arbitrary number of nodes
func NewDefaultConfigNNodes(binaryPath string, numNodes uint32, networkID uint32, genesisPath string) (network.Config, error) {
func NewDefaultConfigNNodes(
binaryPath string,
numNodes uint32,
networkID uint32,
genesisPath string,
upgradePath string,
beaconConfig map[ids.NodeID]netip.AddrPort,
) (network.Config, error) {
if networkID == 0 {
networkID = constants.DefaultNetworkID
}
Expand Down Expand Up @@ -386,7 +397,7 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32, networkID uint32
nodeConfigs = append(nodeConfigs, nodeConfig)
port += 2
}
if int(numNodes) == 1 && !utils.IsPublicNetwork(networkID) {
if int(numNodes) == 1 && !utils.IsPublicNetwork(networkID) && len(beaconConfig) == 0 {
flags[config.SybilProtectionEnabledKey] = false
}
cfg := network.Config{
Expand All @@ -397,6 +408,14 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32, networkID uint32
ChainConfigFiles: map[string]string{},
UpgradeConfigFiles: map[string]string{},
SubnetConfigFiles: map[string]string{},
BeaconConfig: beaconConfig,
}
if len(upgradePath) != 0 {
upgrade, err := os.ReadFile(upgradePath)
if err != nil {
return network.Config{}, fmt.Errorf("could not read upgrade file: %w", err)
}
cfg.Upgrade = string(upgrade)
}
if utils.IsCustomNetwork(networkID) {
var genesis []byte
Expand All @@ -423,8 +442,21 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32, networkID uint32
}

// NewDefaultConfig creates a new default network config
func NewDefaultConfig(binaryPath string, networkID uint32, genesisPath string) (network.Config, error) {
return NewDefaultConfigNNodes(binaryPath, constants.DefaultNumNodes, networkID, genesisPath)
func NewDefaultConfig(
binaryPath string,
networkID uint32,
genesisPath string,
upgradePath string,
beaconConfig map[ids.NodeID]netip.AddrPort,
) (network.Config, error) {
return NewDefaultConfigNNodes(
binaryPath,
constants.DefaultNumNodes,
networkID,
genesisPath,
upgradePath,
beaconConfig,
)
}

func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Config) error {
Expand All @@ -435,21 +467,23 @@ func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Co

ln.networkID = networkConfig.NetworkID
if len(networkConfig.Genesis) != 0 {
ln.genesis = []byte(networkConfig.Genesis)
genesisNetworkID, err := utils.NetworkIDFromGenesis(ln.genesis)
ln.genesisData = []byte(networkConfig.Genesis)
genesisNetworkID, err := utils.NetworkIDFromGenesis(ln.genesisData)
if err != nil {
return err
}
switch {
case ln.networkID == 0:
ln.networkID = genesisNetworkID
case ln.networkID != genesisNetworkID:
if ln.genesis, err = utils.SetGenesisNetworkID(ln.genesis, ln.networkID); err != nil {
if ln.genesisData, err = utils.SetGenesisNetworkID(ln.genesisData, ln.networkID); err != nil {
return fmt.Errorf("couldn't set network ID to genesis: %w", err)
}
}
}

ln.upgradeData = []byte(networkConfig.Upgrade)

// save node defaults
ln.flags = networkConfig.Flags
ln.binaryPath = networkConfig.BinaryPath
Expand Down Expand Up @@ -1167,7 +1201,7 @@ func (ln *localNetwork) buildArgs(

// Write staking key/cert etc. to disk so the new node can use them,
// and get flag that point the node to those files
fileFlags, err := writeFiles(ln.genesis, dataDir, nodeConfig)
fileFlags, err := writeFiles(ln.genesisData, ln.upgradeData, dataDir, nodeConfig)
if err != nil {
return buildArgsReturn{}, err
}
Expand Down
8 changes: 4 additions & 4 deletions local/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func TestNewNetworkOneNode(t *testing.T) {
require.Len(names, 1)

// Assert that the network's genesis was set
require.EqualValues(networkConfig.Genesis, string(net.genesis))
require.EqualValues(networkConfig.Genesis, string(net.genesisData))
}

// Test that NewNetwork returns an error when
Expand Down Expand Up @@ -583,7 +583,7 @@ func TestGenerateDefaultNetwork(t *testing.T) {
t.Parallel()
require := require.New(t)
binaryPath := "pepito"
networkConfig, err := NewDefaultConfig(binaryPath, 0, "")
networkConfig, err := NewDefaultConfig(binaryPath, 0, "", "", nil)
require.NoError(err)
net, err := newNetwork(
logging.NoLog{},
Expand Down Expand Up @@ -1104,7 +1104,7 @@ func emptyNetworkConfig() (network.Config, error) {
// keys and certificates.
func testNetworkConfig(t *testing.T) network.Config {
require := require.New(t)
networkConfig, err := NewDefaultConfigNNodes("pepito", 3, 0, "")
networkConfig, err := NewDefaultConfigNNodes("pepito", 3, 0, "", "", nil)
require.NoError(err)
for i := 0; i < 3; i++ {
networkConfig.NodeConfigs[i].Name = fmt.Sprintf("node%d", i)
Expand Down Expand Up @@ -1371,7 +1371,7 @@ func TestWriteFiles(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
flags, err := writeFiles(tt.genesis, tmpDir, &tt.nodeConfig)
flags, err := writeFiles(tt.genesis, nil, tmpDir, &tt.nodeConfig)
if tt.shouldErr {
require.Error(err)
return
Expand Down
2 changes: 1 addition & 1 deletion local/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (ln *localNetwork) persistNetwork() error {
// save network conf
networkConfig := network.Config{
NetworkID: ln.networkID,
Genesis: string(ln.genesis),
Genesis: string(ln.genesisData),
Flags: networkConfigFlags,
NodeConfigs: nodeConfigs,
BinaryPath: ln.binaryPath,
Expand Down
2 changes: 2 additions & 0 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type Config struct {
SubnetConfigFiles map[string]string `json:"subnetConfigFiles"`
// Beacon config used for all nodes, can be empty
BeaconConfig map[ids.NodeID]netip.AddrPort `json:"beaconConfig"`
// Upgrade file used for all nodes, can be empty
Upgrade string `json:"upgrade"`
}

// Validate returns an error if this config is invalid
Expand Down
Loading

0 comments on commit 5df7f9e

Please sign in to comment.