Skip to content

Commit

Permalink
Merge branch 'main' into feat/fast-catchup
Browse files Browse the repository at this point in the history
# Conflicts:
#	cmd/root.go
#	internal/participation.go
#	internal/state.go
#	internal/status.go
#	ui/modal/controller.go
#	ui/modal/model.go
#	ui/modals/transaction/view.go
  • Loading branch information
PhearZero committed Dec 23, 2024
2 parents b38db59 + 1366a94 commit 5acfe83
Show file tree
Hide file tree
Showing 18 changed files with 402 additions and 126 deletions.
9 changes: 8 additions & 1 deletion api/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package api

import "net/http"
import (
"io"
"net/http"
)

type HttpPkg struct {
HttpPkgInterface
Expand All @@ -9,9 +12,13 @@ type HttpPkg struct {
func (HttpPkg) Get(url string) (resp *http.Response, err error) {
return http.Get(url)
}
func (HttpPkg) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
return http.Post(url, contentType, body)
}

var Http HttpPkg

type HttpPkgInterface interface {
Get(url string) (resp *http.Response, err error)
Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)
}
18 changes: 8 additions & 10 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
set -euo pipefail


BANNER=$(cat <<'EOF'
_____ .__ __________
/ _ \ | | ____ ____\______ \__ __ ____
/ /_\ \| | / ___\ / _ \| _/ | \/ \
/ | \ |__/ /_/ > <_> ) | \ | / | \
\____|__ /____/\___ / \____/|____|_ /____/|___| /
\/ /_____/ \/ \/
EOF
)

BANNER=' _____ .__ __________
/ _ \ | | ____ ____\______ \__ __ ____
/ /_\ \| | / ___\ / _ \| _/ | \/ \
/ | \ |__/ /_/ > <_> ) | \ | / | \
\____|__ /____/\___ / \____/|____|_ /____/|___| /
\/ /_____/ \/ \/ '

os=$(uname -ms)
release="https://github.com/algorandfoundation/algorun-tui/releases/download"
version="v1.0.0-beta.1"
version="v1.0.0-beta.2"

Red=''
Green=''
Expand Down
78 changes: 78 additions & 0 deletions internal/algod/participation/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -173,3 +174,80 @@ func IsActive(part api.ParticipationKey, account api.AccountParticipation) bool
}
return equal

Check warning on line 175 in internal/algod/participation/participation.go

View check run for this annotation

Codecov / codecov/patch

internal/algod/participation/participation.go#L168-L175

Added lines #L168 - L175 were not covered by tests
}

// OnlineShortLinkBody represents the request payload for creating an online short link.
// It contains account details, cryptographic keys in Base64, validity range, key dilution, and network information.
type OnlineShortLinkBody struct {
Account string `json:"account"`
VoteKeyB64 string `json:"voteKeyB64"`
SelectionKeyB64 string `json:"selectionKeyB64"`
StateProofKeyB64 string `json:"stateProofKeyB64"`
VoteFirstValid int `json:"voteFirstValid"`
VoteLastValid int `json:"voteLastValid"`
KeyDilution int `json:"keyDilution"`
Network string `json:"network"`
}

// GetOnlineShortLink sends a POST request to create an online short link
// and returns the response or an error if it occurs.
func GetOnlineShortLink(http api.HttpPkgInterface, part OnlineShortLinkBody) (ShortLinkResponse, error) {
var response ShortLinkResponse
data, err := json.Marshal(part)
if err != nil {
return response, err
}
res, err := http.Post("http://b.nodekit.run/online", "applicaiton/json", bytes.NewReader(data))
if err != nil {
return response, err
}
defer res.Body.Close()

err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return response, err
}

return response, nil
}

// ShortLinkResponse represents the response structure for a shortened link,
// containing its unique identifier.
type ShortLinkResponse struct {
Id string `json:"id"`
}

// OfflineShortLinkBody represents the request body for creating an
// offline short link containing an address and network.
type OfflineShortLinkBody struct {
Account string `json:"account"`
Network string `json:"network"`
}

// GetOfflineShortLink sends an OnlineShortLinkBody to create an offline short link and returns the corresponding response.
// Uses the provided HttpPkgInterface for the POST request and handles JSON encoding/decoding of request and response.
// Returns an OfflineShortLinkResponse on success or an error if the operation fails.
func GetOfflineShortLink(http api.HttpPkgInterface, offline OfflineShortLinkBody) (ShortLinkResponse, error) {
var response ShortLinkResponse
data, err := json.Marshal(offline)
if err != nil {
return response, err
}
res, err := http.Post("http://b.nodekit.run/offline", "applicaiton/json", bytes.NewReader(data))
if err != nil {
return response, err
}
defer res.Body.Close()

err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return response, err
}

return response, nil
}

// ToShortLink generates a shortened URL string using the unique
// identifier from the provided ShortLinkResponse.
func ToShortLink(link ShortLinkResponse) string {
return fmt.Sprintf("https://b.nodekit.run/%s", link.Id)
}
91 changes: 91 additions & 0 deletions internal/algod/participation/participation_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package participation

import (
"bytes"
"context"
"fmt"
"github.com/algorandfoundation/algorun-tui/api"
"github.com/algorandfoundation/algorun-tui/internal/test"
"github.com/algorandfoundation/algorun-tui/internal/test/mock"
"io"
"net/http"
"testing"
)

Expand Down Expand Up @@ -208,3 +211,91 @@ func Test_RemovePartKeyByID(t *testing.T) {
}
})
}

var onlineShortLinkResponseStr = `{
"id": "WKIPKTWIFZQJ2"
}`

type testOnlineShortner struct {
api.HttpPkgInterface
}

// TODO: toggle between Unit/Integration tests
func (testOnlineShortner) Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
responseBody := io.NopCloser(bytes.NewReader([]byte(onlineShortLinkResponseStr)))
return &http.Response{
Status: "",
StatusCode: 0,
Proto: "",
ProtoMajor: 0,
ProtoMinor: 0,
Header: nil,
Body: responseBody,
ContentLength: 0,
TransferEncoding: nil,
Close: false,
Uncompressed: false,
Trailer: nil,
Request: nil,
TLS: nil,
}, nil
}
func Test_ToOnlineShortLink(t *testing.T) {
link, err := GetOnlineShortLink(new(testOnlineShortner), OnlineShortLinkBody{
Account: "JPEGRZ6G4IBZCOC7UV6QZWJ6TENNKRIPENUJTLG5K7PKIKMVTJHUGERARE",
VoteKeyB64: "WWHePYtNZ2T3sHkqdd/38EvoFWrnIKPrTo6xN/4T1l4=",
SelectionKeyB64: "e4kBLu7zXOorjLVzJHOiAn+IhOBsYBCqqHKaJCiCdJs=",
StateProofKeyB64: "1GdNPOck+t6yXvuXxrDEPKqgi4I2sTaNugV1kd5ksUW2G1U6x1FT0WR3aT3ZSSmbYoDt3cVrB3vIPJA8GkqSYg==",
VoteFirstValid: 3118965,
VoteLastValid: 3148965,
KeyDilution: 995,
Network: "mainnet",
})
if err != nil {
t.Error(err)
}
if link.Id != "WKIPKTWIFZQJ2" {
t.Error("Link should be a known hash")
}
}

var offlineShortLinkResponseStr = `{
"id": "D3O3GEG2UD2GW"
}`

type testOfflineShortner struct {
api.HttpPkgInterface
}

// TODO: toggle between Unit/Integration tests
func (testOfflineShortner) Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
responseBody := io.NopCloser(bytes.NewReader([]byte(offlineShortLinkResponseStr)))
return &http.Response{
Status: "",
StatusCode: 0,
Proto: "",
ProtoMajor: 0,
ProtoMinor: 0,
Header: nil,
Body: responseBody,
ContentLength: 0,
TransferEncoding: nil,
Close: false,
Uncompressed: false,
Trailer: nil,
Request: nil,
TLS: nil,
}, nil
}
func Test_ToOfflineShortLink(t *testing.T) {
link, err := GetOfflineShortLink(new(testOfflineShortner), OfflineShortLinkBody{
Account: "JPEGRZ6G4IBZCOC7UV6QZWJ6TENNKRIPENUJTLG5K7PKIKMVTJHUGERARE",
Network: "mainnet",
})
if err != nil {
t.Error(err)
}
if link.Id != "D3O3GEG2UD2GW" {
t.Error("Link should be a known hash")
}
}
56 changes: 56 additions & 0 deletions ui/app/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package app

import (
"encoding/base64"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/algod/participation"
"strings"

"github.com/algorandfoundation/algorun-tui/api"
tea "github.com/charmbracelet/bubbletea"
)

func EmitCreateShortLink(offline bool, part *api.ParticipationKey, state *algod.StateModel) tea.Cmd {
if part == nil || state == nil {
return nil
}

var loraNetwork = strings.Replace(strings.Replace(state.Status.Network, "-v1.0", "", 1), "-v1", "", 1)
if loraNetwork == "dockernet" || loraNetwork == "tuinet" {
loraNetwork = "localnet"
}

if offline {
res, err := participation.GetOfflineShortLink(state.HttpPkg, participation.OfflineShortLinkBody{

Check warning on line 24 in ui/app/url.go

View check run for this annotation

Codecov / codecov/patch

ui/app/url.go#L24

Added line #L24 was not covered by tests
Account: part.Address,
Network: loraNetwork,
})
if err != nil {
return func() tea.Msg {
return err
}
}
return func() tea.Msg {
return res
}
}

res, err := participation.GetOnlineShortLink(state.HttpPkg, participation.OnlineShortLinkBody{

Check warning on line 38 in ui/app/url.go

View check run for this annotation

Codecov / codecov/patch

ui/app/url.go#L38

Added line #L38 was not covered by tests
Account: part.Address,
VoteKeyB64: base64.RawURLEncoding.EncodeToString(part.Key.VoteParticipationKey),
SelectionKeyB64: base64.RawURLEncoding.EncodeToString(part.Key.SelectionParticipationKey),
StateProofKeyB64: base64.RawURLEncoding.EncodeToString(*part.Key.StateProofKey),
VoteFirstValid: part.Key.VoteFirstValid,
VoteLastValid: part.Key.VoteLastValid,
KeyDilution: part.Key.VoteKeyDilution,
Network: loraNetwork,
})
if err != nil {
return func() tea.Msg {
return err
}
}
return func() tea.Msg {
return res
}
}
1 change: 1 addition & 0 deletions ui/internal/test/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func GetState(client api.ClientWithResponsesInterface) *algod.StateModel {
Admin: false,
Watching: false,
Client: client,
HttpPkg: new(api.HttpPkg),
Context: context.Background(),
}
values := make(map[string]algod.Account)
Expand Down
5 changes: 5 additions & 0 deletions ui/modal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package modal

import (
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/algod/participation"
"github.com/algorandfoundation/algorun-tui/ui/app"
"github.com/algorandfoundation/algorun-tui/ui/modals/generate"
"github.com/algorandfoundation/algorun-tui/ui/style"
Expand Down Expand Up @@ -31,6 +32,10 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
m.Open = true
m.exceptionModal.Message = msg.Error()
m.SetType(app.ExceptionModal)
case participation.ShortLinkResponse:

Check warning on line 35 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L35

Added line #L35 was not covered by tests
m.Open = true
m.SetShortLink(msg)
m.SetType(app.TransactionModal)
case *algod.StateModel:
m.State = msg
m.transactionModal.State = msg
Expand Down
4 changes: 4 additions & 0 deletions ui/modal/modal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package modal
import (
"bytes"
"errors"
"github.com/algorandfoundation/algorun-tui/internal/algod/participation"
"github.com/algorandfoundation/algorun-tui/internal/test/mock"
"github.com/algorandfoundation/algorun-tui/ui/app"
"github.com/algorandfoundation/algorun-tui/ui/internal/test"
Expand Down Expand Up @@ -60,6 +61,9 @@ func Test_Snapshot(t *testing.T) {
t.Run("TransactionModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.State.Status.Network = "testnet-v1.0"
model.SetShortLink(participation.ShortLinkResponse{
Id: "1234",
})
model.SetKey(&mock.Keys[0])
model.SetActive(true)
model.SetType(app.TransactionModal)
Expand Down
10 changes: 10 additions & 0 deletions ui/modal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package modal
import (
"github.com/algorandfoundation/algorun-tui/api"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/algod/participation"
"github.com/algorandfoundation/algorun-tui/ui/app"
"github.com/algorandfoundation/algorun-tui/ui/modals/confirm"
"github.com/algorandfoundation/algorun-tui/ui/modals/exception"
Expand All @@ -26,6 +27,10 @@ type ViewModel struct {
// Address defines the string format address of the entity
Address string

// Link represents a reference to a ShortLinkResponse,
// typically used for processing or displaying shortened link data.
Link *participation.ShortLinkResponse

// Views
infoModal *info.ViewModel
transactionModal *transaction.ViewModel
Expand Down Expand Up @@ -61,6 +66,11 @@ func (m *ViewModel) SetActive(active bool) {
m.transactionModal.UpdateState()
}

func (m *ViewModel) SetShortLink(res participation.ShortLinkResponse) {
m.Link = &res
m.transactionModal.Link = &res
}

// SetType updates the modal type of the ViewModel and configures its title, controls, and border color accordingly.
func (m *ViewModel) SetType(modal app.ModalType) {
m.Type = modal
Expand Down
Loading

0 comments on commit 5acfe83

Please sign in to comment.