From c47dac4f9cfdd4de3070cfdef53e405f9d4a75be Mon Sep 17 00:00:00 2001 From: Will Winder Date: Mon, 6 Jun 2022 11:05:49 -0400 Subject: [PATCH] Usability improvements, support future protocol. --- cmd/tui/main.go | 31 ++++++++++++++--------- go.mod | 2 +- go.sum | 4 +-- tui/internal/bubbles/explorer/explorer.go | 19 +++++++++++--- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/cmd/tui/main.go b/cmd/tui/main.go index 6b0deb7..7fc1a83 100644 --- a/cmd/tui/main.go +++ b/cmd/tui/main.go @@ -61,17 +61,23 @@ func init() { func getRequestorOrExit(algodDataDir, url, token string) *messages.Requestor { // Initialize from -d, ALGORAND_DATA, or provided URL/Token - if algodDataDir == "" { - algodDataDir = os.Getenv("ALGORAND_DATA") - if algodDataDir != "" { - fmt.Println("Using ALGORAND_DATA environment variable.") - } + + if algodDataDir != "" && (url != "" || token != "") { + fmt.Fprintln(os.Stderr, "Do not use -u/-t with -d.") + os.Exit(1) } - // Lookup URL/Token - if algodDataDir != "" { - if url != "" || token != "" { - fmt.Fprintln(os.Stderr, "Do not use -u/-t with -d or the ALGORAND_DATA environment variable.") + // If url/token are missing, attempt to use environment variable. + if url == "" && token == "" { + if algodDataDir == "" { + algodDataDir = os.Getenv("ALGORAND_DATA") + if algodDataDir != "" { + fmt.Println("Using ALGORAND_DATA environment variable.") + } + } + + if algodDataDir == "" { + fmt.Fprintln(os.Stderr, "Algod is not available.\nMust provide url and token with -u/-t or a data directory with -d or the ALGORAND_DATA environment variable.") os.Exit(1) } @@ -85,9 +91,6 @@ func getRequestorOrExit(algodDataDir, url, token string) *messages.Requestor { os.Exit(1) } url = strings.TrimSpace(string(netaddrbytes)) - if !strings.HasPrefix(url, "http") { - url = "http://" + url - } tokenBytes, err := ioutil.ReadFile(tokenpath) if err != nil { fmt.Fprintf(os.Stderr, "Unable to read Token from file (%s): %s\n", tokenpath, err.Error()) @@ -96,6 +99,10 @@ func getRequestorOrExit(algodDataDir, url, token string) *messages.Requestor { token = string(tokenBytes) } + if !strings.HasPrefix(url, "http") { + url = "http://" + url + } + if url == "" || token == "" { fmt.Fprintln(os.Stderr, "Must provide a way to get the algod REST API.") os.Exit(1) diff --git a/go.mod b/go.mod index dc984d9..4a5852c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/algorand/node-ui go 1.16 require ( - github.com/algorand/go-algorand-sdk v1.14.1 + github.com/algorand/go-algorand-sdk v1.16.0 github.com/calyptia/go-bubble-table v0.1.0 github.com/charmbracelet/bubbles v0.10.3 github.com/charmbracelet/bubbletea v0.20.0 diff --git a/go.sum b/go.sum index c74f940..a065134 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbf github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= github.com/algorand/falcon v0.0.0-20220130164023-c9e1d466f123/go.mod h1:OkQyHlGvS0kLNcIWbC21/uQcnbfwSOQm+wiqWwBG9pQ= github.com/algorand/go-algorand v0.0.0-20220323144801-17c0feef002f/go.mod h1:ehGHRKxrRgN0fF+vm6kHLykiQ1ana3qc52N5UQzkFPM= -github.com/algorand/go-algorand-sdk v1.14.1 h1:ZS3qfqK4gGZw5vsT6P2eeMHCLTr1s3AnqWFxhdmxEKM= -github.com/algorand/go-algorand-sdk v1.14.1/go.mod h1:IM0k8f3UnqGoxZ0U560r3SwORHtvCT2gQfvgMOEm0rg= +github.com/algorand/go-algorand-sdk v1.16.0 h1:3i3Bq4QNVwzu/Kbqf7p+0F/YDtLhlc2qP3+zn4wSzY4= +github.com/algorand/go-algorand-sdk v1.16.0/go.mod h1:Kx2pxVS7ViANRZl0fhWjR6LqCs7r3b5cAb12qc7hju0= github.com/algorand/go-codec v1.1.8 h1:XDSreeeZY8gMst6Edz4RBkl08/DGMJOeHYkoXL2B7wI= github.com/algorand/go-codec v1.1.8/go.mod h1:XhzVs6VVyWMLu6cApb9/192gBjGRVGm5cX5j203Heg4= github.com/algorand/go-codec/codec v1.1.8 h1:lsFuhcOH2LiEhpBH3BVUUkdevVmwCRyvb7FCAAPeY6U= diff --git a/tui/internal/bubbles/explorer/explorer.go b/tui/internal/bubbles/explorer/explorer.go index db1922c..5afe2a2 100644 --- a/tui/internal/bubbles/explorer/explorer.go +++ b/tui/internal/bubbles/explorer/explorer.go @@ -1,8 +1,8 @@ package explorer import ( + "bytes" "context" - table "github.com/calyptia/go-bubble-table" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/viewport" @@ -108,6 +108,10 @@ func (m Model) Init() tea.Cmd { return m.initBlocksCmd } +func lenientDecode(data []byte, objptr interface{}) error { + return msgpack.NewLenientDecoder(bytes.NewReader(data)).Decode(&objptr) +} + func (m Model) nextBlockCmd(round uint64) tea.Cmd { return func() tea.Msg { _, err := m.requestor.Client.StatusAfterBlock(round).Do(context.Background()) @@ -119,7 +123,12 @@ func (m Model) nextBlockCmd(round uint64) tea.Cmd { return BlocksMsg{err: err} } item := blockItem{Round: round} - err = msgpack.Decode(blk, &item.Block) + //err = msgpack.Decode(blk, &item.Block) + err = lenientDecode(blk, &item.Block) + if err != nil { + return err + } + if err != nil { return BlocksMsg{ err: err, @@ -189,7 +198,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { backup := m.blocks m.blocks = msg.blocks m.blocks = append(m.blocks, backup...) - cmds = append(cmds, m.nextBlockCmd(m.blocks[0].Round+1)) + next := uint64(0) + if len(m.blocks) > 0 { + next = m.blocks[0].Round + 1 + } + cmds = append(cmds, m.nextBlockCmd(next)) } t, tableCmd := m.table.Update(msg)