Skip to content

Commit

Permalink
Merge pull request #715 from ava-labs/load-aliases-with-snapshot
Browse files Browse the repository at this point in the history
Load aliases with snapshot
  • Loading branch information
felipemadero authored Apr 11, 2024
2 parents cd7b4f1 + bdafb60 commit 7523771
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 31 deletions.
32 changes: 23 additions & 9 deletions local/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,31 @@ func (ln *localNetwork) RegisterBlockchainAliases(
continue
}
blockchainAlias := chainSpec.BlockchainAlias
chainID := chainInfos[i].blockchainID.String()
blockchainID := chainInfos[i].blockchainID.String()
ln.log.Info("registering blockchain alias",
zap.String("alias", blockchainAlias),
zap.String("chain-id", chainID))
for nodeName, node := range ln.nodes {
if node.paused {
continue
}
if err := node.client.AdminAPI().AliasChain(ctx, chainID, blockchainAlias); err != nil {
return fmt.Errorf("failure to register blockchain alias %v on node %v: %w", blockchainAlias, nodeName, err)
}
zap.String("chain-id", blockchainID))
if err := ln.setBlockchainAlias(ctx, blockchainID, blockchainAlias); err != nil {
return err
}
ln.blockchainAliases[blockchainID] = append(ln.blockchainAliases[blockchainID], blockchainAlias)
}
return nil
}

func (ln *localNetwork) setBlockchainAlias(ctx context.Context, blockchainID string, blockchainAlias string) error {
for nodeName, node := range ln.nodes {
if node.paused {
continue
}
if err := node.client.AdminAPI().AliasChain(ctx, blockchainID, blockchainAlias); err != nil {
return fmt.Errorf(
"failure to register blockchain alias %s for blockchain ID %s on node %s: %w",
blockchainAlias,
blockchainID,
nodeName,
err,
)
}
}
return nil
Expand Down
11 changes: 7 additions & 4 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ type localNetwork struct {
redirectStderr bool
// map from subnet id to elastic subnet tx id
subnetID2ElasticSubnetID map[ids.ID]ids.ID
// map from blockchain id to blockchain aliases
blockchainAliases map[string][]string
}

type deprecatedFlagEsp struct {
Expand All @@ -135,7 +137,7 @@ var (
deprecatedFlagsSupportBytes []byte
deprecatedFlagsSupport []deprecatedFlagEsp
// snapshots directory
defaultSnapshotsDir string
DefaultSnapshotsDir string
)

// populate default network config from embedded default directory
Expand All @@ -149,14 +151,14 @@ func init() {
if err != nil {
panic(err)
}
defaultSnapshotsDir = filepath.Join(usr.HomeDir, snapshotsRelPath)
DefaultSnapshotsDir = filepath.Join(usr.HomeDir, snapshotsRelPath)
}

// NewNetwork returns a new network that uses the given log.
// Files (e.g. logs, databases) default to being written at directory [rootDir].
// If there isn't a directory at [dir] one will be created.
// If len([dir]) == 0, files will be written underneath a new temporary directory.
// Snapshots are saved to snapshotsDir, defaults to defaultSnapshotsDir if not given
// Snapshots are saved to snapshotsDir, defaults to DefaultSnapshotsDir if not given
func NewNetwork(
log logging.Logger,
networkConfig network.Config,
Expand Down Expand Up @@ -214,7 +216,7 @@ func newNetwork(
}
}
if snapshotsDir == "" {
snapshotsDir = defaultSnapshotsDir
snapshotsDir = DefaultSnapshotsDir
}
// create the snapshots dir if not present
err = os.MkdirAll(snapshotsDir, os.ModePerm)
Expand All @@ -236,6 +238,7 @@ func newNetwork(
redirectStdout: redirectStdout,
redirectStderr: redirectStderr,
subnetID2ElasticSubnetID: map[ids.ID]ids.ID{},
blockchainAliases: map[string][]string{},
}
return net, nil
}
Expand Down
59 changes: 51 additions & 8 deletions local/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
type NetworkState struct {
// Map from subnet id to elastic subnet tx id
SubnetID2ElasticSubnetID map[string]string `json:"subnetID2ElasticSubnetID"`
// Map from blockchain id to blockchain aliases
BlockchainAliases map[string][]string `json:"blockchainAliases"`
}

// snapshots generated using older ANR versions may contain deprecated avago flags
Expand Down Expand Up @@ -203,6 +205,7 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
}
networkState := NetworkState{
SubnetID2ElasticSubnetID: subnetID2ElasticSubnetID,
BlockchainAliases: ln.blockchainAliases,
}
networkStateJSON, err := json.MarshalIndent(networkState, "", " ")
if err != nil {
Expand Down Expand Up @@ -328,13 +331,54 @@ func (ln *localNetwork) loadSnapshot(
}
ln.subnetID2ElasticSubnetID[subnetID] = elasticSubnetID
}
for k, v := range networkState.BlockchainAliases {
ln.blockchainAliases[k] = v
}
}
if err := ln.loadConfig(ctx, networkConfig); err != nil {
return err
}
return ln.loadConfig(ctx, networkConfig)
if err := ln.healthy(ctx); err != nil {
return err
}
// add aliases included in the snapshot state
for blockchainID, blockchainAliases := range ln.blockchainAliases {
for _, blockchainAlias := range blockchainAliases {
if err := ln.setBlockchainAlias(ctx, blockchainID, blockchainAlias); err != nil {
return err
}
}
}
// add aliases for blockchain names
node := ln.getNode()
blockchains, err := node.GetAPIClient().PChainAPI().GetBlockchains(ctx)
if err != nil {
return err
}
for _, blockchain := range blockchains {
if blockchain.Name == "C-Chain" || blockchain.Name == "X-Chain" {
continue
}
if err := ln.setBlockchainAlias(ctx, blockchain.ID.String(), blockchain.Name); err != nil {
// non fatal error: not required by user
ln.log.Warn(err.Error())
}
}
return nil
}

// Remove network snapshot
func (ln *localNetwork) RemoveSnapshot(snapshotName string) error {
snapshotDir := filepath.Join(ln.snapshotsDir, snapshotPrefix+snapshotName)
return RemoveSnapshot(ln.snapshotsDir, snapshotName)
}

// Get network snapshots
func (ln *localNetwork) GetSnapshotNames() ([]string, error) {
return GetSnapshotNames(ln.snapshotsDir)
}

func RemoveSnapshot(snapshotsDir string, snapshotName string) error {
snapshotDir := filepath.Join(snapshotsDir, snapshotPrefix+snapshotName)
_, err := os.Stat(snapshotDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand All @@ -349,17 +393,16 @@ func (ln *localNetwork) RemoveSnapshot(snapshotName string) error {
return nil
}

// Get network snapshots
func (ln *localNetwork) GetSnapshotNames() ([]string, error) {
_, err := os.Stat(ln.snapshotsDir)
func GetSnapshotNames(snapshotsDir string) ([]string, error) {
_, err := os.Stat(snapshotsDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("snapshots dir %q does not exists", ln.snapshotsDir)
return nil, fmt.Errorf("snapshots dir %q does not exists", snapshotsDir)
} else {
return nil, fmt.Errorf("failure accessing snapshots dir %q: %w", ln.snapshotsDir, err)
return nil, fmt.Errorf("failure accessing snapshots dir %q: %w", snapshotsDir, err)
}
}
matches, err := filepath.Glob(filepath.Join(ln.snapshotsDir, snapshotPrefix+"*"))
matches, err := filepath.Glob(filepath.Join(snapshotsDir, snapshotPrefix+"*"))
if err != nil {
return nil, err
}
Expand Down
17 changes: 7 additions & 10 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"go.uber.org/multierr"

"github.com/ava-labs/avalanche-network-runner/local"
"github.com/ava-labs/avalanche-network-runner/network"
"github.com/ava-labs/avalanche-network-runner/network/node"
"github.com/ava-labs/avalanche-network-runner/rpcpb"
Expand Down Expand Up @@ -133,6 +134,10 @@ func New(cfg Config, log logging.Logger) (Server, error) {
return nil, err
}

if cfg.SnapshotsDir == "" {
cfg.SnapshotsDir = local.DefaultSnapshotsDir
}

s := &server{
cfg: cfg,
log: log,
Expand Down Expand Up @@ -1412,11 +1417,7 @@ func (s *server) RemoveSnapshot(_ context.Context, req *rpcpb.RemoveSnapshotRequ

s.log.Info("RemoveSnapshot", zap.String("snapshot-name", req.SnapshotName))

if s.network == nil {
return nil, ErrNotBootstrapped
}

if err := s.network.nw.RemoveSnapshot(req.SnapshotName); err != nil {
if err := local.RemoveSnapshot(s.cfg.SnapshotsDir, req.SnapshotName); err != nil {
s.log.Warn("snapshot remove failed to complete", zap.Error(err))
return nil, err
}
Expand All @@ -1429,11 +1430,7 @@ func (s *server) GetSnapshotNames(context.Context, *rpcpb.GetSnapshotNamesReques

s.log.Info("GetSnapshotNames")

if s.network == nil {
return nil, ErrNotBootstrapped
}

snapshotNames, err := s.network.nw.GetSnapshotNames()
snapshotNames, err := local.GetSnapshotNames(s.cfg.SnapshotsDir)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7523771

Please sign in to comment.