Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/main/scala/org/ergoplatform/nodeView/mempool/ErgoMemPool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ class ErgoMemPool private[mempool](private[mempool] val pool: OrderedTxPool,
}

val poolWithoutTx = removeTx(this, tx)
val doubleSpentTransactionIds = tx.inputs.flatMap(i =>
val doubleSpentTransactionIds: Set[ModifierId] = tx.inputs.flatMap(i =>
poolWithoutTx.pool.inputs.get(i.boxId)
).toSet
val doubleSpentTransactions = doubleSpentTransactionIds.flatMap { txId =>
poolWithoutTx.pool.orderedTransactions.get(txId)
poolWithoutTx.pool.currentTransaction(txId).map(_._2)
}
doubleSpentTransactions.foldLeft(poolWithoutTx) { case (pool, tx) =>
removeTx(pool, tx.transaction)
Expand Down Expand Up @@ -192,22 +192,32 @@ class ErgoMemPool private[mempool](private[mempool] val pool: OrderedTxPool,
validationStartTime: Long): (ErgoMemPool, ProcessingOutcome) = {
val tx = unconfirmedTransaction.transaction

val doubleSpendingWtxs = tx.inputs.flatMap { inp =>
val doubleSpendingTxIds: Set[ModifierId] = tx.inputs.flatMap { inp =>
pool.inputs.get(inp.boxId)
}.toSet

val feeF = feeFactor(unconfirmedTransaction)

if (doubleSpendingWtxs.nonEmpty) {
val doubleSpendingEntries = doubleSpendingTxIds.toSeq.flatMap(pool.currentTransaction)

if (doubleSpendingEntries.nonEmpty) {
val ownWtx = weighted(tx, feeF)
val doubleSpendingWtxs: Set[WeightedTxId] = doubleSpendingEntries.map(_._1).toSet
val doubleSpendingTotalWeight = doubleSpendingWtxs.map(_.weight).sum / doubleSpendingWtxs.size
if (ownWtx.weight > doubleSpendingTotalWeight) {
val doubleSpendingTxs = doubleSpendingWtxs.map(wtx => pool.orderedTransactions(wtx)).toSeq
val doubleSpendingTxs = doubleSpendingEntries.map(_._2).distinct
// Remove the losers FIRST so the shared `inputs`/`outputs` map entries are gone before the
// winning tx writes its own. With put-then-remove, the loser's `tx.inputs.map(_.boxId)` would
// delete the winner's just-written entry for the shared box, leaving the winner orphaned
// in the index and breaking subsequent double-spend detection.
val p = pool.remove(doubleSpendingTxs).put(unconfirmedTransaction, feeF)
val updPool = new ErgoMemPool(p, stats, sortingOption)
updPool -> new ProcessingOutcome.Accepted(unconfirmedTransaction, validationStartTime)
} else {
this -> new ProcessingOutcome.DoubleSpendingLoser(doubleSpendingWtxs.map(_.id), validationStartTime)
this -> new ProcessingOutcome.DoubleSpendingLoser(
doubleSpendingWtxs.map(_.id),
validationStartTime
)
}
} else {
val poolSizeLimit = nodeSettings.mempoolCapacity
Expand Down
Loading
Loading