Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Mar 2, 2023
1 parent 6b885f5 commit 7057b7a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 84 deletions.
6 changes: 3 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ func StartCmd() *cobra.Command {
WithChainID(config.Chain.ID).
WithFromAddress(info.GetAddress()).
WithFromName(config.Keyring.From).
WithGasAdjustment(config.Chain.GasAdjustment).
WithGas(config.Chain.Gas).
WithGasAdjustment(config.Chain.GasAdjustment).
WithGasPrices(config.Chain.GasPrices).
WithKeyring(kr).
WithLogger(log).
WithRemotes(remotes).
WithSignModeStr("").
WithSimulateAndExecute(config.Chain.SimulateAndExecute)

account, err := client.QueryAccount(client.FromAddress)
account, err := client.QueryAccount(client.FromAddress())
if err != nil {
return err
}
if account == nil {
return fmt.Errorf("account does not exist with address %s", client.FromAddress)
return fmt.Errorf("account does not exist with address %s", client.FromAddress())
}

log.Info("Fetching the GeoIP location info...")
Expand Down
12 changes: 6 additions & 6 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
)

type Context struct {
logger tmlog.Logger
service types.Service
handler http.Handler
bandwidth *hubtypes.Bandwidth
client *lite.Client
config *types.Config
database *gorm.DB
handler http.Handler
location *types.GeoIPLocation
logger tmlog.Logger
service types.Service
}

func NewContext() *Context {
Expand All @@ -32,27 +32,27 @@ func NewContext() *Context {
func (c *Context) WithBandwidth(v *hubtypes.Bandwidth) *Context { c.bandwidth = v; return c }
func (c *Context) WithClient(v *lite.Client) *Context { c.client = v; return c }
func (c *Context) WithConfig(v *types.Config) *Context { c.config = v; return c }
func (c *Context) WithDatabase(v *gorm.DB) *Context { c.database = v; return c }
func (c *Context) WithHandler(v http.Handler) *Context { c.handler = v; return c }
func (c *Context) WithLocation(v *types.GeoIPLocation) *Context { c.location = v; return c }
func (c *Context) WithLogger(v tmlog.Logger) *Context { c.logger = v; return c }
func (c *Context) WithService(v types.Service) *Context { c.service = v; return c }
func (c *Context) WithDatabase(v *gorm.DB) *Context { c.database = 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) Database() *gorm.DB { return c.database }
func (c *Context) Handler() http.Handler { return c.handler }
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) Operator() sdk.AccAddress { return c.client.FromAddress() }
func (c *Context) RemoteURL() string { return c.Config().Node.RemoteURL }
func (c *Context) Service() types.Service { return c.service }
func (c *Context) Database() *gorm.DB { return c.database }

func (c *Context) IntervalUpdateSessions() time.Duration {
return c.Config().Node.IntervalUpdateSessions
Expand Down
6 changes: 3 additions & 3 deletions context/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ func (c *Context) RemovePeer(key string) error {

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

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

Expand All @@ -24,7 +24,7 @@ func (c *Context) RemovePeer(key string) error {
func (c *Context) HasPeer(key string) (bool, error) {
data, err := base64.StdEncoding.DecodeString(key)
if err != nil {
c.Log().Error("Failed to decode the key", "error", err, "key", key)
c.Log().Error("failed to decode the key", "error", err, "key", key)
return false, err
}

Expand Down
8 changes: 4 additions & 4 deletions context/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *Context) RegisterNode() error {
),
)
if err != nil {
c.Log().Error("Failed to register the node", "error", err)
c.Log().Error("failed to register the node", "error", err)
return err
}

Expand All @@ -40,7 +40,7 @@ func (c *Context) UpdateNodeInfo() error {
),
)
if err != nil {
c.Log().Error("Failed to update the node info", "error", err)
c.Log().Error("failed to update the node info", "error", err)
return err
}

Expand All @@ -57,7 +57,7 @@ func (c *Context) UpdateNodeStatus() error {
),
)
if err != nil {
c.Log().Error("Failed to update the node status", "error", err)
c.Log().Error("failed to update the node status", "error", err)
return err
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func (c *Context) UpdateSessions(items ...types.Session) error {
messages...,
)
if err != nil {
c.Log().Error("Failed to update the sessions", "error", err)
c.Log().Error("failed to update the sessions", "error", err)
return err
}

Expand Down
75 changes: 40 additions & 35 deletions lite/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
)

type Client struct {
*sync.Mutex
tmlog.Logger
client.Context
tx.Factory
mutex *sync.Mutex
ctx client.Context
log tmlog.Logger
txf tx.Factory
remotes []string
}

func NewClient() *Client {
return &Client{
Mutex: &sync.Mutex{},
mutex: &sync.Mutex{},
}
}

Expand All @@ -48,12 +48,12 @@ func NewDefaultClient() *Client {
}

func (c *Client) WithContext(v client.Context) *Client {
c.Context = v
c.ctx = v
return c
}

func (c *Client) WithLogger(v tmlog.Logger) *Client {
c.Logger = v
c.log = v
return c
}

Expand All @@ -63,61 +63,50 @@ func (c *Client) WithRemotes(v []string) *Client {
}

func (c *Client) WithAccountRetriever(v client.AccountRetriever) *Client {
c.Context = c.Context.WithAccountRetriever(v)
c.Factory = c.Factory.WithAccountRetriever(v)
return c
}

func (c *Client) WithTxConfig(v client.TxConfig) *Client {
c.Context = c.Context.WithTxConfig(v)
c.Factory = c.Factory.WithTxConfig(v)
c.ctx = c.ctx.WithAccountRetriever(v)
c.txf = c.txf.WithAccountRetriever(v)
return c
}

func (c *Client) WithChainID(v string) *Client {
c.Context = c.Context.WithChainID(v)
c.Factory = c.Factory.WithChainID(v)
c.ctx = c.ctx.WithChainID(v)
c.txf = c.txf.WithChainID(v)
return c
}

func (c *Client) WithFeeGranterAddress(v sdk.AccAddress) *Client {
c.Context = c.Context.WithFeeGranterAddress(v)
c.ctx = c.ctx.WithFeeGranterAddress(v)
return c
}

func (c *Client) WithFromAddress(v sdk.AccAddress) *Client {
c.Context = c.Context.WithFromAddress(v)
c.ctx = c.ctx.WithFromAddress(v)
return c
}

func (c *Client) WithFromName(v string) *Client {
c.Context = c.Context.WithFromName(v)
return c
}

func (c *Client) WithKeyring(v keyring.Keyring) *Client {
c.Context = c.Context.WithKeyring(v)
c.Factory = c.Factory.WithKeybase(v)
c.ctx = c.ctx.WithFromName(v)
return c
}

func (c *Client) WithGas(v uint64) *Client {
c.Factory = c.Factory.WithGas(v)
c.txf = c.txf.WithGas(v)
return c
}

func (c *Client) WithSimulateAndExecute(v bool) *Client {
c.Factory = c.Factory.WithSimulateAndExecute(v)
func (c *Client) WithGasAdjustment(v float64) *Client {
c.txf = c.txf.WithGasAdjustment(v)
return c
}

func (c *Client) WithGasAdjustment(v float64) *Client {
c.Factory = c.Factory.WithGasAdjustment(v)
func (c *Client) WithGasPrices(v string) *Client {
c.txf = c.txf.WithGasPrices(v)
return c
}

func (c *Client) WithGasPrices(v string) *Client {
c.Factory = c.Factory.WithGasPrices(v)
func (c *Client) WithKeyring(v keyring.Keyring) *Client {
c.ctx = c.ctx.WithKeyring(v)
c.txf = c.txf.WithKeybase(v)
return c
}

Expand All @@ -130,7 +119,23 @@ func (c *Client) WithSignModeStr(v string) *Client {
m = signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON
}

c.Context = c.Context.WithSignModeStr(v)
c.Factory = c.Factory.WithSignMode(m)
c.ctx = c.ctx.WithSignModeStr(v)
c.txf = c.txf.WithSignMode(m)
return c
}

func (c *Client) WithSimulateAndExecute(v bool) *Client {
c.txf = c.txf.WithSimulateAndExecute(v)
return c
}

func (c *Client) WithTxConfig(v client.TxConfig) *Client {
c.ctx = c.ctx.WithTxConfig(v)
c.txf = c.txf.WithTxConfig(v)
return c
}

func (c *Client) FromAddress() sdk.AccAddress { return c.ctx.FromAddress }
func (c *Client) FromName() string { return c.ctx.FromName }
func (c *Client) SimulateAndExecute() bool { return c.txf.SimulateAndExecute() }
func (c *Client) TxConfig() client.TxConfig { return c.ctx.TxConfig }

0 comments on commit 7057b7a

Please sign in to comment.