wip refactor examples/basecoin

This commit is contained in:
rigelrozanski
2018-02-17 16:32:30 -05:00
committed by Ethan Buchman
parent e628756bea
commit 890fadc8c3
11 changed files with 76 additions and 274 deletions
+21
View File
@@ -30,3 +30,24 @@ func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
}
return &genDoc, nil
}
// read app state from the genesis file
//func GenesisAppState(genesisFile string) (state json.RawMessage, err error) {
//if genesisFile == "" {
//return
//}
//jsonBlob, err := ioutil.ReadFile(genesisFile)
//if err != nil {
//return nil, err
//}
//data := make(map[string]interface{})
//err = json.Unmarshal(jsonBlob, &data)
//if err != nil {
//return nil, err
//}
//state, ok := data["app_state"].(json.RawMessage)
//if !ok {
//return nil, errors.New("app state genesis parse error")
//}
//return state, nil
//}
+2 -24
View File
@@ -1,41 +1,19 @@
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: check_tools get_vendor_deps build test
########################################
### Build
all: get_vendor_deps build test
build:
go build $(BUILD_FLAGS) -o build/basecoin ./cmd/...
########################################
### Tools & dependencies
check_tools:
cd tools && $(MAKE) check
get_tools:
cd tools && $(MAKE)
get_vendor_deps:
@rm -rf vendor/
@echo "--> Running glide install"
@glide install
########################################
### Testing
test:
@go test $(PACKAGES)
benchmark:
@go test -bench=. $(PACKAGES)
# 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 check_tools get_tools get_vendor_deps test benchmark
.PHONY: build get_vendor_deps test benchmark
+7 -12
View File
@@ -2,7 +2,6 @@ package app
import (
"fmt"
"os"
bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -15,22 +14,20 @@ import (
const appName = "BasecoinApp"
// BasecoinApp - extended ABCI application
// Extended ABCI application
type BasecoinApp struct {
*bam.BaseApp
router bam.Router
cdc *wire.Codec
multiStore sdk.CommitMultiStore //TODO distinguish this store from *bam.BaseApp.cms <- is this one master?? confused
router bam.Router
cdc *wire.Codec
// The key to access the substores.
// keys to access the substores
capKeyMainStore *sdk.KVStoreKey
capKeyIBCStore *sdk.KVStoreKey
// Object mappers:
// object mappers
accountMapper sdk.AccountMapper
}
// NewBasecoinApp - create new BasecoinApp
// TODO: This should take in more configuration options.
// TODO: This should be moved into baseapp to isolate complexity
func NewBasecoinApp(genesisPath string) *BasecoinApp {
@@ -73,8 +70,7 @@ func (app *BasecoinApp) RunForever() {
// Start the ABCI server
srv, err := server.NewServer("0.0.0.0:46658", "socket", app)
if err != nil {
fmt.Println(err)
os.Exit(1)
cmn.Exit(err.Error())
}
srv.Start()
@@ -89,7 +85,6 @@ func (app *BasecoinApp) RunForever() {
// Load the stores
func (app *BasecoinApp) loadStores() {
if err := app.LoadLatestVersion(app.capKeyMainStore); err != nil {
fmt.Println(err)
os.Exit(1)
cmn.Exit(err.Error())
}
}
+1
View File
@@ -61,6 +61,7 @@ func (app *BasecoinApp) initBaseAppInitStater() {
//-----------------------------------------------------
// State to Unmarshal
type GenesisState struct {
Accounts []*GenesisAccount `accounts`
}
-1
View File
@@ -1 +0,0 @@
bin/*
-62
View File
@@ -1,62 +0,0 @@
all: install_glide check get_vendor_deps install
########################################
### Glide
GLIDE = github.com/tendermint/glide
GLIDE_CHECK := $(shell command -v glide 2> /dev/null)
check:
ifndef GLIDE_CHECK
@echo "No glide in path. Install with 'make install_glide'."
else
@echo "Found glide in path."
endif
install_glide:
ifdef GLIDE_CHECK
@echo "Glide is already installed. Run 'make update_glide' to update."
else
@echo "$(ansi_grn)Installing glide$(ansi_end)"
go get -v $(GLIDE)
endif
update_glide:
@echo "$(ansi_grn)Updating glide$(ansi_end)"
go get -u -v $(GLIDE)
########################################
### Install tools
get_vendor_deps: check
@rm -rf vendor/
@echo "--> Running glide install"
@glide install
install: get_vendor_deps
@echo "$(ansi_grn)Installing tools$(ansi_end)"
@echo "$(ansi_yel)Install go-vendorinstall$(ansi_end)"
go build -o bin/go-vendorinstall go-vendorinstall/*.go
@echo "$(ansi_yel)Install gometalinter.v2$(ansi_end)"
GOBIN=$(CURDIR)/bin ./bin/go-vendorinstall gopkg.in/alecthomas/gometalinter.v2
@echo "$(ansi_grn)Done installing tools$(ansi_end)"
########################################
# ANSI colors
ansi_red=\033[0;31m
ansi_grn=\033[0;32m
ansi_yel=\033[0;33m
ansi_end=\033[0m
# 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: check install_glide update_glide get_vendor_deps install
-18
View File
@@ -1,18 +0,0 @@
hash: a163b1c4806024cfc9062db75a0abed285ec40461243e59af0e147db2c4bf0ce
updated: 2018-01-15T19:02:49.834182027-08:00
imports:
- name: github.com/inconshreveable/mousetrap
version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75
- name: github.com/rigelrozanski/common
version: f691f115798593d783b9999b1263c2f4ffecc439
- name: github.com/rigelrozanski/shelldown
version: 2e18b6eb9bf428aa524e71433296e0b7c73ae0a3
subpackages:
- cmd/shelldown
- name: github.com/spf13/cobra
version: 7b2c5ac9fc04fc5efafb60700713d4fa609b777b
- name: github.com/spf13/pflag
version: 97afa5e7ca8a08a383cb259e06636b5e2cc7897f
- name: gopkg.in/alecthomas/gometalinter.v2
version: 88d47c66988c5a5cb3945925da47c883800a94df
testImports: []
-6
View File
@@ -1,6 +0,0 @@
package: github.com/cosmos/cosmos-sdk/tools
import:
- package: github.com/rigelrozanski/shelldown
subpackages:
- cmd/shelldown
- package: gopkg.in/alecthomas/gometalinter.v2
@@ -1,129 +0,0 @@
// https://raw.githubusercontent.com/roboll/go-vendorinstall/a3e9f0a5d5861b3bb16b93200b2c359c9846b3c5/main.go
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
source = flag.String("source", "vendor", "source directory")
target = flag.String("target", "", "target directory (defaults to $GOBIN, if not set $GOPATH/bin)")
commands = flag.String("commands", "", "comma separated list of commands to execute after go install in temporary environment")
quiet = flag.Bool("quiet", false, "disable output")
)
func main() {
flag.Parse()
packages := flag.Args()
if len(packages) < 1 {
fail(errors.New("no packages: specify a package"))
}
gopath, err := ioutil.TempDir("", "go-vendorinstall-gopath")
if err != nil {
fail(err)
}
print(fmt.Sprintf("gopath: %s", gopath))
defer func() {
if err := os.RemoveAll(gopath); err != nil {
fail(err)
}
}()
if len(*target) == 0 {
if gobin := os.Getenv("GOBIN"); len(gobin) > 0 {
target = &gobin
} else {
bin := fmt.Sprintf("%s/bin", os.Getenv("GOPATH"))
target = &bin
}
}
gobin, err := filepath.Abs(*target)
if err != nil {
fail(err)
}
print(fmt.Sprintf("gobin: %s", gobin))
if err := link(gopath, *source); err != nil {
fail(err)
}
oldpath := os.Getenv("PATH")
path := fmt.Sprintf("%s%s%s", gobin, string(os.PathListSeparator), os.Getenv("PATH"))
os.Setenv("PATH", fmt.Sprintf("%s%s%s", gobin, string(os.PathListSeparator), os.Getenv("PATH")))
defer os.Setenv("PATH", oldpath)
env := []string{fmt.Sprintf("PATH=%s", path), fmt.Sprintf("GOPATH=%s", gopath), fmt.Sprintf("GOBIN=%s", gobin)}
args := append([]string{"install"}, packages...)
if out, err := doexec("go", gopath, args, env); err != nil {
print(string(out))
fail(err)
}
if len(*commands) > 0 {
for _, cmd := range strings.Split(*commands, ",") {
split := strings.Split(cmd, " ")
if out, err := doexec(split[0], gopath, split[1:], env); err != nil {
print(string(out))
fail(err)
}
}
}
}
func print(msg string) {
if !*quiet {
fmt.Println(msg)
}
}
func fail(err error) {
fmt.Printf("error: %s", err.Error())
os.Exit(1)
}
func link(gopath, source string) error {
srcdir, err := filepath.Abs(source)
if err != nil {
return err
}
linkto := filepath.Join(gopath, "src")
if err := os.MkdirAll(linkto, 0777); err != nil {
return err
}
files, err := ioutil.ReadDir(srcdir)
if err != nil {
return err
}
for _, file := range files {
real := filepath.Join(srcdir, file.Name())
link := filepath.Join(linkto, file.Name())
if err := os.Symlink(real, link); err != nil {
return err
}
}
return nil
}
func doexec(bin, dir string, args []string, env []string) ([]byte, error) {
print(fmt.Sprintf("%s %s", bin, strings.Join(args, " ")))
cmd := exec.Command(bin, args...)
cmd.Env = env
cmd.Dir = dir
return cmd.CombinedOutput()
}
-12
View File
@@ -1,12 +0,0 @@
package main
import (
// Include dependencies here so glide picks them up
// and installs sub-dependencies.
// TODO: Ideally this gets auto-imported on glide update.
// Any way to make that happen?
_ "github.com/rigelrozanski/common"
)
func main() {}
+45 -10
View File
@@ -3,25 +3,60 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
crypto "github.com/tendermint/go-crypto"
)
var _ sdk.Account = (*AppAccount)(nil)
// Custom extensions for this application. This is just an example of
// extending auth.BaseAccount with custom fields.
//
// This is compatible with the stock auth.AccountStore, since
// auth.AccountStore uses the flexible go-wire library.
type AppAccount struct {
auth.BaseAccount
// Custom extensions for this application. This is just an example of
// extending auth.BaseAccount with custom fields.
//
// This is compatible with the stock auth.AccountStore, since
// auth.AccountStore uses the flexible go-wire library.
Name string
}
func (acc AppAccount) GetName() string {
return acc.Name
// nolint
func (acc AppAccount) GetName() string { return acc.Name }
func (acc *AppAccount) SetName(name string) { acc.Name = name }
//___________________________________________________________________________________
// We use GenesisAccount instead of AppAccount for cleaner json input of PubKey
type GenesisAccount struct {
Name string `json:"name"`
Address crypto.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
PubKey cmn.HexBytes `json:"public_key"`
Sequence int64 `json:"sequence"`
}
func (acc *AppAccount) SetName(name string) {
acc.Name = name
func NewGenesisAccount(aa *AppAccount) *GenesisAccount {
return &GenesisAccount{
Name: aa.Name,
Address: aa.Address,
Coins: aa.Coins,
PubKey: aa.PubKey.Bytes(),
Sequence: aa.Sequence,
}
}
// convert GenesisAccount to AppAccount
func (ga *GenesisAccount) toAppAccount() (acc *AppAccount, err error) {
pk, err := crypto.PubKeyFromBytes(ga.PubKey)
if err != nil {
return
}
baseAcc := auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins,
PubKey: pk,
Sequence: ga.Sequence,
}
return &AppAccount{
BaseAccount: baseAcc,
Name: ga.Name,
}, nil
}