Skip to content

Commit

Permalink
wip: polish UI components
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 8, 2024
1 parent d78a619 commit 3ef50da
Show file tree
Hide file tree
Showing 24 changed files with 521 additions and 456 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
}

state := internal.StateModel{
Offset: viper.GetInt("offset"),
Status: internal.StatusModel{
State: "INITIALIZING",
Version: "NA",
Expand Down
27 changes: 19 additions & 8 deletions internal/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ func GetAccount(client *api.ClientWithResponses, address string) (api.Account, e
return *r.JSON200, nil
}

func getExpiresTime(t Time, key api.ParticipationKey, state *StateModel) time.Time {
now := t.Now()
var expires = now.Add(-(time.Hour * 24 * 365 * 100))
if key.LastBlockProposal != nil && state.Status.LastRound != 0 && state.Metrics.RoundTime != 0 {
roundDiff := max(0, *key.EffectiveLastValid-int(state.Status.LastRound))
distance := int(state.Metrics.RoundTime) * roundDiff
expires = now.Add(time.Duration(distance))
}
return expires
}

// AccountsFromParticipationKeys maps an array of api.ParticipationKey to a keyed map of Account
func AccountsFromState(state *StateModel, t Time, client *api.ClientWithResponses) map[string]Account {
values := make(map[string]Account)
Expand All @@ -135,23 +146,23 @@ func AccountsFromState(state *StateModel, t Time, client *api.ClientWithResponse
panic(err)
}
}
now := t.Now()
var expires = now.Add(-(time.Hour * 24 * 365 * 100))
if key.EffectiveLastValid != nil {
roundDiff := max(0, *key.EffectiveLastValid-int(state.Status.LastRound))
distance := int(state.Metrics.RoundTime) * roundDiff
expires = now.Add(time.Duration(distance))
}

values[key.Address] = Account{
Address: key.Address,
Status: account.Status,
Balance: account.Amount / 1000000,
Expires: expires,
Expires: getExpiresTime(t, key, state),
Keys: 1,
}
} else {
val.Keys++
if val.Expires.Before(t.Now()) {
now := t.Now()
var expires = getExpiresTime(t, key, state)
if !expires.Before(now) {
val.Expires = expires
}
}
values[key.Address] = val
}
}
Expand Down
9 changes: 7 additions & 2 deletions internal/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func waitForNewKey(
client *api.ClientWithResponses,
keys *[]api.ParticipationKey,
interval time.Duration,
timeout time.Duration,
) (*[]api.ParticipationKey, error) {
if timeout <= 0*time.Second {
return nil, errors.New("timeout occurred waiting for new key")
}
timeout = timeout - interval
// Fetch the latest keys
currentKeys, err := GetPartKeys(ctx, client)
if err != nil {
Expand All @@ -49,7 +54,7 @@ func waitForNewKey(
if len(*currentKeys) == len(*keys) {
// Sleep then try again
time.Sleep(interval)
return waitForNewKey(ctx, client, keys, interval)
return waitForNewKey(ctx, client, keys, interval, timeout)
}
return currentKeys, nil
}
Expand Down Expand Up @@ -97,7 +102,7 @@ func GenerateKeyPair(
}

// Wait for the api to have a new key
keys, err := waitForNewKey(ctx, client, originalKeys, 2*time.Second)
keys, err := waitForNewKey(ctx, client, originalKeys, 2*time.Second, 20*time.Second)
if err != nil {
return nil, err
}
Expand Down
26 changes: 24 additions & 2 deletions internal/participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package internal
import (
"context"
"fmt"
"testing"

"github.com/algorandfoundation/hack-tui/api"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
"testing"
"time"
)

func Test_ListParticipationKeys(t *testing.T) {
Expand Down Expand Up @@ -201,3 +201,25 @@ func Test_RemovePartKeyByID(t *testing.T) {
}
})
}

func Test_Timeout(t *testing.T) {
ctx := context.Background()
// Setup elevated client
apiToken, err := securityprovider.NewSecurityProviderApiKey("header", "X-Algo-API-Token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
if err != nil {
t.Fatal(err)
}
client, err := api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))
if err != nil {
t.Fatal(err)
}

keys, err := GetPartKeys(ctx, client)
if err != nil {
t.Fatal(err)
}
_, err = waitForNewKey(ctx, client, keys, 100*time.Millisecond, 1*time.Second)
if err == nil {
t.Fatal("Did not error")
}
}
8 changes: 7 additions & 1 deletion internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import (
)

type StateModel struct {
// Models
Status StatusModel
Metrics MetricsModel
Accounts map[string]Account
ParticipationKeys *[]api.ParticipationKey

// Application State
Admin bool
Offset int

// TODO: handle contexts instead of adding it to state
Admin bool
Watching bool

// RPC
Client *api.ClientWithResponses
Context context.Context
}
Expand Down
67 changes: 0 additions & 67 deletions ui/error.go

This file was deleted.

24 changes: 24 additions & 0 deletions ui/modal/cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package modal

import (
"github.com/algorandfoundation/hack-tui/api"
tea "github.com/charmbracelet/bubbletea"
)

type Event struct {
Key *api.ParticipationKey
Address string
Type string
}

type ShowModal Event

func EmitShowModal(evt Event) tea.Cmd {
return func() tea.Msg {
return ShowModal(evt)
}
}

type DeleteFinished string

type DeleteKey *api.ParticipationKey
Loading

0 comments on commit 3ef50da

Please sign in to comment.