Skip to content

Commit 118f128

Browse files
committed
scanInputBlock passing only id now, tests fixed
1 parent 33bf2b7 commit 118f128

6 files changed

Lines changed: 270 additions & 769 deletions

File tree

src/main/scala/org/ergoplatform/nodeView/ErgoNodeViewHolder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ abstract class ErgoNodeViewHolder[State <: ErgoState[State]](settings: ErgoSetti
378378
val updMp = memoryPool().removeWithDoubleSpends(txs)
379379
updateNodeView(updatedMempool = Some(updMp))
380380

381-
val newVault = vault().scanInputBlock(id, txs)
381+
val newVault = vault().scanInputBlock(id)
382382
updateNodeView(updatedVault = Some(newVault))
383383
case None =>
384384
}

src/main/scala/org/ergoplatform/nodeView/wallet/ErgoWallet.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class ErgoWallet(historyReader: ErgoHistoryReader, settings: ErgoSettings, param
4646
this
4747
}
4848

49-
def scanInputBlock(inputBlockId: ModifierId, txs: Seq[ErgoTransaction]): ErgoWallet = {
50-
walletActor ! ScanInputBlock(inputBlockId, txs)
49+
def scanInputBlock(inputBlockId: ModifierId): ErgoWallet = {
50+
walletActor ! ScanInputBlock(inputBlockId)
5151
this
5252
}
5353

src/main/scala/org/ergoplatform/nodeView/wallet/ErgoWalletActor.scala

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -236,36 +236,41 @@ class ErgoWalletActor(settings: ErgoSettings,
236236
val newState = state.copy(offChainRegistry = newOffChainRegistry)
237237
context.become(loadedWallet(newState))
238238

239-
case ScanInputBlock(inputBlockId, txs) =>
239+
case ScanInputBlock(inputBlockId) =>
240240
if (state.inputBlockIds.contains(inputBlockId)) {
241241
log.warn(s"Ignoring duplicate ScanInputBlock for $inputBlockId")
242242
} else {
243-
val dustLimit = settings.walletSettings.dustLimit
244-
245-
// Process all transactions atomically and record the net diff for rollback support
246-
val (finalRegistry, allAdded, allRemovedOffChain, allRemovedOnChain) =
247-
txs.foldLeft(
248-
(state.offChainRegistry, Seq.empty[TrackedBox], Seq.empty[TrackedBox], Seq.empty[Balance])
249-
) { case ((registry, addedAcc, removedOffAcc, removedOnAcc), tx) =>
250-
val newWalletBoxes = WalletScanLogic.extractWalletOutputs(tx, None, state.walletVars, dustLimit)
251-
val inputs = WalletScanLogic.extractInputBoxes(tx)
252-
val (newRegistry, removedOff, removedOn) =
253-
registry.updateOnTransactionWithDiff(newWalletBoxes, inputs, state.walletVars.externalScans)
254-
(newRegistry, addedAcc ++ newWalletBoxes, removedOffAcc ++ removedOff, removedOnAcc ++ removedOn)
255-
}
243+
historyReader.getInputBlockTransactions(inputBlockId) match {
244+
case Some(txs) =>
245+
val dustLimit = settings.walletSettings.dustLimit
246+
247+
// Process all transactions atomically and record the net diff for rollback support
248+
val (finalRegistry, allAdded, allRemovedOffChain, allRemovedOnChain) =
249+
txs.foldLeft(
250+
(state.offChainRegistry, Seq.empty[TrackedBox], Seq.empty[TrackedBox], Seq.empty[Balance])
251+
) { case ((registry, addedAcc, removedOffAcc, removedOnAcc), tx) =>
252+
val newWalletBoxes = WalletScanLogic.extractWalletOutputs(tx, None, state.walletVars, dustLimit)
253+
val inputs = WalletScanLogic.extractInputBoxes(tx)
254+
val (newRegistry, removedOff, removedOn) =
255+
registry.updateOnTransactionWithDiff(newWalletBoxes, inputs, state.walletVars.externalScans)
256+
(newRegistry, addedAcc ++ newWalletBoxes, removedOffAcc ++ removedOff, removedOnAcc ++ removedOn)
257+
}
256258

257-
val diff = InputBlockDiff(allAdded, allRemovedOffChain, allRemovedOnChain)
258-
val registryWithDiff = finalRegistry.copy(
259-
inputBlockDiffs = finalRegistry.inputBlockDiffs + (inputBlockId -> diff)
260-
)
261-
262-
val newInputBlockIds = state.inputBlockIds :+ inputBlockId
263-
val baseState = state.copy(
264-
offChainRegistry = registryWithDiff,
265-
inputBlockIds = newInputBlockIds
266-
)
267-
val newState = ergoWalletService.updateUtxoState(baseState, historyReader)
268-
context.become(loadedWallet(newState))
259+
val diff = InputBlockDiff(allAdded, allRemovedOffChain, allRemovedOnChain)
260+
val registryWithDiff = finalRegistry.copy(
261+
inputBlockDiffs = finalRegistry.inputBlockDiffs + (inputBlockId -> diff)
262+
)
263+
264+
val newInputBlockIds = state.inputBlockIds :+ inputBlockId
265+
val baseState = state.copy(
266+
offChainRegistry = registryWithDiff,
267+
inputBlockIds = newInputBlockIds
268+
)
269+
val newState = ergoWalletService.updateUtxoState(baseState, historyReader)
270+
context.become(loadedWallet(newState))
271+
case None =>
272+
log.warn(s"No transactions found for input block $inputBlockId")
273+
}
269274
}
270275

271276
case RollbackInputBlock(inputBlockId) =>
@@ -274,11 +279,12 @@ class ErgoWalletActor(settings: ErgoSettings,
274279
// we must rollback the target block AND all blocks that were added after it.
275280
val keepIndex = state.inputBlockIds.indexOf(inputBlockId)
276281
val (newInputBlockIds, newRegistry) = if (keepIndex >= 0) {
277-
val blocksToRollback = state.inputBlockIds.drop(keepIndex)
282+
val rolledBackIds = state.inputBlockIds.drop(keepIndex)
278283
// Rollback in reverse chronological order (last block first) to maintain consistency
279-
val rolledBackRegistry = blocksToRollback.reverse.foldLeft(state.offChainRegistry) { (reg, blockId) =>
284+
val rolledBackRegistry = rolledBackIds.reverse.foldLeft(state.offChainRegistry) { (reg, blockId) =>
280285
reg.rollbackInputBlock(blockId)
281286
}
287+
log.info(s"Rolled back input blocks: ${rolledBackIds.mkString(", ")}")
282288
(state.inputBlockIds.take(keepIndex), rolledBackRegistry)
283289
} else {
284290
// Block not found, nothing to rollback

src/main/scala/org/ergoplatform/nodeView/wallet/ErgoWalletActorMessages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object ErgoWalletActorMessages {
4444
*/
4545
final case class ScanOffChain(tx: ErgoTransaction)
4646

47-
final case class ScanInputBlock(inputBlockId: ModifierId, txs: Seq[ErgoTransaction])
47+
final case class ScanInputBlock(inputBlockId: ModifierId)
4848

4949
/**
5050
* Rollback changes from a specific input block

src/main/scala/org/ergoplatform/nodeView/wallet/persistence/OffChainRegistry.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ object InputBlockDiff {
3939
* Unlike regular mempool transactions, input blocks are explicitly tracked with
4040
* reversible diffs to support rollback without rebuilding the entire offchain state.
4141
*
42-
* When input block transactions are confirmed on-chain (via `ScanOnChain`), the wallet:
43-
*
44-
* - Removes the confirmed transactions from `inputBlockTxs` (see `ErgoWalletActor`)
45-
* - The on-chain scan process naturally updates `onChainBalances` via `updateOnBlock`
46-
* - Off-chain boxes that became on-chain are cleaned from `offChainBoxes`
42+
* When input block transactions are confirmed on-chain (via `ScanOnChain`), the wallet:
43+
*
44+
* - The on-chain scan process naturally updates `onChainBalances` via `updateOnBlock`
45+
* - Off-chain boxes that became on-chain are cleaned from `offChainBoxes`
4746
*
4847
* The wallet digest (balance) is computed as:
4948
* `sum(offChainBoxes) + sum(onChainBalances)`

0 commit comments

Comments
 (0)