-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
R4R: CoreContext Refactor #1741
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
debbf0d
Merge pull request #1741: CoreContext Refactor
87becc0
Update keyTypes to be a global var
0355028
Add TODOs to update SendTx
8abd1e7
Merge branch 'develop' into bez/1551-core-ctx-refactor
cwgoes 8e034fb
Address PR comments and concerns
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,115 @@ | ||
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" | ||
|
||
// CLIContext implements a typical CLI context created in SDK modules for | ||
// transaction handling and queries. | ||
type CLIContext 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 | ||
} | ||
|
||
// NewCLIContext returns a new initialized CLIContext with parameters from the | ||
// command line using Viper. | ||
func NewCLIContext() CLIContext { | ||
var rpc rpcclient.Client | ||
|
||
nodeURI := viper.GetString(client.FlagNode) | ||
if nodeURI != "" { | ||
rpc = rpcclient.NewHTTP(nodeURI, "/websocket") | ||
} | ||
|
||
return CLIContext{ | ||
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 CLIContext) WithCodec(cdc *wire.Codec) CLIContext { | ||
ctx.Codec = cdc | ||
return ctx | ||
} | ||
|
||
// WithAccountDecoder returns a copy of the context with an updated account | ||
// decoder. | ||
func (ctx CLIContext) WithAccountDecoder(decoder auth.AccountDecoder) CLIContext { | ||
ctx.AccDecoder = decoder | ||
return ctx | ||
} | ||
|
||
// WithLogger returns a copy of the context with an updated logger. | ||
func (ctx CLIContext) WithLogger(w io.Writer) CLIContext { | ||
ctx.Logger = w | ||
return ctx | ||
} | ||
|
||
// WithAccountStore returns a copy of the context with an updated AccountStore. | ||
func (ctx CLIContext) WithAccountStore(accountStore string) CLIContext { | ||
ctx.AccountStore = accountStore | ||
return ctx | ||
} | ||
|
||
// WithFromAddressName returns a copy of the context with an updated from | ||
// address. | ||
func (ctx CLIContext) WithFromAddressName(addrName string) CLIContext { | ||
ctx.FromAddressName = addrName | ||
return ctx | ||
} | ||
|
||
// WithTrustNode returns a copy of the context with an updated TrustNode flag. | ||
func (ctx CLIContext) WithTrustNode(trustNode bool) CLIContext { | ||
ctx.TrustNode = trustNode | ||
return ctx | ||
} | ||
|
||
// WithNodeURI returns a copy of the context with an updated node URI. | ||
func (ctx CLIContext) WithNodeURI(nodeURI string) CLIContext { | ||
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 CLIContext) WithClient(client rpcclient.Client) CLIContext { | ||
ctx.Client = client | ||
return ctx | ||
} | ||
|
||
// WithUseLedger returns a copy of the context with an updated UseLedger flag. | ||
func (ctx CLIContext) WithUseLedger(useLedger bool) CLIContext { | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
propose: QueryContext -> CliContext. Since Query isn't the only thing we're doing there.