Skip to content

Commit

Permalink
basecoin init, default to in-proc, fix up guide
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Mar 13, 2017
1 parent 6f83510 commit 44189d9
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 106 deletions.
28 changes: 4 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,25 @@ WARNING: Currently uses plain-text private keys for transactions and is otherwis

## Prerequisites

* Go to https://golang.org/doc/install to install Golang.
* You will also need to set the $GOPATH environment variable as per the instructions [here](https://golang.org/doc/code.html#GOPATH).
[Install and setup Golang](https://tendermint.com/docs/guides/install-go).

## Installation

On a good day, basecoin can be installed like a normal Go program:

```
go get -u github.com/tendermint/basecoin/cmd/basecoin
```

See the [install guide](/docs/guide/install.md) for more details.

If that fails, or if another branch is required, you may have to compile from source.
You will first need to install the Golang package manager [`glide`](https://github.com/Masterminds/glide).

```
cd $GOPATH/src/github.com/tendermint/basecoin
git checkout develop # (until we release tendermint v0.9)
make get_vendor_deps
make install
```

This will create the `basecoin` binary in `$GOPATH/bin`.


## Command Line Interface

The basecoin CLI can be used to start a stand-alone basecoin instance (`basecoin start`),
or to start basecoin with Tendermint in the same process (`basecoin start --in-proc`).
It can also be used to send transactions, eg. `basecoin tx send --to 0x4793A333846E5104C46DD9AB9A00E31821B2F301 --amount 100btc,10gold`
See `basecoin --help` and `basecoin [cmd] --help` for more details`.

## Learn more
## Guide

1. Getting started with the [Basecoin tool](/docs/guide/basecoin-basics.md)
1. Learn more about [Basecoin's design](/docs/guide/basecoin-design.md)
1. Extend Basecoin [using the plugin system](/docs/guide/example-plugin.md)
1. Learn more about [plugin design](/docs/guide/plugin-design.md)
1. See some [more example applications](/docs/guide/more-examples.md)
1. More features of the [Basecoin tool](/docs/guide/basecoin-tool.md)
1. Learn how to use [InterBlockchain Communication (IBC)](/docs/guide/ibc.md)
1. [Deploy testnets](deployment.md) running your basecoin application.

Expand Down
1 change: 1 addition & 0 deletions cmd/basecoin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
app.Usage = "basecoin [command] [args...]"
app.Version = version.Version
app.Commands = []cli.Command{
commands.InitCmd,
commands.StartCmd,
commands.TxCmd,
commands.QueryCmd,
Expand Down
12 changes: 3 additions & 9 deletions cmd/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ var (
// TODO: move to config file
// eyesCacheSizePtr := flag.Int("eyes-cache-size", 10000, "MerkleEyes db cache size, for embedded")

DirFlag = cli.StringFlag{
Name: "dir",
Value: ".",
Usage: "Root directory",
}

InProcTMFlag = cli.BoolFlag{
Name: "in-proc",
Usage: "Run Tendermint in-process with the App",
ABCIServerFlag = cli.BoolFlag{
Name: "abci-server",
Usage: "Run the Basecoin app and ABCI server, but not Tendermint (run Tendermint in another process)",
}
)

Expand Down
116 changes: 116 additions & 0 deletions cmd/commands/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package commands

import (
"io/ioutil"
"os"
"path"

"github.com/urfave/cli"

cmn "github.com/tendermint/go-common"
tmcfg "github.com/tendermint/tendermint/config/tendermint"
types "github.com/tendermint/tendermint/types"
)

var InitCmd = cli.Command{
Name: "init",
Usage: "Initialize a basecoin blockchain",
ArgsUsage: "",
Action: func(c *cli.Context) error {
return cmdInit(c)
},
Flags: []cli.Flag{
ChainIDFlag,
},
}

func cmdInit(c *cli.Context) error {
basecoinDir := BasecoinRoot("")
tmDir := path.Join(basecoinDir, "tendermint")

// initalize tendermint
tmConfig := tmcfg.GetConfig(tmDir)

privValFile := tmConfig.GetString("priv_validator_file")
if _, err := os.Stat(privValFile); os.IsNotExist(err) {
privValidator := types.GenPrivValidator()
privValidator.SetFile(privValFile)
privValidator.Save()

genFile := tmConfig.GetString("genesis_file")

if _, err := os.Stat(genFile); os.IsNotExist(err) {
genDoc := types.GenesisDoc{
ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)),
}
genDoc.Validators = []types.GenesisValidator{types.GenesisValidator{
PubKey: privValidator.PubKey,
Amount: 10,
}}

genDoc.SaveAs(genFile)
}
log.Notice("Initialized Tendermint", "genesis", tmConfig.GetString("genesis_file"), "priv_validator", tmConfig.GetString("priv_validator_file"))
} else {
log.Notice("Already initialized Tendermint", "priv_validator", tmConfig.GetString("priv_validator_file"))
}

// initalize basecoin
genesisFile := path.Join(basecoinDir, "genesis.json")
key1File := path.Join(basecoinDir, "key.json")
key2File := path.Join(basecoinDir, "key2.json")

if err := ioutil.WriteFile(genesisFile, []byte(genesisJSON), 0644); err != nil {
return err
}
if err := ioutil.WriteFile(key1File, []byte(key1JSON), 0400); err != nil {
return err
}
if err := ioutil.WriteFile(key2File, []byte(key2JSON), 0400); err != nil {
return err
}

log.Notice("Initialized Basecoin", "genesis", genesisFile, "key", key1File)

return nil
}

const genesisJSON = `[
"base/chainID", "test_chain_id",
"base/account", {
"pub_key": {
"type": "ed25519",
"data": "619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
},
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}
]`

const key1JSON = `{
"address": "1B1BE55F969F54064628A63B9559E7C21C925165",
"priv_key": [
1,
"C70D6934B4F55F1B7BC33B56B9CA8A2061384AFC19E91E44B40C4BBA182953D1619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
],
"pub_key": [
1,
"619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
]
}`

const key2JSON = `{
"address": "1DA7C74F9C219229FD54CC9F7386D5A3839F0090",
"priv_key": [
1,
"34BAE9E65CE8245FAD035A0E3EED9401BDE8785FFB3199ACCF8F5B5DDF7486A8352195DA90CB0B90C24295B90AEBA25A5A71BC61BAB2FE2387241D439698B7B8"
],
"pub_key": [
1,
"352195DA90CB0B90C24295B90AEBA25A5A71BC61BAB2FE2387241D439698B7B8"
]
}`
6 changes: 4 additions & 2 deletions cmd/commands/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"io/ioutil"
"path"

"github.com/urfave/cli"

Expand Down Expand Up @@ -60,14 +61,15 @@ func genKey() *Key {
}
}

func LoadKey(filePath string) *Key {
func LoadKey(keyFile string) *Key {
filePath := path.Join(BasecoinRoot(""), keyFile)
keyJSONBytes, err := ioutil.ReadFile(filePath)
if err != nil {
cmn.Exit(err.Error())
}
key := wire.ReadJSON(&Key{}, keyJSONBytes, &err).(*Key)
if err != nil {
cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
cmn.Exit(cmn.Fmt("Error reading key from %v: %v\n", filePath, err))
}
return key
}
7 changes: 7 additions & 0 deletions cmd/commands/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package commands

import (
"github.com/tendermint/go-logger"
)

var log = logger.New("module", "commands")
29 changes: 14 additions & 15 deletions cmd/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

"github.com/tendermint/abci/server"
cmn "github.com/tendermint/go-common"
cfg "github.com/tendermint/go-config"
//logger "github.com/tendermint/go-logger"
eyes "github.com/tendermint/merkleeyes/client"

tmcfg "github.com/tendermint/tendermint/config/tendermint"
Expand All @@ -23,8 +21,6 @@ import (
"github.com/tendermint/basecoin/types"
)

var config cfg.Config

const EyesCacheSize = 10000

var StartCmd = cli.Command{
Expand All @@ -37,8 +33,7 @@ var StartCmd = cli.Command{
Flags: []cli.Flag{
AddrFlag,
EyesFlag,
DirFlag,
InProcTMFlag,
ABCIServerFlag,
ChainIDFlag,
},
}
Expand All @@ -56,11 +51,12 @@ func RegisterStartPlugin(name string, newPlugin func() types.Plugin) {
}

func cmdStart(c *cli.Context) error {
basecoinDir := BasecoinRoot("")

// Connect to MerkleEyes
var eyesCli *eyes.Client
if c.String("eyes") == "local" {
eyesCli = eyes.NewLocalClient(path.Join(c.String("dir"), "merkleeyes.db"), EyesCacheSize)
eyesCli = eyes.NewLocalClient(path.Join(basecoinDir, "merkleeyes.db"), EyesCacheSize)
} else {
var err error
eyesCli, err = eyes.NewClient(c.String("eyes"))
Expand All @@ -81,7 +77,7 @@ func cmdStart(c *cli.Context) error {
}

// If genesis file exists, set key-value options
genesisFile := path.Join(c.String("dir"), "genesis.json")
genesisFile := path.Join(basecoinDir, "genesis.json")
if _, err := os.Stat(genesisFile); err == nil {
err := basecoinApp.LoadGenesis(genesisFile)
if err != nil {
Expand All @@ -91,12 +87,14 @@ func cmdStart(c *cli.Context) error {
fmt.Printf("No genesis file at %s, skipping...\n", genesisFile)
}

if c.Bool("in-proc") {
startTendermint(c, basecoinApp)
} else {
if c.Bool("abci-server") {
// run just the abci app/server
if err := startBasecoinABCI(c, basecoinApp); err != nil {
return err
}
} else {
// start the app with tendermint in-process
startTendermint(basecoinDir, basecoinApp)
}

return nil
Expand All @@ -117,17 +115,18 @@ func startBasecoinABCI(c *cli.Context, basecoinApp *app.Basecoin) error {

}

func startTendermint(c *cli.Context, basecoinApp *app.Basecoin) {
func startTendermint(dir string, basecoinApp *app.Basecoin) {
// Get configuration
config = tmcfg.GetConfig("")
tmConfig := tmcfg.GetConfig(path.Join(dir, "tendermint"))

// logger.SetLogLevel("notice") //config.GetString("log_level"))

// parseFlags(config, args[1:]) // Command line overrides

// Create & start tendermint node
privValidatorFile := config.GetString("priv_validator_file")
privValidatorFile := tmConfig.GetString("priv_validator_file")
privValidator := tmtypes.LoadOrGenPrivValidator(privValidatorFile)
n := node.NewNode(config, privValidator, proxy.NewLocalClientCreator(basecoinApp))
n := node.NewNode(tmConfig, privValidator, proxy.NewLocalClientCreator(basecoinApp))

n.Start()

Expand Down
3 changes: 1 addition & 2 deletions cmd/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
client "github.com/tendermint/go-rpc/client"
"github.com/tendermint/go-wire"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
)

var TxFlags = []cli.Flag{
Expand Down Expand Up @@ -144,7 +143,7 @@ func AppTx(c *cli.Context, name string, data []byte) error {
gas := int64(c.Int("gas"))
chainID := c.String("chain_id")

privKey := tmtypes.LoadPrivValidator(fromFile)
privKey := LoadKey(fromFile)

sequence, err := getSeq(c, privKey.Address)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions cmd/commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"encoding/hex"
"errors"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -20,6 +21,16 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
)

func BasecoinRoot(rootDir string) string {
if rootDir == "" {
rootDir = os.Getenv("BASECOIN_ROOT")
}
if rootDir == "" {
rootDir = os.Getenv("HOME") + "/.basecoin"
}
return rootDir
}

// Returns true for non-empty hex-string prefixed with "0x"
func isHex(s string) bool {
if len(s) > 2 && s[:2] == "0x" {
Expand Down
Loading

0 comments on commit 44189d9

Please sign in to comment.