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

View File

@ -8,10 +8,11 @@ import (
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
) )
// Client defines an interface for an ABCI client. Client-related errors (e.g. // Client defines an interface for an ABCI client.
// network errors) are returned as a second return value for most calls // All `Async` methods return a `ReqRes` object.
// (sometimes there is no response). Application-related errors are reflected // All `Sync` methods return the appropriate protobuf ResponseXxx struct and an error.
// in response via ABCI error codes and logs. // Note these are client errors, eg. ABCI socket connectivity issues.
// Application-related errors are reflected in response via ABCI error codes and logs.
type Client interface { type Client interface {
cmn.Service cmn.Service
@ -21,28 +22,26 @@ type Client interface {
FlushAsync() *ReqRes FlushAsync() *ReqRes
EchoAsync(msg string) *ReqRes EchoAsync(msg string) *ReqRes
InfoAsync(types.RequestInfo) *ReqRes InfoAsync(types.RequestInfo) *ReqRes
SetOptionAsync(key string, value string) *ReqRes SetOptionAsync(types.RequestSetOption) *ReqRes
DeliverTxAsync(tx []byte) *ReqRes DeliverTxAsync(tx []byte) *ReqRes
CheckTxAsync(tx []byte) *ReqRes CheckTxAsync(tx []byte) *ReqRes
QueryAsync(types.RequestQuery) *ReqRes QueryAsync(types.RequestQuery) *ReqRes
CommitAsync() *ReqRes CommitAsync() *ReqRes
InitChainAsync(types.RequestInitChain) *ReqRes
BeginBlockAsync(types.RequestBeginBlock) *ReqRes
EndBlockAsync(types.RequestEndBlock) *ReqRes
FlushSync() error FlushSync() error
EchoSync(msg string) (*types.ResponseEcho, error) EchoSync(msg string) (*types.ResponseEcho, error)
InfoSync(types.RequestInfo) (*types.ResponseInfo, error) InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
SetOptionSync(key string, value string) (log string, err error) SetOptionSync(types.RequestSetOption) (*types.ResponseSetOption, error)
DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error)
CheckTxSync(tx []byte) (*types.ResponseCheckTx, error) CheckTxSync(tx []byte) (*types.ResponseCheckTx, error)
QuerySync(types.RequestQuery) (*types.ResponseQuery, error) QuerySync(types.RequestQuery) (*types.ResponseQuery, error)
CommitSync() (*types.ResponseCommit, error) CommitSync() (*types.ResponseCommit, error)
InitChainSync(types.RequestInitChain) (*types.ResponseInitChain, error)
InitChainAsync(types.RequestInitChain) *ReqRes BeginBlockSync(types.RequestBeginBlock) (*types.ResponseBeginBlock, error)
BeginBlockAsync(types.RequestBeginBlock) *ReqRes EndBlockSync(types.RequestEndBlock) (*types.ResponseEndBlock, error)
EndBlockAsync(height uint64) *ReqRes
InitChainSync(types.RequestInitChain) error
BeginBlockSync(types.RequestBeginBlock) error
EndBlockSync(height uint64) (*types.ResponseEndBlock, error)
} }
//---------------------------------------- //----------------------------------------

View File

@ -150,8 +150,8 @@ func (cli *grpcClient) InfoAsync(params types.RequestInfo) *ReqRes {
return cli.finishAsyncCall(req, &types.Response{&types.Response_Info{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_Info{res}})
} }
func (cli *grpcClient) SetOptionAsync(key string, value string) *ReqRes { func (cli *grpcClient) SetOptionAsync(params types.RequestSetOption) *ReqRes {
req := types.ToRequestSetOption(key, value) req := types.ToRequestSetOption(params)
res, err := cli.client.SetOption(context.Background(), req.GetSetOption(), grpc.FailFast(true)) res, err := cli.client.SetOption(context.Background(), req.GetSetOption(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.StopForError(err) cli.StopForError(err)
@ -213,8 +213,8 @@ func (cli *grpcClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes {
return cli.finishAsyncCall(req, &types.Response{&types.Response_BeginBlock{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_BeginBlock{res}})
} }
func (cli *grpcClient) EndBlockAsync(height uint64) *ReqRes { func (cli *grpcClient) EndBlockAsync(params types.RequestEndBlock) *ReqRes {
req := types.ToRequestEndBlock(height) req := types.ToRequestEndBlock(params)
res, err := cli.client.EndBlock(context.Background(), req.GetEndBlock(), grpc.FailFast(true)) res, err := cli.client.EndBlock(context.Background(), req.GetEndBlock(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.StopForError(err) cli.StopForError(err)
@ -260,12 +260,9 @@ func (cli *grpcClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, err
return reqres.Response.GetInfo(), cli.Error() return reqres.Response.GetInfo(), cli.Error()
} }
func (cli *grpcClient) SetOptionSync(key string, value string) (log string, err error) { func (cli *grpcClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
reqres := cli.SetOptionAsync(key, value) reqres := cli.SetOptionAsync(req)
if err := cli.Error(); err != nil { return reqres.Response.GetSetOption(), cli.Error()
return "", err
}
return reqres.Response.GetSetOption().Log, nil
} }
func (cli *grpcClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) { func (cli *grpcClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) {
@ -288,17 +285,17 @@ func (cli *grpcClient) CommitSync() (*types.ResponseCommit, error) {
return reqres.Response.GetCommit(), cli.Error() return reqres.Response.GetCommit(), cli.Error()
} }
func (cli *grpcClient) InitChainSync(params types.RequestInitChain) error { func (cli *grpcClient) InitChainSync(params types.RequestInitChain) (*types.ResponseInitChain, error) {
cli.InitChainAsync(params) reqres := cli.InitChainAsync(params)
return cli.Error() return reqres.Response.GetInitChain(), cli.Error()
} }
func (cli *grpcClient) BeginBlockSync(params types.RequestBeginBlock) error { func (cli *grpcClient) BeginBlockSync(params types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
cli.BeginBlockAsync(params) reqres := cli.BeginBlockAsync(params)
return cli.Error() return reqres.Response.GetBeginBlock(), cli.Error()
} }
func (cli *grpcClient) EndBlockSync(height uint64) (*types.ResponseEndBlock, error) { func (cli *grpcClient) EndBlockSync(params types.RequestEndBlock) (*types.ResponseEndBlock, error) {
reqres := cli.EndBlockAsync(height) reqres := cli.EndBlockAsync(params)
return reqres.Response.GetEndBlock(), cli.Error() return reqres.Response.GetEndBlock(), cli.Error()
} }

View File

@ -53,21 +53,21 @@ func (app *localClient) EchoAsync(msg string) *ReqRes {
func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes { func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes {
app.mtx.Lock() app.mtx.Lock()
resInfo := app.Application.Info(req) res := app.Application.Info(req)
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestInfo(req), types.ToRequestInfo(req),
types.ToResponseInfo(resInfo), types.ToResponseInfo(res),
) )
} }
func (app *localClient) SetOptionAsync(key string, value string) *ReqRes { func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
app.mtx.Lock() app.mtx.Lock()
log := app.Application.SetOption(key, value) res := app.Application.SetOption(req)
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestSetOption(key, value), types.ToRequestSetOption(req),
types.ToResponseSetOption(log), types.ToResponseSetOption(res),
) )
} }
@ -77,7 +77,7 @@ func (app *localClient) DeliverTxAsync(tx []byte) *ReqRes {
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestDeliverTx(tx), types.ToRequestDeliverTx(tx),
types.ToResponseDeliverTx(res.Code, res.Data, res.Log, res.Tags), types.ToResponseDeliverTx(res),
) )
} }
@ -87,17 +87,17 @@ func (app *localClient) CheckTxAsync(tx []byte) *ReqRes {
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestCheckTx(tx), types.ToRequestCheckTx(tx),
types.ToResponseCheckTx(res.Code, res.Data, res.Log), types.ToResponseCheckTx(res),
) )
} }
func (app *localClient) QueryAsync(reqQuery types.RequestQuery) *ReqRes { func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes {
app.mtx.Lock() app.mtx.Lock()
resQuery := app.Application.Query(reqQuery) res := app.Application.Query(req)
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestQuery(reqQuery), types.ToRequestQuery(req),
types.ToResponseQuery(resQuery), types.ToResponseQuery(res),
) )
} }
@ -107,38 +107,38 @@ func (app *localClient) CommitAsync() *ReqRes {
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestCommit(), types.ToRequestCommit(),
types.ToResponseCommit(res.Code, res.Data, res.Log), types.ToResponseCommit(res),
) )
} }
func (app *localClient) InitChainAsync(params types.RequestInitChain) *ReqRes { func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
app.mtx.Lock() app.mtx.Lock()
app.Application.InitChain(params) res := app.Application.InitChain(req)
reqRes := app.callback( reqRes := app.callback(
types.ToRequestInitChain(params), types.ToRequestInitChain(req),
types.ToResponseInitChain(), types.ToResponseInitChain(res),
) )
app.mtx.Unlock() app.mtx.Unlock()
return reqRes return reqRes
} }
func (app *localClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes { func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
app.mtx.Lock() app.mtx.Lock()
app.Application.BeginBlock(params) res := app.Application.BeginBlock(req)
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestBeginBlock(params), types.ToRequestBeginBlock(req),
types.ToResponseBeginBlock(), types.ToResponseBeginBlock(res),
) )
} }
func (app *localClient) EndBlockAsync(height uint64) *ReqRes { func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
app.mtx.Lock() app.mtx.Lock()
resEndBlock := app.Application.EndBlock(height) res := app.Application.EndBlock(req)
app.mtx.Unlock() app.mtx.Unlock()
return app.callback( return app.callback(
types.ToRequestEndBlock(height), types.ToRequestEndBlock(req),
types.ToResponseEndBlock(resEndBlock), types.ToResponseEndBlock(res),
) )
} }
@ -159,11 +159,11 @@ func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, er
return &res, nil return &res, nil
} }
func (app *localClient) SetOptionSync(key string, value string) (log string, err error) { func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
app.mtx.Lock() app.mtx.Lock()
log = app.Application.SetOption(key, value) res := app.Application.SetOption(req)
app.mtx.Unlock() app.mtx.Unlock()
return log, nil return &res, nil
} }
func (app *localClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) { func (app *localClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) {
@ -194,23 +194,23 @@ func (app *localClient) CommitSync() (*types.ResponseCommit, error) {
return &res, nil return &res, nil
} }
func (app *localClient) InitChainSync(params types.RequestInitChain) error { func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
app.mtx.Lock() app.mtx.Lock()
app.Application.InitChain(params) res := app.Application.InitChain(req)
app.mtx.Unlock() app.mtx.Unlock()
return nil return &res, nil
} }
func (app *localClient) BeginBlockSync(params types.RequestBeginBlock) error { func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
app.mtx.Lock() app.mtx.Lock()
app.Application.BeginBlock(params) res := app.Application.BeginBlock(req)
app.mtx.Unlock() app.mtx.Unlock()
return nil return &res, nil
} }
func (app *localClient) EndBlockSync(height uint64) (*types.ResponseEndBlock, error) { func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
app.mtx.Lock() app.mtx.Lock()
res := app.Application.EndBlock(height) res := app.Application.EndBlock(req)
app.mtx.Unlock() app.mtx.Unlock()
return &res, nil return &res, nil
} }

View File

@ -239,8 +239,8 @@ func (cli *socketClient) InfoAsync(req types.RequestInfo) *ReqRes {
return cli.queueRequest(types.ToRequestInfo(req)) return cli.queueRequest(types.ToRequestInfo(req))
} }
func (cli *socketClient) SetOptionAsync(key string, value string) *ReqRes { func (cli *socketClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
return cli.queueRequest(types.ToRequestSetOption(key, value)) return cli.queueRequest(types.ToRequestSetOption(req))
} }
func (cli *socketClient) DeliverTxAsync(tx []byte) *ReqRes { func (cli *socketClient) DeliverTxAsync(tx []byte) *ReqRes {
@ -259,16 +259,16 @@ func (cli *socketClient) CommitAsync() *ReqRes {
return cli.queueRequest(types.ToRequestCommit()) return cli.queueRequest(types.ToRequestCommit())
} }
func (cli *socketClient) InitChainAsync(params types.RequestInitChain) *ReqRes { func (cli *socketClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
return cli.queueRequest(types.ToRequestInitChain(params)) return cli.queueRequest(types.ToRequestInitChain(req))
} }
func (cli *socketClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes { func (cli *socketClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
return cli.queueRequest(types.ToRequestBeginBlock(params)) return cli.queueRequest(types.ToRequestBeginBlock(req))
} }
func (cli *socketClient) EndBlockAsync(height uint64) *ReqRes { func (cli *socketClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
return cli.queueRequest(types.ToRequestEndBlock(height)) return cli.queueRequest(types.ToRequestEndBlock(req))
} }
//---------------------------------------- //----------------------------------------
@ -294,13 +294,10 @@ func (cli *socketClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, e
return reqres.Response.GetInfo(), cli.Error() return reqres.Response.GetInfo(), cli.Error()
} }
func (cli *socketClient) SetOptionSync(key string, value string) (log string, err error) { func (cli *socketClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
reqres := cli.queueRequest(types.ToRequestSetOption(key, value)) reqres := cli.queueRequest(types.ToRequestSetOption(req))
cli.FlushSync() cli.FlushSync()
if err := cli.Error(); err != nil { return reqres.Response.GetSetOption(), cli.Error()
return "", err
}
return reqres.Response.GetSetOption().Log, nil
} }
func (cli *socketClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) { func (cli *socketClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) {
@ -327,20 +324,20 @@ func (cli *socketClient) CommitSync() (*types.ResponseCommit, error) {
return reqres.Response.GetCommit(), cli.Error() return reqres.Response.GetCommit(), cli.Error()
} }
func (cli *socketClient) InitChainSync(params types.RequestInitChain) error { func (cli *socketClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
cli.queueRequest(types.ToRequestInitChain(params)) reqres := cli.queueRequest(types.ToRequestInitChain(req))
cli.FlushSync() cli.FlushSync()
return cli.Error() return reqres.Response.GetInitChain(), cli.Error()
} }
func (cli *socketClient) BeginBlockSync(params types.RequestBeginBlock) error { func (cli *socketClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
cli.queueRequest(types.ToRequestBeginBlock(params)) reqres := cli.queueRequest(types.ToRequestBeginBlock(req))
cli.FlushSync() cli.FlushSync()
return cli.Error() return reqres.Response.GetBeginBlock(), cli.Error()
} }
func (cli *socketClient) EndBlockSync(height uint64) (*types.ResponseEndBlock, error) { func (cli *socketClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
reqres := cli.queueRequest(types.ToRequestEndBlock(height)) reqres := cli.queueRequest(types.ToRequestEndBlock(req))
cli.FlushSync() cli.FlushSync()
return reqres.Response.GetEndBlock(), cli.Error() return reqres.Response.GetEndBlock(), cli.Error()
} }

View File

@ -355,12 +355,13 @@ func cmdInfo(cmd *cobra.Command, args []string) error {
// Set an option on the application // Set an option on the application
func cmdSetOption(cmd *cobra.Command, args []string) error { func cmdSetOption(cmd *cobra.Command, args []string) error {
resLog, err := client.SetOptionSync(args[0], args[1]) key, val := args[0], args[1]
res, err := client.SetOptionSync(types.RequestSetOption{key, val})
if err != nil { if err != nil {
return err return err
} }
printResponse(cmd, args, response{ printResponse(cmd, args, response{
Log: resLog, Log: res.Log,
}) })
return nil return nil
} }

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
}

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)
}
}

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)} 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" { if key == "serial" && value == "on" {
app.serial = true app.serial = true
} }
return "" return types.ResponseSetOption{}
} }
func (app *CounterApplication) DeliverTx(tx []byte) types.ResponseDeliverTx { func (app *CounterApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {

View File

@ -10,6 +10,8 @@ import (
dbm "github.com/tendermint/tmlibs/db" dbm "github.com/tendermint/tmlibs/db"
) )
var _ types.Application = (*DummyApplication)(nil)
type DummyApplication struct { type DummyApplication struct {
types.BaseApplication types.BaseApplication

View File

@ -93,7 +93,7 @@ func TestPersistentDummyInfo(t *testing.T) {
Height: uint64(height), Height: uint64(height),
} }
dummy.BeginBlock(types.RequestBeginBlock{hash, header}) dummy.BeginBlock(types.RequestBeginBlock{hash, header})
dummy.EndBlock(height) dummy.EndBlock(types.RequestEndBlock{header.Height})
dummy.Commit() dummy.Commit()
resInfo = dummy.Info(types.RequestInfo{}) resInfo = dummy.Info(types.RequestInfo{})
@ -182,7 +182,7 @@ func makeApplyBlock(t *testing.T, dummy types.Application, heightInt int, diff [
t.Fatal(r) t.Fatal(r)
} }
} }
resEndBlock := dummy.EndBlock(height) resEndBlock := dummy.EndBlock(types.RequestEndBlock{header.Height})
dummy.Commit() dummy.Commit()
valsEqual(t, diff, resEndBlock.Diffs) valsEqual(t, diff, resEndBlock.Diffs)

View File

@ -21,6 +21,8 @@ const (
//----------------------------------------- //-----------------------------------------
var _ types.Application = (*PersistentDummyApplication)(nil)
type PersistentDummyApplication struct { type PersistentDummyApplication struct {
app *DummyApplication app *DummyApplication
@ -50,15 +52,15 @@ func (app *PersistentDummyApplication) SetLogger(l log.Logger) {
app.logger = l app.logger = l
} }
func (app *PersistentDummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) { func (app *PersistentDummyApplication) Info(req types.RequestInfo) types.ResponseInfo {
resInfo = app.app.Info(req) res := app.app.Info(req)
resInfo.LastBlockHeight = app.app.state.LatestVersion() res.LastBlockHeight = app.app.state.LatestVersion()
resInfo.LastBlockAppHash = app.app.state.Hash() res.LastBlockAppHash = app.app.state.Hash()
return resInfo return res
} }
func (app *PersistentDummyApplication) SetOption(key string, value string) (log string) { func (app *PersistentDummyApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
return app.app.SetOption(key, value) 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
@ -102,23 +104,25 @@ func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.
} }
// Save the validators in the merkle tree // Save the validators in the merkle tree
func (app *PersistentDummyApplication) InitChain(params types.RequestInitChain) { func (app *PersistentDummyApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
for _, v := range params.Validators { for _, v := range req.Validators {
r := app.updateValidator(v) r := app.updateValidator(v)
if r.IsErr() { if r.IsErr() {
app.logger.Error("Error updating validators", "r", r) app.logger.Error("Error updating validators", "r", r)
} }
} }
return types.ResponseInitChain{}
} }
// Track the block hash and header information // 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 // reset valset changes
app.changes = make([]*types.Validator, 0) app.changes = make([]*types.Validator, 0)
return types.ResponseBeginBlock{}
} }
// Update the validator set // 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} return types.ResponseEndBlock{Diffs: app.changes}
} }

View File

@ -168,33 +168,32 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types
case *types.Request_Flush: case *types.Request_Flush:
responses <- types.ToResponseFlush() responses <- types.ToResponseFlush()
case *types.Request_Info: case *types.Request_Info:
resInfo := s.app.Info(*r.Info) res := s.app.Info(*r.Info)
responses <- types.ToResponseInfo(resInfo) responses <- types.ToResponseInfo(res)
case *types.Request_SetOption: case *types.Request_SetOption:
so := r.SetOption res := s.app.SetOption(*r.SetOption)
logStr := s.app.SetOption(so.Key, so.Value) responses <- types.ToResponseSetOption(res)
responses <- types.ToResponseSetOption(logStr)
case *types.Request_DeliverTx: case *types.Request_DeliverTx:
res := s.app.DeliverTx(r.DeliverTx.Tx) res := s.app.DeliverTx(r.DeliverTx.Tx)
responses <- types.ToResponseDeliverTx(res.Code, res.Data, res.Log, res.Tags) responses <- types.ToResponseDeliverTx(res)
case *types.Request_CheckTx: case *types.Request_CheckTx:
res := s.app.CheckTx(r.CheckTx.Tx) res := s.app.CheckTx(r.CheckTx.Tx)
responses <- types.ToResponseCheckTx(res.Code, res.Data, res.Log) responses <- types.ToResponseCheckTx(res)
case *types.Request_Commit: case *types.Request_Commit:
res := s.app.Commit() res := s.app.Commit()
responses <- types.ToResponseCommit(res.Code, res.Data, res.Log) responses <- types.ToResponseCommit(res)
case *types.Request_Query: case *types.Request_Query:
resQuery := s.app.Query(*r.Query) res := s.app.Query(*r.Query)
responses <- types.ToResponseQuery(resQuery) responses <- types.ToResponseQuery(res)
case *types.Request_InitChain: case *types.Request_InitChain:
s.app.InitChain(*r.InitChain) res := s.app.InitChain(*r.InitChain)
responses <- types.ToResponseInitChain() responses <- types.ToResponseInitChain(res)
case *types.Request_BeginBlock: case *types.Request_BeginBlock:
s.app.BeginBlock(*r.BeginBlock) res := s.app.BeginBlock(*r.BeginBlock)
responses <- types.ToResponseBeginBlock() responses <- types.ToResponseBeginBlock(res)
case *types.Request_EndBlock: case *types.Request_EndBlock:
resEndBlock := s.app.EndBlock(r.EndBlock.Height) res := s.app.EndBlock(*r.EndBlock)
responses <- types.ToResponseEndBlock(resEndBlock) responses <- types.ToResponseEndBlock(res)
default: default:
responses <- types.ToResponseException("Unknown request") responses <- types.ToResponseException("Unknown request")
} }

View File

@ -5,21 +5,23 @@ import (
) )
// Application is an interface that enables any finite, deterministic state machine // Application is an interface that enables any finite, deterministic state machine
// to be driven by a blockchain-based replication engine via the ABCI // to be driven by a blockchain-based replication engine via the ABCI.
// All methods take a RequestXxx argument and return a ResponseXxx argument,
// except CheckTx/DeliverTx, which take `tx []byte`, and `Commit`, which takes nothing.
type Application interface { type Application interface {
// Info/Query Connection // Info/Query Connection
Info(RequestInfo) ResponseInfo // Return application info Info(RequestInfo) ResponseInfo // Return application info
SetOption(key string, value string) (log string) // Set application option SetOption(RequestSetOption) ResponseSetOption // Set application option
Query(RequestQuery) ResponseQuery // Query for state Query(RequestQuery) ResponseQuery // Query for state
// Mempool Connection // Mempool Connection
CheckTx(tx []byte) ResponseCheckTx // Validate a tx for the mempool CheckTx(tx []byte) ResponseCheckTx // Validate a tx for the mempool
// Consensus Connection // Consensus Connection
InitChain(RequestInitChain) // Initialize blockchain with validators and other info from TendermintCore InitChain(RequestInitChain) ResponseInitChain // Initialize blockchain with validators and other info from TendermintCore
BeginBlock(RequestBeginBlock) // Signals the beginning of a block BeginBlock(RequestBeginBlock) ResponseBeginBlock // Signals the beginning of a block
DeliverTx(tx []byte) ResponseDeliverTx // Deliver a tx for full processing DeliverTx(tx []byte) ResponseDeliverTx // Deliver a tx for full processing
EndBlock(height uint64) ResponseEndBlock // Signals the end of a block, returns changes to the validator set EndBlock(RequestEndBlock) ResponseEndBlock // Signals the end of a block, returns changes to the validator set
Commit() ResponseCommit // Commit the state and return the application Merkle root hash Commit() ResponseCommit // Commit the state and return the application Merkle root hash
} }
@ -48,7 +50,8 @@ func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*Respon
} }
func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) { func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
return &ResponseSetOption{app.app.SetOption(req.Key, req.Value)}, nil resSetOption := app.app.SetOption(*req)
return &resSetOption, nil
} }
func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) { func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
@ -72,16 +75,16 @@ func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*Re
} }
func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) { func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
app.app.InitChain(*req) resInitChain := app.app.InitChain(*req)
return &ResponseInitChain{}, nil // NOTE: empty return return &resInitChain, nil
} }
func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) { func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
app.app.BeginBlock(*req) resBeginBlock := app.app.BeginBlock(*req)
return &ResponseBeginBlock{}, nil // NOTE: empty return return &resBeginBlock, nil
} }
func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) { func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
resEndBlock := app.app.EndBlock(req.Height) resEndBlock := app.app.EndBlock(*req)
return &resEndBlock, nil return &resEndBlock, nil
} }

View File

@ -11,8 +11,8 @@ func (BaseApplication) Info(req RequestInfo) ResponseInfo {
return ResponseInfo{} return ResponseInfo{}
} }
func (BaseApplication) SetOption(key string, value string) (log string) { func (BaseApplication) SetOption(req RequestSetOption) ResponseSetOption {
return "" return ResponseSetOption{}
} }
func (BaseApplication) DeliverTx(tx []byte) ResponseDeliverTx { func (BaseApplication) DeliverTx(tx []byte) ResponseDeliverTx {
@ -31,12 +31,14 @@ func (BaseApplication) Query(req RequestQuery) ResponseQuery {
return ResponseQuery{Code: CodeType_OK} return ResponseQuery{Code: CodeType_OK}
} }
func (BaseApplication) InitChain(req RequestInitChain) { func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
return ResponseInitChain{}
} }
func (BaseApplication) BeginBlock(req RequestBeginBlock) { func (BaseApplication) BeginBlock(req RequestBeginBlock) ResponseBeginBlock {
return ResponseBeginBlock{}
} }
func (BaseApplication) EndBlock(height uint64) ResponseEndBlock { func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
return ResponseEndBlock{} return ResponseEndBlock{}
} }

View File

@ -25,21 +25,21 @@ func ToRequestInfo(req RequestInfo) *Request {
} }
} }
func ToRequestSetOption(key string, value string) *Request { func ToRequestSetOption(req RequestSetOption) *Request {
return &Request{ return &Request{
Value: &Request_SetOption{&RequestSetOption{key, value}}, Value: &Request_SetOption{&req},
} }
} }
func ToRequestDeliverTx(txBytes []byte) *Request { func ToRequestDeliverTx(tx []byte) *Request {
return &Request{ return &Request{
Value: &Request_DeliverTx{&RequestDeliverTx{txBytes}}, Value: &Request_DeliverTx{&RequestDeliverTx{tx}},
} }
} }
func ToRequestCheckTx(txBytes []byte) *Request { func ToRequestCheckTx(tx []byte) *Request {
return &Request{ return &Request{
Value: &Request_CheckTx{&RequestCheckTx{txBytes}}, Value: &Request_CheckTx{&RequestCheckTx{tx}},
} }
} }
@ -67,9 +67,9 @@ func ToRequestBeginBlock(req RequestBeginBlock) *Request {
} }
} }
func ToRequestEndBlock(height uint64) *Request { func ToRequestEndBlock(req RequestEndBlock) *Request {
return &Request{ return &Request{
Value: &Request_EndBlock{&RequestEndBlock{height}}, Value: &Request_EndBlock{&req},
} }
} }
@ -93,57 +93,57 @@ func ToResponseFlush() *Response {
} }
} }
func ToResponseInfo(resInfo ResponseInfo) *Response { func ToResponseInfo(res ResponseInfo) *Response {
return &Response{ return &Response{
Value: &Response_Info{&resInfo}, Value: &Response_Info{&res},
} }
} }
func ToResponseSetOption(log string) *Response { func ToResponseSetOption(res ResponseSetOption) *Response {
return &Response{ return &Response{
Value: &Response_SetOption{&ResponseSetOption{log}}, Value: &Response_SetOption{&res},
} }
} }
func ToResponseDeliverTx(code CodeType, data []byte, log string, tags []*KVPair) *Response { func ToResponseDeliverTx(res ResponseDeliverTx) *Response {
return &Response{ return &Response{
Value: &Response_DeliverTx{&ResponseDeliverTx{code, data, log, tags}}, Value: &Response_DeliverTx{&res},
} }
} }
func ToResponseCheckTx(code CodeType, data []byte, log string) *Response { func ToResponseCheckTx(res ResponseCheckTx) *Response {
return &Response{ return &Response{
Value: &Response_CheckTx{&ResponseCheckTx{code, data, log}}, Value: &Response_CheckTx{&res},
} }
} }
func ToResponseCommit(code CodeType, data []byte, log string) *Response { func ToResponseCommit(res ResponseCommit) *Response {
return &Response{ return &Response{
Value: &Response_Commit{&ResponseCommit{code, data, log}}, Value: &Response_Commit{&res},
} }
} }
func ToResponseQuery(resQuery ResponseQuery) *Response { func ToResponseQuery(res ResponseQuery) *Response {
return &Response{ return &Response{
Value: &Response_Query{&resQuery}, Value: &Response_Query{&res},
} }
} }
func ToResponseInitChain() *Response { func ToResponseInitChain(res ResponseInitChain) *Response {
return &Response{ return &Response{
Value: &Response_InitChain{&ResponseInitChain{}}, Value: &Response_InitChain{&res},
} }
} }
func ToResponseBeginBlock() *Response { func ToResponseBeginBlock(res ResponseBeginBlock) *Response {
return &Response{ return &Response{
Value: &Response_BeginBlock{&ResponseBeginBlock{}}, Value: &Response_BeginBlock{&res},
} }
} }
func ToResponseEndBlock(resEndBlock ResponseEndBlock) *Response { func ToResponseEndBlock(res ResponseEndBlock) *Response {
return &Response{ return &Response{
Value: &Response_EndBlock{&resEndBlock}, Value: &Response_EndBlock{&res},
} }
} }