From 87c2ee69fd40a46aff2e3c6e1cb825da1a3c3d5a Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 5 Jul 2021 16:58:31 +0200 Subject: [PATCH] fix errors in abci --- abci/example/example_test.go | 30 ++++++------ abci/example/kvstore/kvstore.go | 32 ------------- abci/example/kvstore/kvstore_test.go | 3 +- abci/example/kvstore/persistent_kvstore.go | 55 +++++----------------- rpc/client/mock/abci.go | 7 +-- 5 files changed, 33 insertions(+), 94 deletions(-) diff --git a/abci/example/example_test.go b/abci/example/example_test.go index 01f12ead8..0a170e1b8 100644 --- a/abci/example/example_test.go +++ b/abci/example/example_test.go @@ -76,20 +76,22 @@ func testStream(t *testing.T, app types.Application) { client.SetResponseCallback(func(req *types.Request, res *types.Response) { // Process response switch r := res.Value.(type) { - case *types.Response_DeliverTx: - counter++ - if r.DeliverTx.Code != code.CodeTypeOK { - t.Error("DeliverTx failed with ret_code", r.DeliverTx.Code) - } - if counter > numDeliverTxs { - t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs) - } - if counter == numDeliverTxs { - go func() { - time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow - close(done) - }() - return + case *types.Response_FinalizeBlock: + for _, tx := range r.FinalizeBlock.Txs { + counter++ + if tx.Code != code.CodeTypeOK { + t.Error("DeliverTx failed with ret_code", tx.Code) + } + if counter > numDeliverTxs { + t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs) + } + if counter == numDeliverTxs { + go func() { + time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow + close(done) + }() + return + } } case *types.Response_Flush: // ignore diff --git a/abci/example/kvstore/kvstore.go b/abci/example/kvstore/kvstore.go index 3cf9c5413..44f790603 100644 --- a/abci/example/kvstore/kvstore.go +++ b/abci/example/kvstore/kvstore.go @@ -85,38 +85,6 @@ func (app *Application) Info(req types.RequestInfo) (resInfo types.ResponseInfo) } } -// tx is either "key=value" or just arbitrary bytes -func (app *Application) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx { - var key, value string - - parts := bytes.Split(req.Tx, []byte("=")) - if len(parts) == 2 { - key, value = string(parts[0]), string(parts[1]) - } else { - key, value = string(req.Tx), string(req.Tx) - } - - err := app.state.db.Set(prefixKey([]byte(key)), []byte(value)) - if err != nil { - panic(err) - } - app.state.Size++ - - events := []types.Event{ - { - Type: "app", - Attributes: []types.EventAttribute{ - {Key: "creator", Value: "Cosmoshi Netowoko", Index: true}, - {Key: "key", Value: key, Index: true}, - {Key: "index_key", Value: "index is working", Index: true}, - {Key: "noindex_key", Value: "index is working", Index: false}, - }, - }, - } - - return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events} -} - // tx is either "key=value" or just arbitrary bytes func (app *Application) FinalizeBlock(req types.RequestFinalizeBlock) types.ResponseFinalizeBlock { var key, value string diff --git a/abci/example/kvstore/kvstore_test.go b/abci/example/kvstore/kvstore_test.go index 08b1ee785..fc818e0bf 100644 --- a/abci/example/kvstore/kvstore_test.go +++ b/abci/example/kvstore/kvstore_test.go @@ -113,8 +113,7 @@ func TestPersistentKVStoreInfo(t *testing.T) { header := tmproto.Header{ Height: height, } - kvstore.BeginBlock(types.RequestBeginBlock{Hash: hash, Header: header}) - kvstore.EndBlock(types.RequestEndBlock{Height: header.Height}) + kvstore.FinalizeBlock(types.RequestFinalizeBlock{Hash: hash, Header: header}) kvstore.Commit() resInfo = kvstore.Info(types.RequestInfo{}) diff --git a/abci/example/kvstore/persistent_kvstore.go b/abci/example/kvstore/persistent_kvstore.go index a935255d7..a0cc8df87 100644 --- a/abci/example/kvstore/persistent_kvstore.go +++ b/abci/example/kvstore/persistent_kvstore.go @@ -66,19 +66,19 @@ func (app *PersistentKVStoreApplication) Info(req types.RequestInfo) types.Respo return res } -// tx is either "val:pubkey!power" or "key=value" or just arbitrary bytes -func (app *PersistentKVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx { - // if it starts with "val:", update the validator set - // format is "val:pubkey!power" - if isValidatorTx(req.Tx) { - // update validators in the merkle tree - // and in app.ValUpdates - return app.execValidatorTx(req.Tx) - } +// // tx is either "val:pubkey!power" or "key=value" or just arbitrary bytes +// func (app *PersistentKVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx { +// // if it starts with "val:", update the validator set +// // format is "val:pubkey!power" +// if isValidatorTx(req.Tx) { +// // update validators in the merkle tree +// // and in app.ValUpdates +// return app.execValidatorTx(req.Tx) +// } - // otherwise, update the key-value store - return app.app.DeliverTx(req) -} +// // otherwise, update the key-value store +// return app.app.DeliverTx(req) +// } func (app *PersistentKVStoreApplication) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx { return app.app.CheckTx(req) @@ -119,37 +119,6 @@ func (app *PersistentKVStoreApplication) InitChain(req types.RequestInitChain) t return types.ResponseInitChain{} } -// Track the block hash and header information -func (app *PersistentKVStoreApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock { - // reset valset changes - app.ValUpdates = make([]types.ValidatorUpdate, 0) - - // Punish validators who committed equivocation. - for _, ev := range req.ByzantineValidators { - if ev.Type == types.EvidenceType_DUPLICATE_VOTE { - addr := string(ev.Validator.Address) - if pubKey, ok := app.valAddrToPubKeyMap[addr]; ok { - app.updateValidator(types.ValidatorUpdate{ - PubKey: pubKey, - Power: ev.Validator.Power - 1, - }) - app.logger.Info("Decreased val power by 1 because of the equivocation", - "val", addr) - } else { - app.logger.Error("Wanted to punish val, but can't find it", - "val", addr) - } - } - } - - return types.ResponseBeginBlock{} -} - -// Update the validator set -func (app *PersistentKVStoreApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock { - return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates} -} - func (app *PersistentKVStoreApplication) ListSnapshots( req types.RequestListSnapshots) types.ResponseListSnapshots { return types.ResponseListSnapshots{} diff --git a/rpc/client/mock/abci.go b/rpc/client/mock/abci.go index 0737deec0..7ee2cfd03 100644 --- a/rpc/client/mock/abci.go +++ b/rpc/client/mock/abci.go @@ -55,7 +55,8 @@ func (a ABCIApp) BroadcastTxCommit(ctx context.Context, tx types.Tx) (*ctypes.Re if res.CheckTx.IsErr() { return &res, nil } - res.DeliverTx = a.App.DeliverTx(abci.RequestDeliverTx{Tx: tx}) + fb := a.App.FinalizeBlock(abci.RequestFinalizeBlock{Txs: [][]byte{tx}}) + res.DeliverTx = *fb.Txs[0] res.Height = -1 // TODO return &res, nil } @@ -64,7 +65,7 @@ func (a ABCIApp) BroadcastTxAsync(ctx context.Context, tx types.Tx) (*ctypes.Res c := a.App.CheckTx(abci.RequestCheckTx{Tx: tx}) // and this gets written in a background thread... if !c.IsErr() { - go func() { a.App.DeliverTx(abci.RequestDeliverTx{Tx: tx}) }() + go func() { a.App.FinalizeBlock(abci.RequestFinalizeBlock{Txs: [][]byte{tx}}) }() } return &ctypes.ResultBroadcastTx{ Code: c.Code, @@ -79,7 +80,7 @@ func (a ABCIApp) BroadcastTxSync(ctx context.Context, tx types.Tx) (*ctypes.Resu c := a.App.CheckTx(abci.RequestCheckTx{Tx: tx}) // and this gets written in a background thread... if !c.IsErr() { - go func() { a.App.DeliverTx(abci.RequestDeliverTx{Tx: tx}) }() + go func() { a.App.FinalizeBlock(abci.RequestFinalizeBlock{Txs: [][]byte{tx}}) }() } return &ctypes.ResultBroadcastTx{ Code: c.Code,