Skip to content

Commit

Permalink
feat(ARCO-199): remove status from GetBlockByHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Nov 6, 2024
1 parent bec4276 commit 3233510
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 25 deletions.
4 changes: 2 additions & 2 deletions internal/blocktx/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func (p *Processor) competingChainsExist(ctx context.Context, block *blocktx_api
}

if block.Status == blocktx_api.Status_LONGEST {
competingBlock, err := p.store.GetBlockByHeight(ctx, block.Height, blocktx_api.Status_LONGEST)
competingBlock, err := p.store.GetBlockByHeight(ctx, block.Height)
if err != nil && !errors.Is(err, store.ErrBlockNotFound) {
return false, err
}
Expand Down Expand Up @@ -728,7 +728,7 @@ func (p *Processor) storeTransactions(ctx context.Context, blockID uint64, block
break
}

bump, err := bc.NewBUMPFromMerkleTreeAndIndex(block.Height, merkleTree, uint64(txIndex))
bump, err := bc.NewBUMPFromMerkleTreeAndIndex(block.Height, merkleTree, uint64(txIndex)) // NOSONAR
if err != nil {
return errors.Join(ErrFailedToCreateBUMP, fmt.Errorf("tx hash %s, block height: %d", hash.String(), block.Height), err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/blocktx/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestHandleBlock(t *testing.T) {
}
return nil, store.ErrBlockNotFound
},
GetBlockByHeightFunc: func(_ context.Context, _ uint64, _ blocktx_api.Status) (*blocktx_api.Block, error) {
GetBlockByHeightFunc: func(_ context.Context, _ uint64) (*blocktx_api.Block, error) {
return nil, store.ErrBlockNotFound
},
GetChainTipFunc: func(_ context.Context) (*blocktx_api.Block, error) {
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestHandleBlockReorgAndOrphans(t *testing.T) {
Processed: true,
}, nil
},
GetBlockByHeightFunc: func(_ context.Context, _ uint64, _ blocktx_api.Status) (*blocktx_api.Block, error) {
GetBlockByHeightFunc: func(_ context.Context, _ uint64) (*blocktx_api.Block, error) {
if tc.hasCompetingBlock {
blockHash, err := chainhash.NewHashFromStr("0000000000000000087590e1ad6360c0c491556c9af75c0d22ce9324cb5713cf")
require.NoError(t, err)
Expand Down
14 changes: 4 additions & 10 deletions internal/blocktx/store/mocks/blocktx_store_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/blocktx/store/postgresql/get_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func (p *PostgreSQL) GetBlock(ctx context.Context, hash *chainhash.Hash) (*block
return p.queryBlockByPredicate(ctx, predicate, hash[:])
}

func (p *PostgreSQL) GetBlockByHeight(ctx context.Context, height uint64, status blocktx_api.Status) (*blocktx_api.Block, error) {
predicate := "WHERE height = $1 AND status = $2"
func (p *PostgreSQL) GetBlockByHeight(ctx context.Context, height uint64) (*blocktx_api.Block, error) {
predicate := "WHERE height = $1 AND is_longest = true"

return p.queryBlockByPredicate(ctx, predicate, height, status)
return p.queryBlockByPredicate(ctx, predicate, height)
}

func (p *PostgreSQL) GetChainTip(ctx context.Context) (*blocktx_api.Block, error) {
Expand Down
9 changes: 2 additions & 7 deletions internal/blocktx/store/postgresql/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,18 @@ func TestPostgresDB(t *testing.T) {

height := uint64(822015)
expectedHashAtHeightLongest := testutils.RevChainhash(t, "c9b4e1e4dcf9188416027511671b9346be8ef93c0ddf59060000000000000000")
expectedHashAtHeightStale := testutils.RevChainhash(t, "00000000000000000659df0d3cf98ebe46931b67117502168418f9dce4e1b4c9")

heightNotFound := uint64(812222)

expectedTipHeight := uint64(822020)
hashAtTip := testutils.RevChainhash(t, "76404890880cb36ce68100abb05b3a958e17c0ed274d5c0a0000000000000000")

// when -> then
actualBlock, err := postgresDB.GetBlockByHeight(context.Background(), height, blocktx_api.Status_LONGEST)
actualBlock, err := postgresDB.GetBlockByHeight(context.Background(), height)
require.NoError(t, err)
require.Equal(t, expectedHashAtHeightLongest[:], actualBlock.Hash)

actualBlock, err = postgresDB.GetBlockByHeight(context.Background(), height, blocktx_api.Status_STALE)
require.NoError(t, err)
require.Equal(t, expectedHashAtHeightStale[:], actualBlock.Hash)

actualBlock, err = postgresDB.GetBlockByHeight(context.Background(), heightNotFound, blocktx_api.Status_LONGEST)
actualBlock, err = postgresDB.GetBlockByHeight(context.Background(), heightNotFound)
require.Nil(t, actualBlock)
require.Equal(t, store.ErrBlockNotFound, err)

Expand Down
2 changes: 1 addition & 1 deletion internal/blocktx/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
type BlocktxStore interface {
RegisterTransactions(ctx context.Context, txHashes [][]byte) (updatedTxs []*chainhash.Hash, err error)
GetBlock(ctx context.Context, hash *chainhash.Hash) (*blocktx_api.Block, error)
GetBlockByHeight(ctx context.Context, height uint64, status blocktx_api.Status) (*blocktx_api.Block, error)
GetBlockByHeight(ctx context.Context, height uint64) (*blocktx_api.Block, error)
GetChainTip(ctx context.Context) (*blocktx_api.Block, error)
UpsertBlock(ctx context.Context, block *blocktx_api.Block) (uint64, error)
UpsertBlockTransactions(ctx context.Context, blockID uint64, txsWithMerklePaths []TxWithMerklePath) error
Expand Down

0 comments on commit 3233510

Please sign in to comment.