From 53ac7fcf14834dd35de0d118d561c4814198f578 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 6 Dec 2022 14:22:35 -0300 Subject: [PATCH] Rejects empty transactions in the example kvstore (#9823) (#9841) * Rejects empty transactions in the example kvstore * Add code for rejected transaction; Add test for txn rejection; * Apply suggestions from code review Co-authored-by: Sergio Mena (cherry picked from commit 49502dae92047cd1fa8cf3b6b2ba348fb6de8da3) Co-authored-by: Lasaro Camargos --- abci/example/code/code.go | 1 + abci/example/kvstore/kvstore.go | 4 ++++ abci/example/kvstore/kvstore_test.go | 18 ++++++++++++++++++ abci/example/kvstore/persistent_kvstore.go | 6 +++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/abci/example/code/code.go b/abci/example/code/code.go index 6d011ed9d..660bac987 100644 --- a/abci/example/code/code.go +++ b/abci/example/code/code.go @@ -8,4 +8,5 @@ const ( CodeTypeUnauthorized uint32 = 3 CodeTypeUnknownError uint32 = 4 CodeTypeExecuted uint32 = 5 + CodeTypeRejected uint32 = 6 ) diff --git a/abci/example/kvstore/kvstore.go b/abci/example/kvstore/kvstore.go index e39291390..38aab62ca 100644 --- a/abci/example/kvstore/kvstore.go +++ b/abci/example/kvstore/kvstore.go @@ -122,6 +122,10 @@ func (app *Application) DeliverTx(req types.RequestDeliverTx) types.ResponseDeli } func (app *Application) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx { + if len(req.Tx) == 0 { + return types.ResponseCheckTx{Code: code.CodeTypeRejected} + } + if req.Type == types.CheckTxType_Recheck { if _, ok := app.txToRemove[string(req.Tx)]; ok { return types.ResponseCheckTx{Code: code.CodeTypeExecuted, GasWanted: 1} diff --git a/abci/example/kvstore/kvstore_test.go b/abci/example/kvstore/kvstore_test.go index 5d91f3699..77d6e933f 100644 --- a/abci/example/kvstore/kvstore_test.go +++ b/abci/example/kvstore/kvstore_test.go @@ -70,6 +70,24 @@ func TestKVStoreKV(t *testing.T) { testKVStore(t, kvstore, tx, key, value) } +func TestPersistentKVStoreEmptyTX(t *testing.T) { + dir, err := os.MkdirTemp("/tmp", "abci-kvstore-test") // TODO + if err != nil { + t.Fatal(err) + } + kvstore := NewPersistentKVStoreApplication(dir) + tx := []byte("") + reqCheck := types.RequestCheckTx{Tx: tx} + resCheck := kvstore.CheckTx(reqCheck) + require.Equal(t, resCheck.Code, code.CodeTypeRejected) + + txs := make([][]byte, 0, 4) + txs = append(txs, []byte("key=value"), []byte("key"), []byte(""), []byte("kee=value")) + reqPrepare := types.RequestPrepareProposal{Txs: txs, MaxTxBytes: 10 * 1024} + resPrepare := kvstore.PrepareProposal(reqPrepare) + require.Equal(t, len(reqPrepare.Txs), len(resPrepare.Txs)+1, "Empty transaction not properly removed") +} + func TestPersistentKVStoreKV(t *testing.T) { dir, err := os.MkdirTemp("/tmp", "abci-kvstore-test") // TODO if err != nil { diff --git a/abci/example/kvstore/persistent_kvstore.go b/abci/example/kvstore/persistent_kvstore.go index 500d4c5c9..8aa255b75 100644 --- a/abci/example/kvstore/persistent_kvstore.go +++ b/abci/example/kvstore/persistent_kvstore.go @@ -324,11 +324,15 @@ func (app *PersistentKVStoreApplication) execPrepareTx(tx []byte) types.Response } // substPrepareTx substitutes all the transactions prefixed with 'prepare' in the -// proposal for transactions with the prefix stripped. +// proposal for transactions with the prefix stripped, while discarding invalid empty transactions. func (app *PersistentKVStoreApplication) substPrepareTx(blockData [][]byte, maxTxBytes int64) [][]byte { txs := make([][]byte, 0, len(blockData)) var totalBytes int64 for _, tx := range blockData { + if len(tx) == 0 { + continue + } + txMod := tx if isPrepareTx(tx) { txMod = bytes.Replace(tx, []byte(PreparePrefix), []byte(ReplacePrefix), 1)