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 <sergio@informal.systems>
(cherry picked from commit 49502dae92)

Co-authored-by: Lasaro Camargos <lasaro@informal.systems>
This commit is contained in:
mergify[bot]
2022-12-06 14:22:35 -03:00
committed by GitHub
parent 82c83d9750
commit 53ac7fcf14
4 changed files with 28 additions and 1 deletions

View File

@@ -8,4 +8,5 @@ const (
CodeTypeUnauthorized uint32 = 3
CodeTypeUnknownError uint32 = 4
CodeTypeExecuted uint32 = 5
CodeTypeRejected uint32 = 6
)

View File

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

View File

@@ -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 {

View File

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