From 02a2008c0d1445b4682808b06f8db9f94adee405 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 12:39:43 +0300 Subject: [PATCH 1/9] move gaia to cmd, update makefile --- Makefile | 24 ++++++++++++++---------- {examples/gaia => cmd}/gaiacli/client.go | 0 {examples/gaia => cmd}/gaiacli/key.go | 0 {examples/gaia => cmd}/gaiacli/main.go | 0 {examples/gaia => cmd}/gaiad/main.go | 0 examples/gaia/README.md | 3 --- 6 files changed, 14 insertions(+), 13 deletions(-) rename {examples/gaia => cmd}/gaiacli/client.go (100%) rename {examples/gaia => cmd}/gaiacli/key.go (100%) rename {examples/gaia => cmd}/gaiacli/main.go (100%) rename {examples/gaia => cmd}/gaiad/main.go (100%) delete mode 100644 examples/gaia/README.md diff --git a/Makefile b/Makefile index 023e4f8c6..253434574 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,16 @@ ci: get_tools get_vendor_deps build test_cover ### Build # This can be unified later, here for easy demos -gaia: - go build $(BUILD_FLAGS) -o build/gaiad ./examples/gaia/gaiad - go build $(BUILD_FLAGS) -o build/gaiacli ./examples/gaia/gaiacli - build: - @rm -rf $(shell pwd)/examples/basecoin/vendor/ - @rm -rf $(shell pwd)/examples/democoin/vendor/ +ifeq ($(OS),Windows_NT) + go build $(BUILD_FLAGS) -o build/gaiad.exe ./cmd/gaiad + go build $(BUILD_FLAGS) -o build/gaiacli.exe ./cmd/gaiacli +else + go build $(BUILD_FLAGS) -o build/gaiad ./cmd/gaiad + go build $(BUILD_FLAGS) -o build/gaiacli ./cmd/gaiacli +endif + +build_examples: ifeq ($(OS),Windows_NT) go build $(BUILD_FLAGS) -o build/basecoind.exe ./examples/basecoin/cmd/basecoind go build $(BUILD_FLAGS) -o build/basecli.exe ./examples/basecoin/cmd/basecli @@ -33,6 +36,10 @@ else endif install: + go install $(BUILD_FLAGS) ./cmd/gaiad + go install $(BUILD_FLAGS) ./cmd/gaiacli + +install_examples: go install $(BUILD_FLAGS) ./examples/basecoin/cmd/basecoind go install $(BUILD_FLAGS) ./examples/basecoin/cmd/basecli go install $(BUILD_FLAGS) ./examples/democoin/cmd/democoind @@ -84,12 +91,9 @@ test: test_unit # test_cli # go test -coverprofile=c.out && go tool cover -html=c.out test_unit: - @rm -rf examples/basecoin/vendor/ - @rm -rf examples/democoin/vendor/ @go test $(PACKAGES) test_cover: - @rm -rf examples/basecoin/vendor/ @bash tests/test_cover.sh benchmark: @@ -123,4 +127,4 @@ devdoc_update: # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. # https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: build dist check_tools get_tools get_vendor_deps draw_deps test test_unit test_tutorial benchmark devdoc_init devdoc devdoc_save devdoc_update +.PHONY: build build_examples install install_examples dist check_tools get_tools get_vendor_deps draw_deps test test_unit test_tutorial benchmark devdoc_init devdoc devdoc_save devdoc_update diff --git a/examples/gaia/gaiacli/client.go b/cmd/gaiacli/client.go similarity index 100% rename from examples/gaia/gaiacli/client.go rename to cmd/gaiacli/client.go diff --git a/examples/gaia/gaiacli/key.go b/cmd/gaiacli/key.go similarity index 100% rename from examples/gaia/gaiacli/key.go rename to cmd/gaiacli/key.go diff --git a/examples/gaia/gaiacli/main.go b/cmd/gaiacli/main.go similarity index 100% rename from examples/gaia/gaiacli/main.go rename to cmd/gaiacli/main.go diff --git a/examples/gaia/gaiad/main.go b/cmd/gaiad/main.go similarity index 100% rename from examples/gaia/gaiad/main.go rename to cmd/gaiad/main.go diff --git a/examples/gaia/README.md b/examples/gaia/README.md deleted file mode 100644 index 485af236f..000000000 --- a/examples/gaia/README.md +++ /dev/null @@ -1,3 +0,0 @@ -Gaiad is the abci application, which can be run stand-alone, or in-process with tendermint. - -Gaiacli is a client application, which connects to tendermint rpc, and sends transactions and queries the state. It uses light-client proofs to guarantee the results even if it doesn't have 100% trust in the node it connects to. From 9e6bc70e288d380dea814205cc2bdbb2b8efa597 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:08:40 +0300 Subject: [PATCH 2/9] remove basecoin license/makefile --- examples/basecoin/LICENSE | 204 ------------------------------------- examples/basecoin/Makefile | 22 ---- 2 files changed, 226 deletions(-) delete mode 100644 examples/basecoin/LICENSE delete mode 100644 examples/basecoin/Makefile diff --git a/examples/basecoin/LICENSE b/examples/basecoin/LICENSE deleted file mode 100644 index a3811d788..000000000 --- a/examples/basecoin/LICENSE +++ /dev/null @@ -1,204 +0,0 @@ -Cosmos-SDK Basecoin (template) -License: Apache2.0 - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "{}" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2018 All in Bits, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/examples/basecoin/Makefile b/examples/basecoin/Makefile deleted file mode 100644 index e0cf14caa..000000000 --- a/examples/basecoin/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -PACKAGES=$(shell go list ./... | grep -v '/vendor/') -BUILD_FLAGS = -ldflags "-X github.com/cosmos/cosmos-sdk/examples/basecoin/version.GitCommit=`git rev-parse --short HEAD`" - -all: get_tools get_vendor_deps build test - -get_tools: - go get github.com/golang/dep/cmd/dep - -build: - go build $(BUILD_FLAGS) -o build/basecoin ./cmd/... - -get_vendor_deps: - @rm -rf vendor/ - @dep ensure - -test: - @go test $(PACKAGES) - -benchmark: - @go test -bench=. $(PACKAGES) - -.PHONY: all build test benchmark From f79d06c9f3adfba807f792c86f65944bb10a10ee Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:09:02 +0300 Subject: [PATCH 3/9] refactor basecoind, change GenAppOptions --- examples/basecoin/cmd/basecoind/main.go | 66 ++++++++----------------- mock/app.go | 5 +- server/init.go | 27 +++++++--- server/pre_run.go | 42 ++++++++++++++++ server/show_node_id.go | 4 +- 5 files changed, 86 insertions(+), 58 deletions(-) create mode 100644 server/pre_run.go diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index ad26d08c4..9b38660c4 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -7,14 +7,9 @@ import ( "path/filepath" "github.com/spf13/cobra" - "github.com/spf13/viper" abci "github.com/tendermint/abci/types" - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" - cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tmlibs/cli" - tmflags "github.com/tendermint/tmlibs/cli/flags" - cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -25,40 +20,20 @@ import ( // basecoindCmd is the entry point for this binary var ( - context = server.NewContext(nil, nil) - basecoindCmd = &cobra.Command{ - Use: "gaiad", - Short: "Gaia Daemon (server)", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Name() == version.VersionCmd.Name() { - return nil - } - config, err := tcmd.ParseConfig() - if err != nil { - return err - } - logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) - logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel()) - if err != nil { - return err - } - if viper.GetBool(cli.TraceFlag) { - logger = log.NewTracingLogger(logger) - } - logger = logger.With("module", "main") - context.Config = config - context.Logger = logger - return nil - }, + context = server.NewContext(nil, nil) + rootCmd = &cobra.Command{ + Use: "basecoind", + Short: "Basecoin Daemon (server)", + PersistentPreRunE: server.PersistentPreRunEFn(context), } ) -// defaultOptions sets up the app_options for the -// default genesis file -func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) { - addr, secret, err := server.GenerateCoinKey() - if err != nil { - return nil, "", nil, err +// defaultOptions sets up the app_state for the +// default genesis file. It is a server.GenAppState +// used for `basecoind init`. +func defaultOptions(args ...string) (json.RawMessage, error) { + if len(args) != 2 { + return nil, fmt.Errorf("Expected 2 args: address and coin denom") } opts := fmt.Sprintf(`{ "accounts": [{ @@ -70,24 +45,25 @@ func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error } ] }] - }`, addr) - return json.RawMessage(opts), secret, addr, nil + }`, args) + return json.RawMessage(opts), nil } func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { - dbMain, err := dbm.NewGoLevelDB("basecoin", filepath.Join(rootDir, "data")) + dataDir := filepath.Join(rootDir, "data") + dbMain, err := dbm.NewGoLevelDB("basecoin", dataDir) if err != nil { return nil, err } - dbAcc, err := dbm.NewGoLevelDB("basecoin-acc", filepath.Join(rootDir, "data")) + dbAcc, err := dbm.NewGoLevelDB("basecoin-acc", dataDir) if err != nil { return nil, err } - dbIBC, err := dbm.NewGoLevelDB("basecoin-ibc", filepath.Join(rootDir, "data")) + dbIBC, err := dbm.NewGoLevelDB("basecoin-ibc", dataDir) if err != nil { return nil, err } - dbStaking, err := dbm.NewGoLevelDB("basecoin-staking", filepath.Join(rootDir, "data")) + dbStaking, err := dbm.NewGoLevelDB("basecoin-staking", dataDir) if err != nil { return nil, err } @@ -102,17 +78,17 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { } func main() { - basecoindCmd.AddCommand( + rootCmd.AddCommand( server.InitCmd(defaultOptions, context), server.StartCmd(generateApp, context), server.UnsafeResetAllCmd(context), - server.ShowNodeIdCmd(context), + server.ShowNodeIDCmd(context), server.ShowValidatorCmd(context), version.VersionCmd, ) // prepare and add flags rootDir := os.ExpandEnv("$HOME/.basecoind") - executor := cli.PrepareBaseCmd(basecoindCmd, "BC", rootDir) + executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir) executor.Execute() } diff --git a/mock/app.go b/mock/app.go index eda490a8e..e884a3ede 100644 --- a/mock/app.go +++ b/mock/app.go @@ -6,7 +6,6 @@ import ( "path/filepath" abci "github.com/tendermint/abci/types" - cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -107,7 +106,7 @@ func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci // GenInitOptions can be passed into InitCmd, // returns a static string of a few key-values that can be parsed // by InitChainer -func GenInitOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) { +func GenInitOptions(args ...string) (json.RawMessage, error) { opts := []byte(`{ "values": [ { @@ -120,5 +119,5 @@ func GenInitOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error } ] }`) - return opts, "", nil, nil + return opts, nil } diff --git a/server/init.go b/server/init.go index 4e0049ed4..3a48e9fe5 100644 --- a/server/init.go +++ b/server/init.go @@ -13,12 +13,15 @@ import ( cmn "github.com/tendermint/tmlibs/common" ) +// testnetInformation contains the info necessary +// to setup a testnet including this account and validator. type testnetInformation struct { - Secret string `json:"secret"` + Secret string `json:"secret"` + + ChainID string `json:"chain_id"` Account string `json:"account"` Validator tmtypes.GenesisValidator `json:"validator"` NodeID p2p.ID `json:"node_id"` - ChainID string `json:"chain_id"` } // InitCmd will initialize all files for tendermint, @@ -39,11 +42,11 @@ func InitCmd(gen GenAppState, ctx *Context) *cobra.Command { return &cobraCmd } -// GenAppState can parse command-line to -// generate default app_state for the genesis file. -// Also must return generated seed and address +// GenAppState takes a list of arguments and +// returns a default app_state to be included in +// in the genesis file. // This is application-specific -type GenAppState func(args []string) (json.RawMessage, string, cmn.HexBytes, error) +type GenAppState func(args ...string) (json.RawMessage, error) type initCmd struct { genAppState GenAppState @@ -67,14 +70,22 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error { return nil } + // generate secrete and address + addr, secret, err := GenerateCoinKey() + if err != nil { + return err + } + + var DEFAULT_DENOM = "mycoin" + // Now, we want to add the custom app_state - appState, secret, address, err := c.genAppState(args) + appState, err := c.genAppState(addr.String(), DEFAULT_DENOM) if err != nil { return err } testnetInfo.Secret = secret - testnetInfo.Account = address.String() + testnetInfo.Account = addr.String() // And add them to the genesis file genFile := config.GenesisFile() diff --git a/server/pre_run.go b/server/pre_run.go new file mode 100644 index 000000000..c82a8f364 --- /dev/null +++ b/server/pre_run.go @@ -0,0 +1,42 @@ +package server + +import ( + "os" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/cosmos/cosmos-sdk/version" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + cfg "github.com/tendermint/tendermint/config" + "github.com/tendermint/tmlibs/cli" + tmflags "github.com/tendermint/tmlibs/cli/flags" + "github.com/tendermint/tmlibs/log" +) + +// PersistentPreRunEFn returns a PersistentPreRunE function for cobra +// that initailizes the passed in context with a properly configured +// logger and config objecy +func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + if cmd.Name() == version.VersionCmd.Name() { + return nil + } + config, err := tcmd.ParseConfig() + if err != nil { + return err + } + logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) + logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel()) + if err != nil { + return err + } + if viper.GetBool(cli.TraceFlag) { + logger = log.NewTracingLogger(logger) + } + logger = logger.With("module", "main") + context.Config = config + context.Logger = logger + return nil + } +} diff --git a/server/show_node_id.go b/server/show_node_id.go index 8c81d734f..bb2270e29 100644 --- a/server/show_node_id.go +++ b/server/show_node_id.go @@ -8,8 +8,8 @@ import ( "github.com/tendermint/tendermint/p2p" ) -// ShowNodeIdCmd - ported from Tendermint, dump node ID to stdout -func ShowNodeIdCmd(ctx *Context) *cobra.Command { +// ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout +func ShowNodeIDCmd(ctx *Context) *cobra.Command { cmd := showNodeId{ctx} return &cobra.Command{ Use: "show_node_id", From 609ea0d0f0a2d8948024eef850b0d4585376adec Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:24:53 +0300 Subject: [PATCH 4/9] more daemon refactoring --- examples/basecoin/cmd/basecoind/main.go | 33 +--- examples/democoin/LICENSE | 204 ------------------------ examples/democoin/Makefile | 22 --- examples/democoin/cmd/democoind/main.go | 83 +++------- mock/app.go | 2 +- server/{pre_run.go => cmd.go} | 15 ++ server/init.go | 27 +++- server/start.go | 8 +- 8 files changed, 65 insertions(+), 329 deletions(-) delete mode 100644 examples/democoin/LICENSE delete mode 100644 examples/democoin/Makefile rename server/{pre_run.go => cmd.go} (79%) diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 9b38660c4..315333008 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -1,8 +1,6 @@ package main import ( - "encoding/json" - "fmt" "os" "path/filepath" @@ -15,7 +13,6 @@ import ( "github.com/cosmos/cosmos-sdk/examples/basecoin/app" "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/version" ) // basecoindCmd is the entry point for this binary @@ -28,27 +25,6 @@ var ( } ) -// defaultOptions sets up the app_state for the -// default genesis file. It is a server.GenAppState -// used for `basecoind init`. -func defaultOptions(args ...string) (json.RawMessage, error) { - if len(args) != 2 { - return nil, fmt.Errorf("Expected 2 args: address and coin denom") - } - opts := fmt.Sprintf(`{ - "accounts": [{ - "address": "%s", - "coins": [ - { - "denom": "mycoin", - "amount": 9007199254740992 - } - ] - }] - }`, args) - return json.RawMessage(opts), nil -} - func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { dataDir := filepath.Join(rootDir, "data") dbMain, err := dbm.NewGoLevelDB("basecoin", dataDir) @@ -78,14 +54,7 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { } func main() { - rootCmd.AddCommand( - server.InitCmd(defaultOptions, context), - server.StartCmd(generateApp, context), - server.UnsafeResetAllCmd(context), - server.ShowNodeIDCmd(context), - server.ShowValidatorCmd(context), - version.VersionCmd, - ) + server.AddCommands(rootCmd, server.DefaultGenAppState, generateApp, context) // prepare and add flags rootDir := os.ExpandEnv("$HOME/.basecoind") diff --git a/examples/democoin/LICENSE b/examples/democoin/LICENSE deleted file mode 100644 index 1697a7443..000000000 --- a/examples/democoin/LICENSE +++ /dev/null @@ -1,204 +0,0 @@ -Cosmos-SDK Democoin (template) -License: Apache2.0 - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "{}" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2018 All in Bits, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/examples/democoin/Makefile b/examples/democoin/Makefile deleted file mode 100644 index 067d03e9b..000000000 --- a/examples/democoin/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -PACKAGES=$(shell go list ./... | grep -v '/vendor/') -BUILD_FLAGS = -ldflags "-X github.com/cosmos/cosmos-sdk/examples/democoin/version.GitCommit=`git rev-parse --short HEAD`" - -all: get_tools get_vendor_deps build test - -get_tools: - go get github.com/golang/dep/cmd/dep - -build: - go build $(BUILD_FLAGS) -o build/democoin ./cmd/... - -get_vendor_deps: - @rm -rf vendor/ - @dep ensure - -test: - @go test $(PACKAGES) - -benchmark: - @go test -bench=. $(PACKAGES) - -.PHONY: all build test benchmark diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 114c28d0f..04436eec2 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -2,82 +2,48 @@ package main import ( "encoding/json" - "fmt" "os" "path/filepath" "github.com/spf13/cobra" - "github.com/spf13/viper" abci "github.com/tendermint/abci/types" - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" - cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tmlibs/cli" - tmflags "github.com/tendermint/tmlibs/cli/flags" - cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" "github.com/cosmos/cosmos-sdk/examples/democoin/app" "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/version" + sdk "github.com/cosmos/cosmos-sdk/types" ) // democoindCmd is the entry point for this binary var ( - context = server.NewContext(nil, nil) - democoindCmd = &cobra.Command{ - Use: "democoind", - Short: "Gaia Daemon (server)", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Name() == version.VersionCmd.Name() { - return nil - } - config, err := tcmd.ParseConfig() - if err != nil { - return err - } - logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) - logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel()) - if err != nil { - return err - } - if viper.GetBool(cli.TraceFlag) { - logger = log.NewTracingLogger(logger) - } - logger = logger.With("module", "main") - context.Config = config - context.Logger = logger - return nil - }, + context = server.NewContext(nil, nil) + rootCmd = &cobra.Command{ + Use: "democoind", + Short: "Gaia Daemon (server)", + PersistentPreRunE: server.PersistentPreRunEFn(context), } ) -// defaultOptions sets up the app_options for the +// defaultAppState sets up the app_state for the // default genesis file -func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) { - addr, secret, err := server.GenerateCoinKey() +func defaultAppState(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) { + baseJSON, err := server.DefaultGenAppState(args, addr, coinDenom) if err != nil { - return nil, "", nil, err + return nil, err } - fmt.Println("Secret phrase to access coins:") - fmt.Println(secret) - - opts := fmt.Sprintf(`{ - "accounts": [{ - "address": "%s", - "coins": [ - { - "denom": "mycoin", - "amount": 9007199254740992 - } - ] - }], - "cool": { + var jsonMap map[string]json.RawMessage + err = json.Unmarshal(baseJSON, jsonMap) + if err != nil { + return nil, err + } + jsonMap["cool"] = json.RawMessage(`{ "trend": "ice-cold" - } - }`, addr) - return json.RawMessage(opts), "", nil, nil + }`) + bz, err := json.Marshal(jsonMap) + return json.RawMessage(bz), err } func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { @@ -108,17 +74,10 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { } func main() { - democoindCmd.AddCommand( - server.InitCmd(defaultOptions, context), - server.StartCmd(generateApp, context), - server.UnsafeResetAllCmd(context), - server.ShowNodeIdCmd(context), - server.ShowValidatorCmd(context), - version.VersionCmd, - ) + server.AddCommands(rootCmd, defaultAppState, generateApp, context) // prepare and add flags rootDir := os.ExpandEnv("$HOME/.democoind") - executor := cli.PrepareBaseCmd(democoindCmd, "BC", rootDir) + executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir) executor.Execute() } diff --git a/mock/app.go b/mock/app.go index e884a3ede..631cc3c31 100644 --- a/mock/app.go +++ b/mock/app.go @@ -106,7 +106,7 @@ func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci // GenInitOptions can be passed into InitCmd, // returns a static string of a few key-values that can be parsed // by InitChainer -func GenInitOptions(args ...string) (json.RawMessage, error) { +func GenInitOptions(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) { opts := []byte(`{ "values": [ { diff --git a/server/pre_run.go b/server/cmd.go similarity index 79% rename from server/pre_run.go rename to server/cmd.go index c82a8f364..a85e761dd 100644 --- a/server/pre_run.go +++ b/server/cmd.go @@ -40,3 +40,18 @@ func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error return nil } } + +func AddCommands( + rootCmd *cobra.Command, + appState GenAppState, appCreator AppCreator, + context *Context) { + + rootCmd.AddCommand( + InitCmd(appState, context), + StartCmd(appCreator, context), + UnsafeResetAllCmd(context), + ShowNodeIDCmd(context), + ShowValidatorCmd(context), + version.VersionCmd, + ) +} diff --git a/server/init.go b/server/init.go index 3a48e9fe5..e172816a5 100644 --- a/server/init.go +++ b/server/init.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" cfg "github.com/tendermint/tendermint/config" @@ -42,11 +43,29 @@ func InitCmd(gen GenAppState, ctx *Context) *cobra.Command { return &cobraCmd } -// GenAppState takes a list of arguments and -// returns a default app_state to be included in +// GenAppState takes the command line args, as well +// as an address and coin denomination. +// It returns a default app_state to be included in // in the genesis file. // This is application-specific -type GenAppState func(args ...string) (json.RawMessage, error) +type GenAppState func(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) + +// DefaultGenAppState expects two args: an account address +// and a coin denomination, and gives lots of coins to that address. +func DefaultGenAppState(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) { + opts := fmt.Sprintf(`{ + "accounts": [{ + "address": "%s", + "coins": [ + { + "denom": "%s", + "amount": 9007199254740992 + } + ] + }] + }`, addr.String(), coinDenom) + return json.RawMessage(opts), nil +} type initCmd struct { genAppState GenAppState @@ -79,7 +98,7 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error { var DEFAULT_DENOM = "mycoin" // Now, we want to add the custom app_state - appState, err := c.genAppState(addr.String(), DEFAULT_DENOM) + appState, err := c.genAppState(args, addr, DEFAULT_DENOM) if err != nil { return err } diff --git a/server/start.go b/server/start.go index 574e1584e..6aee5d316 100644 --- a/server/start.go +++ b/server/start.go @@ -21,13 +21,13 @@ const ( flagAddress = "address" ) -// appGenerator lets us lazily initialize app, using home dir +// AppCreator lets us lazily initialize app, using home dir // and other flags (?) to start -type appCreator func(string, log.Logger) (abci.Application, error) +type AppCreator func(string, log.Logger) (abci.Application, error) // StartCmd runs the service passed in, either // stand-alone, or in-process with tendermint -func StartCmd(app appCreator, ctx *Context) *cobra.Command { +func StartCmd(app AppCreator, ctx *Context) *cobra.Command { start := startCmd{ appCreator: app, context: ctx, @@ -48,7 +48,7 @@ func StartCmd(app appCreator, ctx *Context) *cobra.Command { } type startCmd struct { - appCreator appCreator + appCreator AppCreator context *Context } From 9fbbdbbe0f5aa65b1fa262f3aaa55369693268d1 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:31:33 +0300 Subject: [PATCH 5/9] server: consolidate files --- examples/basecoin/README.md | 70 ---------------------------- examples/democoin/README.md | 70 ---------------------------- server/context.go | 15 ------ server/init.go | 92 +++++++++++++++++++++++++------------ server/key.go | 34 -------------- server/reset.go | 27 ----------- server/show_node_id.go | 33 ------------- server/show_validator.go | 35 -------------- server/tm_cmds.go | 85 ++++++++++++++++++++++++++++++++++ server/{cmd.go => util.go} | 11 +++++ 10 files changed, 159 insertions(+), 313 deletions(-) delete mode 100644 examples/basecoin/README.md delete mode 100644 examples/democoin/README.md delete mode 100644 server/context.go delete mode 100644 server/key.go delete mode 100644 server/reset.go delete mode 100644 server/show_node_id.go delete mode 100644 server/show_validator.go create mode 100644 server/tm_cmds.go rename server/{cmd.go => util.go} (86%) diff --git a/examples/basecoin/README.md b/examples/basecoin/README.md deleted file mode 100644 index e6de9480d..000000000 --- a/examples/basecoin/README.md +++ /dev/null @@ -1,70 +0,0 @@ -# Basecoin - -This is the "Basecoin" example application built on the Cosmos-Sdk. This -"Basecoin" is not affiliated with [Coinbase](http://www.getbasecoin.com/), nor -the [stable coin](http://www.getbasecoin.com/). - -Assuming you've run `make get_tools && make get_vendor_deps` from the root of -this repository, run `make build` here to build the `basecoind` and `basecli` -binaries. - -If you want to create a new application, start by copying the Basecoin app. - - -# Building your own Blockchain - -Basecoin is the equivalent of an ERC20 token contract for blockchains. In order -to deploy your own application all you need to do is clone `examples/basecoin` -and run it. Now you are already running your own blockchain. In the following -I will explain how to add functionality to your blockchain. This is akin to -defining your own vesting schedule within a contract or setting a specific -multisig. You are just extending the base layer with extra functionality here -and there. - -## Structure of Basecoin - -Basecoin is build with the cosmos-sdk. It is a sample application that works -with any engine that implements the ABCI protocol. Basecoin defines multiple -unique modules as well as uses modules directly from the sdk. If you want -to modify Basecoin, you either remove or add modules according to your wishes. - - -## Modules - -A module is a fundamental unit in the cosmos-sdk. A module defines its own -transaction, handles its own state as well as its own state transition logic. -Globally, in the `app/app.go` file you just have to define a key for that -module to access some parts of the state, as well as initialise the module -object and finally add it to the transaction router. The router ensures that -every module only gets its own messages. - - -## Transactions - -A user can send a transaction to the running blockchain application. This -transaction can be of any of the ones that are supported by any of the -registered modules. - -### CheckTx - -Once a user has submitted their transaction to the engine, -the engine will first run `checkTx` to confirm that it is a valid transaction. -The module has to define a handler that knows how to handle every transaction -type. The corresponding handler gets invoked with the checkTx flag set to true. -This means that the handler shouldn't do any expensive operations, but it can -and should write to the checkTx state. - -### DeliverTx - -The engine calls `deliverTx` when a new block has been agreed upon in -consensus. Again, the corresponding module will have its handler invoked -and the state and context is passed in. During deliverTx execution the -transaction needs to be processed fully and the results are written to the -application state. - - -## CLI - -The cosmos-sdk contains a number of helper libraries in `clients/` to build cli -and RPC interfaces for your specific application. - diff --git a/examples/democoin/README.md b/examples/democoin/README.md deleted file mode 100644 index fe65abda4..000000000 --- a/examples/democoin/README.md +++ /dev/null @@ -1,70 +0,0 @@ -# Democoin - -This is the "Democoin" example application built on the Cosmos-Sdk. This -"Democoin" is not affiliated with [Coinbase](http://www.getdemocoin.com/), nor -the [stable coin](http://www.getdemocoin.com/). - -Assuming you've run `make get_tools && make get_vendor_deps` from the root of -this repository, run `make build` here to build the `democoind` and `basecli` -binaries. - -If you want to create a new application, start by copying the Democoin app. - - -# Building your own Blockchain - -Democoin is the equivalent of an ERC20 token contract for blockchains. In order -to deploy your own application all you need to do is clone `examples/democoin` -and run it. Now you are already running your own blockchain. In the following -I will explain how to add functionality to your blockchain. This is akin to -defining your own vesting schedule within a contract or setting a specific -multisig. You are just extending the base layer with extra functionality here -and there. - -## Structure of Democoin - -Democoin is build with the cosmos-sdk. It is a sample application that works -with any engine that implements the ABCI protocol. Democoin defines multiple -unique modules as well as uses modules directly from the sdk. If you want -to modify Democoin, you either remove or add modules according to your wishes. - - -## Modules - -A module is a fundamental unit in the cosmos-sdk. A module defines its own -transaction, handles its own state as well as its own state transition logic. -Globally, in the `app/app.go` file you just have to define a key for that -module to access some parts of the state, as well as initialise the module -object and finally add it to the transaction router. The router ensures that -every module only gets its own messages. - - -## Transactions - -A user can send a transaction to the running blockchain application. This -transaction can be of any of the ones that are supported by any of the -registered modules. - -### CheckTx - -Once a user has submitted their transaction to the engine, -the engine will first run `checkTx` to confirm that it is a valid transaction. -The module has to define a handler that knows how to handle every transaction -type. The corresponding handler gets invoked with the checkTx flag set to true. -This means that the handler shouldn't do any expensive operations, but it can -and should write to the checkTx state. - -### DeliverTx - -The engine calls `deliverTx` when a new block has been agreed upon in -consensus. Again, the corresponding module will have its handler invoked -and the state and context is passed in. During deliverTx execution the -transaction needs to be processed fully and the results are written to the -application state. - - -## CLI - -The cosmos-sdk contains a number of helper libraries in `clients/` to build cli -and RPC interfaces for your specific application. - diff --git a/server/context.go b/server/context.go deleted file mode 100644 index def2eddb4..000000000 --- a/server/context.go +++ /dev/null @@ -1,15 +0,0 @@ -package server - -import ( - cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tmlibs/log" -) - -type Context struct { - Config *cfg.Config - Logger log.Logger -} - -func NewContext(config *cfg.Config, logger log.Logger) *Context { - return &Context{config, logger} -} diff --git a/server/init.go b/server/init.go index e172816a5..688bc38e3 100644 --- a/server/init.go +++ b/server/init.go @@ -8,10 +8,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" + "github.com/tendermint/go-crypto/keys" + "github.com/tendermint/go-crypto/keys/words" cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/p2p" tmtypes "github.com/tendermint/tendermint/types" cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" ) // testnetInformation contains the info necessary @@ -25,6 +28,11 @@ type testnetInformation struct { NodeID p2p.ID `json:"node_id"` } +type initCmd struct { + genAppState GenAppState + context *Context +} + // InitCmd will initialize all files for tendermint, // along with proper app_state. // The application can pass in a function to generate @@ -43,35 +51,6 @@ func InitCmd(gen GenAppState, ctx *Context) *cobra.Command { return &cobraCmd } -// GenAppState takes the command line args, as well -// as an address and coin denomination. -// It returns a default app_state to be included in -// in the genesis file. -// This is application-specific -type GenAppState func(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) - -// DefaultGenAppState expects two args: an account address -// and a coin denomination, and gives lots of coins to that address. -func DefaultGenAppState(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) { - opts := fmt.Sprintf(`{ - "accounts": [{ - "address": "%s", - "coins": [ - { - "denom": "%s", - "amount": 9007199254740992 - } - ] - }] - }`, addr.String(), coinDenom) - return json.RawMessage(opts), nil -} - -type initCmd struct { - genAppState GenAppState - context *Context -} - func (c initCmd) run(cmd *cobra.Command, args []string) error { // Store testnet information as we go var testnetInfo testnetInformation @@ -174,6 +153,34 @@ func (c initCmd) initTendermintFiles(config *cfg.Config, info *testnetInformatio return nil } +//------------------------------------------------------------------- + +// GenAppState takes the command line args, as well +// as an address and coin denomination. +// It returns a default app_state to be included in +// in the genesis file. +// This is application-specific +type GenAppState func(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) + +// DefaultGenAppState expects two args: an account address +// and a coin denomination, and gives lots of coins to that address. +func DefaultGenAppState(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) { + opts := fmt.Sprintf(`{ + "accounts": [{ + "address": "%s", + "coins": [ + { + "denom": "%s", + "amount": 9007199254740992 + } + ] + }] + }`, addr.String(), coinDenom) + return json.RawMessage(opts), nil +} + +//------------------------------------------------------------------- + // GenesisDoc involves some tendermint-specific structures we don't // want to parse, so we just grab it into a raw object format, // so we can add one line. @@ -199,3 +206,30 @@ func addGenesisState(filename string, appState json.RawMessage) error { return ioutil.WriteFile(filename, out, 0600) } + +//------------------------------------------------------------------- + +// GenerateCoinKey returns the address of a public key, +// along with the secret phrase to recover the private key. +// You can give coins to this address and return the recovery +// phrase to the user to access them. +func GenerateCoinKey() (sdk.Address, string, error) { + // construct an in-memory key store + codec, err := words.LoadCodec("english") + if err != nil { + return nil, "", err + } + keybase := keys.New( + dbm.NewMemDB(), + codec, + ) + + // generate a private key, with recovery phrase + info, secret, err := keybase.Create("name", "pass", keys.AlgoEd25519) + if err != nil { + return nil, "", err + } + + addr := info.PubKey.Address() + return addr, secret, nil +} diff --git a/server/key.go b/server/key.go deleted file mode 100644 index aed1f9d1f..000000000 --- a/server/key.go +++ /dev/null @@ -1,34 +0,0 @@ -package server - -import ( - "github.com/tendermint/go-crypto/keys" - "github.com/tendermint/go-crypto/keys/words" - dbm "github.com/tendermint/tmlibs/db" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -// GenerateCoinKey returns the address of a public key, -// along with the secret phrase to recover the private key. -// You can give coins to this address and return the recovery -// phrase to the user to access them. -func GenerateCoinKey() (sdk.Address, string, error) { - // construct an in-memory key store - codec, err := words.LoadCodec("english") - if err != nil { - return nil, "", err - } - keybase := keys.New( - dbm.NewMemDB(), - codec, - ) - - // generate a private key, with recovery phrase - info, secret, err := keybase.Create("name", "pass", keys.AlgoEd25519) - if err != nil { - return nil, "", err - } - - addr := info.PubKey.Address() - return addr, secret, nil -} diff --git a/server/reset.go b/server/reset.go deleted file mode 100644 index 2cff9169f..000000000 --- a/server/reset.go +++ /dev/null @@ -1,27 +0,0 @@ -package server - -import ( - "github.com/spf13/cobra" - - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" -) - -// UnsafeResetAllCmd - extension of the tendermint command, resets initialization -func UnsafeResetAllCmd(ctx *Context) *cobra.Command { - cmd := resetAll{ctx} - return &cobra.Command{ - Use: "unsafe_reset_all", - Short: "Reset all blockchain data", - RunE: cmd.run, - } -} - -type resetAll struct { - context *Context -} - -func (r resetAll) run(cmd *cobra.Command, args []string) error { - cfg := r.context.Config - tcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), r.context.Logger) - return nil -} diff --git a/server/show_node_id.go b/server/show_node_id.go deleted file mode 100644 index bb2270e29..000000000 --- a/server/show_node_id.go +++ /dev/null @@ -1,33 +0,0 @@ -package server - -import ( - "fmt" - - "github.com/spf13/cobra" - - "github.com/tendermint/tendermint/p2p" -) - -// ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout -func ShowNodeIDCmd(ctx *Context) *cobra.Command { - cmd := showNodeId{ctx} - return &cobra.Command{ - Use: "show_node_id", - Short: "Show this node's ID", - RunE: cmd.run, - } -} - -type showNodeId struct { - context *Context -} - -func (s showNodeId) run(cmd *cobra.Command, args []string) error { - cfg := s.context.Config - nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) - if err != nil { - return err - } - fmt.Println(nodeKey.ID()) - return nil -} diff --git a/server/show_validator.go b/server/show_validator.go deleted file mode 100644 index 9f0cd9419..000000000 --- a/server/show_validator.go +++ /dev/null @@ -1,35 +0,0 @@ -package server - -import ( - "fmt" - - "github.com/spf13/cobra" - - "github.com/tendermint/go-wire/data" - "github.com/tendermint/tendermint/types" -) - -// ShowValidator - ported from Tendermint, show this node's validator info -func ShowValidatorCmd(ctx *Context) *cobra.Command { - cmd := showValidator{ctx} - return &cobra.Command{ - Use: "show_validator", - Short: "Show this node's validator info", - RunE: cmd.run, - } -} - -type showValidator struct { - context *Context -} - -func (s showValidator) run(cmd *cobra.Command, args []string) error { - cfg := s.context.Config - privValidator := types.LoadOrGenPrivValidatorFS(cfg.PrivValidatorFile()) - pubKeyJSONBytes, err := data.ToJSON(privValidator.PubKey) - if err != nil { - return err - } - fmt.Println(string(pubKeyJSONBytes)) - return nil -} diff --git a/server/tm_cmds.go b/server/tm_cmds.go new file mode 100644 index 000000000..3e0aefa94 --- /dev/null +++ b/server/tm_cmds.go @@ -0,0 +1,85 @@ +package server + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/tendermint/go-wire/data" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + "github.com/tendermint/tendermint/p2p" + "github.com/tendermint/tendermint/types" +) + +// ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout +func ShowNodeIDCmd(ctx *Context) *cobra.Command { + cmd := showNodeId{ctx} + return &cobra.Command{ + Use: "show_node_id", + Short: "Show this node's ID", + RunE: cmd.run, + } +} + +type showNodeId struct { + context *Context +} + +func (s showNodeId) run(cmd *cobra.Command, args []string) error { + cfg := s.context.Config + nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) + if err != nil { + return err + } + fmt.Println(nodeKey.ID()) + return nil +} + +//-------------------------------------------------------------------------------- + +// ShowValidator - ported from Tendermint, show this node's validator info +func ShowValidatorCmd(ctx *Context) *cobra.Command { + cmd := showValidator{ctx} + return &cobra.Command{ + Use: "show_validator", + Short: "Show this node's validator info", + RunE: cmd.run, + } +} + +type showValidator struct { + context *Context +} + +func (s showValidator) run(cmd *cobra.Command, args []string) error { + cfg := s.context.Config + privValidator := types.LoadOrGenPrivValidatorFS(cfg.PrivValidatorFile()) + pubKeyJSONBytes, err := data.ToJSON(privValidator.PubKey) + if err != nil { + return err + } + fmt.Println(string(pubKeyJSONBytes)) + return nil +} + +//------------------------------------------------------------------------------ + +// UnsafeResetAllCmd - extension of the tendermint command, resets initialization +func UnsafeResetAllCmd(ctx *Context) *cobra.Command { + cmd := resetAll{ctx} + return &cobra.Command{ + Use: "unsafe_reset_all", + Short: "Reset all blockchain data", + RunE: cmd.run, + } +} + +type resetAll struct { + context *Context +} + +func (r resetAll) run(cmd *cobra.Command, args []string) error { + cfg := r.context.Config + tcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), r.context.Logger) + return nil +} diff --git a/server/cmd.go b/server/util.go similarity index 86% rename from server/cmd.go rename to server/util.go index a85e761dd..a41eb179e 100644 --- a/server/cmd.go +++ b/server/util.go @@ -14,6 +14,17 @@ import ( "github.com/tendermint/tmlibs/log" ) +type Context struct { + Config *cfg.Config + Logger log.Logger +} + +func NewContext(config *cfg.Config, logger log.Logger) *Context { + return &Context{config, logger} +} + +//-------------------------------------------------------------------- + // PersistentPreRunEFn returns a PersistentPreRunE function for cobra // that initailizes the passed in context with a properly configured // logger and config objecy From c74c57a2f0a98bc3f045ffa3de5df9e7a953b24c Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:34:59 +0300 Subject: [PATCH 6/9] remove empty publish dir --- publish/print_txs.go | 62 -------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 publish/print_txs.go diff --git a/publish/print_txs.go b/publish/print_txs.go deleted file mode 100644 index 55cd2b488..000000000 --- a/publish/print_txs.go +++ /dev/null @@ -1,62 +0,0 @@ -// +build scripts - -package main - -/* -import ( - "encoding/json" - "fmt" - "os" - "time" - - "github.com/gorilla/websocket" - "github.com/tendermint/go-wire" - _ "github.com/tendermint/tendermint/rpc/core/types" // Register RPCResponse > Result types - "github.com/tendermint/tendermint/rpc/lib/client" - "github.com/tendermint/tendermint/rpc/lib/types" - cmn "github.com/tendermint/tmlibs/common" -) - -func main() { - ws := rpcclient.NewWSClient(os.Args[1]+":46657", "/websocket") - - _, err := ws.Start() - if err != nil { - cmn.Exit(err.Error()) - } - - // Read a bunch of responses - go func() { - for { - res, ok := <-ws.ResultsCh - if !ok { - break - } - //fmt.Println(counter, "res:", Blue(string(res))) - var result []interface{} - err := json.Unmarshal([]byte(string(res)), &result) - if err != nil { - Exit("Error unmarshalling block: " + err.Error()) - } - height := result[1].(map[string]interface{})["block"].(map[string]interface{})["header"].(map[string]interface{})["height"] - txs := result[1].(map[string]interface{})["block"].(map[string]interface{})["data"].(map[string]interface{})["txs"] - if len(txs.([]interface{})) > 0 { - fmt.Println(">>", height, txs) - } - } - }() - - for i := 0; i < 100000; i++ { - request := rpctypes.NewRPCRequest("fakeid", "block", Arr(i)) - reqBytes := wire.JSONBytes(request) - err = ws.WriteMessage(websocket.TextMessage, reqBytes) - if err != nil { - cmn.Exit("writing websocket request: " + err.Error()) - } - } - - time.Sleep(time.Second * 1000) - - ws.Stop() -} -*/ From 4dfc5c085259de30f2f42d84c1e8aa4426e1ae85 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:41:11 +0300 Subject: [PATCH 7/9] fix democoind init. use rootCmd --- examples/basecoin/cmd/basecli/main.go | 31 +++++++++++-------------- examples/democoin/cmd/democli/main.go | 31 +++++++++++-------------- examples/democoin/cmd/democoind/main.go | 4 ++-- 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index a0152aee9..6271adcec 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -1,7 +1,6 @@ package main import ( - "errors" "os" "github.com/spf13/cobra" @@ -24,18 +23,14 @@ import ( "github.com/cosmos/cosmos-sdk/examples/basecoin/types" ) -// gaiacliCmd is the entry point for this binary +// rootCmd is the entry point for this binary var ( - basecliCmd = &cobra.Command{ + rootCmd = &cobra.Command{ Use: "basecli", Short: "Basecoin light-client", } ) -func todoNotImplemented(_ *cobra.Command, _ []string) error { - return errors.New("TODO: Command not yet implemented") -} - func main() { // disable sorting cobra.EnableCommandSorting = false @@ -48,36 +43,36 @@ func main() { // with the cdc // add standard rpc, and tx commands - rpc.AddCommands(basecliCmd) - basecliCmd.AddCommand(client.LineBreak) - tx.AddCommands(basecliCmd, cdc) - basecliCmd.AddCommand(client.LineBreak) + rpc.AddCommands(rootCmd) + rootCmd.AddCommand(client.LineBreak) + tx.AddCommands(rootCmd, cdc) + rootCmd.AddCommand(client.LineBreak) // add query/post commands (custom to binary) - basecliCmd.AddCommand( + rootCmd.AddCommand( client.GetCommands( authcmd.GetAccountCmd("main", cdc, types.GetAccountDecoder(cdc)), )...) - basecliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( bankcmd.SendTxCmd(cdc), )...) - basecliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( ibccmd.IBCTransferCmd(cdc), )...) - basecliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( ibccmd.IBCRelayCmd(cdc), simplestakingcmd.BondTxCmd(cdc), )...) - basecliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( simplestakingcmd.UnbondTxCmd(cdc), )...) // add proxy, version and key info - basecliCmd.AddCommand( + rootCmd.AddCommand( client.LineBreak, lcd.ServeCommand(cdc), keys.Commands(), @@ -86,6 +81,6 @@ func main() { ) // prepare and add flags - executor := cli.PrepareMainCmd(basecliCmd, "BC", os.ExpandEnv("$HOME/.basecli")) + executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.basecli")) executor.Execute() } diff --git a/examples/democoin/cmd/democli/main.go b/examples/democoin/cmd/democli/main.go index 602e5478e..bf499458b 100644 --- a/examples/democoin/cmd/democli/main.go +++ b/examples/democoin/cmd/democli/main.go @@ -1,7 +1,6 @@ package main import ( - "errors" "os" "github.com/spf13/cobra" @@ -24,18 +23,14 @@ import ( "github.com/cosmos/cosmos-sdk/examples/democoin/types" ) -// gaiacliCmd is the entry point for this binary +// rootCmd is the entry point for this binary var ( - democliCmd = &cobra.Command{ + rootCmd = &cobra.Command{ Use: "democli", Short: "Democoin light-client", } ) -func todoNotImplemented(_ *cobra.Command, _ []string) error { - return errors.New("TODO: Command not yet implemented") -} - func main() { // disable sorting cobra.EnableCommandSorting = false @@ -48,36 +43,36 @@ func main() { // with the cdc // add standard rpc, and tx commands - rpc.AddCommands(democliCmd) - democliCmd.AddCommand(client.LineBreak) - tx.AddCommands(democliCmd, cdc) - democliCmd.AddCommand(client.LineBreak) + rpc.AddCommands(rootCmd) + rootCmd.AddCommand(client.LineBreak) + tx.AddCommands(rootCmd, cdc) + rootCmd.AddCommand(client.LineBreak) // add query/post commands (custom to binary) - democliCmd.AddCommand( + rootCmd.AddCommand( client.GetCommands( authcmd.GetAccountCmd("main", cdc, types.GetAccountDecoder(cdc)), )...) - democliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( bankcmd.SendTxCmd(cdc), )...) - democliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( ibccmd.IBCTransferCmd(cdc), )...) - democliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( ibccmd.IBCRelayCmd(cdc), simplestakingcmd.BondTxCmd(cdc), )...) - democliCmd.AddCommand( + rootCmd.AddCommand( client.PostCommands( simplestakingcmd.UnbondTxCmd(cdc), )...) // add proxy, version and key info - democliCmd.AddCommand( + rootCmd.AddCommand( client.LineBreak, lcd.ServeCommand(cdc), keys.Commands(), @@ -86,6 +81,6 @@ func main() { ) // prepare and add flags - executor := cli.PrepareMainCmd(democliCmd, "BC", os.ExpandEnv("$HOME/.democli")) + executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.democli")) executor.Execute() } diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 04436eec2..6fc593067 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -22,7 +22,7 @@ var ( context = server.NewContext(nil, nil) rootCmd = &cobra.Command{ Use: "democoind", - Short: "Gaia Daemon (server)", + Short: "Democoin Daemon (server)", PersistentPreRunE: server.PersistentPreRunEFn(context), } ) @@ -35,7 +35,7 @@ func defaultAppState(args []string, addr sdk.Address, coinDenom string) (json.Ra return nil, err } var jsonMap map[string]json.RawMessage - err = json.Unmarshal(baseJSON, jsonMap) + err = json.Unmarshal(baseJSON, &jsonMap) if err != nil { return nil, err } From 31a6806fd46a5c4a09c7f106bf53736e8dc2a679 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:47:54 +0300 Subject: [PATCH 8/9] fix gaia --- cmd/gaiacli/main.go | 14 +++--- cmd/gaiad/main.go | 106 ++++++++++++++++---------------------------- 2 files changed, 44 insertions(+), 76 deletions(-) diff --git a/cmd/gaiacli/main.go b/cmd/gaiacli/main.go index dce125acb..0eb29c19c 100644 --- a/cmd/gaiacli/main.go +++ b/cmd/gaiacli/main.go @@ -17,9 +17,9 @@ const ( flagFee = "fee" ) -// gaiacliCmd is the entry point for this binary +// rootCmd is the entry point for this binary var ( - gaiacliCmd = &cobra.Command{ + rootCmd = &cobra.Command{ Use: "gaiacli", Short: "Gaia light-client", } @@ -54,16 +54,16 @@ func main() { cobra.EnableCommandSorting = false // generic client commands - AddClientCommands(gaiacliCmd) + AddClientCommands(rootCmd) // query commands (custom to binary) - gaiacliCmd.AddCommand( + rootCmd.AddCommand( GetCommands(getAccountCmd)...) // post tx commands (custom to binary) - gaiacliCmd.AddCommand( + rootCmd.AddCommand( PostCommands(postSendCommand())...) // add proxy, version and key info - gaiacliCmd.AddCommand( + rootCmd.AddCommand( lineBreak, serveCommand(), KeyCommands(), @@ -72,6 +72,6 @@ func main() { ) // prepare and add flags - executor := cli.PrepareBaseCmd(gaiacliCmd, "GA", os.ExpandEnv("$HOME/.gaiacli")) + executor := cli.PrepareBaseCmd(rootCmd, "GA", os.ExpandEnv("$HOME/.gaiacli")) executor.Execute() } diff --git a/cmd/gaiad/main.go b/cmd/gaiad/main.go index f42452135..d5f2311e4 100644 --- a/cmd/gaiad/main.go +++ b/cmd/gaiad/main.go @@ -1,95 +1,63 @@ package main import ( - "encoding/json" - "fmt" "os" + "path/filepath" "github.com/spf13/cobra" - "github.com/spf13/viper" abci "github.com/tendermint/abci/types" - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" - cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tmlibs/cli" - tmflags "github.com/tendermint/tmlibs/cli/flags" - cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" - "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/examples/basecoin/app" "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/version" ) -// gaiadCmd is the entry point for this binary +// rootCmd is the entry point for this binary var ( - context = server.NewContext(nil, nil) - gaiadCmd = &cobra.Command{ - Use: "gaiad", - Short: "Gaia Daemon (server)", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Name() == version.VersionCmd.Name() { - return nil - } - config, err := tcmd.ParseConfig() - if err != nil { - return err - } - logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) - logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel()) - if err != nil { - return err - } - if viper.GetBool(cli.TraceFlag) { - logger = log.NewTracingLogger(logger) - } - logger = logger.With("module", "main") - context.Config = config - context.Logger = logger - return nil - }, + context = server.NewContext(nil, nil) + rootCmd = &cobra.Command{ + Use: "gaiad", + Short: "Gaia Daemon (server)", + PersistentPreRunE: server.PersistentPreRunEFn(context), } ) -// defaultOptions sets up the app_options for the -// default genesis file -func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) { - addr, secret, err := server.GenerateCoinKey() - if err != nil { - return nil, "", nil, err - } - fmt.Println("Secret phrase to access coins:") - fmt.Println(secret) - - opts := fmt.Sprintf(`{ - "accounts": [{ - "address": "%s", - "coins": [ - { - "denom": "mycoin", - "amount": 9007199254740992 - } - ] - }] - }`, addr) - return json.RawMessage(opts), secret, addr, nil -} - +// TODO: distinguish from basecoin func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { - // TODO: set this to something real - app := new(baseapp.BaseApp) - return app, nil + dataDir := filepath.Join(rootDir, "data") + dbMain, err := dbm.NewGoLevelDB("gaia", dataDir) + if err != nil { + return nil, err + } + dbAcc, err := dbm.NewGoLevelDB("gaia-acc", dataDir) + if err != nil { + return nil, err + } + dbIBC, err := dbm.NewGoLevelDB("gaia-ibc", dataDir) + if err != nil { + return nil, err + } + dbStaking, err := dbm.NewGoLevelDB("gaia-staking", dataDir) + if err != nil { + return nil, err + } + dbs := map[string]dbm.DB{ + "main": dbMain, + "acc": dbAcc, + "ibc": dbIBC, + "staking": dbStaking, + } + bapp := app.NewBasecoinApp(logger, dbs) + return bapp, nil } func main() { - gaiadCmd.AddCommand( - server.InitCmd(defaultOptions, context), - server.StartCmd(generateApp, context), - server.UnsafeResetAllCmd(context), - version.VersionCmd, - ) + server.AddCommands(rootCmd, server.DefaultGenAppState, generateApp, context) // prepare and add flags - executor := cli.PrepareBaseCmd(gaiadCmd, "GA", os.ExpandEnv("$HOME/.gaiad")) + executor := cli.PrepareBaseCmd(rootCmd, "GA", os.ExpandEnv("$HOME/.gaiad")) executor.Execute() } From 19f96d48c6301d41b91c17f35dfe8b0583032a22 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 5 Apr 2018 13:51:32 +0300 Subject: [PATCH 9/9] fix mock test --- mock/app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mock/app_test.go b/mock/app_test.go index 47db93e1c..18449631c 100644 --- a/mock/app_test.go +++ b/mock/app_test.go @@ -21,7 +21,7 @@ func TestInitApp(t *testing.T) { require.NoError(t, err) // initialize it future-way - opts, _, _, err := GenInitOptions(nil) + opts, err := GenInitOptions(nil, nil, "") require.NoError(t, err) req := abci.RequestInitChain{AppStateBytes: opts} app.InitChain(req)