Skip to content

Commit

Permalink
set missing values
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Mar 27, 2024
1 parent 0205b1a commit afd498e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 60 deletions.
79 changes: 35 additions & 44 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const blockTime = 1 * time.Second

type Client struct {
cc cosmosclient.Client
cosmosclient.Client
}

func New(ctx context.Context, host string) (Client, error) {
Expand All @@ -26,22 +26,12 @@ func New(ctx context.Context, host string) (Client, error) {
return Client{}, err
}

return Client{cc: client}, nil
}

func (c Client) Status(ctx context.Context, fn func(coretypes.ResultStatus) error) context.CancelFunc {
return c.BlockCallback(ctx, func(int64) error {
status, err := c.cc.RPC.Status(ctx)
if err != nil {
return err
}
return fn(*status)
})
return Client{Client: client}, nil
}

func (c Client) NetInfo(ctx context.Context, fn func(coretypes.ResultNetInfo) error) context.CancelFunc {
return c.BlockCallback(ctx, func(int64) error {
netInfo, err := c.cc.RPC.NetInfo(ctx)
netInfo, err := c.RPC.NetInfo(ctx)
if err != nil {
return err
}
Expand All @@ -51,15 +41,15 @@ func (c Client) NetInfo(ctx context.Context, fn func(coretypes.ResultNetInfo) er

func (c Client) Health(ctx context.Context, fn func(*coretypes.ResultHealth, error) error) context.CancelFunc {
return c.BlockCallback(ctx, func(int64) error {
return fn(c.cc.RPC.Health(ctx))
return fn(c.RPC.Health(ctx))
})
}

func (c Client) Validators(ctx context.Context, fn func(coretypes.ResultValidators) error) context.CancelFunc {
return c.BlockCallback(ctx, func(height int64) error {
page := 1
count := 1_000
validators, err := c.cc.RPC.Validators(ctx, &height, &page, &count)
validators, err := c.RPC.Validators(ctx, &height, &page, &count)
if err != nil {
return err
}
Expand All @@ -68,43 +58,18 @@ func (c Client) Validators(ctx context.Context, fn func(coretypes.ResultValidato
}

func (c Client) BlockCallback(ctx context.Context, fn func(height int64) error) context.CancelFunc {
return c.callback(ctx, blockTime, func() error {
if err := c.cc.WaitForNextBlock(ctx); err != nil {
return Callback(ctx, blockTime, func() error {
if err := c.WaitForNextBlock(ctx); err != nil {
return err
}
height, err := c.cc.LatestBlockHeight(ctx)
height, err := c.LatestBlockHeight(ctx)
if err != nil {
return err
}
return fn(height)
})
}

func (c Client) callback(ctx context.Context, d time.Duration, fn func() error) context.CancelFunc {
ctx, cancel := context.WithCancel(ctx)
ticker := time.NewTicker(d)

go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := fn(); err != nil {
// TODO find a better way to send logs
// fmt.Println(err)
cancel()
return
}
}
}
}()
return func() {
cancel()
ticker.Stop()
}
}

func (c Client) NewBlock(ctx context.Context, fn func(types.EventDataNewBlock) error) (context.CancelFunc, error) {
return c.Subscribe(
ctx,
Expand Down Expand Up @@ -152,7 +117,7 @@ func (c Client) Tx(ctx context.Context, fn func(types.EventDataTx) error) (conte

func (c Client) Subscribe(ctx context.Context, subscriber, query string, fn func(coretypes.ResultEvent) error) (context.CancelFunc, error) {
ctx, cancel := context.WithCancel(ctx)
out, err := c.cc.RPC.Subscribe(ctx, subscriber, query)
out, err := c.RPC.Subscribe(ctx, subscriber, query)
if err != nil {
return cancel, err
}
Expand All @@ -175,3 +140,29 @@ func (c Client) Subscribe(ctx context.Context, subscriber, query string, fn func

return cancel, nil
}

func Callback(ctx context.Context, d time.Duration, fn func() error) context.CancelFunc {
ctx, cancel := context.WithCancel(ctx)
ticker := time.NewTicker(d)

go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := fn(); err != nil {
// TODO find a better way to send logs
// fmt.Println(err)
cancel()
return
}
}
}
}()

return func() {
cancel()
ticker.Stop()
}
}
4 changes: 2 additions & 2 deletions pkg/view/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (w *Widget) SetSecondsPerBlock(txt string, opts ...text.WriteOption) error
return w.secondsPerBlock.Write(txt, opts...)
}

func (w *Widget) SetMaxBlockSize(txt string, opts ...text.WriteOption) error {
func (w *Widget) SetMaxBlockSize(blockSize int64, opts ...text.WriteOption) error {
w.maxBlockSize.Reset()
return w.maxBlockSize.Write(txt, opts...)
return w.maxBlockSize.Write(strconv.FormatInt(blockSize, 10), opts...)
}

func (w *Widget) SetValidators(validators int, opts ...text.WriteOption) error {
Expand Down
42 changes: 28 additions & 14 deletions services/explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package explorer

import (
"context"
"time"

"github.com/cometbft/cometbft/libs/json"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
Expand All @@ -27,30 +28,43 @@ func Run(ctx context.Context, host string) error {
return err
}

cancel := c.Status(ctx, func(status coretypes.ResultStatus) error {
return v.SetCurrentNetwork(status.NodeInfo.Network)
})
defer cancel()
defer client.Callback(ctx, 1*time.Second, func() error {
now := time.Now()
return v.SetTime(now.Format("2006-01-02\n03:04:05 PM\n"))
})()

status, err := c.Status(ctx)
if err != nil {
return err
}
if err := v.SetCurrentNetwork(status.NodeInfo.Network); err != nil {
return err
}

params, err := c.RPC.ConsensusParams(ctx, nil)
if err != nil {
return err
}
if err := v.SetMaxBlockSize(params.ConsensusParams.Block.MaxBytes); err != nil {
return err
}

cancel = c.NetInfo(ctx, func(info coretypes.ResultNetInfo) error {
defer c.NetInfo(ctx, func(info coretypes.ResultNetInfo) error {
return v.SetPeers(info.NPeers)
})
defer cancel()
})()

cancel = c.Health(ctx, func(health *coretypes.ResultHealth, err error) error {
defer c.Health(ctx, func(health *coretypes.ResultHealth, err error) error {
if health != nil && err == nil {
return v.SetHealth(statusConnected)
}
return v.SetHealth(statusNotConnected)
})
defer cancel()
})()

cancel = c.Validators(ctx, func(validators coretypes.ResultValidators) error {
defer c.Validators(ctx, func(validators coretypes.ResultValidators) error {
return v.SetValidators(validators.Total)
})
defer cancel()
})()

cancel, err = c.NewBlock(ctx, func(block types.EventDataNewBlock) error {
cancel, err := c.NewBlock(ctx, func(block types.EventDataNewBlock) error {
return v.SetBlock(block.Block.Height)
})
defer cancel()
Expand Down

0 comments on commit afd498e

Please sign in to comment.