Skip to content

Commit

Permalink
refactor(ARCO-312): Setting which decides whether a received block is…
Browse files Browse the repository at this point in the history
… considered from the longest blockchain by default
  • Loading branch information
boecklim committed Jan 9, 2025
1 parent 0770104 commit e97e4a5
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cmd/arc/services/blocktx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (

"go.opentelemetry.io/otel/attribute"

"github.com/libsv/go-p2p"

"github.com/bitcoin-sv/arc/internal/grpc_opts"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/client/nats_core"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/client/nats_jetstream"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/nats_connection"
"github.com/bitcoin-sv/arc/internal/tracing"
"github.com/libsv/go-p2p"

"github.com/bitcoin-sv/arc/config"
"github.com/bitcoin-sv/arc/internal/blocktx"
Expand Down Expand Up @@ -127,6 +128,7 @@ func StartBlockTx(logger *slog.Logger, arcConfig *config.ArcConfig) (func(), err
blocktx.WithRegisterTxsInterval(btxConfig.RegisterTxsInterval),
blocktx.WithMessageQueueClient(mqClient),
blocktx.WithMaxBlockProcessingDuration(btxConfig.MaxBlockProcessingDuration),
blocktx.WithIncomingIsLongest(btxConfig.IncomingIsLongest),
)

blockRequestCh := make(chan blocktx.BlockRequest, blockProcessingBuffer)
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type BlocktxConfig struct {
MaxAllowedBlockHeightMismatch int `mapstructure:"maxAllowedBlockHeightMismatch"`
MessageQueue *MessageQueueConfig `mapstructure:"mq"`
P2pReadBufferSize int `mapstructure:"p2pReadBufferSize"`
IncomingIsLongest bool `mapstructure:"incomingIsLongest"`
}

type DbConfig struct {
Expand Down
1 change: 1 addition & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func getBlocktxConfig() *BlocktxConfig {
MaxBlockProcessingDuration: 5 * time.Minute,
MessageQueue: &MessageQueueConfig{},
P2pReadBufferSize: 8 * 1024 * 1024,
IncomingIsLongest: false,
}
}

Expand Down
1 change: 1 addition & 0 deletions config/example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ blocktx:
registerTxsInterval: 10s # time interval to read from the channel registered transactions
maxBlockProcessingDuration: 5m # maximum time a blocktx can spend on processing a block before unlocking it to be requested again
monitorPeers: false # if enabled, peers which do not receive alive signal from nodes will be restarted
incomingIsLongest: false # whether each new block received is considered to be from the longest blockchain. If there are a lot of block gaps in blocktx database it is advisable to set this to true
fillGaps:
enabled: true
interval: 15m # time interval to check and fill gaps in processed blocks
Expand Down
4 changes: 2 additions & 2 deletions internal/blocktx/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Processor struct {
processGuardsMap sync.Map
stats *processorStats
statCollectionInterval time.Duration
hasGaps bool
incomingIsLongest bool

now func() time.Time
maxBlockProcessingDuration time.Duration
Expand Down Expand Up @@ -551,7 +551,7 @@ func (p *Processor) verifyAndInsertBlock(ctx context.Context, blockMsg *p2p.Bloc
Chainwork: calculateChainwork(blockMsg.Header.Bits).String(),
}

if p.hasGaps {
if p.incomingIsLongest {
incomingBlock.Status = blocktx_api.Status_LONGEST
} else {
err = p.assignBlockStatus(ctx, incomingBlock, previousBlockHash)
Expand Down
6 changes: 6 additions & 0 deletions internal/blocktx/processor_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ func WithMaxBlockProcessingDuration(d time.Duration) func(*Processor) {
processor.maxBlockProcessingDuration = d
}
}

func WithIncomingIsLongest(enabled bool) func(*Processor) {
return func(processor *Processor) {
processor.incomingIsLongest = enabled
}
}

0 comments on commit e97e4a5

Please sign in to comment.