Browse Source

adapt Tendermint to new abci.Client interface

which was introduced in https://github.com/tendermint/abci/pull/130
pull/835/head
Anton Kaliaev 7 years ago
parent
commit
f65e357d2b
No known key found for this signature in database GPG Key ID: 7B6881D965918214
16 changed files with 94 additions and 75 deletions
  1. +24
    -15
      consensus/mempool_test.go
  2. +5
    -10
      consensus/replay.go
  3. +1
    -1
      glide.lock
  4. +1
    -1
      glide.yaml
  5. +9
    -3
      mempool/mempool_test.go
  6. +14
    -14
      proxy/app_conn.go
  7. +2
    -2
      proxy/app_conn_test.go
  8. +3
    -3
      rpc/client/mock/abci.go
  9. +2
    -2
      rpc/client/mock/abci_test.go
  10. +1
    -1
      rpc/core/abci.go
  11. +7
    -7
      rpc/core/mempool.go
  12. +1
    -1
      rpc/core/tx.go
  13. +9
    -9
      rpc/core/types/responses.go
  14. +10
    -2
      state/execution.go
  15. +1
    -1
      state/state.go
  16. +4
    -3
      state/state_test.go

+ 24
- 15
consensus/mempool_test.go View File

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

+ 5
- 10
consensus/replay.go View File

@ -385,22 +385,17 @@ type mockProxyApp struct {
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]
mock.txCount += 1
return abci.Result{
r.Code,
r.Data,
r.Log,
r.Tags,
}
return *r
}
func (mock *mockProxyApp) EndBlock(height uint64) abci.ResponseEndBlock {
mock.txCount = 0
return mock.abciResponses.EndBlock
return *mock.abciResponses.EndBlock
}
func (mock *mockProxyApp) Commit() abci.Result {
return abci.NewResultOK(mock.appHash, "")
func (mock *mockProxyApp) Commit() abci.ResponseCommit {
return abci.ResponseCommit{Code: abci.CodeType_OK, Data: mock.appHash}
}

+ 1
- 1
glide.lock View File

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


+ 1
- 1
glide.yaml View File

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


+ 9
- 3
mempool/mempool_test.go View File

@ -172,13 +172,19 @@ func TestSerialReap(t *testing.T) {
for i := start; i < end; i++ {
txBytes := make([]byte, 8)
binary.BigEndian.PutUint64(txBytes, uint64(i))
res := appConnCon.DeliverTxSync(txBytes)
if !res.IsOK() {
res, err := appConnCon.DeliverTxSync(txBytes)
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",
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 {
t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log)
}


+ 14
- 14
proxy/app_conn.go View File

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

+ 2
- 2
proxy/app_conn_test.go View File

@ -17,7 +17,7 @@ import (
type AppConnTest interface {
EchoAsync(string) *abcicli.ReqRes
FlushSync() error
InfoSync(types.RequestInfo) (types.ResponseInfo, error)
InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
}
type appConnTest struct {
@ -36,7 +36,7 @@ func (app *appConnTest) FlushSync() error {
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)
}


+ 3
- 3
rpc/client/mock/abci.go 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) {
res := ctypes.ResultBroadcastTxCommit{}
res.CheckTx = a.App.CheckTx(tx)
if !res.CheckTx.IsOK() {
if res.CheckTx.IsErr() {
return &res, nil
}
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) {
c := a.App.CheckTx(tx)
// and this gets written in a background thread...
if c.IsOK() {
if !c.IsErr() {
go func() { a.App.DeliverTx(tx) }() // nolint: errcheck
}
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) {
c := a.App.CheckTx(tx)
// and this gets written in a background thread...
if c.IsOK() {
if !c.IsErr() {
go func() { a.App.DeliverTx(tx) }() // nolint: errcheck
}
return &ctypes.ResultBroadcastTx{c.Code, c.Data, c.Log, tx.Hash()}, nil


+ 2
- 2
rpc/client/mock/abci_test.go View File

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


+ 1
- 1
rpc/core/abci.go View File

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

+ 7
- 7
rpc/core/mempool.go View File

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


+ 1
- 1
rpc/core/tx.go View File

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


+ 9
- 9
rpc/core/types/responses.go View File

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


+ 10
- 2
state/execution.go View File

@ -248,7 +248,11 @@ func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, bl
defer mempool.Unlock()
// 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() {
s.logger.Error("Error in proxyAppConn.CommitSync", "err", res)
return res
@ -275,7 +279,11 @@ func ExecCommitBlock(appConnConsensus proxy.AppConnConsensus, block *types.Block
return nil, err
}
// 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() {
logger.Error("Error in proxyAppConn.CommitSync", "err", res)
return nil, res


+ 1
- 1
state/state.go View File

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


+ 4
- 3
state/state_test.go View File

@ -80,7 +80,7 @@ func TestABCIResponsesSaveLoad(t *testing.T) {
abciResponses := NewABCIResponses(block)
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.EndBlock = abci.ResponseEndBlock{Diffs: []*abci.Validator{
abciResponses.EndBlock = &abci.ResponseEndBlock{Diffs: []*abci.Validator{
{
PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
Power: 10,
@ -198,12 +198,13 @@ func makeHeaderPartsResponses(state *State, height int,
block := makeBlock(height, state)
_, val := state.Validators.GetByIndex(0)
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 !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
abciResponses.EndBlock = abci.ResponseEndBlock{
abciResponses.EndBlock = &abci.ResponseEndBlock{
Diffs: []*abci.Validator{
{val.PubKey.Bytes(), 0},
{pubkey.Bytes(), 10},


Loading…
Cancel
Save