From 45c0b2b82b648d9e7a0d42eceb09df6cf7ba2e6e Mon Sep 17 00:00:00 2001 From: William Berman Date: Mon, 11 Jan 2021 13:00:44 -0800 Subject: [PATCH] Transactions in unsynced blocks no-longer assumed pruned --- storage/modules/block_storage.go | 16 +++++++++++++--- storage/modules/block_storage_test.go | 17 +++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/storage/modules/block_storage.go b/storage/modules/block_storage.go index 8875921a..5b18ba0b 100644 --- a/storage/modules/block_storage.go +++ b/storage/modules/block_storage.go @@ -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( diff --git a/storage/modules/block_storage_test.go b/storage/modules/block_storage_test.go index 08c92ce6..2c0cafed 100644 --- a/storage/modules/block_storage_test.go +++ b/storage/modules/block_storage_test.go @@ -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,