From 1fcd548cbee5186cffcdb5466bbc5dbb5b7e9597 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 28 Oct 2024 12:54:48 -0400 Subject: [PATCH] consensus: Ban empty v2 transactions --- consensus/state.go | 3 ++- consensus/validation.go | 4 +++- consensus/validation_test.go | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 4fcc42c..9353634 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -341,7 +341,8 @@ func (s State) V2TransactionWeight(txn types.V2Transaction) uint64 { for _, a := range txn.Attestations { a.EncodeTo(e) } - e.WriteBytes(txn.ArbitraryData) + e.Write(txn.ArbitraryData) + e.Flush() return uint64(wc.n) } diff --git a/consensus/validation.go b/consensus/validation.go index 0ba3a3d..369afa0 100644 --- a/consensus/validation.go +++ b/consensus/validation.go @@ -888,7 +888,9 @@ func ValidateV2Transaction(ms *MidState, txn types.V2Transaction) error { return errors.New("v2 transactions are not allowed until v2 hardfork begins") } else if err := validateV2CurrencyOverflow(ms, txn); err != nil { return err - } else if weight := ms.base.V2TransactionWeight(txn); weight > ms.base.MaxBlockWeight() { + } else if weight := ms.base.V2TransactionWeight(txn); weight == 0 { + return errors.New("transactions cannot be empty") + } else if weight > ms.base.MaxBlockWeight() { return fmt.Errorf("transaction exceeds maximum block weight (%v > %v)", weight, ms.base.MaxBlockWeight()) } else if err := validateV2Siacoins(ms, txn); err != nil { return err diff --git a/consensus/validation_test.go b/consensus/validation_test.go index b3d76b6..f23a6f8 100644 --- a/consensus/validation_test.go +++ b/consensus/validation_test.go @@ -988,6 +988,12 @@ func TestValidateV2Block(t *testing.T) { }) }, }, + { + "empty v2 transaction", + func(b *types.Block) { + b.V2.Transactions = append(b.V2.Transactions, types.V2Transaction{}) + }, + }, { "wrong parent ID", func(b *types.Block) {