-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1741: CoreContext Refactor
- Loading branch information
Aleksandr Bezobchuk
committed
Jul 24, 2018
1 parent
75eeaad
commit 7312475
Showing
47 changed files
with
1,437 additions
and
1,129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package context | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/spf13/viper" | ||
rpcclient "github.com/tendermint/tendermint/rpc/client" | ||
) | ||
|
||
const ctxAccStoreName = "acc" | ||
|
||
// QueryContext implements a typical context created in SDK modules for | ||
// queries. | ||
type QueryContext struct { | ||
Codec *wire.Codec | ||
AccDecoder auth.AccountDecoder | ||
Client rpcclient.Client | ||
Logger io.Writer | ||
Height int64 | ||
NodeURI string | ||
FromAddressName string | ||
AccountStore string | ||
TrustNode bool | ||
UseLedger bool | ||
Async bool | ||
JSON bool | ||
PrintResponse bool | ||
} | ||
|
||
// NewQueryContextFromCLI returns a new initialized QueryContext with | ||
// parameters from the command line using Viper. | ||
func NewQueryContextFromCLI() QueryContext { | ||
var rpc rpcclient.Client | ||
|
||
nodeURI := viper.GetString(client.FlagNode) | ||
if nodeURI != "" { | ||
rpc = rpcclient.NewHTTP(nodeURI, "/websocket") | ||
} | ||
|
||
return QueryContext{ | ||
Client: rpc, | ||
NodeURI: nodeURI, | ||
AccountStore: ctxAccStoreName, | ||
FromAddressName: viper.GetString(client.FlagFrom), | ||
Height: viper.GetInt64(client.FlagHeight), | ||
TrustNode: viper.GetBool(client.FlagTrustNode), | ||
UseLedger: viper.GetBool(client.FlagUseLedger), | ||
Async: viper.GetBool(client.FlagAsync), | ||
JSON: viper.GetBool(client.FlagJson), | ||
PrintResponse: viper.GetBool(client.FlagPrintResponse), | ||
} | ||
} | ||
|
||
// WithCodec returns a copy of the context with an updated codec. | ||
func (ctx QueryContext) WithCodec(cdc *wire.Codec) QueryContext { | ||
ctx.Codec = cdc | ||
return ctx | ||
} | ||
|
||
// WithAccountDecoder returns a copy of the context with an updated account | ||
// decoder. | ||
func (ctx QueryContext) WithAccountDecoder(decoder auth.AccountDecoder) QueryContext { | ||
ctx.AccDecoder = decoder | ||
return ctx | ||
} | ||
|
||
// WithLogger returns a copy of the context with an updated logger. | ||
func (ctx QueryContext) WithLogger(w io.Writer) QueryContext { | ||
ctx.Logger = w | ||
return ctx | ||
} | ||
|
||
// WithAccountStore returns a copy of the context with an updated AccountStore. | ||
func (ctx QueryContext) WithAccountStore(accountStore string) QueryContext { | ||
ctx.AccountStore = accountStore | ||
return ctx | ||
} | ||
|
||
// WithFromAddressName returns a copy of the context with an updated from | ||
// address. | ||
func (ctx QueryContext) WithFromAddressName(addrName string) QueryContext { | ||
ctx.FromAddressName = addrName | ||
return ctx | ||
} | ||
|
||
// WithTrustNode returns a copy of the context with an updated TrustNode flag. | ||
func (ctx QueryContext) WithTrustNode(trustNode bool) QueryContext { | ||
ctx.TrustNode = trustNode | ||
return ctx | ||
} | ||
|
||
// WithNodeURI returns a copy of the context with an updated node URI. | ||
func (ctx QueryContext) WithNodeURI(nodeURI string) QueryContext { | ||
ctx.NodeURI = nodeURI | ||
ctx.Client = rpcclient.NewHTTP(nodeURI, "/websocket") | ||
return ctx | ||
} | ||
|
||
// WithClient returns a copy of the context with an updated RPC client | ||
// instance. | ||
func (ctx QueryContext) WithClient(client rpcclient.Client) QueryContext { | ||
ctx.Client = client | ||
return ctx | ||
} | ||
|
||
// WithUseLedger returns a copy of the context with an updated UseLedger flag. | ||
func (ctx QueryContext) WithUseLedger(useLedger bool) QueryContext { | ||
ctx.UseLedger = useLedger | ||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package context | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// ErrInvalidAccount returns a standardized error reflecting that a given | ||
// account address does not exist. | ||
func ErrInvalidAccount(addr sdk.AccAddress) error { | ||
return errors.Errorf(`No account with address %s was found in the state. | ||
Are you sure there has been a transaction involving it?`, addr) | ||
} |
Oops, something went wrong.