adapt Tendermint to new abci.Client interface

which was introduced in https://github.com/tendermint/abci/pull/130
This commit is contained in:
Anton Kaliaev 2017-11-22 18:55:09 -06:00
parent 4a31532897
commit f65e357d2b
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
16 changed files with 94 additions and 75 deletions

View File

@ -2,6 +2,7 @@ package consensus
import ( import (
"encoding/binary" "encoding/binary"
"fmt"
"testing" "testing"
"time" "time"
@ -188,33 +189,41 @@ func (app *CounterApplication) Info(req abci.RequestInfo) abci.ResponseInfo {
return abci.ResponseInfo{Data: cmn.Fmt("txs:%v", app.txCount)} return abci.ResponseInfo{Data: cmn.Fmt("txs:%v", app.txCount)}
} }
func (app *CounterApplication) DeliverTx(tx []byte) abci.Result { func (app *CounterApplication) DeliverTx(tx []byte) abci.ResponseDeliverTx {
return runTx(tx, &app.txCount) txValue := txAsUint64(tx)
if txValue != uint64(app.txCount) {
return abci.ResponseDeliverTx{
Code: abci.CodeType_BadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
}
app.txCount += 1
return abci.ResponseDeliverTx{Code: abci.CodeType_OK}
} }
func (app *CounterApplication) CheckTx(tx []byte) abci.Result { func (app *CounterApplication) CheckTx(tx []byte) abci.ResponseCheckTx {
return runTx(tx, &app.mempoolTxCount) txValue := txAsUint64(tx)
if txValue != uint64(app.mempoolTxCount) {
return abci.ResponseCheckTx{
Code: abci.CodeType_BadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.mempoolTxCount, txValue)}
}
app.mempoolTxCount += 1
return abci.ResponseCheckTx{Code: abci.CodeType_OK}
} }
func runTx(tx []byte, countPtr *int) abci.Result { func txAsUint64(tx []byte) uint64 {
count := *countPtr
tx8 := make([]byte, 8) tx8 := make([]byte, 8)
copy(tx8[len(tx8)-len(tx):], tx) copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8) return binary.BigEndian.Uint64(tx8)
if txValue != uint64(count) {
return abci.ErrBadNonce.AppendLog(cmn.Fmt("Invalid nonce. Expected %v, got %v", count, txValue))
}
*countPtr += 1
return abci.OK
} }
func (app *CounterApplication) Commit() abci.Result { func (app *CounterApplication) Commit() abci.ResponseCommit {
app.mempoolTxCount = app.txCount app.mempoolTxCount = app.txCount
if app.txCount == 0 { if app.txCount == 0 {
return abci.OK return abci.ResponseCommit{Code: abci.CodeType_OK}
} else { } else {
hash := make([]byte, 8) hash := make([]byte, 8)
binary.BigEndian.PutUint64(hash, uint64(app.txCount)) binary.BigEndian.PutUint64(hash, uint64(app.txCount))
return abci.NewResultOK(hash, "") return abci.ResponseCommit{Code: abci.CodeType_OK, Data: hash}
} }
} }

View File

@ -385,22 +385,17 @@ type mockProxyApp struct {
abciResponses *sm.ABCIResponses abciResponses *sm.ABCIResponses
} }
func (mock *mockProxyApp) DeliverTx(tx []byte) abci.Result { func (mock *mockProxyApp) DeliverTx(tx []byte) abci.ResponseDeliverTx {
r := mock.abciResponses.DeliverTx[mock.txCount] r := mock.abciResponses.DeliverTx[mock.txCount]
mock.txCount += 1 mock.txCount += 1
return abci.Result{ return *r
r.Code,
r.Data,
r.Log,
r.Tags,
}
} }
func (mock *mockProxyApp) EndBlock(height uint64) abci.ResponseEndBlock { func (mock *mockProxyApp) EndBlock(height uint64) abci.ResponseEndBlock {
mock.txCount = 0 mock.txCount = 0
return mock.abciResponses.EndBlock return *mock.abciResponses.EndBlock
} }
func (mock *mockProxyApp) Commit() abci.Result { func (mock *mockProxyApp) Commit() abci.ResponseCommit {
return abci.NewResultOK(mock.appHash, "") return abci.ResponseCommit{Code: abci.CodeType_OK, Data: mock.appHash}
} }

2
glide.lock generated
View File

@ -98,7 +98,7 @@ imports:
- leveldb/table - leveldb/table
- leveldb/util - leveldb/util
- name: github.com/tendermint/abci - name: github.com/tendermint/abci
version: 6b47155e08732f46dafdcef185d23f0ff9ff24a5 version: 2cfad8523a54d64271d7cbc69a39433eab918aa0
subpackages: subpackages:
- client - client
- example/counter - example/counter

View File

@ -18,7 +18,7 @@ import:
- package: github.com/spf13/viper - package: github.com/spf13/viper
version: v1.0.0 version: v1.0.0
- package: github.com/tendermint/abci - package: github.com/tendermint/abci
version: 6b47155e08732f46dafdcef185d23f0ff9ff24a5 version: 2cfad8523a54d64271d7cbc69a39433eab918aa0
subpackages: subpackages:
- client - client
- example/dummy - example/dummy

View File

@ -172,13 +172,19 @@ func TestSerialReap(t *testing.T) {
for i := start; i < end; i++ { for i := start; i < end; i++ {
txBytes := make([]byte, 8) txBytes := make([]byte, 8)
binary.BigEndian.PutUint64(txBytes, uint64(i)) binary.BigEndian.PutUint64(txBytes, uint64(i))
res := appConnCon.DeliverTxSync(txBytes) res, err := appConnCon.DeliverTxSync(txBytes)
if !res.IsOK() { if err != nil {
t.Errorf("Client error committing tx: %v", err)
}
if res.IsErr() {
t.Errorf("Error committing tx. Code:%v result:%X log:%v", t.Errorf("Error committing tx. Code:%v result:%X log:%v",
res.Code, res.Data, res.Log) res.Code, res.Data, res.Log)
} }
} }
res := appConnCon.CommitSync() res, err := appConnCon.CommitSync()
if err != nil {
t.Errorf("Client error committing: %v", err)
}
if len(res.Data) != 8 { if len(res.Data) != 8 {
t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log) t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log)
} }

View File

@ -12,12 +12,12 @@ type AppConnConsensus interface {
SetResponseCallback(abcicli.Callback) SetResponseCallback(abcicli.Callback)
Error() error Error() error
InitChainSync(types.RequestInitChain) (err error) InitChainSync(types.RequestInitChain) error
BeginBlockSync(types.RequestBeginBlock) (err error) BeginBlockSync(types.RequestBeginBlock) error
DeliverTxAsync(tx []byte) *abcicli.ReqRes DeliverTxAsync(tx []byte) *abcicli.ReqRes
EndBlockSync(height uint64) (types.ResponseEndBlock, error) EndBlockSync(height uint64) (*types.ResponseEndBlock, error)
CommitSync() (res types.Result) CommitSync() (*types.ResponseCommit, error)
} }
type AppConnMempool interface { type AppConnMempool interface {
@ -33,9 +33,9 @@ type AppConnMempool interface {
type AppConnQuery interface { type AppConnQuery interface {
Error() error Error() error
EchoSync(string) (res types.Result) EchoSync(string) (*types.ResponseEcho, error)
InfoSync(types.RequestInfo) (types.ResponseInfo, error) InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
QuerySync(types.RequestQuery) (types.ResponseQuery, error) QuerySync(types.RequestQuery) (*types.ResponseQuery, error)
// SetOptionSync(key string, value string) (res types.Result) // SetOptionSync(key string, value string) (res types.Result)
} }
@ -61,11 +61,11 @@ func (app *appConnConsensus) Error() error {
return app.appConn.Error() return app.appConn.Error()
} }
func (app *appConnConsensus) InitChainSync(req types.RequestInitChain) (err error) { func (app *appConnConsensus) InitChainSync(req types.RequestInitChain) error {
return app.appConn.InitChainSync(req) return app.appConn.InitChainSync(req)
} }
func (app *appConnConsensus) BeginBlockSync(req types.RequestBeginBlock) (err error) { func (app *appConnConsensus) BeginBlockSync(req types.RequestBeginBlock) error {
return app.appConn.BeginBlockSync(req) return app.appConn.BeginBlockSync(req)
} }
@ -73,11 +73,11 @@ func (app *appConnConsensus) DeliverTxAsync(tx []byte) *abcicli.ReqRes {
return app.appConn.DeliverTxAsync(tx) return app.appConn.DeliverTxAsync(tx)
} }
func (app *appConnConsensus) EndBlockSync(height uint64) (types.ResponseEndBlock, error) { func (app *appConnConsensus) EndBlockSync(height uint64) (*types.ResponseEndBlock, error) {
return app.appConn.EndBlockSync(height) return app.appConn.EndBlockSync(height)
} }
func (app *appConnConsensus) CommitSync() (res types.Result) { func (app *appConnConsensus) CommitSync() (*types.ResponseCommit, error) {
return app.appConn.CommitSync() return app.appConn.CommitSync()
} }
@ -131,14 +131,14 @@ func (app *appConnQuery) Error() error {
return app.appConn.Error() return app.appConn.Error()
} }
func (app *appConnQuery) EchoSync(msg string) (res types.Result) { func (app *appConnQuery) EchoSync(msg string) (*types.ResponseEcho, error) {
return app.appConn.EchoSync(msg) return app.appConn.EchoSync(msg)
} }
func (app *appConnQuery) InfoSync(req types.RequestInfo) (types.ResponseInfo, error) { func (app *appConnQuery) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
return app.appConn.InfoSync(req) return app.appConn.InfoSync(req)
} }
func (app *appConnQuery) QuerySync(reqQuery types.RequestQuery) (types.ResponseQuery, error) { func (app *appConnQuery) QuerySync(reqQuery types.RequestQuery) (*types.ResponseQuery, error) {
return app.appConn.QuerySync(reqQuery) return app.appConn.QuerySync(reqQuery)
} }

View File

@ -17,7 +17,7 @@ import (
type AppConnTest interface { type AppConnTest interface {
EchoAsync(string) *abcicli.ReqRes EchoAsync(string) *abcicli.ReqRes
FlushSync() error FlushSync() error
InfoSync(types.RequestInfo) (types.ResponseInfo, error) InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
} }
type appConnTest struct { type appConnTest struct {
@ -36,7 +36,7 @@ func (app *appConnTest) FlushSync() error {
return app.appConn.FlushSync() return app.appConn.FlushSync()
} }
func (app *appConnTest) InfoSync(req types.RequestInfo) (types.ResponseInfo, error) { func (app *appConnTest) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
return app.appConn.InfoSync(req) return app.appConn.InfoSync(req)
} }

View File

@ -38,7 +38,7 @@ func (a ABCIApp) ABCIQueryWithOptions(path string, data data.Bytes, opts client.
func (a ABCIApp) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { func (a ABCIApp) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
res := ctypes.ResultBroadcastTxCommit{} res := ctypes.ResultBroadcastTxCommit{}
res.CheckTx = a.App.CheckTx(tx) res.CheckTx = a.App.CheckTx(tx)
if !res.CheckTx.IsOK() { if res.CheckTx.IsErr() {
return &res, nil return &res, nil
} }
res.DeliverTx = a.App.DeliverTx(tx) res.DeliverTx = a.App.DeliverTx(tx)
@ -48,7 +48,7 @@ func (a ABCIApp) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit
func (a ABCIApp) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { func (a ABCIApp) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
c := a.App.CheckTx(tx) c := a.App.CheckTx(tx)
// and this gets written in a background thread... // and this gets written in a background thread...
if c.IsOK() { if !c.IsErr() {
go func() { a.App.DeliverTx(tx) }() // nolint: errcheck go func() { a.App.DeliverTx(tx) }() // nolint: errcheck
} }
return &ctypes.ResultBroadcastTx{c.Code, c.Data, c.Log, tx.Hash()}, nil return &ctypes.ResultBroadcastTx{c.Code, c.Data, c.Log, tx.Hash()}, nil
@ -57,7 +57,7 @@ func (a ABCIApp) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error
func (a ABCIApp) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { func (a ABCIApp) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
c := a.App.CheckTx(tx) c := a.App.CheckTx(tx)
// and this gets written in a background thread... // and this gets written in a background thread...
if c.IsOK() { if !c.IsErr() {
go func() { a.App.DeliverTx(tx) }() // nolint: errcheck go func() { a.App.DeliverTx(tx) }() // nolint: errcheck
} }
return &ctypes.ResultBroadcastTx{c.Code, c.Data, c.Log, tx.Hash()}, nil return &ctypes.ResultBroadcastTx{c.Code, c.Data, c.Log, tx.Hash()}, nil

View File

@ -37,8 +37,8 @@ func TestABCIMock(t *testing.T) {
BroadcastCommit: mock.Call{ BroadcastCommit: mock.Call{
Args: goodTx, Args: goodTx,
Response: &ctypes.ResultBroadcastTxCommit{ Response: &ctypes.ResultBroadcastTxCommit{
CheckTx: abci.Result{Data: data.Bytes("stand")}, CheckTx: abci.ResponseCheckTx{Data: data.Bytes("stand")},
DeliverTx: abci.Result{Data: data.Bytes("deliver")}, DeliverTx: abci.ResponseDeliverTx{Data: data.Bytes("deliver")},
}, },
Error: errors.New("bad tx"), Error: errors.New("bad tx"),
}, },

View File

@ -93,5 +93,5 @@ func ABCIInfo() (*ctypes.ResultABCIInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &ctypes.ResultABCIInfo{resInfo}, nil return &ctypes.ResultABCIInfo{*resInfo}, nil
} }

View File

@ -177,8 +177,8 @@ func BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
if checkTxR.Code != abci.CodeType_OK { if checkTxR.Code != abci.CodeType_OK {
// CheckTx failed! // CheckTx failed!
return &ctypes.ResultBroadcastTxCommit{ return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR.Result(), CheckTx: *checkTxR,
DeliverTx: abci.Result{}, DeliverTx: abci.ResponseDeliverTx{},
Hash: tx.Hash(), Hash: tx.Hash(),
}, nil }, nil
} }
@ -191,23 +191,23 @@ func BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
case deliverTxResMsg := <-deliverTxResCh: case deliverTxResMsg := <-deliverTxResCh:
deliverTxRes := deliverTxResMsg.(types.TMEventData).Unwrap().(types.EventDataTx) deliverTxRes := deliverTxResMsg.(types.TMEventData).Unwrap().(types.EventDataTx)
// The tx was included in a block. // The tx was included in a block.
deliverTxR := &abci.ResponseDeliverTx{ deliverTxR := abci.ResponseDeliverTx{
Code: deliverTxRes.Result.Code, Code: deliverTxRes.Result.Code,
Data: deliverTxRes.Result.Data, Data: deliverTxRes.Result.Data,
Log: deliverTxRes.Result.Log, Log: deliverTxRes.Result.Log,
} }
logger.Info("DeliverTx passed ", "tx", data.Bytes(tx), "response", deliverTxR) logger.Info("DeliverTx passed ", "tx", data.Bytes(tx), "response", deliverTxR)
return &ctypes.ResultBroadcastTxCommit{ return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR.Result(), CheckTx: *checkTxR,
DeliverTx: deliverTxR.Result(), DeliverTx: deliverTxR,
Hash: tx.Hash(), Hash: tx.Hash(),
Height: deliverTxRes.Height, Height: deliverTxRes.Height,
}, nil }, nil
case <-timer.C: case <-timer.C:
logger.Error("failed to include tx") logger.Error("failed to include tx")
return &ctypes.ResultBroadcastTxCommit{ return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR.Result(), CheckTx: *checkTxR,
DeliverTx: abci.Result{}, DeliverTx: abci.ResponseDeliverTx{},
Hash: tx.Hash(), Hash: tx.Hash(),
}, fmt.Errorf("Timed out waiting for transaction to be included in a block") }, fmt.Errorf("Timed out waiting for transaction to be included in a block")
} }

View File

@ -94,7 +94,7 @@ func Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) {
return &ctypes.ResultTx{ return &ctypes.ResultTx{
Height: height, Height: height,
Index: index, Index: index,
TxResult: r.Result.Result(), TxResult: r.Result,
Tx: r.Tx, Tx: r.Tx,
Proof: proof, Proof: proof,
}, nil }, nil

View File

@ -104,18 +104,18 @@ type ResultBroadcastTx struct {
} }
type ResultBroadcastTxCommit struct { type ResultBroadcastTxCommit struct {
CheckTx abci.Result `json:"check_tx"` CheckTx abci.ResponseCheckTx `json:"check_tx"`
DeliverTx abci.Result `json:"deliver_tx"` DeliverTx abci.ResponseDeliverTx `json:"deliver_tx"`
Hash data.Bytes `json:"hash"` Hash data.Bytes `json:"hash"`
Height uint64 `json:"height"` Height uint64 `json:"height"`
} }
type ResultTx struct { type ResultTx struct {
Height uint64 `json:"height"` Height uint64 `json:"height"`
Index uint32 `json:"index"` Index uint32 `json:"index"`
TxResult abci.Result `json:"tx_result"` TxResult abci.ResponseDeliverTx `json:"tx_result"`
Tx types.Tx `json:"tx"` Tx types.Tx `json:"tx"`
Proof types.TxProof `json:"proof,omitempty"` Proof types.TxProof `json:"proof,omitempty"`
} }
type ResultUnconfirmedTxs struct { type ResultUnconfirmedTxs struct {

View File

@ -248,7 +248,11 @@ func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, bl
defer mempool.Unlock() defer mempool.Unlock()
// Commit block, get hash back // Commit block, get hash back
res := proxyAppConn.CommitSync() res, err := proxyAppConn.CommitSync()
if err != nil {
s.logger.Error("Client error during proxyAppConn.CommitSync", "err", err)
return err
}
if res.IsErr() { if res.IsErr() {
s.logger.Error("Error in proxyAppConn.CommitSync", "err", res) s.logger.Error("Error in proxyAppConn.CommitSync", "err", res)
return res return res
@ -275,7 +279,11 @@ func ExecCommitBlock(appConnConsensus proxy.AppConnConsensus, block *types.Block
return nil, err return nil, err
} }
// Commit block, get hash back // Commit block, get hash back
res := appConnConsensus.CommitSync() res, err := appConnConsensus.CommitSync()
if err != nil {
logger.Error("Client error during proxyAppConn.CommitSync", "err", res)
return nil, err
}
if res.IsErr() { if res.IsErr() {
logger.Error("Error in proxyAppConn.CommitSync", "err", res) logger.Error("Error in proxyAppConn.CommitSync", "err", res)
return nil, res return nil, res

View File

@ -279,7 +279,7 @@ type ABCIResponses struct {
Height int Height int
DeliverTx []*abci.ResponseDeliverTx DeliverTx []*abci.ResponseDeliverTx
EndBlock abci.ResponseEndBlock EndBlock *abci.ResponseEndBlock
txs types.Txs // reference for indexing results by hash txs types.Txs // reference for indexing results by hash
} }

View File

@ -80,7 +80,7 @@ func TestABCIResponsesSaveLoad(t *testing.T) {
abciResponses := NewABCIResponses(block) abciResponses := NewABCIResponses(block)
abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Tags: []*abci.KVPair{}} abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Tags: []*abci.KVPair{}}
abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: []*abci.KVPair{}} abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: []*abci.KVPair{}}
abciResponses.EndBlock = abci.ResponseEndBlock{Diffs: []*abci.Validator{ abciResponses.EndBlock = &abci.ResponseEndBlock{Diffs: []*abci.Validator{
{ {
PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(), PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
Power: 10, Power: 10,
@ -198,12 +198,13 @@ func makeHeaderPartsResponses(state *State, height int,
block := makeBlock(height, state) block := makeBlock(height, state)
_, val := state.Validators.GetByIndex(0) _, val := state.Validators.GetByIndex(0)
abciResponses := &ABCIResponses{ abciResponses := &ABCIResponses{
Height: height, Height: height,
EndBlock: &abci.ResponseEndBlock{Diffs: []*abci.Validator{}},
} }
// if the pubkey is new, remove the old and add the new // if the pubkey is new, remove the old and add the new
if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) { if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
abciResponses.EndBlock = abci.ResponseEndBlock{ abciResponses.EndBlock = &abci.ResponseEndBlock{
Diffs: []*abci.Validator{ Diffs: []*abci.Validator{
{val.PubKey.Bytes(), 0}, {val.PubKey.Bytes(), 0},
{pubkey.Bytes(), 10}, {pubkey.Bytes(), 10},