Skip to content

Commit 92574e6

Browse files
authored
Anti-spam measurements against dust attack (#2223)
* BlockCandidateTransactions patch * Fix condition * Fix fee * Fix bug * Reject from mempool * Fix hasCoinbaseInput * Fix position * Bump version to v0.12.14
1 parent bd14202 commit 92574e6

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

domain/miningmanager/mempool/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
RejectDifficulty RejectCode = 0x44
5252
RejectImmatureSpend RejectCode = 0x45
5353
RejectBadOrphan RejectCode = 0x64
54+
RejectSpamTx RejectCode = 0x65
5455
)
5556

5657
// Map of reject codes back strings for pretty printing.

domain/miningmanager/mempool/mempool.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package mempool
22

33
import (
4+
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
5+
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
46
"sync"
57

68
"github.com/kaspanet/kaspad/domain/consensusreference"
@@ -141,7 +143,57 @@ func (mp *mempool) BlockCandidateTransactions() []*externalapi.DomainTransaction
141143
mp.mtx.RLock()
142144
defer mp.mtx.RUnlock()
143145

144-
return mp.transactionsPool.allReadyTransactions()
146+
readyTxs := mp.transactionsPool.allReadyTransactions()
147+
var candidateTxs []*externalapi.DomainTransaction
148+
var spamTx *externalapi.DomainTransaction
149+
var spamTxNewestUTXODaaScore uint64
150+
for _, tx := range readyTxs {
151+
if len(tx.Outputs) > len(tx.Inputs) {
152+
hasCoinbaseInput := false
153+
for _, input := range tx.Inputs {
154+
if input.UTXOEntry.IsCoinbase() {
155+
hasCoinbaseInput = true
156+
break
157+
}
158+
}
159+
160+
numExtraOuts := len(tx.Outputs) - len(tx.Inputs)
161+
if !hasCoinbaseInput && numExtraOuts > 2 && tx.Fee < uint64(numExtraOuts)*constants.SompiPerKaspa {
162+
log.Debugf("Filtered spam tx %s", consensushashing.TransactionID(tx))
163+
continue
164+
}
165+
166+
if hasCoinbaseInput || tx.Fee > uint64(numExtraOuts)*constants.SompiPerKaspa {
167+
candidateTxs = append(candidateTxs, tx)
168+
} else {
169+
txNewestUTXODaaScore := tx.Inputs[0].UTXOEntry.BlockDAAScore()
170+
for _, input := range tx.Inputs {
171+
if input.UTXOEntry.BlockDAAScore() > txNewestUTXODaaScore {
172+
txNewestUTXODaaScore = input.UTXOEntry.BlockDAAScore()
173+
}
174+
}
175+
176+
if spamTx != nil {
177+
if txNewestUTXODaaScore < spamTxNewestUTXODaaScore {
178+
spamTx = tx
179+
spamTxNewestUTXODaaScore = txNewestUTXODaaScore
180+
}
181+
} else {
182+
spamTx = tx
183+
spamTxNewestUTXODaaScore = txNewestUTXODaaScore
184+
}
185+
}
186+
} else {
187+
candidateTxs = append(candidateTxs, tx)
188+
}
189+
}
190+
191+
if spamTx != nil {
192+
log.Debugf("Adding spam tx candidate %s", consensushashing.TransactionID(spamTx))
193+
candidateTxs = append(candidateTxs, spamTx)
194+
}
195+
196+
return candidateTxs
145197
}
146198

147199
func (mp *mempool) RevalidateHighPriorityTransactions() (validTransactions []*externalapi.DomainTransaction, err error) {

domain/miningmanager/mempool/validate_and_insert_transaction.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package mempool
22

33
import (
44
"fmt"
5-
65
"github.com/kaspanet/kaspad/infrastructure/logger"
76

87
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"

domain/miningmanager/mempool/validate_transaction.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mempool
22

33
import (
44
"fmt"
5+
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
56

67
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
78
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
@@ -44,6 +45,20 @@ func (mp *mempool) validateTransactionInIsolation(transaction *externalapi.Domai
4445
}
4546

4647
func (mp *mempool) validateTransactionInContext(transaction *externalapi.DomainTransaction) error {
48+
hasCoinbaseInput := false
49+
for _, input := range transaction.Inputs {
50+
if input.UTXOEntry.IsCoinbase() {
51+
hasCoinbaseInput = true
52+
break
53+
}
54+
}
55+
56+
numExtraOuts := len(transaction.Outputs) - len(transaction.Inputs)
57+
if !hasCoinbaseInput && numExtraOuts > 2 && transaction.Fee < uint64(numExtraOuts)*constants.SompiPerKaspa {
58+
log.Warnf("Rejected spam tx %s from mempool (%d outputs)", consensushashing.TransactionID(transaction), len(transaction.Outputs))
59+
return transactionRuleError(RejectSpamTx, fmt.Sprintf("Rejected spam tx %s from mempool", consensushashing.TransactionID(transaction)))
60+
}
61+
4762
if !mp.config.AcceptNonStandard {
4863
err := mp.checkTransactionStandardInContext(transaction)
4964
if err != nil {

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
1111
const (
1212
appMajor uint = 0
1313
appMinor uint = 12
14-
appPatch uint = 13
14+
appPatch uint = 14
1515
)
1616

1717
// appBuild is defined as a variable so it can be overridden during the build

0 commit comments

Comments
 (0)