From 37131fcb15b1a827dd7f9a0a1230c58dc70ee77f Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 17 Jan 2017 17:35:59 +0100 Subject: [PATCH 1/2] Add proof support and update dependencies --- app/app.go | 9 +++++++++ glide.lock | 6 +++--- glide.yaml | 5 ++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index a2a824814fc3..e2981458afc7 100644 --- a/app/app.go +++ b/app/app.go @@ -126,6 +126,15 @@ func (app *Basecoin) Query(query []byte) (res abci.Result) { return app.eyesCli.QuerySync(query) } +// TMSP::Query +func (app *Basecoin) Proof(key []byte, height uint64) (res abci.Result) { + if len(key) == 0 { + return abci.ErrEncodingError.SetLog("Key cannot be zero length") + } + + return app.eyesCli.ProofSync(key, height) +} + // TMSP::Commit func (app *Basecoin) Commit() (res abci.Result) { diff --git a/glide.lock b/glide.lock index 948a5da3bade..02388cec0593 100644 --- a/glide.lock +++ b/glide.lock @@ -41,7 +41,7 @@ imports: - leveldb/table - leveldb/util - name: github.com/tendermint/abci - version: 05096de3687ac582bec63860b3dd384acd9149aa + version: fdc047ae7af0f0189dd9f21b932eb92ce455ffa5 subpackages: - client - server @@ -85,12 +85,12 @@ imports: subpackages: - term - name: github.com/tendermint/merkleeyes - version: 2cf87e5f049ab6131aa4ea188c1b5b629d9b3bf9 + version: 4340a256d3d0fb44831826cf68aa1f6f4a2a151c subpackages: - app - client - name: github.com/tendermint/tendermint - version: cf0cb9558aaecbf3ddb071eb863df77e55d828ed + version: 00d4157b80b4052b21d27460b33e411bfe07d79d subpackages: - rpc/core/types - types diff --git a/glide.yaml b/glide.yaml index b3fddf0747be..65e2a4425021 100644 --- a/glide.yaml +++ b/glide.yaml @@ -15,9 +15,8 @@ import: - package: github.com/tendermint/merkleeyes version: develop - package: github.com/tendermint/tendermint - version: develop + version: abci_proof - package: github.com/tendermint/abci - version: develop - + version: abci_proof - package: github.com/gorilla/websocket version: v1.1.0 From f3aa0d47cfe640f64bbaf5152804756af7b8e2d9 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 18 Jan 2017 20:28:07 +0100 Subject: [PATCH 2/2] Added demo cmd paytovote, to show using plugins in real app --- Makefile | 9 +++-- app/genesis.go | 63 +++++++++++++++++++++++++++++++++ cmd/basecoin/main.go | 61 ++++--------------------------- cmd/paytovote/main.go | 53 +++++++++++++++++++++++++++ plugins/counter/counter.go | 3 +- plugins/counter/counter_test.go | 2 +- 6 files changed, 131 insertions(+), 60 deletions(-) create mode 100644 app/genesis.go create mode 100644 cmd/paytovote/main.go diff --git a/Makefile b/Makefile index d22c6561df99..42ccfc69c96d 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,11 @@ all: test install NOVENDOR = go list github.com/tendermint/basecoin/... | grep -v /vendor/ - -install: + +build: + go build github.com/tendermint/basecoin/cmd/... + +install: go install github.com/tendermint/basecoin/cmd/... test: @@ -20,4 +23,4 @@ update_deps: get_vendor_deps: go get github.com/Masterminds/glide glide install - + diff --git a/app/genesis.go b/app/genesis.go new file mode 100644 index 000000000000..93848c893ac3 --- /dev/null +++ b/app/genesis.go @@ -0,0 +1,63 @@ +package app + +import ( + "encoding/json" + "fmt" + "reflect" + + "github.com/pkg/errors" + cmn "github.com/tendermint/go-common" +) + +func (app *Basecoin) LoadGenesis(path string) error { + kvz, err := loadGenesis(path) + if err != nil { + return err + } + for _, kv := range kvz { + log := app.SetOption(kv.Key, kv.Value) + // TODO: remove debug output + fmt.Printf("Set %v=%v. Log: %v", kv.Key, kv.Value, log) + } + return nil +} + +type keyValue struct { + Key string `json:"key"` + Value string `json:"value"` +} + +func loadGenesis(filePath string) (kvz []keyValue, err error) { + kvz_ := []interface{}{} + bytes, err := cmn.ReadFile(filePath) + if err != nil { + return nil, errors.Wrap(err, "loading genesis file") + } + err = json.Unmarshal(bytes, &kvz_) + if err != nil { + return nil, errors.Wrap(err, "parsing genesis file") + } + if len(kvz_)%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 { + keyIfc := kvz_[i] + valueIfc := kvz_[i+1] + var key, value string + key, ok := keyIfc.(string) + if !ok { + return nil, errors.Errorf("genesis had invalid key %v of type %v", keyIfc, reflect.TypeOf(keyIfc)) + } + if value_, ok := valueIfc.(string); ok { + value = value_ + } else { + valueBytes, err := json.Marshal(valueIfc) + if err != nil { + return nil, errors.Errorf("genesis had invalid value %v: %v", value_, err.Error()) + } + value = string(valueBytes) + } + kvz = append(kvz, keyValue{key, value}) + } + return kvz, nil +} diff --git a/cmd/basecoin/main.go b/cmd/basecoin/main.go index 8d67c2d45def..5b06087c20d1 100644 --- a/cmd/basecoin/main.go +++ b/cmd/basecoin/main.go @@ -1,19 +1,15 @@ package main import ( - "encoding/json" "flag" - "fmt" - "reflect" "github.com/tendermint/abci/server" "github.com/tendermint/basecoin/app" - . "github.com/tendermint/go-common" + cmn "github.com/tendermint/go-common" eyes "github.com/tendermint/merkleeyes/client" ) func main() { - addrPtr := flag.String("address", "tcp://0.0.0.0:46658", "Listen address") eyesPtr := flag.String("eyes", "local", "MerkleEyes address, or 'local' for embedded") genFilePath := flag.String("genesis", "", "Genesis file, if any") @@ -22,7 +18,7 @@ func main() { // Connect to MerkleEyes eyesCli, err := eyes.NewClient(*eyesPtr, "socket") if err != nil { - Exit("connect to MerkleEyes: " + err.Error()) + cmn.Exit("connect to MerkleEyes: " + err.Error()) } // Create Basecoin app @@ -30,65 +26,22 @@ func main() { // If genesis file was specified, set key-value options if *genFilePath != "" { - kvz := loadGenesis(*genFilePath) - for _, kv := range kvz { - log := app.SetOption(kv.Key, kv.Value) - fmt.Println(Fmt("Set %v=%v. Log: %v", kv.Key, kv.Value, log)) + err := app.LoadGenesis(*genFilePath) + if err != nil { + cmn.Exit(cmn.Fmt("%+v", err)) } } // Start the listener svr, err := server.NewServer(*addrPtr, "socket", app) if err != nil { - Exit("create listener: " + err.Error()) + cmn.Exit("create listener: " + err.Error()) } // Wait forever - TrapSignal(func() { + cmn.TrapSignal(func() { // Cleanup svr.Stop() }) } - -//---------------------------------------- - -type KeyValue struct { - Key string `json:"key"` - Value string `json:"value"` -} - -func loadGenesis(filePath string) (kvz []KeyValue) { - kvz_ := []interface{}{} - bytes, err := ReadFile(filePath) - if err != nil { - Exit("loading genesis file: " + err.Error()) - } - err = json.Unmarshal(bytes, &kvz_) - if err != nil { - Exit("parsing genesis file: " + err.Error()) - } - if len(kvz_)%2 != 0 { - Exit("genesis cannot have an odd number of items. Format = [key1, value1, key2, value2, ...]") - } - for i := 0; i < len(kvz_); i += 2 { - keyIfc := kvz_[i] - valueIfc := kvz_[i+1] - var key, value string - key, ok := keyIfc.(string) - if !ok { - Exit(Fmt("genesis had invalid key %v of type %v", keyIfc, reflect.TypeOf(keyIfc))) - } - if value_, ok := valueIfc.(string); ok { - value = value_ - } else { - valueBytes, err := json.Marshal(valueIfc) - if err != nil { - Exit(Fmt("genesis had invalid value %v: %v", value_, err.Error())) - } - value = string(valueBytes) - } - kvz = append(kvz, KeyValue{key, value}) - } - return kvz -} diff --git a/cmd/paytovote/main.go b/cmd/paytovote/main.go new file mode 100644 index 000000000000..a477af9ce094 --- /dev/null +++ b/cmd/paytovote/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "flag" + + "github.com/tendermint/abci/server" + "github.com/tendermint/basecoin/app" + "github.com/tendermint/basecoin/plugins/counter" + cmn "github.com/tendermint/go-common" + eyes "github.com/tendermint/merkleeyes/client" +) + +func main() { + addrPtr := flag.String("address", "tcp://0.0.0.0:46658", "Listen address") + eyesPtr := flag.String("eyes", "local", "MerkleEyes address, or 'local' for embedded") + genFilePath := flag.String("genesis", "", "Genesis file, if any") + flag.Parse() + + // Connect to MerkleEyes + eyesCli, err := eyes.NewClient(*eyesPtr, "socket") + if err != nil { + cmn.Exit("connect to MerkleEyes: " + err.Error()) + } + + // Create Basecoin app + app := app.NewBasecoin(eyesCli) + + // add plugins + // TODO: add some more, like the cool voting app + counter := counter.New("counter") + app.RegisterPlugin(counter) + + // If genesis file was specified, set key-value options + if *genFilePath != "" { + err := app.LoadGenesis(*genFilePath) + if err != nil { + cmn.Exit(cmn.Fmt("%+v", err)) + } + } + + // Start the listener + svr, err := server.NewServer(*addrPtr, "socket", app) + if err != nil { + cmn.Exit("create listener: " + err.Error()) + } + + // Wait forever + cmn.TrapSignal(func() { + // Cleanup + svr.Stop() + }) + +} diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 61945f20c1b7..5dd42d1238b9 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -32,7 +32,7 @@ func (cp *CounterPlugin) StateKey() []byte { return []byte(fmt.Sprintf("CounterPlugin{name=%v}.State", cp.name)) } -func NewCounterPlugin(name string) *CounterPlugin { +func New(name string) *CounterPlugin { return &CounterPlugin{ name: name, } @@ -43,7 +43,6 @@ func (cp *CounterPlugin) SetOption(store types.KVStore, key string, value string } func (cp *CounterPlugin) RunTx(store types.KVStore, ctx types.CallContext, txBytes []byte) (res abci.Result) { - // Decode tx var tx CounterTx err := wire.ReadBinaryBytes(txBytes, &tx) diff --git a/plugins/counter/counter_test.go b/plugins/counter/counter_test.go index b9aa889464ca..a1f0de599893 100644 --- a/plugins/counter/counter_test.go +++ b/plugins/counter/counter_test.go @@ -23,7 +23,7 @@ func TestCounterPlugin(t *testing.T) { // Add Counter plugin counterPluginName := "testcounter" - counterPlugin := NewCounterPlugin(counterPluginName) + counterPlugin := New(counterPluginName) bcApp.RegisterPlugin(counterPlugin) // Account initialization