Skip to content

Commit

Permalink
feat: include height range in getting the chain tip
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Dec 23, 2024
1 parent cd4c900 commit 2e162a5
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 59 deletions.
10 changes: 8 additions & 2 deletions internal/blocktx/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (p *Processor) publishMinedTxs(txHashes []*chainhash.Hash) error {
hashesBytes[i] = h[:]
}

minedTxs, err := p.store.GetMinedTransactions(p.ctx, hashesBytes, false)
minedTxs, err := p.store.GetMinedTransactions(p.ctx, hashesBytes)
if err != nil {
return fmt.Errorf("failed to get mined transactions: %v", err)
}
Expand Down Expand Up @@ -613,7 +613,13 @@ func (p *Processor) assignBlockStatus(ctx context.Context, block *blocktx_api.Bl

//lint:ignore U1000 Ignored until gaps are filling quickly again TODO: remove this ignore
func (p *Processor) longestTipExists(ctx context.Context) (bool, error) {
_, err := p.store.GetChainTip(ctx)
const (
hoursPerDay = 24
blocksPerHour = 6
)
heightRange := p.dataRetentionDays * hoursPerDay * blocksPerHour

_, err := p.store.GetChainTip(ctx, heightRange)
if err != nil && !errors.Is(err, store.ErrBlockNotFound) {
return false, err
}
Expand Down
10 changes: 5 additions & 5 deletions internal/blocktx/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ func TestHandleBlock(t *testing.T) {
GetLongestBlockByHeightFunc: func(_ context.Context, _ uint64) (*blocktx_api.Block, error) {
return nil, store.ErrBlockNotFound
},
GetChainTipFunc: func(_ context.Context) (*blocktx_api.Block, error) {
GetChainTipFunc: func(_ context.Context, _ int) (*blocktx_api.Block, error) {
return nil, store.ErrBlockNotFound
},
UpsertBlockFunc: func(_ context.Context, _ *blocktx_api.Block) (uint64, error) {
return 0, nil
},
GetMinedTransactionsFunc: func(_ context.Context, _ [][]byte, _ bool) ([]store.TransactionBlock, error) {
GetMinedTransactionsFunc: func(_ context.Context, _ [][]byte) ([]store.TransactionBlock, error) {
return nil, nil
},
GetRegisteredTxsByBlockHashesFunc: func(_ context.Context, _ [][]byte) ([]store.TransactionBlock, error) {
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestHandleBlockReorgAndOrphans(t *testing.T) {
}
return nil, store.ErrBlockNotFound
},
GetChainTipFunc: func(_ context.Context) (*blocktx_api.Block, error) {
GetChainTipFunc: func(_ context.Context, _ int) (*blocktx_api.Block, error) {
return &blocktx_api.Block{}, nil
},
UpsertBlockFunc: func(_ context.Context, block *blocktx_api.Block) (uint64, error) {
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestHandleBlockReorgAndOrphans(t *testing.T) {
GetRegisteredTxsByBlockHashesFunc: func(_ context.Context, _ [][]byte) ([]store.TransactionBlock, error) {
return nil, nil
},
GetMinedTransactionsFunc: func(_ context.Context, _ [][]byte, _ bool) ([]store.TransactionBlock, error) {
GetMinedTransactionsFunc: func(_ context.Context, _ [][]byte) ([]store.TransactionBlock, error) {
return nil, nil
},
MarkBlockAsDoneFunc: func(_ context.Context, _ *chainhash.Hash, _, _ uint64) error {
Expand Down Expand Up @@ -714,7 +714,7 @@ func TestStartProcessRequestTxs(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// given
storeMock := &storeMocks.BlocktxStoreMock{
GetMinedTransactionsFunc: func(_ context.Context, hashes [][]byte, _ bool) ([]store.TransactionBlock, error) {
GetMinedTransactionsFunc: func(_ context.Context, hashes [][]byte) ([]store.TransactionBlock, error) {
for _, hash := range hashes {
require.Equal(t, testdata.TX1Hash[:], hash)
}
Expand Down
52 changes: 26 additions & 26 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.

Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@
tx_count: 36724
status: 10
chainwork: '123456'
- inserted_at: 2023-12-15 14:50:00
id: 5
hash: 0x000000000000000005167c951069b0e3c803753b8ebeaa2ddaca85b89526b297
prevhash: 0x76404890880cb36ce68100abb05b3a958e17c0ed274d5c0a0000000000000000
merkleroot: 0xc458aa382364e216c9c0533175ec8579a544c750ca181b18296e784d1dc53085
height: 822025
processed_at: 2023-12-15 14:40:00
size: 8630000
tx_count: 36724
status: 30 # ORPHANED
is_longest: false
chainwork: '123456'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
prevhash: 0x000000000000000001a7aa3999410ca53fb645851531ec0a7a5cb9ce2d4ae313
merkleroot: 0x0d72bf92e7862df18d1935c171ca4dbb70d268b0f025e46716e913bc7e4f2bdb
height: 826481
status: 10 # STALE
status: 10
is_longest: true
processed_at: 2024-01-10 13:06:06.122
size: 108689370
Expand Down
10 changes: 7 additions & 3 deletions internal/blocktx/store/postgresql/get_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ func (p *PostgreSQL) GetLongestBlockByHeight(ctx context.Context, height uint64)
return p.queryBlockByPredicate(ctx, predicate, height)
}

func (p *PostgreSQL) GetChainTip(ctx context.Context) (*blocktx_api.Block, error) {
predicate := "WHERE height = (SELECT MAX(height) FROM blocktx.blocks blks WHERE blks.is_longest = true)"
func (p *PostgreSQL) GetChainTip(ctx context.Context, heightRange int) (*blocktx_api.Block, error) {
predicate := `WHERE height = (SELECT MAX(height) from blocktx.blocks WHERE is_longest = true)
AND is_longest = true
AND height > (SELECT MAX(height) - $1 from blocktx.blocks)
AND processed_at IS NOT NULL
`

return p.queryBlockByPredicate(ctx, predicate)
return p.queryBlockByPredicate(ctx, predicate, heightRange)
}

func (p *PostgreSQL) queryBlockByPredicate(ctx context.Context, predicate string, predicateParams ...any) (*blocktx_api.Block, error) {
Expand Down
9 changes: 2 additions & 7 deletions internal/blocktx/store/postgresql/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ import (
"github.com/lib/pq"
)

func (p *PostgreSQL) GetMinedTransactions(ctx context.Context, hashes [][]byte, onlyLongestChain bool) (minedTransactions []store.TransactionBlock, err error) {
func (p *PostgreSQL) GetMinedTransactions(ctx context.Context, hashes [][]byte) (minedTransactions []store.TransactionBlock, err error) {
ctx, span := tracing.StartTracing(ctx, "GetMinedTransactions", p.tracingEnabled, p.tracingAttributes...)
defer func() {
tracing.EndTracing(span, err)
}()

if onlyLongestChain {
predicate := "WHERE t.hash = ANY($1) AND b.is_longest = true"
return p.getTransactionBlocksByPredicate(ctx, predicate, pq.Array(hashes))
}

predicate := "WHERE t.hash = ANY($1) AND (b.status = $2 OR b.status = $3) AND b.processed_at IS NOT NULL"

return p.getTransactionBlocksByPredicate(ctx, predicate,
Expand All @@ -30,7 +25,7 @@ func (p *PostgreSQL) GetMinedTransactions(ctx context.Context, hashes [][]byte,
}

func (p *PostgreSQL) GetRegisteredTxsByBlockHashes(ctx context.Context, blockHashes [][]byte) (registeredTxs []store.TransactionBlock, err error) {
ctx, span := tracing.StartTracing(ctx, "GetMinedTransactions", p.tracingEnabled, p.tracingAttributes...)
ctx, span := tracing.StartTracing(ctx, "GetRegisteredTxsByBlockHashes", p.tracingEnabled, p.tracingAttributes...)
defer func() {
tracing.EndTracing(span, err)
}()
Expand Down
26 changes: 14 additions & 12 deletions internal/blocktx/store/postgresql/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,14 @@ func TestPostgresDB(t *testing.T) {
require.Nil(t, actualBlock)
require.Equal(t, store.ErrBlockNotFound, err)

actualBlock, err = postgresDB.GetChainTip(context.Background())
actualBlock, err = postgresDB.GetChainTip(context.Background(), 10)
require.NoError(t, err)
require.Equal(t, hashAtTip[:], actualBlock.Hash)
require.Equal(t, expectedTipHeight, actualBlock.Height)

actualBlock, err = postgresDB.GetChainTip(context.Background(), 2)
require.Nil(t, actualBlock)
require.Equal(t, store.ErrBlockNotFound, err)
})

t.Run("get block gaps", func(t *testing.T) {
Expand Down Expand Up @@ -460,16 +464,7 @@ func TestPostgresDB(t *testing.T) {
}

// when
onlyLongestChain := true
actualTxs, err := postgresDB.GetMinedTransactions(ctx, [][]byte{txHash1[:], txHash2[:], txHash3[:]}, onlyLongestChain)

// then
require.NoError(t, err)
require.ElementsMatch(t, expectedTxs[:2], actualTxs)

// when
onlyLongestChain = false
actualTxs, err = postgresDB.GetMinedTransactions(ctx, [][]byte{txHash1[:], txHash2[:], txHash3[:]}, onlyLongestChain)
actualTxs, err := postgresDB.GetMinedTransactions(ctx, [][]byte{txHash1[:], txHash2[:], txHash3[:]})

// then
require.NoError(t, err)
Expand Down Expand Up @@ -920,6 +915,13 @@ func TestPostgresStore_UpsertBlockTransactions_CompetingBlocks(t *testing.T) {
MerklePath: "merkle-path-1",
BlockStatus: blocktx_api.Status_LONGEST,
},
{
TxHash: txHash[:],
BlockHash: testutils.RevChainhash(t, "7258b02da70a3e367e4c993b049fa9b76ef8f090ef9fd2010000000000000000")[:],
BlockHeight: uint64(826481),
MerklePath: "merkle-path-2",
BlockStatus: blocktx_api.Status_STALE,
},
}

// when
Expand All @@ -930,7 +932,7 @@ func TestPostgresStore_UpsertBlockTransactions_CompetingBlocks(t *testing.T) {
require.NoError(t, err)

// then
actual, err := sut.GetMinedTransactions(ctx, [][]byte{txHash[:]}, true)
actual, err := sut.GetMinedTransactions(ctx, [][]byte{txHash[:]})
require.NoError(t, err)

require.ElementsMatch(t, expected, actual)
Expand Down
4 changes: 2 additions & 2 deletions internal/blocktx/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ 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)
GetLongestBlockByHeight(ctx context.Context, height uint64) (*blocktx_api.Block, error)
GetChainTip(ctx context.Context) (*blocktx_api.Block, error)
GetChainTip(ctx context.Context, heightRange int) (*blocktx_api.Block, error)
UpsertBlock(ctx context.Context, block *blocktx_api.Block) (uint64, error)
UpsertBlockTransactions(ctx context.Context, blockID uint64, txsWithMerklePaths []TxWithMerklePath) error
MarkBlockAsDone(ctx context.Context, hash *chainhash.Hash, size uint64, txCount uint64) error
GetBlockGaps(ctx context.Context, heightRange int) ([]*BlockGap, error)
ClearBlocktxTable(ctx context.Context, retentionDays int32, table string) (*blocktx_api.RowsAffectedResponse, error)
GetMinedTransactions(ctx context.Context, hashes [][]byte, onlyLongestChain bool) ([]TransactionBlock, error)
GetMinedTransactions(ctx context.Context, hashes [][]byte) ([]TransactionBlock, error)
GetLongestChainFromHeight(ctx context.Context, height uint64) ([]*blocktx_api.Block, error)
GetStaleChainBackFromHash(ctx context.Context, hash []byte) ([]*blocktx_api.Block, error)
GetOrphansBackToNonOrphanAncestor(ctx context.Context, hash []byte) (orphans []*blocktx_api.Block, nonOrphanAncestor *blocktx_api.Block, err error)
Expand Down
2 changes: 1 addition & 1 deletion internal/metamorph/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (p *Processor) updateMined(ctx context.Context, txsBlocks []*blocktx_api.Tr
}

func (p *Processor) rebroadcastStaleTxs(ctx context.Context, txsBlocks []*blocktx_api.TransactionBlock) {
ctx, span := tracing.StartTracing(ctx, "rebroadcastStaleTxs", p.tracingEnabled, p.tracingAttributes...)
_, span := tracing.StartTracing(ctx, "rebroadcastStaleTxs", p.tracingEnabled, p.tracingAttributes...)
defer tracing.EndTracing(span, nil)

for _, tx := range txsBlocks {
Expand Down

0 comments on commit 2e162a5

Please sign in to comment.