Skip to content

Commit

Permalink
Remove cli lambda funcs (#1442)
Browse files Browse the repository at this point in the history
* Remove anon functions from cli

* Remove unused getKeyType function

* Remove ImportANR and importAvalancheOps from CLI
  • Loading branch information
aaronbuchwald authored Aug 29, 2024
1 parent 807de32 commit 99299f2
Show file tree
Hide file tree
Showing 20 changed files with 502 additions and 693 deletions.
158 changes: 21 additions & 137 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ package cli

import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/ava-labs/avalanche-network-runner/client"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/units"
"gopkg.in/yaml.v2"

"github.com/ava-labs/hypersdk/api/jsonrpc"
"github.com/ava-labs/hypersdk/api/ws"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/cli/prompt"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/fees"
"github.com/ava-labs/hypersdk/pubsub"
Expand All @@ -27,11 +20,11 @@ import (
)

func (h *Handler) ImportChain() error {
chainID, err := h.PromptID("chainID")
chainID, err := prompt.ID("chainID")
if err != nil {
return err
}
uri, err := h.PromptString("uri", 0, consts.MaxInt)
uri, err := prompt.String("uri", 0, consts.MaxInt)
if err != nil {
return err
}
Expand All @@ -44,137 +37,24 @@ func (h *Handler) ImportChain() error {
return nil
}

func (h *Handler) ImportANR() error {
ctx := context.Background()

// Delete previous items
oldChains, err := h.DeleteChains()
if err != nil {
return err
}
if len(oldChains) > 0 {
utils.Outf("{{yellow}}deleted old chains:{{/}} %+v\n", oldChains)
}

// Load new items from ANR
anrCli, err := client.New(client.Config{
Endpoint: "0.0.0.0:12352",
DialTimeout: 10 * time.Second,
}, logging.NoLog{})
if err != nil {
return err
}
status, err := anrCli.Status(ctx)
if err != nil {
return err
}
subnets := map[ids.ID][]ids.ID{}
for chain, chainInfo := range status.ClusterInfo.CustomChains {
chainID, err := ids.FromString(chain)
if err != nil {
return err
}
subnetID, err := ids.FromString(chainInfo.SubnetId)
if err != nil {
return err
}
chainIDs, ok := subnets[subnetID]
if !ok {
chainIDs = []ids.ID{}
}
chainIDs = append(chainIDs, chainID)
subnets[subnetID] = chainIDs
}
var filledChainID ids.ID
for _, nodeInfo := range status.ClusterInfo.NodeInfos {
if len(nodeInfo.WhitelistedSubnets) == 0 {
continue
}
trackedSubnets := strings.Split(nodeInfo.WhitelistedSubnets, ",")
for _, subnet := range trackedSubnets {
subnetID, err := ids.FromString(subnet)
if err != nil {
return err
}
for _, chainID := range subnets[subnetID] {
uri := fmt.Sprintf("%s/ext/bc/%s", nodeInfo.Uri, chainID)
if err := h.StoreChain(chainID, uri); err != nil {
return err
}
utils.Outf(
"{{yellow}}stored chainID:{{/}} %s {{yellow}}uri:{{/}} %s\n",
chainID,
uri,
)
filledChainID = chainID
}
}
}
return h.StoreDefaultChain(filledChainID)
}

type AvalancheOpsConfig struct {
Resources struct {
CreatedNodes []struct {
HTTPEndpoint string `yaml:"httpEndpoint"`
} `yaml:"created_nodes"`
} `yaml:"resource"`
VMInstall struct {
ChainID string `yaml:"chain_id"`
} `yaml:"vm_install"`
}

func (h *Handler) ImportOps(opsPath string) error {
oldChains, err := h.DeleteChains()
if err != nil {
return err
}
if len(oldChains) > 0 {
utils.Outf("{{yellow}}deleted old chains:{{/}} %+v\n", oldChains)
}

// Load yaml file
var opsConfig AvalancheOpsConfig
yamlFile, err := os.ReadFile(opsPath)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &opsConfig)
func (h *Handler) SetDefaultChain() error {
chains, err := h.GetChains()
if err != nil {
return err
}

// Load chainID
chainID, err := ids.FromString(opsConfig.VMInstall.ChainID)
chainID, _, err := prompt.SelectChain("set default chain", chains)
if err != nil {
return err
}

// Add chains
for _, node := range opsConfig.Resources.CreatedNodes {
uri := fmt.Sprintf("%s/ext/bc/%s", node.HTTPEndpoint, chainID)
if err := h.StoreChain(chainID, uri); err != nil {
return err
}
utils.Outf(
"{{yellow}}stored chainID:{{/}} %s {{yellow}}uri:{{/}} %s\n",
chainID,
uri,
)
}
return h.StoreDefaultChain(chainID)
}

func (h *Handler) SetDefaultChain() error {
chainID, _, err := h.PromptChain("set default chain", nil)
func (h *Handler) PrintChainInfo() error {
chains, err := h.GetChains()
if err != nil {
return err
}
return h.StoreDefaultChain(chainID)
}

func (h *Handler) PrintChainInfo() error {
_, uris, err := h.PromptChain("select chainID", nil)
_, uris, err := prompt.SelectChain("select chainID", chains)
if err != nil {
return err
}
Expand All @@ -192,17 +72,21 @@ func (h *Handler) PrintChainInfo() error {
return nil
}

func (h *Handler) WatchChain(hideTxs bool, getParser func(string) (chain.Parser, error), handleTx func(*chain.Transaction, *chain.Result)) error {
func (h *Handler) WatchChain(hideTxs bool) error {
ctx := context.Background()
chainID, uris, err := h.PromptChain("select chainID", nil)
chains, err := h.GetChains()
if err != nil {
return err
}
chainID, uris, err := prompt.SelectChain("select chainID", chains)
if err != nil {
return err
}
if err := h.CloseDatabase(); err != nil {
return err
}
utils.Outf("{{yellow}}uri:{{/}} %s\n", uris[0])
parser, err := getParser(uris[0])
parser, err := h.c.GetParser(uris[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -254,8 +138,8 @@ func (h *Handler) WatchChain(hideTxs bool, getParser func(string) (chain.Parser,
len(blk.Txs),
blk.StateRoot,
float64(blk.Size())/units.KiB,
ParseDimensions(consumed),
ParseDimensions(prices),
consumed,
prices,
float64(window.Sum(tpsWindow))/tpsDivisor,
time.Now().UnixMilli()-blk.Tmstmp,
time.Since(lastBlockDetailed).Milliseconds(),
Expand All @@ -267,8 +151,8 @@ func (h *Handler) WatchChain(hideTxs bool, getParser func(string) (chain.Parser,
len(blk.Txs),
blk.StateRoot,
float64(blk.Size())/units.KiB,
ParseDimensions(consumed),
ParseDimensions(prices),
consumed,
prices,
)
window.Update(&tpsWindow, window.WindowSliceSize-consts.Uint64Len, uint64(len(blk.Txs)))
}
Expand All @@ -278,7 +162,7 @@ func (h *Handler) WatchChain(hideTxs bool, getParser func(string) (chain.Parser,
continue
}
for i, tx := range blk.Txs {
handleTx(tx, results[i])
h.c.HandleTx(tx, results[i])
}
}
return nil
Expand Down
13 changes: 13 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@ package cli
import (
"github.com/ava-labs/avalanchego/database"

"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/pebble"
)

type Controller interface {
DatabasePath() string
Symbol() string
Decimals() uint8
Address(codec.Address) string
ParseAddress(string) (codec.Address, error)
GetParser(string) (chain.Parser, error)
HandleTx(*chain.Transaction, *chain.Result)
LookupBalance(address string, uri string) (uint64, error)
}

type Handler struct {
c Controller

Expand Down
52 changes: 0 additions & 52 deletions cli/dependencies.go

This file was deleted.

Loading

0 comments on commit 99299f2

Please sign in to comment.