Skip to content

Commit

Permalink
Usability improvements, support future protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Jun 6, 2022
1 parent 0e1a77c commit c47dac4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
31 changes: 19 additions & 12 deletions cmd/tui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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())
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
19 changes: 16 additions & 3 deletions tui/internal/bubbles/explorer/explorer.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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())
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c47dac4

Please sign in to comment.