Skip to content

Commit

Permalink
Refactored code added error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Jun 22, 2021
1 parent bacb21e commit fba8130
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 244 deletions.
30 changes: 15 additions & 15 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ func (c *Context) WithRouter(v *mux.Router) *Context { c.router = v;
func (c *Context) WithService(v types.Service) *Context { c.service = v; return c }
func (c *Context) WithSessions(v *types.Sessions) *Context { c.sessions = v; return c }

func (c *Context) Address() hubtypes.NodeAddress { return c.Operator().Bytes() }
func (c *Context) Bandwidth() *hubtypes.Bandwidth { return c.bandwidth }
func (c *Context) Client() *lite.Client { return c.client }
func (c *Context) Config() *types.Config { return c.config }
func (c *Context) IntervalSetSessions() time.Duration { return c.Config().Node.IntervalSetSessions }
func (c *Context) IntervalSetStatus() time.Duration { return c.Config().Node.IntervalSetStatus }
func (c *Context) ListenOn() string { return c.Config().Node.ListenOn }
func (c *Context) Location() *types.GeoIPLocation { return c.location }
func (c *Context) Log() tmlog.Logger { return c.logger }
func (c *Context) Moniker() string { return c.Config().Node.Moniker }
func (c *Context) Operator() sdk.AccAddress { return c.client.FromAddress() }
func (c *Context) RemoteURL() string { return c.Config().Node.RemoteURL }
func (c *Context) Router() *mux.Router { return c.router }
func (c *Context) Service() types.Service { return c.service }
func (c *Context) Sessions() *types.Sessions { return c.sessions }
func (c *Context) Address() hubtypes.NodeAddress { return c.Operator().Bytes() }
func (c *Context) Bandwidth() *hubtypes.Bandwidth { return c.bandwidth }
func (c *Context) Client() *lite.Client { return c.client }
func (c *Context) Config() *types.Config { return c.config }
func (c *Context) IntervalSetSessions() time.Duration { return c.Config().Node.IntervalSetSessions }
func (c *Context) IntervalUpdateStatus() time.Duration { return c.Config().Node.IntervalUpdateStatus }
func (c *Context) ListenOn() string { return c.Config().Node.ListenOn }
func (c *Context) Location() *types.GeoIPLocation { return c.location }
func (c *Context) Log() tmlog.Logger { return c.logger }
func (c *Context) Moniker() string { return c.Config().Node.Moniker }
func (c *Context) Operator() sdk.AccAddress { return c.client.FromAddress() }
func (c *Context) RemoteURL() string { return c.Config().Node.RemoteURL }
func (c *Context) Router() *mux.Router { return c.router }
func (c *Context) Service() types.Service { return c.service }
func (c *Context) Sessions() *types.Sessions { return c.sessions }

func (c *Context) IntervalUpdateSessions() time.Duration {
return c.Config().Node.IntervalUpdateSessions
Expand Down
46 changes: 46 additions & 0 deletions context/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package context

import (
"encoding/base64"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func (c *Context) RemovePeer(key string) error {
c.Log().Info("Removing peer from service", "key", key)

data, err := base64.StdEncoding.DecodeString(key)
if err != nil {
c.Log().Error("Failed to decode the key", "error", err)
return err
}

if err := c.Service().RemovePeer(data); err != nil {
c.Log().Error("Failed to remove the peer from service", "error", err)
return err
}

c.Log().Info("Removed peer from service...")
return nil
}

func (c *Context) RemoveSession(key string, address sdk.AccAddress) error {
c.Log().Info("Removing session from list", "key", key, "address", address)

c.Sessions().DeleteByKey(key)
c.Sessions().DeleteByAddress(address)

c.Log().Info("Removed session from list...")
return nil
}

func (c *Context) RemovePeerAndSession(key string, address sdk.AccAddress) error {
if err := c.RemovePeer(key); err != nil {
return err
}
if err := c.RemoveSession(key, address); err != nil {
return err
}

return nil
}
96 changes: 96 additions & 0 deletions context/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package context

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/types"
nodetypes "github.com/sentinel-official/hub/x/node/types"
sessiontypes "github.com/sentinel-official/hub/x/session/types"

"github.com/sentinel-official/dvpn-node/types"
)

func (c *Context) RegisterNode() error {
c.Log().Info("Registering node...")

_, err := c.Client().BroadcastTx(
nodetypes.NewMsgRegisterRequest(
c.Operator(),
c.Provider(),
c.Price(),
c.RemoteURL(),
),
)
if err != nil {
c.Log().Error("Failed to register node", "error", err)
return err
}

return nil
}

func (c *Context) UpdateNodeInfo() error {
c.Log().Info("Updating node info...")

_, err := c.Client().BroadcastTx(
nodetypes.NewMsgUpdateRequest(
c.Address(),
c.Provider(),
c.Price(),
c.RemoteURL(),
),
)
if err != nil {
c.Log().Error("Failed to update node info", "error", err)
return err
}

return nil
}

func (c *Context) UpdateNodeStatus() error {
c.Log().Info("Updating node status...")

_, err := c.Client().BroadcastTx(
nodetypes.NewMsgSetStatusRequest(
c.Address(),
hubtypes.StatusActive,
),
)
if err != nil {
c.Log().Error("Failed to update node status", "error", err)
return err
}

return nil
}

func (c *Context) UpdateSessions(items ...types.Session) error {
c.Log().Info("Updating sessions...")

var messages []sdk.Msg
for _, item := range items {
messages = append(messages,
sessiontypes.NewMsgUpdateRequest(
c.Address(),
sessiontypes.Proof{
Id: item.ID,
Duration: time.Since(item.ConnectedAt),
Bandwidth: hubtypes.NewBandwidthFromInt64(item.Download, item.Upload),
},
nil,
),
)
}

_, err := c.Client().BroadcastTx(
messages...,
)
if err != nil {
c.Log().Error("Failed to update sessions", "error", err)
return err
}

return nil
}
7 changes: 6 additions & 1 deletion lite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ func (c *Client) BroadcastTx(messages ...sdk.Msg) (res *sdk.TxResponse, err erro
)

if c.SimulateAndExecute() {
c.Log().Info("Calculating the gas by simulating the transaction...")
c.Log().Info("Calculating the Gas by simulating the transaction...")

_, adjusted, err := tx.CalculateGas(c.ctx.QueryWithData, txf, messages...)
if err != nil {
c.Log().Error("Failed to calculate the Gas", "error", err)
return nil, err
}

Expand All @@ -39,15 +41,18 @@ func (c *Client) BroadcastTx(messages ...sdk.Msg) (res *sdk.TxResponse, err erro

txb, err := tx.BuildUnsignedTx(txf, messages...)
if err != nil {
c.Log().Error("Failed to build the unsigned transaction", "error", err)
return nil, err
}

if err := tx.Sign(txf, c.From(), txb, true); err != nil {
c.Log().Error("Failed to sign the transaction", "error", err)
return nil, err
}

txBytes, err := c.TxConfig().TxEncoder()(txb.GetTx())
if err != nil {
c.Log().Error("Failed to encode the transaction", "error", err)
return nil, err
}

Expand Down
9 changes: 1 addition & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ import (
"github.com/sentinel-official/dvpn-node/types"
)

func init() {
hubtypes.GetConfig().Seal()
cobra.EnableCommandSorting = false
}

func main() {
hubtypes.GetConfig().Seal()
root := &cobra.Command{
Use: "sentinel-dvpn-node",
SilenceUsage: true,
Expand All @@ -26,11 +22,8 @@ func main() {
root.AddCommand(
cmd.ConfigCmd(),
cmd.KeysCmd(),
flags.LineBreak,
wireguard.Command(),
flags.LineBreak,
cmd.StartCmd(),
flags.LineBreak,
version.NewVersionCommand(),
)

Expand Down
11 changes: 6 additions & 5 deletions node/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (n *Node) jobSetSessions() error {
for ; ; <-t.C {
peers, err := n.Service().Peers()
if err != nil {
n.Log().Error("Failed to get connected peers", "error", err)
return err
}
n.Log().Info("Connected peers", "count", len(peers))
Expand Down Expand Up @@ -48,11 +49,11 @@ func (n *Node) jobSetSessions() error {
}

func (n *Node) jobUpdateStatus() error {
n.Log().Info("Starting job", "name", "update_status", "interval", n.IntervalSetStatus())
n.Log().Info("Starting job", "name", "update_status", "interval", n.IntervalUpdateStatus())

t := time.NewTicker(n.IntervalSetStatus())
t := time.NewTicker(n.IntervalUpdateStatus())
for ; ; <-t.C {
if err := n.updateStatus(); err != nil {
if err := n.UpdateNodeStatus(); err != nil {
return err
}
}
Expand Down Expand Up @@ -98,7 +99,7 @@ func (n *Node) jobUpdateSessions() error {
}()

if remove {
if err := n.RemovePeerAndSession(items[i]); err != nil {
if err := n.RemovePeerAndSession(items[i].Key, items[i].Address); err != nil {
return err
}
}
Expand All @@ -110,7 +111,7 @@ func (n *Node) jobUpdateSessions() error {
if len(items) == 0 {
continue
}
if err := n.updateSessions(items...); err != nil {
if err := n.UpdateSessions(items...); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func (n *Node) Initialize() error {
}

if result == nil {
return n.register()
return n.RegisterNode()
}

return n.updateInfo()
return n.UpdateNodeInfo()
}

func (n *Node) Start() error {
Expand Down
49 changes: 0 additions & 49 deletions node/service.go

This file was deleted.

Loading

0 comments on commit fba8130

Please sign in to comment.