Skip to content

Commit

Permalink
Nits
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Jul 24, 2020
1 parent 4077794 commit 3f93620
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
15 changes: 13 additions & 2 deletions internal/storage/block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ func (b *BlockStorage) AddBlock(

// Store all transaction hashes
for _, txn := range block.Transactions {
err = b.storeTransactionHash(ctx, transaction, block.BlockIdentifier, txn.TransactionIdentifier)
err = b.storeTransactionHash(
ctx,
transaction,
block.BlockIdentifier,
txn.TransactionIdentifier,
)
if err != nil {
return fmt.Errorf("%w: unable to store transaction hash", err)
}
Expand Down Expand Up @@ -423,7 +428,13 @@ func (b *BlockStorage) storeTransactionHash(
}

if _, exists := blocks[block.Hash]; exists {
return fmt.Errorf("%w: duplicate transaction %s found in block %s:%d", ErrDuplicateTransactionHash, blockTransaction.Hash, block.Hash, block.Index)
return fmt.Errorf(
"%w: duplicate transaction %s found in block %s:%d",
ErrDuplicateTransactionHash,
blockTransaction.Hash,
block.Hash,
block.Index,
)
}

blocks[block.Hash] = block.Index
Expand Down
32 changes: 26 additions & 6 deletions internal/storage/block_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ func TestBlock(t *testing.T) {
storage := NewBlockStorage(database)

t.Run("Get non-existent tx", func(t *testing.T) {
txBlocks, headDistance, err := storage.FindTransaction(ctx, newBlock.Transactions[0].TransactionIdentifier)
txBlocks, headDistance, err := storage.FindTransaction(
ctx,
newBlock.Transactions[0].TransactionIdentifier,
)
assert.NoError(t, err)
assert.Nil(t, txBlocks)
assert.Equal(t, int64(-1), headDistance)
Expand All @@ -281,7 +284,10 @@ func TestBlock(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, newBlock.BlockIdentifier, head)

txBlocks, headDistance, err := storage.FindTransaction(ctx, newBlock.Transactions[0].TransactionIdentifier)
txBlocks, headDistance, err := storage.FindTransaction(
ctx,
newBlock.Transactions[0].TransactionIdentifier,
)
assert.NoError(t, err)
assert.Len(t, txBlocks, 1)
assert.Equal(t, newBlock.BlockIdentifier, txBlocks[0])
Expand Down Expand Up @@ -315,10 +321,17 @@ func TestBlock(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, newBlock2.BlockIdentifier, head)

txBlocks, headDistance, err := storage.FindTransaction(ctx, newBlock.Transactions[0].TransactionIdentifier)
txBlocks, headDistance, err := storage.FindTransaction(
ctx,
newBlock.Transactions[0].TransactionIdentifier,
)
assert.NoError(t, err)
assert.Len(t, txBlocks, 2)
assert.ElementsMatch(t, []*types.BlockIdentifier{newBlock.BlockIdentifier, newBlock2.BlockIdentifier}, txBlocks)
assert.ElementsMatch(
t,
[]*types.BlockIdentifier{newBlock.BlockIdentifier, newBlock2.BlockIdentifier},
txBlocks,
)
assert.Equal(t, int64(1), headDistance)
})

Expand All @@ -337,10 +350,17 @@ func TestBlock(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, newBlock2.BlockIdentifier, head)

txBlocks, headDistance, err := storage.FindTransaction(ctx, newBlock.Transactions[0].TransactionIdentifier)
txBlocks, headDistance, err := storage.FindTransaction(
ctx,
newBlock.Transactions[0].TransactionIdentifier,
)
assert.NoError(t, err)
assert.Len(t, txBlocks, 2)
assert.ElementsMatch(t, []*types.BlockIdentifier{newBlock.BlockIdentifier, newBlock2.BlockIdentifier}, txBlocks)
assert.ElementsMatch(
t,
[]*types.BlockIdentifier{newBlock.BlockIdentifier, newBlock2.BlockIdentifier},
txBlocks,
)
assert.Equal(t, int64(1), headDistance)
})

Expand Down

0 comments on commit 3f93620

Please sign in to comment.