everything takes Request, returns Response; expect DeliverTx/CheckTx/Commit

This commit is contained in:
Ethan Buchman
2017-11-27 19:04:21 +00:00
parent 67d2a5f66d
commit c7f54fb56c
15 changed files with 174 additions and 291 deletions
-65
View File
@@ -1,65 +0,0 @@
package main
import (
"flag"
"os"
"github.com/tendermint/abci/server"
"github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
func main() {
addrPtr := flag.String("addr", "tcp://0.0.0.0:46658", "Listen address")
abciPtr := flag.String("abci", "socket", "socket | grpc")
flag.Parse()
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
// Start the listener
srv, err := server.NewServer(*addrPtr, *abciPtr, NewChainAwareApplication())
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
srv.SetLogger(logger.With("module", "abci-server"))
if _, err := srv.Start(); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
// Wait forever
cmn.TrapSignal(func() {
// Cleanup
srv.Stop()
})
}
type ChainAwareApplication struct {
types.BaseApplication
beginCount int
endCount int
}
func NewChainAwareApplication() *ChainAwareApplication {
return &ChainAwareApplication{}
}
func (app *ChainAwareApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
return types.ResponseQuery{
Value: []byte(cmn.Fmt("%d,%d", app.beginCount, app.endCount)),
}
}
func (app *ChainAwareApplication) BeginBlock(reqBeginBlock types.RequestBeginBlock) {
app.beginCount++
}
func (app *ChainAwareApplication) EndBlock(height uint64) (resEndBlock types.ResponseEndBlock) {
app.endCount++
return
}
-57
View File
@@ -1,57 +0,0 @@
package main
import (
"strconv"
"strings"
"testing"
abcicli "github.com/tendermint/abci/client"
"github.com/tendermint/abci/server"
"github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
)
func TestChainAware(t *testing.T) {
app := NewChainAwareApplication()
// Start the listener
srv, err := server.NewServer("unix://test.sock", "socket", app)
if err != nil {
t.Fatal(err)
}
srv.SetLogger(log.TestingLogger().With("module", "abci-server"))
if _, err := srv.Start(); err != nil {
t.Fatal(err.Error())
}
defer srv.Stop()
// Connect to the socket
client := abcicli.NewSocketClient("unix://test.sock", false)
client.SetLogger(log.TestingLogger().With("module", "abci-client"))
if _, err := client.Start(); err != nil {
t.Fatalf("Error starting socket client: %v", err.Error())
}
defer client.Stop()
n := uint64(5)
hash := []byte("fake block hash")
header := &types.Header{}
for i := uint64(0); i < n; i++ {
client.BeginBlockSync(types.RequestBeginBlock{hash, header})
client.EndBlockSync(i)
client.CommitSync()
}
r := app.Query(types.RequestQuery{})
spl := strings.Split(string(r.Value), ",")
if len(spl) != 2 {
t.Fatal("expected %d,%d ; got %s", n, n, string(r.Value))
}
beginCount, _ := strconv.Atoi(spl[0])
endCount, _ := strconv.Atoi(spl[1])
if uint64(beginCount) != n {
t.Fatalf("expected beginCount of %d, got %d", n, beginCount)
} else if uint64(endCount) != n {
t.Fatalf("expected endCount of %d, got %d", n, endCount)
}
}
+3 -2
View File
@@ -24,11 +24,12 @@ func (app *CounterApplication) Info(req types.RequestInfo) types.ResponseInfo {
return types.ResponseInfo{Data: cmn.Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
}
func (app *CounterApplication) SetOption(key string, value string) (log string) {
func (app *CounterApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
key, value := req.Key, req.Value
if key == "serial" && value == "on" {
app.serial = true
}
return ""
return types.ResponseSetOption{}
}
func (app *CounterApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
+2
View File
@@ -10,6 +10,8 @@ import (
dbm "github.com/tendermint/tmlibs/db"
)
var _ types.Application = (*DummyApplication)(nil)
type DummyApplication struct {
types.BaseApplication
+2 -2
View File
@@ -93,7 +93,7 @@ func TestPersistentDummyInfo(t *testing.T) {
Height: uint64(height),
}
dummy.BeginBlock(types.RequestBeginBlock{hash, header})
dummy.EndBlock(height)
dummy.EndBlock(types.RequestEndBlock{header.Height})
dummy.Commit()
resInfo = dummy.Info(types.RequestInfo{})
@@ -182,7 +182,7 @@ func makeApplyBlock(t *testing.T, dummy types.Application, heightInt int, diff [
t.Fatal(r)
}
}
resEndBlock := dummy.EndBlock(height)
resEndBlock := dummy.EndBlock(types.RequestEndBlock{header.Height})
dummy.Commit()
valsEqual(t, diff, resEndBlock.Diffs)
+15 -11
View File
@@ -21,6 +21,8 @@ const (
//-----------------------------------------
var _ types.Application = (*PersistentDummyApplication)(nil)
type PersistentDummyApplication struct {
app *DummyApplication
@@ -50,15 +52,15 @@ func (app *PersistentDummyApplication) SetLogger(l log.Logger) {
app.logger = l
}
func (app *PersistentDummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
resInfo = app.app.Info(req)
resInfo.LastBlockHeight = app.app.state.LatestVersion()
resInfo.LastBlockAppHash = app.app.state.Hash()
return resInfo
func (app *PersistentDummyApplication) Info(req types.RequestInfo) types.ResponseInfo {
res := app.app.Info(req)
res.LastBlockHeight = app.app.state.LatestVersion()
res.LastBlockAppHash = app.app.state.Hash()
return res
}
func (app *PersistentDummyApplication) SetOption(key string, value string) (log string) {
return app.app.SetOption(key, value)
func (app *PersistentDummyApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
return app.app.SetOption(req)
}
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
@@ -102,23 +104,25 @@ func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.
}
// Save the validators in the merkle tree
func (app *PersistentDummyApplication) InitChain(params types.RequestInitChain) {
for _, v := range params.Validators {
func (app *PersistentDummyApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
for _, v := range req.Validators {
r := app.updateValidator(v)
if r.IsErr() {
app.logger.Error("Error updating validators", "r", r)
}
}
return types.ResponseInitChain{}
}
// Track the block hash and header information
func (app *PersistentDummyApplication) BeginBlock(params types.RequestBeginBlock) {
func (app *PersistentDummyApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
// reset valset changes
app.changes = make([]*types.Validator, 0)
return types.ResponseBeginBlock{}
}
// Update the validator set
func (app *PersistentDummyApplication) EndBlock(height uint64) (resEndBlock types.ResponseEndBlock) {
func (app *PersistentDummyApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
return types.ResponseEndBlock{Diffs: app.changes}
}