Skip to content

Commit

Permalink
Merge branch 'main' into update-readme-with-academy-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero authored Jul 21, 2023
2 parents d3abdbb + 2f774b4 commit aecf355
Show file tree
Hide file tree
Showing 16 changed files with 2,402 additions and 880 deletions.
78 changes: 39 additions & 39 deletions README.md

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ type Client interface {
LoadSnapshot(ctx context.Context, snapshotName string, opts ...OpOption) (*rpcpb.LoadSnapshotResponse, error)
RemoveSnapshot(ctx context.Context, snapshotName string) (*rpcpb.RemoveSnapshotResponse, error)
GetSnapshotNames(ctx context.Context) ([]string, error)
ListSubnets(ctx context.Context) ([]string, error)
ListBlockchains(ctx context.Context) ([]*rpcpb.CustomChainInfo, error)
ListRpcs(ctx context.Context) ([]*rpcpb.BlockchainRpcs, error)
VMID(ctx context.Context, vmName string) (string, error)
}

type client struct {
Expand Down Expand Up @@ -392,6 +396,42 @@ func (c *client) GetSnapshotNames(ctx context.Context) ([]string, error) {
return resp.SnapshotNames, nil
}

func (c *client) ListSubnets(ctx context.Context) ([]string, error) {
c.log.Info("list subnets")
resp, err := c.controlc.ListSubnets(ctx, &rpcpb.ListSubnetsRequest{})
if err != nil {
return nil, err
}
return resp.SubnetIds, nil
}

func (c *client) ListBlockchains(ctx context.Context) ([]*rpcpb.CustomChainInfo, error) {
c.log.Info("list blockchains")
resp, err := c.controlc.ListBlockchains(ctx, &rpcpb.ListBlockchainsRequest{})
if err != nil {
return nil, err
}
return resp.Blockchains, nil
}

func (c *client) ListRpcs(ctx context.Context) ([]*rpcpb.BlockchainRpcs, error) {
c.log.Info("list rpcs")
resp, err := c.controlc.ListRpcs(ctx, &rpcpb.ListRpcsRequest{})
if err != nil {
return nil, err
}
return resp.BlockchainsRpcs, nil
}

func (c *client) VMID(ctx context.Context, vmName string) (string, error) {
c.log.Info("vmud")
resp, err := c.controlc.VMID(ctx, &rpcpb.VMIDRequest{VmName: vmName})
if err != nil {
return "", err
}
return resp.VmId, nil
}

func (c *client) Close() error {
c.closeOnce.Do(func() {
close(c.closed)
Expand Down
138 changes: 135 additions & 3 deletions cmd/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func NewCommand() *cobra.Command {
newLoadSnapshotCommand(),
newRemoveSnapshotCommand(),
newGetSnapshotNamesCommand(),
newVMIDCommand(),
newListSubnetsCommand(),
newListBlockchainsCommand(),
newListRPCsCommand(),
)

return cmd
Expand Down Expand Up @@ -244,9 +248,6 @@ func newStartCommand() *cobra.Command {
false,
"true to assign dynamic ports",
)
if err := cmd.MarkPersistentFlagRequired("avalanchego-path"); err != nil {
panic(err)
}
return cmd
}

Expand Down Expand Up @@ -1343,6 +1344,137 @@ func getSnapshotNamesFunc(*cobra.Command, []string) error {
return nil
}

func newVMIDCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "vmid vm-name",
Short: "Returns the vm id associated to the given vm name.",
RunE: VMIDFunc,
Args: cobra.ExactArgs(1),
}
return cmd
}

func VMIDFunc(_ *cobra.Command, args []string) error {
vmName := args[0]
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
vmID, err := cli.VMID(ctx, vmName)
cancel()
if err != nil {
return err
}

ux.Print(log, logging.Green.Wrap("VMID: %s"), vmID)
return nil
}

func newListSubnetsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-subnets",
Short: "Lists all subnet ids of the network.",
RunE: listSubnetsFunc,
Args: cobra.ExactArgs(0),
}
return cmd
}

func listSubnetsFunc(*cobra.Command, []string) error {
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
subnetIDs, err := cli.ListSubnets(ctx)
cancel()
if err != nil {
return err
}

for _, subnetID := range subnetIDs {
ux.Print(log, logging.Green.Wrap("Subnet ID: %s"), subnetID)
}

return nil
}

func newListBlockchainsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-blockchains",
Short: "Lists all blockchain ids of the network.",
RunE: listBlockchainsFunc,
Args: cobra.ExactArgs(0),
}
return cmd
}

func listBlockchainsFunc(*cobra.Command, []string) error {
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
resp, err := cli.ListBlockchains(ctx)
cancel()
if err != nil {
return err
}

ux.Print(log, "")
for _, blockchain := range resp {
ux.Print(log, logging.Green.Wrap("Blockchain ID: %s"), blockchain.ChainId)
ux.Print(log, logging.Green.Wrap(" VM Name: %s"), blockchain.ChainName)
ux.Print(log, logging.Green.Wrap(" VM ID: %s"), blockchain.VmId)
ux.Print(log, logging.Green.Wrap(" Subnet ID: %s"), blockchain.SubnetId)
ux.Print(log, "")
}

return nil
}

func newListRPCsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-rpcs",
Short: "Lists rpcs for all blockchain of the network.",
RunE: listRPCsFunc,
Args: cobra.ExactArgs(0),
}
return cmd
}

func listRPCsFunc(*cobra.Command, []string) error {
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
resp, err := cli.ListRpcs(ctx)
cancel()
if err != nil {
return err
}

ux.Print(log, "")
for _, blockchainRpcs := range resp {
ux.Print(log, logging.Green.Wrap("Blockchain ID: %s"), blockchainRpcs.BlockchainId)
for _, rpc := range blockchainRpcs.Rpcs {
ux.Print(log, logging.Green.Wrap(" %s: %s"), rpc.NodeName, rpc.Rpc)
}
ux.Print(log, "")
}
return nil
}

func newClient() (client.Client, error) {
if err := setLogs(); err != nil {
return nil, err
Expand Down
15 changes: 12 additions & 3 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"regexp"
"strings"
"sync"
"syscall"
"time"

"github.com/ava-labs/avalanche-network-runner/api"
Expand Down Expand Up @@ -479,6 +478,18 @@ func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Co
return nil
}

// See network.Network
func (ln *localNetwork) GetNetworkID() (uint32, error) {
ln.lock.Lock()
defer ln.lock.Unlock()

if ln.stopCalled() {
return 0, network.ErrStopped
}

return ln.networkID, nil
}

// See network.Network
func (ln *localNetwork) AddNode(nodeConfig node.Config) (node.Node, error) {
ln.lock.Lock()
Expand Down Expand Up @@ -843,7 +854,6 @@ func (ln *localNetwork) pauseNode(ctx context.Context, nodeName string) error {
if exitCode := node.process.Stop(ctx); exitCode != 0 {
return fmt.Errorf("node %q exited with exit code: %d", nodeName, exitCode)
}
syscall.Sync()
node.paused = true
return nil
}
Expand Down Expand Up @@ -964,7 +974,6 @@ func (ln *localNetwork) restartNode(
if err := ln.removeNode(ctx, nodeName); err != nil {
return err
}
syscall.Sync()
}

if _, err := ln.addNode(nodeConfig); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions local/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"

"github.com/ava-labs/avalanche-network-runner/api"
"github.com/ava-labs/avalanche-network-runner/network"
Expand Down Expand Up @@ -156,7 +155,6 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
if err := ln.stop(ctx); err != nil {
return "", err
}
syscall.Sync()
// create main snapshot dirs
snapshotDBDir := filepath.Join(snapshotDir, defaultDBSubdir)
if err := os.MkdirAll(snapshotDBDir, os.ModePerm); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type BlockchainSpec struct {

// Network is an abstraction of an Avalanche network
type Network interface {
// Returns the network ID for the currently running network
// Returns ErrStopped if Stop() was previously called.
GetNetworkID() (uint32, error)
// Returns nil if all the nodes in the network are healthy.
// A stopped network is considered unhealthy.
// Timeout is given by the context parameter.
Expand Down
Loading

0 comments on commit aecf355

Please sign in to comment.