diff --git a/internal/blocktx/processor.go b/internal/blocktx/processor.go index b06a0ba7e..0642a45b9 100644 --- a/internal/blocktx/processor.go +++ b/internal/blocktx/processor.go @@ -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 } @@ -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) } diff --git a/internal/blocktx/processor_test.go b/internal/blocktx/processor_test.go index a0808b129..3e03333dc 100644 --- a/internal/blocktx/processor_test.go +++ b/internal/blocktx/processor_test.go @@ -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) { @@ -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) diff --git a/internal/blocktx/store/mocks/blocktx_store_mock.go b/internal/blocktx/store/mocks/blocktx_store_mock.go index cdef56f2e..6c7c79189 100644 --- a/internal/blocktx/store/mocks/blocktx_store_mock.go +++ b/internal/blocktx/store/mocks/blocktx_store_mock.go @@ -36,7 +36,7 @@ var _ store.BlocktxStore = &BlocktxStoreMock{} // GetBlockFunc: func(ctx context.Context, hash *chainhash.Hash) (*blocktx_api.Block, error) { // panic("mock out the GetBlock method") // }, -// GetBlockByHeightFunc: func(ctx context.Context, height uint64, status blocktx_api.Status) (*blocktx_api.Block, error) { +// GetBlockByHeightFunc: func(ctx context.Context, height uint64) (*blocktx_api.Block, error) { // panic("mock out the GetBlockByHeight method") // }, // GetBlockGapsFunc: func(ctx context.Context, heightRange int) ([]*store.BlockGap, error) { @@ -110,7 +110,7 @@ type BlocktxStoreMock struct { GetBlockFunc func(ctx context.Context, hash *chainhash.Hash) (*blocktx_api.Block, error) // GetBlockByHeightFunc mocks the GetBlockByHeight method. - GetBlockByHeightFunc func(ctx context.Context, height uint64, status blocktx_api.Status) (*blocktx_api.Block, error) + GetBlockByHeightFunc func(ctx context.Context, height uint64) (*blocktx_api.Block, error) // GetBlockGapsFunc mocks the GetBlockGaps method. GetBlockGapsFunc func(ctx context.Context, heightRange int) ([]*store.BlockGap, error) @@ -201,8 +201,6 @@ type BlocktxStoreMock struct { Ctx context.Context // Height is the height argument value. Height uint64 - // Status is the status argument value. - Status blocktx_api.Status } // GetBlockGaps holds details about calls to the GetBlockGaps method. GetBlockGaps []struct { @@ -525,23 +523,21 @@ func (mock *BlocktxStoreMock) GetBlockCalls() []struct { } // GetBlockByHeight calls GetBlockByHeightFunc. -func (mock *BlocktxStoreMock) GetBlockByHeight(ctx context.Context, height uint64, status blocktx_api.Status) (*blocktx_api.Block, error) { +func (mock *BlocktxStoreMock) GetBlockByHeight(ctx context.Context, height uint64) (*blocktx_api.Block, error) { if mock.GetBlockByHeightFunc == nil { panic("BlocktxStoreMock.GetBlockByHeightFunc: method is nil but BlocktxStore.GetBlockByHeight was just called") } callInfo := struct { Ctx context.Context Height uint64 - Status blocktx_api.Status }{ Ctx: ctx, Height: height, - Status: status, } mock.lockGetBlockByHeight.Lock() mock.calls.GetBlockByHeight = append(mock.calls.GetBlockByHeight, callInfo) mock.lockGetBlockByHeight.Unlock() - return mock.GetBlockByHeightFunc(ctx, height, status) + return mock.GetBlockByHeightFunc(ctx, height) } // GetBlockByHeightCalls gets all the calls that were made to GetBlockByHeight. @@ -551,12 +547,10 @@ func (mock *BlocktxStoreMock) GetBlockByHeight(ctx context.Context, height uint6 func (mock *BlocktxStoreMock) GetBlockByHeightCalls() []struct { Ctx context.Context Height uint64 - Status blocktx_api.Status } { var calls []struct { Ctx context.Context Height uint64 - Status blocktx_api.Status } mock.lockGetBlockByHeight.RLock() calls = mock.calls.GetBlockByHeight diff --git a/internal/blocktx/store/postgresql/get_block.go b/internal/blocktx/store/postgresql/get_block.go index a520af83d..b8852d460 100644 --- a/internal/blocktx/store/postgresql/get_block.go +++ b/internal/blocktx/store/postgresql/get_block.go @@ -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) { diff --git a/internal/blocktx/store/postgresql/postgres_test.go b/internal/blocktx/store/postgresql/postgres_test.go index fa60ab333..aec0d128c 100644 --- a/internal/blocktx/store/postgresql/postgres_test.go +++ b/internal/blocktx/store/postgresql/postgres_test.go @@ -193,7 +193,6 @@ func TestPostgresDB(t *testing.T) { height := uint64(822015) expectedHashAtHeightLongest := testutils.RevChainhash(t, "c9b4e1e4dcf9188416027511671b9346be8ef93c0ddf59060000000000000000") - expectedHashAtHeightStale := testutils.RevChainhash(t, "00000000000000000659df0d3cf98ebe46931b67117502168418f9dce4e1b4c9") heightNotFound := uint64(812222) @@ -201,15 +200,11 @@ func TestPostgresDB(t *testing.T) { 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) diff --git a/internal/blocktx/store/store.go b/internal/blocktx/store/store.go index f3f97a1de..418943556 100644 --- a/internal/blocktx/store/store.go +++ b/internal/blocktx/store/store.go @@ -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