Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactions in unsynced blocks no-longer assumed pruned #292

Merged
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
16 changes: 13 additions & 3 deletions storage/modules/block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,25 +1063,35 @@ func (b *BlockStorage) FindTransaction(

var newestBlock *types.BlockIdentifier
var newestTransaction *types.Transaction
var transactionUnsequenced bool
for _, blockTransaction := range blockTransactions {
if newestBlock == nil || blockTransaction.BlockIdentifier.Index > newestBlock.Index {
// Now that we are optimistically storing data, there is a chance
// we may fetch a transaction from a seen but unsequenced block.
if head != nil && blockTransaction.BlockIdentifier.Index > head.Index {
// We have seen a transaction, but it's in a block that is not yet
// sequenced.
transactionUnsequenced = true
continue
}

newestBlock = blockTransaction.BlockIdentifier
// When `blockTransaction` is pruned, `blockTransaction.Transaction` is set to nil
newestTransaction = blockTransaction.Transaction
}
}

// If the transaction has been pruned, it will be nil.
if newestTransaction == nil {
if newestTransaction != nil {
return newestBlock, newestTransaction, nil
}

if !transactionUnsequenced {
// All matching transaction have been pruned
return nil, nil, storageErrs.ErrCannotAccessPrunedData
}

return newestBlock, newestTransaction, nil
// A transaction exists but we have not yet sequenced the block it is in
return nil, nil, nil
}

func (b *BlockStorage) findBlockTransaction(
Expand Down
17 changes: 15 additions & 2 deletions storage/modules/block_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,25 @@ func TestBlock(t *testing.T) {
err = storage.SeeBlock(ctx, newBlock)
assert.NoError(t, err)

// Attempt to find transaction before it's synced
transaction := storage.db.ReadTransaction(ctx)
newestBlock, newestTransaction, err := storage.FindTransaction(
ctx,
newBlock.Transactions[0].TransactionIdentifier,
transaction,
)
assert.NoError(t, err)
assert.Nil(t, newestBlock)
assert.Nil(t, newestTransaction)
transaction.Discard(ctx)

// Ensure we can FindTransaction in add block transaction.
transaction := storage.db.WriteTransaction(ctx, blockSyncIdentifier, true)
transaction = storage.db.WriteTransaction(ctx, blockSyncIdentifier, true)
// Sequence the block so the transaction can be found
err = storage.storeBlock(ctx, transaction, newBlock.BlockIdentifier)
assert.NoError(t, err)

newestBlock, newestTransaction, err := storage.FindTransaction(
newestBlock, newestTransaction, err = storage.FindTransaction(
ctx,
newBlock.Transactions[0].TransactionIdentifier,
transaction,
Expand Down