mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-08 11:01:20 +00:00
s/Dummy/KVStore/g
This commit is contained in:
parent
c960c52756
commit
a3362ccf35
15
README.md
15
README.md
@ -87,7 +87,7 @@ See [the documentation](http://tendermint.readthedocs.io/en/master/) for more de
|
|||||||
### Examples
|
### Examples
|
||||||
|
|
||||||
Check out the variety of example applications in the [example directory](example/).
|
Check out the variety of example applications in the [example directory](example/).
|
||||||
It also contains the code refered to by the `counter` and `dummy` apps; these apps come
|
It also contains the code refered to by the `counter` and `kvstore` apps; these apps come
|
||||||
built into the `abci-cli` binary.
|
built into the `abci-cli` binary.
|
||||||
|
|
||||||
#### Counter
|
#### Counter
|
||||||
@ -122,21 +122,21 @@ func cmdCounter(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
and can be found in [this file](cmd/abci-cli/abci-cli.go).
|
and can be found in [this file](cmd/abci-cli/abci-cli.go).
|
||||||
|
|
||||||
#### Dummy
|
#### kvstore
|
||||||
|
|
||||||
The `abci-cli dummy` application, which illustrates a simple key-value Merkle tree
|
The `abci-cli kvstore` application, which illustrates a simple key-value Merkle tree
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
func cmdDummy(cmd *cobra.Command, args []string) error {
|
func cmdKVStore(cmd *cobra.Command, args []string) error {
|
||||||
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
||||||
|
|
||||||
// Create the application - in memory or persisted to disk
|
// Create the application - in memory or persisted to disk
|
||||||
var app types.Application
|
var app types.Application
|
||||||
if flagPersist == "" {
|
if flagPersist == "" {
|
||||||
app = dummy.NewDummyApplication()
|
app = dummy.NewKVStoreApplication()
|
||||||
} else {
|
} else {
|
||||||
app = dummy.NewPersistentDummyApplication(flagPersist)
|
app = dummy.NewPersistentKVStoreApplication(flagPersist)
|
||||||
app.(*dummy.PersistentDummyApplication).SetLogger(logger.With("module", "dummy"))
|
app.(*dummy.PersistentKVStoreApplication).SetLogger(logger.With("module", "kvstore"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the listener
|
// Start the listener
|
||||||
@ -157,4 +157,3 @@ func cmdDummy(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
abcicli "github.com/tendermint/abci/client"
|
abcicli "github.com/tendermint/abci/client"
|
||||||
"github.com/tendermint/abci/example/code"
|
"github.com/tendermint/abci/example/code"
|
||||||
"github.com/tendermint/abci/example/counter"
|
"github.com/tendermint/abci/example/counter"
|
||||||
"github.com/tendermint/abci/example/dummy"
|
"github.com/tendermint/abci/example/kvstore"
|
||||||
"github.com/tendermint/abci/server"
|
"github.com/tendermint/abci/server"
|
||||||
servertest "github.com/tendermint/abci/tests/server"
|
servertest "github.com/tendermint/abci/tests/server"
|
||||||
"github.com/tendermint/abci/types"
|
"github.com/tendermint/abci/types"
|
||||||
@ -47,7 +47,7 @@ var (
|
|||||||
flagAddrC string
|
flagAddrC string
|
||||||
flagSerial bool
|
flagSerial bool
|
||||||
|
|
||||||
// dummy
|
// kvstore
|
||||||
flagAddrD string
|
flagAddrD string
|
||||||
flagPersist string
|
flagPersist string
|
||||||
)
|
)
|
||||||
@ -59,7 +59,7 @@ var RootCmd = &cobra.Command{
|
|||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
switch cmd.Use {
|
switch cmd.Use {
|
||||||
case "counter", "dummy": // for the examples apps, don't pre-run
|
case "counter", "kvstore", "dummy": // for the examples apps, don't pre-run
|
||||||
return nil
|
return nil
|
||||||
case "version": // skip running for version command
|
case "version": // skip running for version command
|
||||||
return nil
|
return nil
|
||||||
@ -133,6 +133,12 @@ func addDummyFlags() {
|
|||||||
dummyCmd.PersistentFlags().StringVarP(&flagAddrD, "addr", "", "tcp://0.0.0.0:46658", "listen address")
|
dummyCmd.PersistentFlags().StringVarP(&flagAddrD, "addr", "", "tcp://0.0.0.0:46658", "listen address")
|
||||||
dummyCmd.PersistentFlags().StringVarP(&flagPersist, "persist", "", "", "directory to use for a database")
|
dummyCmd.PersistentFlags().StringVarP(&flagPersist, "persist", "", "", "directory to use for a database")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addKVStoreFlags() {
|
||||||
|
kvstoreCmd.PersistentFlags().StringVarP(&flagAddrD, "addr", "", "tcp://0.0.0.0:46658", "listen address")
|
||||||
|
kvstoreCmd.PersistentFlags().StringVarP(&flagPersist, "persist", "", "", "directory to use for a database")
|
||||||
|
}
|
||||||
|
|
||||||
func addCommands() {
|
func addCommands() {
|
||||||
RootCmd.AddCommand(batchCmd)
|
RootCmd.AddCommand(batchCmd)
|
||||||
RootCmd.AddCommand(consoleCmd)
|
RootCmd.AddCommand(consoleCmd)
|
||||||
@ -150,8 +156,12 @@ func addCommands() {
|
|||||||
// examples
|
// examples
|
||||||
addCounterFlags()
|
addCounterFlags()
|
||||||
RootCmd.AddCommand(counterCmd)
|
RootCmd.AddCommand(counterCmd)
|
||||||
|
// deprecated, left for backwards compatibility
|
||||||
addDummyFlags()
|
addDummyFlags()
|
||||||
RootCmd.AddCommand(dummyCmd)
|
RootCmd.AddCommand(dummyCmd)
|
||||||
|
// replaces dummy, see issue #196
|
||||||
|
addKVStoreFlags()
|
||||||
|
RootCmd.AddCommand(kvstoreCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
var batchCmd = &cobra.Command{
|
var batchCmd = &cobra.Command{
|
||||||
@ -285,13 +295,25 @@ var counterCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// deprecated, left for backwards compatibility
|
||||||
var dummyCmd = &cobra.Command{
|
var dummyCmd = &cobra.Command{
|
||||||
Use: "dummy",
|
Use: "dummy",
|
||||||
|
Deprecated: "use: [abci-cli kvstore] instead",
|
||||||
Short: "ABCI demo example",
|
Short: "ABCI demo example",
|
||||||
Long: "ABCI demo example",
|
Long: "ABCI demo example",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cmdDummy(cmd, args)
|
return cmdKVStore(cmd, args)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var kvstoreCmd = &cobra.Command{
|
||||||
|
Use: "kvstore",
|
||||||
|
Short: "ABCI demo example",
|
||||||
|
Long: "ABCI demo example",
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return cmdKVStore(cmd, args)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -654,16 +676,16 @@ func cmdCounter(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdDummy(cmd *cobra.Command, args []string) error {
|
func cmdKVStore(cmd *cobra.Command, args []string) error {
|
||||||
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
||||||
|
|
||||||
// Create the application - in memory or persisted to disk
|
// Create the application - in memory or persisted to disk
|
||||||
var app types.Application
|
var app types.Application
|
||||||
if flagPersist == "" {
|
if flagPersist == "" {
|
||||||
app = dummy.NewDummyApplication()
|
app = kvstore.NewDummyApplication()
|
||||||
} else {
|
} else {
|
||||||
app = dummy.NewPersistentDummyApplication(flagPersist)
|
app = kvstore.NewPersistentDummyApplication(flagPersist)
|
||||||
app.(*dummy.PersistentDummyApplication).SetLogger(logger.With("module", "dummy"))
|
app.(*kvstore.PersistentDummyApplication).SetLogger(logger.With("module", "kvstore"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the listener
|
// Start the listener
|
||||||
|
@ -16,14 +16,14 @@ import (
|
|||||||
|
|
||||||
abcicli "github.com/tendermint/abci/client"
|
abcicli "github.com/tendermint/abci/client"
|
||||||
"github.com/tendermint/abci/example/code"
|
"github.com/tendermint/abci/example/code"
|
||||||
"github.com/tendermint/abci/example/dummy"
|
"github.com/tendermint/abci/example/kvstore"
|
||||||
abciserver "github.com/tendermint/abci/server"
|
abciserver "github.com/tendermint/abci/server"
|
||||||
"github.com/tendermint/abci/types"
|
"github.com/tendermint/abci/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDummy(t *testing.T) {
|
func TestKVStore(t *testing.T) {
|
||||||
fmt.Println("### Testing Dummy")
|
fmt.Println("### Testing KVStore")
|
||||||
testStream(t, dummy.NewDummyApplication())
|
testStream(t, dummy.NewKVStoreApplication())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBaseApp(t *testing.T) {
|
func TestBaseApp(t *testing.T) {
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
# Dummy
|
# KVStore
|
||||||
|
|
||||||
There are two app's here: the DummyApplication and the PersistentDummyApplication.
|
There are two app's here: the KVStoreApplication and the PersistentKVStoreApplication.
|
||||||
|
|
||||||
## DummyApplication
|
## KVStoreApplication
|
||||||
|
|
||||||
The DummyApplication is a simple merkle key-value store.
|
The KVStoreApplication is a simple merkle key-value store.
|
||||||
Transactions of the form `key=value` are stored as key-value pairs in the tree.
|
Transactions of the form `key=value` are stored as key-value pairs in the tree.
|
||||||
Transactions without an `=` sign set the value to the key.
|
Transactions without an `=` sign set the value to the key.
|
||||||
The app has no replay protection (other than what the mempool provides).
|
The app has no replay protection (other than what the mempool provides).
|
||||||
|
|
||||||
## PersistentDummyApplication
|
## PersistentKVStoreApplication
|
||||||
|
|
||||||
The PersistentDummyApplication wraps the DummyApplication
|
The PersistentKVStoreApplication wraps the KVStoreApplication
|
||||||
and provides two additional features:
|
and provides two additional features:
|
||||||
|
|
||||||
1) persistence of state across app restarts (using Tendermint's ABCI-Handshake mechanism)
|
1) persistence of state across app restarts (using Tendermint's ABCI-Handshake mechanism)
|
@ -1,4 +1,4 @@
|
|||||||
package dummy
|
package kvstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -51,25 +51,25 @@ func prefixKey(key []byte) []byte {
|
|||||||
|
|
||||||
//---------------------------------------------------
|
//---------------------------------------------------
|
||||||
|
|
||||||
var _ types.Application = (*DummyApplication)(nil)
|
var _ types.Application = (*KVStoreApplication)(nil)
|
||||||
|
|
||||||
type DummyApplication struct {
|
type KVStoreApplication struct {
|
||||||
types.BaseApplication
|
types.BaseApplication
|
||||||
|
|
||||||
state State
|
state State
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDummyApplication() *DummyApplication {
|
func NewKVStoreApplication() *KVStoreApplication {
|
||||||
state := loadState(dbm.NewMemDB())
|
state := loadState(dbm.NewMemDB())
|
||||||
return &DummyApplication{state: state}
|
return &KVStoreApplication{state: state}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *DummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
|
func (app *KVStoreApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
|
||||||
return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size)}
|
return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// tx is either "key=value" or just arbitrary bytes
|
// tx is either "key=value" or just arbitrary bytes
|
||||||
func (app *DummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
|
func (app *KVStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
|
||||||
var key, value []byte
|
var key, value []byte
|
||||||
parts := bytes.Split(tx, []byte("="))
|
parts := bytes.Split(tx, []byte("="))
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
@ -87,11 +87,11 @@ func (app *DummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
|
|||||||
return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
|
return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *DummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
|
func (app *KVStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx {
|
||||||
return types.ResponseCheckTx{Code: code.CodeTypeOK}
|
return types.ResponseCheckTx{Code: code.CodeTypeOK}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *DummyApplication) Commit() types.ResponseCommit {
|
func (app *KVStoreApplication) Commit() types.ResponseCommit {
|
||||||
// Using a memdb - just return the big endian size of the db
|
// Using a memdb - just return the big endian size of the db
|
||||||
appHash := make([]byte, 8)
|
appHash := make([]byte, 8)
|
||||||
binary.PutVarint(appHash, app.state.Size)
|
binary.PutVarint(appHash, app.state.Size)
|
||||||
@ -101,7 +101,7 @@ func (app *DummyApplication) Commit() types.ResponseCommit {
|
|||||||
return types.ResponseCommit{Data: appHash}
|
return types.ResponseCommit{Data: appHash}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
|
func (app *KVStoreApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
|
||||||
if reqQuery.Prove {
|
if reqQuery.Prove {
|
||||||
value := app.state.db.Get(prefixKey(reqQuery.Data))
|
value := app.state.db.Get(prefixKey(reqQuery.Data))
|
||||||
resQuery.Index = -1 // TODO make Proof return index
|
resQuery.Index = -1 // TODO make Proof return index
|
@ -1,4 +1,4 @@
|
|||||||
package dummy
|
package kvstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -17,7 +17,7 @@ import (
|
|||||||
"github.com/tendermint/abci/types"
|
"github.com/tendermint/abci/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testDummy(t *testing.T, app types.Application, tx []byte, key, value string) {
|
func testKVStore(t *testing.T, app types.Application, tx []byte, key, value string) {
|
||||||
ar := app.DeliverTx(tx)
|
ar := app.DeliverTx(tx)
|
||||||
require.False(t, ar.IsErr(), ar)
|
require.False(t, ar.IsErr(), ar)
|
||||||
// repeating tx doesn't raise error
|
// repeating tx doesn't raise error
|
||||||
@ -42,44 +42,44 @@ func testDummy(t *testing.T, app types.Application, tx []byte, key, value string
|
|||||||
require.Equal(t, value, string(resQuery.Value))
|
require.Equal(t, value, string(resQuery.Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDummyKV(t *testing.T) {
|
func TestKVStoreKV(t *testing.T) {
|
||||||
dummy := NewDummyApplication()
|
kvstore := NewKVStoreApplication()
|
||||||
key := "abc"
|
key := "abc"
|
||||||
value := key
|
value := key
|
||||||
tx := []byte(key)
|
tx := []byte(key)
|
||||||
testDummy(t, dummy, tx, key, value)
|
testKVStore(t, kvstore, tx, key, value)
|
||||||
|
|
||||||
value = "def"
|
value = "def"
|
||||||
tx = []byte(key + "=" + value)
|
tx = []byte(key + "=" + value)
|
||||||
testDummy(t, dummy, tx, key, value)
|
testKVStore(t, kvstore, tx, key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPersistentDummyKV(t *testing.T) {
|
func TestPersistentKVStoreKV(t *testing.T) {
|
||||||
dir, err := ioutil.TempDir("/tmp", "abci-dummy-test") // TODO
|
dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
dummy := NewPersistentDummyApplication(dir)
|
kvstore := NewPersistentKVStoreApplication(dir)
|
||||||
key := "abc"
|
key := "abc"
|
||||||
value := key
|
value := key
|
||||||
tx := []byte(key)
|
tx := []byte(key)
|
||||||
testDummy(t, dummy, tx, key, value)
|
testKVStore(t, kvstore, tx, key, value)
|
||||||
|
|
||||||
value = "def"
|
value = "def"
|
||||||
tx = []byte(key + "=" + value)
|
tx = []byte(key + "=" + value)
|
||||||
testDummy(t, dummy, tx, key, value)
|
testKVStore(t, kvstore, tx, key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPersistentDummyInfo(t *testing.T) {
|
func TestPersistentKVStoreInfo(t *testing.T) {
|
||||||
dir, err := ioutil.TempDir("/tmp", "abci-dummy-test") // TODO
|
dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
dummy := NewPersistentDummyApplication(dir)
|
kvstore := NewPersistentKVStoreApplication(dir)
|
||||||
InitDummy(dummy)
|
InitKVStore(kvstore)
|
||||||
height := int64(0)
|
height := int64(0)
|
||||||
|
|
||||||
resInfo := dummy.Info(types.RequestInfo{})
|
resInfo := kvstore.Info(types.RequestInfo{})
|
||||||
if resInfo.LastBlockHeight != height {
|
if resInfo.LastBlockHeight != height {
|
||||||
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
|
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
|
||||||
}
|
}
|
||||||
@ -90,11 +90,11 @@ func TestPersistentDummyInfo(t *testing.T) {
|
|||||||
header := types.Header{
|
header := types.Header{
|
||||||
Height: int64(height),
|
Height: int64(height),
|
||||||
}
|
}
|
||||||
dummy.BeginBlock(types.RequestBeginBlock{hash, header, nil, nil})
|
kvstore.BeginBlock(types.RequestBeginBlock{hash, header, nil, nil})
|
||||||
dummy.EndBlock(types.RequestEndBlock{header.Height})
|
kvstore.EndBlock(types.RequestEndBlock{header.Height})
|
||||||
dummy.Commit()
|
kvstore.Commit()
|
||||||
|
|
||||||
resInfo = dummy.Info(types.RequestInfo{})
|
resInfo = kvstore.Info(types.RequestInfo{})
|
||||||
if resInfo.LastBlockHeight != height {
|
if resInfo.LastBlockHeight != height {
|
||||||
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
|
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
|
||||||
}
|
}
|
||||||
@ -103,22 +103,22 @@ func TestPersistentDummyInfo(t *testing.T) {
|
|||||||
|
|
||||||
// add a validator, remove a validator, update a validator
|
// add a validator, remove a validator, update a validator
|
||||||
func TestValUpdates(t *testing.T) {
|
func TestValUpdates(t *testing.T) {
|
||||||
dir, err := ioutil.TempDir("/tmp", "abci-dummy-test") // TODO
|
dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
dummy := NewPersistentDummyApplication(dir)
|
kvstore := NewPersistentKVStoreApplication(dir)
|
||||||
|
|
||||||
// init with some validators
|
// init with some validators
|
||||||
total := 10
|
total := 10
|
||||||
nInit := 5
|
nInit := 5
|
||||||
vals := RandVals(total)
|
vals := RandVals(total)
|
||||||
// iniitalize with the first nInit
|
// iniitalize with the first nInit
|
||||||
dummy.InitChain(types.RequestInitChain{
|
kvstore.InitChain(types.RequestInitChain{
|
||||||
Validators: vals[:nInit],
|
Validators: vals[:nInit],
|
||||||
})
|
})
|
||||||
|
|
||||||
vals1, vals2 := vals[:nInit], dummy.Validators()
|
vals1, vals2 := vals[:nInit], kvstore.Validators()
|
||||||
valsEqual(t, vals1, vals2)
|
valsEqual(t, vals1, vals2)
|
||||||
|
|
||||||
var v1, v2, v3 types.Validator
|
var v1, v2, v3 types.Validator
|
||||||
@ -129,9 +129,9 @@ func TestValUpdates(t *testing.T) {
|
|||||||
tx1 := MakeValSetChangeTx(v1.PubKey, v1.Power)
|
tx1 := MakeValSetChangeTx(v1.PubKey, v1.Power)
|
||||||
tx2 := MakeValSetChangeTx(v2.PubKey, v2.Power)
|
tx2 := MakeValSetChangeTx(v2.PubKey, v2.Power)
|
||||||
|
|
||||||
makeApplyBlock(t, dummy, 1, diff, tx1, tx2)
|
makeApplyBlock(t, kvstore, 1, diff, tx1, tx2)
|
||||||
|
|
||||||
vals1, vals2 = vals[:nInit+2], dummy.Validators()
|
vals1, vals2 = vals[:nInit+2], kvstore.Validators()
|
||||||
valsEqual(t, vals1, vals2)
|
valsEqual(t, vals1, vals2)
|
||||||
|
|
||||||
// remove some validators
|
// remove some validators
|
||||||
@ -144,10 +144,10 @@ func TestValUpdates(t *testing.T) {
|
|||||||
tx2 = MakeValSetChangeTx(v2.PubKey, v2.Power)
|
tx2 = MakeValSetChangeTx(v2.PubKey, v2.Power)
|
||||||
tx3 := MakeValSetChangeTx(v3.PubKey, v3.Power)
|
tx3 := MakeValSetChangeTx(v3.PubKey, v3.Power)
|
||||||
|
|
||||||
makeApplyBlock(t, dummy, 2, diff, tx1, tx2, tx3)
|
makeApplyBlock(t, kvstore, 2, diff, tx1, tx2, tx3)
|
||||||
|
|
||||||
vals1 = append(vals[:nInit-2], vals[nInit+1])
|
vals1 = append(vals[:nInit-2], vals[nInit+1])
|
||||||
vals2 = dummy.Validators()
|
vals2 = kvstore.Validators()
|
||||||
valsEqual(t, vals1, vals2)
|
valsEqual(t, vals1, vals2)
|
||||||
|
|
||||||
// update some validators
|
// update some validators
|
||||||
@ -160,15 +160,15 @@ func TestValUpdates(t *testing.T) {
|
|||||||
diff = []types.Validator{v1}
|
diff = []types.Validator{v1}
|
||||||
tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
|
tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
|
||||||
|
|
||||||
makeApplyBlock(t, dummy, 3, diff, tx1)
|
makeApplyBlock(t, kvstore, 3, diff, tx1)
|
||||||
|
|
||||||
vals1 = append([]types.Validator{v1}, vals1[1:]...)
|
vals1 = append([]types.Validator{v1}, vals1[1:]...)
|
||||||
vals2 = dummy.Validators()
|
vals2 = kvstore.Validators()
|
||||||
valsEqual(t, vals1, vals2)
|
valsEqual(t, vals1, vals2)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeApplyBlock(t *testing.T, dummy types.Application, heightInt int, diff []types.Validator, txs ...[]byte) {
|
func makeApplyBlock(t *testing.T, kvstore types.Application, heightInt int, diff []types.Validator, txs ...[]byte) {
|
||||||
// make and apply block
|
// make and apply block
|
||||||
height := int64(heightInt)
|
height := int64(heightInt)
|
||||||
hash := []byte("foo")
|
hash := []byte("foo")
|
||||||
@ -176,14 +176,14 @@ func makeApplyBlock(t *testing.T, dummy types.Application, heightInt int, diff [
|
|||||||
Height: height,
|
Height: height,
|
||||||
}
|
}
|
||||||
|
|
||||||
dummy.BeginBlock(types.RequestBeginBlock{hash, header, nil, nil})
|
kvstore.BeginBlock(types.RequestBeginBlock{hash, header, nil, nil})
|
||||||
for _, tx := range txs {
|
for _, tx := range txs {
|
||||||
if r := dummy.DeliverTx(tx); r.IsErr() {
|
if r := kvstore.DeliverTx(tx); r.IsErr() {
|
||||||
t.Fatal(r)
|
t.Fatal(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resEndBlock := dummy.EndBlock(types.RequestEndBlock{header.Height})
|
resEndBlock := kvstore.EndBlock(types.RequestEndBlock{header.Height})
|
||||||
dummy.Commit()
|
kvstore.Commit()
|
||||||
|
|
||||||
valsEqual(t, diff, resEndBlock.ValidatorUpdates)
|
valsEqual(t, diff, resEndBlock.ValidatorUpdates)
|
||||||
|
|
||||||
@ -250,8 +250,8 @@ func makeGRPCClientServer(app types.Application, name string) (abcicli.Client, c
|
|||||||
|
|
||||||
func TestClientServer(t *testing.T) {
|
func TestClientServer(t *testing.T) {
|
||||||
// set up socket app
|
// set up socket app
|
||||||
dummy := NewDummyApplication()
|
kvstore := NewKVStoreApplication()
|
||||||
client, server, err := makeSocketClientServer(dummy, "dummy-socket")
|
client, server, err := makeSocketClientServer(kvstore, "kvstore-socket")
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
defer server.Stop()
|
defer server.Stop()
|
||||||
defer client.Stop()
|
defer client.Stop()
|
||||||
@ -259,8 +259,8 @@ func TestClientServer(t *testing.T) {
|
|||||||
runClientTests(t, client)
|
runClientTests(t, client)
|
||||||
|
|
||||||
// set up grpc app
|
// set up grpc app
|
||||||
dummy = NewDummyApplication()
|
kvstore = NewKVStoreApplication()
|
||||||
gclient, gserver, err := makeGRPCClientServer(dummy, "dummy-grpc")
|
gclient, gserver, err := makeGRPCClientServer(kvstore, "kvstore-grpc")
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
defer gserver.Stop()
|
defer gserver.Stop()
|
||||||
defer gclient.Stop()
|
defer gclient.Stop()
|
@ -1,4 +1,4 @@
|
|||||||
package dummy
|
package kvstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tendermint/abci/types"
|
"github.com/tendermint/abci/types"
|
||||||
@ -25,10 +25,10 @@ func RandVals(cnt int) []types.Validator {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitDummy initializes the dummy app with some data,
|
// InitKVStore initializes the dummy app with some data,
|
||||||
// which allows tests to pass and is fine as long as you
|
// which allows tests to pass and is fine as long as you
|
||||||
// don't make any tx that modify the validator state
|
// don't make any tx that modify the validator state
|
||||||
func InitDummy(app *PersistentDummyApplication) {
|
func InitKVStore(app *PersistentKVStoreApplication) {
|
||||||
app.InitChain(types.RequestInitChain{
|
app.InitChain(types.RequestInitChain{
|
||||||
Validators: RandVals(1),
|
Validators: RandVals(1),
|
||||||
AppStateBytes: []byte("[]"),
|
AppStateBytes: []byte("[]"),
|
@ -1,4 +1,4 @@
|
|||||||
package dummy
|
package kvstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -20,10 +20,10 @@ const (
|
|||||||
|
|
||||||
//-----------------------------------------
|
//-----------------------------------------
|
||||||
|
|
||||||
var _ types.Application = (*PersistentDummyApplication)(nil)
|
var _ types.Application = (*PersistentKVStoreApplication)(nil)
|
||||||
|
|
||||||
type PersistentDummyApplication struct {
|
type PersistentKVStoreApplication struct {
|
||||||
app *DummyApplication
|
app *KVStoreApplication
|
||||||
|
|
||||||
// validator set
|
// validator set
|
||||||
ValUpdates []types.Validator
|
ValUpdates []types.Validator
|
||||||
@ -31,8 +31,8 @@ type PersistentDummyApplication struct {
|
|||||||
logger log.Logger
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
|
func NewPersistentKVStoreApplication(dbDir string) *PersistentKVStoreApplication {
|
||||||
name := "dummy"
|
name := "kvstore"
|
||||||
db, err := dbm.NewGoLevelDB(name, dbDir)
|
db, err := dbm.NewGoLevelDB(name, dbDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -40,29 +40,29 @@ func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
|
|||||||
|
|
||||||
state := loadState(db)
|
state := loadState(db)
|
||||||
|
|
||||||
return &PersistentDummyApplication{
|
return &PersistentKVStoreApplication{
|
||||||
app: &DummyApplication{state: state},
|
app: &KVStoreApplication{state: state},
|
||||||
logger: log.NewNopLogger(),
|
logger: log.NewNopLogger(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *PersistentDummyApplication) SetLogger(l log.Logger) {
|
func (app *PersistentKVStoreApplication) SetLogger(l log.Logger) {
|
||||||
app.logger = l
|
app.logger = l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *PersistentDummyApplication) Info(req types.RequestInfo) types.ResponseInfo {
|
func (app *PersistentKVStoreApplication) Info(req types.RequestInfo) types.ResponseInfo {
|
||||||
res := app.app.Info(req)
|
res := app.app.Info(req)
|
||||||
res.LastBlockHeight = app.app.state.Height
|
res.LastBlockHeight = app.app.state.Height
|
||||||
res.LastBlockAppHash = app.app.state.AppHash
|
res.LastBlockAppHash = app.app.state.AppHash
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *PersistentDummyApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
|
func (app *PersistentKVStoreApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
|
||||||
return app.app.SetOption(req)
|
return app.app.SetOption(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
|
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
|
||||||
func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
|
func (app *PersistentKVStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
|
||||||
// if it starts with "val:", update the validator set
|
// if it starts with "val:", update the validator set
|
||||||
// format is "val:pubkey/power"
|
// format is "val:pubkey/power"
|
||||||
if isValidatorTx(tx) {
|
if isValidatorTx(tx) {
|
||||||
@ -75,21 +75,21 @@ func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.ResponseDelive
|
|||||||
return app.app.DeliverTx(tx)
|
return app.app.DeliverTx(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *PersistentDummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
|
func (app *PersistentKVStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx {
|
||||||
return app.app.CheckTx(tx)
|
return app.app.CheckTx(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit will panic if InitChain was not called
|
// Commit will panic if InitChain was not called
|
||||||
func (app *PersistentDummyApplication) Commit() types.ResponseCommit {
|
func (app *PersistentKVStoreApplication) Commit() types.ResponseCommit {
|
||||||
return app.app.Commit()
|
return app.app.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
|
func (app *PersistentKVStoreApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
|
||||||
return app.app.Query(reqQuery)
|
return app.app.Query(reqQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the validators in the merkle tree
|
// Save the validators in the merkle tree
|
||||||
func (app *PersistentDummyApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
|
func (app *PersistentKVStoreApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
|
||||||
for _, v := range req.Validators {
|
for _, v := range req.Validators {
|
||||||
r := app.updateValidator(v)
|
r := app.updateValidator(v)
|
||||||
if r.IsErr() {
|
if r.IsErr() {
|
||||||
@ -100,21 +100,21 @@ func (app *PersistentDummyApplication) InitChain(req types.RequestInitChain) typ
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Track the block hash and header information
|
// Track the block hash and header information
|
||||||
func (app *PersistentDummyApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
|
func (app *PersistentKVStoreApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
|
||||||
// reset valset changes
|
// reset valset changes
|
||||||
app.ValUpdates = make([]types.Validator, 0)
|
app.ValUpdates = make([]types.Validator, 0)
|
||||||
return types.ResponseBeginBlock{}
|
return types.ResponseBeginBlock{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the validator set
|
// Update the validator set
|
||||||
func (app *PersistentDummyApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
|
func (app *PersistentKVStoreApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
|
||||||
return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates}
|
return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates}
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------
|
//---------------------------------------------
|
||||||
// update validators
|
// update validators
|
||||||
|
|
||||||
func (app *PersistentDummyApplication) Validators() (validators []types.Validator) {
|
func (app *PersistentKVStoreApplication) Validators() (validators []types.Validator) {
|
||||||
itr := app.app.state.db.Iterator(nil, nil)
|
itr := app.app.state.db.Iterator(nil, nil)
|
||||||
for ; itr.Valid(); itr.Next() {
|
for ; itr.Valid(); itr.Next() {
|
||||||
if isValidatorTx(itr.Key()) {
|
if isValidatorTx(itr.Key()) {
|
||||||
@ -138,7 +138,7 @@ func isValidatorTx(tx []byte) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// format is "val:pubkey1/power1,addr2/power2,addr3/power3"tx
|
// format is "val:pubkey1/power1,addr2/power2,addr3/power3"tx
|
||||||
func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
|
func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
|
||||||
tx = tx[len(ValidatorSetChangePrefix):]
|
tx = tx[len(ValidatorSetChangePrefix):]
|
||||||
|
|
||||||
//get the pubkey and power
|
//get the pubkey and power
|
||||||
@ -177,7 +177,7 @@ func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.Response
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add, update, or remove a validator
|
// add, update, or remove a validator
|
||||||
func (app *PersistentDummyApplication) updateValidator(v types.Validator) types.ResponseDeliverTx {
|
func (app *PersistentKVStoreApplication) updateValidator(v types.Validator) types.ResponseDeliverTx {
|
||||||
key := []byte("val:" + string(v.PubKey))
|
key := []byte("val:" + string(v.PubKey))
|
||||||
if v.Power == 0 {
|
if v.Power == 0 {
|
||||||
// remove validator
|
// remove validator
|
@ -6,14 +6,14 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
abciclient "github.com/tendermint/abci/client"
|
abciclient "github.com/tendermint/abci/client"
|
||||||
"github.com/tendermint/abci/example/dummy"
|
"github.com/tendermint/abci/example/kvstore"
|
||||||
abciserver "github.com/tendermint/abci/server"
|
abciserver "github.com/tendermint/abci/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestClientServerNoAddrPrefix(t *testing.T) {
|
func TestClientServerNoAddrPrefix(t *testing.T) {
|
||||||
addr := "localhost:46658"
|
addr := "localhost:46658"
|
||||||
transport := "socket"
|
transport := "socket"
|
||||||
app := dummy.NewDummyApplication()
|
app := kvstore.NewKVStoreApplication()
|
||||||
|
|
||||||
server, err := abciserver.NewServer(addr, transport, app)
|
server, err := abciserver.NewServer(addr, transport, app)
|
||||||
assert.NoError(t, err, "expected no error on NewServer")
|
assert.NoError(t, err, "expected no error on NewServer")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user