Skip to content

Commit

Permalink
Fix missing tx index on reorg (#8027)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored Jan 14, 2025
1 parent 11e6615 commit ed1fe3b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,28 +360,37 @@ public void When_HeadBlockIsFarAhead_DoNotIndexTxHash()
}

[Test]
public void When_NewHeadBlock_Remove_TxIndex_OfRemovedBlock()
public void When_NewHeadBlock_DoNotRemove_TxIndex_WhenTxIsInOtherBlockNumber()
{
CreateStorage();
(Block block, TxReceipt[] receipts) = InsertBlock();

if (_receiptConfig.CompactTxIndex)
{
_receiptsDb.GetColumnDb(ReceiptsColumns.Transactions)[receipts[0].TxHash!.Bytes].Should().BeEquivalentTo(Rlp.Encode(block.Number).Bytes);
}
else
{
_receiptsDb.GetColumnDb(ReceiptsColumns.Transactions)[receipts[0].TxHash!.Bytes].Should().NotBeNull();
}
Transaction tx = Build.A.Transaction.SignedAndResolved().TestObject;

Block newHead = Build.A.Block.WithNumber(1).TestObject;
_blockTree.FindBestSuggestedHeader().Returns(newHead.Header);
_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(newHead, block));
Block b1a = Build.A.Block.WithNumber(1).TestObject;
Block b1b = Build.A.Block.WithNumber(1).WithTransactions(tx).TestObject;
Block b2a = Build.A.Block.WithNumber(2).WithParent(b1a).WithTransactions(tx).TestObject;
Block b2b = Build.A.Block.WithNumber(2).WithParent(b1b).TestObject;

Assert.That(
() => _receiptsDb.GetColumnDb(ReceiptsColumns.Transactions)[receipts[0].TxHash!.Bytes],
Is.Null.After(1000, 100)
);
InsertBlock(b1a);
InsertBlock(b1b);
InsertBlock(b2a);
InsertBlock(b2b);

// b1a
_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(b1a, null));

// b1b
_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(b1b, b1a));

// b2a
_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(b1a, b1b));
_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(b2a, null));

// b2b
_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(b1b, b1a));
_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(b2b, b2a));

_storage.FindBlockHash(tx.Hash!).Should().Be(b1b.Hash!);
}

[Test]
Expand Down Expand Up @@ -486,7 +495,12 @@ public void When_NewHeadBlock_ClearOldTxIndex()
.TestObject;
_blockTree.FindBestSuggestedHeader().Returns(farHead);
}
TxReceipt[] receipts = { Build.A.Receipt.WithCalculatedBloom().TestObject };

TxReceipt[] receipts = Array.Empty<TxReceipt>();
if (block.Transactions.Length == 1)
{
receipts = [Build.A.Receipt.WithCalculatedBloom().TestObject];
}
return (block, receipts);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public PersistentReceiptStorage(

private void BlockTreeOnBlockAddedToMain(object? sender, BlockReplacementEventArgs e)
{
if (e.PreviousBlock is not null)
{
RemoveBlockTx(e.PreviousBlock, e.Block);
}
EnsureCanonical(e.Block);
ReceiptsInserted?.Invoke(this, e);

Expand Down Expand Up @@ -362,20 +358,11 @@ public void EnsureCanonical(Block block)
}
}

private void RemoveBlockTx(Block block, Block? exceptBlock = null)
private void RemoveBlockTx(Block block)
{
HashSet<Hash256AsKey> newTxs = null;
if (exceptBlock is not null)
{
newTxs = new HashSet<Hash256AsKey>(exceptBlock.Transactions.Select(static (tx) => new Hash256AsKey(tx.Hash)));
}

using IWriteBatch writeBatch = _transactionDb.StartWriteBatch();
foreach (Transaction tx in block.Transactions)
{
// If the tx is contained in another block, don't remove it. Used for reorg where the same tx
// is contained in the new block
if (newTxs?.Contains(tx.Hash) == true) continue;
writeBatch[tx.Hash.Bytes] = null;
}
}
Expand Down

0 comments on commit ed1fe3b

Please sign in to comment.