Skip to content

Commit

Permalink
Merge pull request #156 from tendermint/feature/replace-binaries-0.7-…
Browse files Browse the repository at this point in the history
…rigel

golint + general cleanup
  • Loading branch information
ethanfrey authored Jul 6, 2017
2 parents ba38f44 + 9b56134 commit 205e38e
Show file tree
Hide file tree
Showing 30 changed files with 215 additions and 224 deletions.
26 changes: 13 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ TUTORIALS=$(shell find docs/guide -name "*md" -type f)
all: get_vendor_deps install test

build:
go build ./cmd/...
@go build ./cmd/...

install:
go install ./cmd/...
go install ./docs/guide/counter/cmd/...
@go install ./cmd/...
@go install ./docs/guide/counter/cmd/...

dist:
@bash scripts/dist.sh
@bash scripts/publish.sh
@bash publish/dist.sh
@bash publish/publish.sh

test: test_unit test_cli test_tutorial

test_unit:
go test `glide novendor`
@go test `glide novendor`
#go run tests/tendermint/*.go

test_cli: tests/cli/shunit2
Expand All @@ -30,26 +30,26 @@ test_cli: tests/cli/shunit2
# @./tests/cli/ibc.sh

test_tutorial: docs/guide/shunit2
shelldown ${TUTORIALS}
for script in docs/guide/*.sh ; do \
@shelldown ${TUTORIALS}
@for script in docs/guide/*.sh ; do \
bash $$script ; \
done

tests/cli/shunit2:
wget "https://raw.githubusercontent.com/kward/shunit2/master/source/2.1/src/shunit2" \
@wget "https://raw.githubusercontent.com/kward/shunit2/master/source/2.1/src/shunit2" \
-q -O tests/cli/shunit2

docs/guide/shunit2:
wget "https://raw.githubusercontent.com/kward/shunit2/master/source/2.1/src/shunit2" \
@wget "https://raw.githubusercontent.com/kward/shunit2/master/source/2.1/src/shunit2" \
-q -O docs/guide/shunit2

get_vendor_deps: tools
glide install
@glide install

build-docker:
docker run -it --rm -v "$(PWD):/go/src/github.com/tendermint/basecoin" -w \
@docker run -it --rm -v "$(PWD):/go/src/github.com/tendermint/basecoin" -w \
"/go/src/github.com/tendermint/basecoin" -e "CGO_ENABLED=0" golang:alpine go build ./cmd/basecoin
docker build -t "tendermint/basecoin" .
@docker build -t "tendermint/basecoin" .

tools:
@go get $(GOTOOLS)
Expand Down
57 changes: 30 additions & 27 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (
"github.com/tendermint/basecoin/version"
)

//nolint
const (
ModuleNameBase = "base"
ChainKey = "chain_id"
)

// Basecoin - The ABCI application
type Basecoin struct {
eyesCli *eyes.Client
state *sm.State
Expand All @@ -30,67 +32,66 @@ type Basecoin struct {
logger log.Logger
}

func NewBasecoin(h basecoin.Handler, eyesCli *eyes.Client, l log.Logger) *Basecoin {
state := sm.NewState(eyesCli, l.With("module", "state"))
// NewBasecoin - create a new instance of the basecoin application
func NewBasecoin(handler basecoin.Handler, eyesCli *eyes.Client, logger log.Logger) *Basecoin {
state := sm.NewState(eyesCli, logger.With("module", "state"))

return &Basecoin{
handler: h,
handler: handler,
eyesCli: eyesCli,
state: state,
cacheState: nil,
logger: l,
logger: logger,
}
}

// placeholder to just handle sendtx
// DefaultHandler - placeholder to just handle sendtx
func DefaultHandler() basecoin.Handler {
// use the default stack
h := coin.NewHandler()
d := stack.NewDispatcher(stack.WrapHandler(h))
return stack.NewDefault().Use(d)
}

// XXX For testing, not thread safe!
// GetState - XXX For testing, not thread safe!
func (app *Basecoin) GetState() *sm.State {
return app.state.CacheWrap()
}

// ABCI::Info
// Info - ABCI
func (app *Basecoin) Info() abci.ResponseInfo {
resp, err := app.eyesCli.InfoSync()
if err != nil {
cmn.PanicCrisis(err)
}
return abci.ResponseInfo{
Data: cmn.Fmt("Basecoin v%v", version.Version),
Data: fmt.Sprintf("Basecoin v%v", version.Version),
LastBlockHeight: resp.LastBlockHeight,
LastBlockAppHash: resp.LastBlockAppHash,
}
}

// ABCI::SetOption
// SetOption - ABCI
func (app *Basecoin) SetOption(key string, value string) string {
module, prefix := splitKey(key)

module, key := splitKey(key)

if module == ModuleNameBase {
return app.setBaseOption(prefix, value)
if key == ChainKey {
app.state.SetChainID(value)
return "Success"
}
return fmt.Sprintf("Error: unknown base option: %s", key)
}

log, err := app.handler.SetOption(app.logger, app.state, module, prefix, value)
log, err := app.handler.SetOption(app.logger, app.state, module, key, value)
if err == nil {
return log
}
return "Error: " + err.Error()
}

func (app *Basecoin) setBaseOption(key, value string) string {
if key == ChainKey {
app.state.SetChainID(value)
return "Success"
}
return fmt.Sprintf("Error: unknown base option: %s", key)
}

// ABCI::DeliverTx
// DeliverTx - ABCI
func (app *Basecoin) DeliverTx(txBytes []byte) abci.Result {
tx, err := basecoin.LoadTx(txBytes)
if err != nil {
Expand All @@ -114,7 +115,7 @@ func (app *Basecoin) DeliverTx(txBytes []byte) abci.Result {
return res.ToABCI()
}

// ABCI::CheckTx
// CheckTx - ABCI
func (app *Basecoin) CheckTx(txBytes []byte) abci.Result {
tx, err := basecoin.LoadTx(txBytes)
if err != nil {
Expand All @@ -136,7 +137,7 @@ func (app *Basecoin) CheckTx(txBytes []byte) abci.Result {
return res.ToABCI()
}

// ABCI::Query
// Query - ABCI
func (app *Basecoin) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
if len(reqQuery.Data) == 0 {
resQuery.Log = "Query cannot be zero length"
Expand All @@ -153,7 +154,7 @@ func (app *Basecoin) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQu
return
}

// ABCI::Commit
// Commit - ABCI
func (app *Basecoin) Commit() (res abci.Result) {

// Commit state
Expand All @@ -168,21 +169,21 @@ func (app *Basecoin) Commit() (res abci.Result) {
return res
}

// ABCI::InitChain
// InitChain - ABCI
func (app *Basecoin) InitChain(validators []*abci.Validator) {
// for _, plugin := range app.plugins.GetList() {
// plugin.InitChain(app.state, validators)
// }
}

// ABCI::BeginBlock
// BeginBlock - ABCI
func (app *Basecoin) BeginBlock(hash []byte, header *abci.Header) {
// for _, plugin := range app.plugins.GetList() {
// plugin.BeginBlock(app.state, hash, header)
// }
}

// ABCI::EndBlock
// EndBlock - ABCI
func (app *Basecoin) EndBlock(height uint64) (res abci.ResponseEndBlock) {
// for _, plugin := range app.plugins.GetList() {
// pluginRes := plugin.EndBlock(app.state, height)
Expand All @@ -191,6 +192,8 @@ func (app *Basecoin) EndBlock(height uint64) (res abci.ResponseEndBlock) {
return
}

//TODO move split key to tmlibs?

// Splits the string at the first '/'.
// if there are none, assign default module ("base").
func splitKey(key string) (string, string) {
Expand Down
14 changes: 8 additions & 6 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
cmn "github.com/tendermint/tmlibs/common"
)

// LoadGenesis - Load the genesis file into memory
func (app *Basecoin) LoadGenesis(path string) error {
genDoc, err := loadGenesis(path)
if err != nil {
Expand Down Expand Up @@ -35,12 +36,13 @@ type keyValue struct {
Value string `json:"value"`
}

// includes tendermint (in the json, we ignore here)
// FullGenesisDoc - includes tendermint (in the json, we ignore here)
type FullGenesisDoc struct {
ChainID string `json:"chain_id"`
AppOptions *GenesisDoc `json:"app_options"`
}

// GenesisDoc - All genesis values
type GenesisDoc struct {
Accounts []json.RawMessage `json:"accounts"`
PluginOptions []json.RawMessage `json:"plugin_options"`
Expand Down Expand Up @@ -73,20 +75,20 @@ func loadGenesis(filePath string) (*FullGenesisDoc, error) {
return genDoc, nil
}

func parseGenesisList(kvz_ []json.RawMessage) (kvz []keyValue, err error) {
if len(kvz_)%2 != 0 {
func parseGenesisList(kvzIn []json.RawMessage) (kvz []keyValue, err error) {
if len(kvzIn)%2 != 0 {
return nil, errors.New("genesis cannot have an odd number of items. Format = [key1, value1, key2, value2, ...]")
}

for i := 0; i < len(kvz_); i += 2 {
for i := 0; i < len(kvzIn); i += 2 {
kv := keyValue{}
rawK := []byte(kvz_[i])
rawK := []byte(kvzIn[i])
err := json.Unmarshal(rawK, &(kv.Key))
if err != nil {
return nil, errors.Errorf("Non-string key: %s", string(rawK))
}
// convert value to string if possible (otherwise raw json)
rawV := kvz_[i+1]
rawV := kvzIn[i+1]
err = json.Unmarshal(rawV, &(kv.Value))
if err != nil {
kv.Value = string(rawV)
Expand Down
2 changes: 2 additions & 0 deletions cmd/basecli/commands/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"github.com/spf13/viper"
)

// AutoCompleteCmd - command to generate bash autocompletions
var AutoCompleteCmd = &cobra.Command{
Use: "complete",
Short: "generate bash autocompletions",
RunE: doAutoComplete,
}

// nolint - flags
const (
FlagOutput = "file"
)
Expand Down
4 changes: 3 additions & 1 deletion cmd/basecli/commands/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/tendermint/basecoin/stack"
)

// AccountQueryCmd - command to query an account
var AccountQueryCmd = &cobra.Command{
Use: "account [address]",
Short: "Get details of an account, with proof",
Expand Down Expand Up @@ -44,7 +45,8 @@ type BaseTxPresenter struct {
proofs.RawPresenter // this handles MakeKey as hex bytes
}

func (_ BaseTxPresenter) ParseData(raw []byte) (interface{}, error) {
// ParseData - unmarshal raw bytes to a basecoin tx
func (BaseTxPresenter) ParseData(raw []byte) (interface{}, error) {
var tx basecoin.Tx
err := wire.ReadBinaryBytes(raw, &tx)
return tx, err
Expand Down
23 changes: 13 additions & 10 deletions cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
coincmd "github.com/tendermint/basecoin/cmd/basecoin/commands"
)

// BaseCli represents the base command when called without any subcommands
// BaseCli - main basecoin client command
var BaseCli = &cobra.Command{
Use: "basecli",
Short: "Light client for tendermint",
Expand All @@ -34,16 +34,19 @@ func main() {
commands.AddBasicFlags(BaseCli)

// Prepare queries
pr := proofs.RootCmd
// These are default parsers, but optional in your app (you can remove key)
pr.AddCommand(proofs.TxCmd)
pr.AddCommand(proofs.KeyCmd)
pr.AddCommand(bcmd.AccountQueryCmd)
proofs.RootCmd.AddCommand(
// These are default parsers, but optional in your app (you can remove key)
proofs.TxCmd,
proofs.KeyCmd,
bcmd.AccountQueryCmd,
)

// you will always want this for the base send command
proofs.TxPresenters.Register("base", bcmd.BaseTxPresenter{})
tr := txs.RootCmd
tr.AddCommand(bcmd.SendTxCmd)
txs.RootCmd.AddCommand(
// This is the default transaction, optional in your app
bcmd.SendTxCmd,
)

// Set up the various commands to use
BaseCli.AddCommand(
Expand All @@ -52,8 +55,8 @@ func main() {
keycmd.RootCmd,
seeds.RootCmd,
rpccmd.RootCmd,
pr,
tr,
proofs.RootCmd,
txs.RootCmd,
proxy.RootCmd,
coincmd.VersionCmd,
bcmd.AutoCompleteCmd,
Expand Down
Loading

0 comments on commit 205e38e

Please sign in to comment.