Skip to content

Commit

Permalink
Merge pull request #215 from bitcoin-sv/fix/metamorph-time-constants
Browse files Browse the repository at this point in the history
fix/time constants
  • Loading branch information
boecklim authored Dec 15, 2023
2 parents 9ac2406 + e4f48b2 commit fb293dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
22 changes: 12 additions & 10 deletions metamorph/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ const (
// length of interval for checking transactions if they are seen on the network
// if not we resend them again for a few times
unseenTransactionRebroadcastingInterval = 60
processExpiredSeenTxsIntervalDefault = 5 * time.Minute
mapExpiryTimeDefault = 24 * time.Hour
LogLevelDefault = slog.LevelInfo

processCheckIfMinedIntervalDefault = 1 * time.Minute
checkIfMinedTimeRange = time.Minute * 60

mapExpiryTimeDefault = 24 * time.Hour
LogLevelDefault = slog.LevelInfo

failedToUpdateStatus = "Failed to update status"
dataRetentionPeriodDefault = 14 * 24 * time.Hour // 14 days
checkIfMinedTimeRange = time.Minute * 20
)

type Processor struct {
Expand All @@ -50,8 +52,8 @@ type Processor struct {
dataRetentionPeriod time.Duration
now func() time.Time

processExpiredSeenTxsInterval time.Duration
processExpiredSeenTxsTicker *time.Ticker
processCheckIfMinedInterval time.Duration
processCheckIfMinedTicker *time.Ticker

processExpiredTxsTicker *time.Ticker

Expand Down Expand Up @@ -90,7 +92,7 @@ func NewProcessor(s store.MetamorphStore, pm p2p.PeerManagerI, btc blocktx.Clien
now: time.Now,
processExpiredTxsTicker: time.NewTicker(unseenTransactionRebroadcastingInterval * time.Second),

processExpiredSeenTxsInterval: processExpiredSeenTxsIntervalDefault,
processCheckIfMinedInterval: processCheckIfMinedIntervalDefault,

stored: stat.NewAtomicStat(),
announcedToNetwork: stat.NewAtomicStats(),
Expand All @@ -111,7 +113,7 @@ func NewProcessor(s store.MetamorphStore, pm p2p.PeerManagerI, btc blocktx.Clien
}

p.ProcessorResponseMap = NewProcessorResponseMap(p.mapExpiryTime, WithLogFile(p.logFile), WithNowResponseMap(p.now))
p.processExpiredSeenTxsTicker = time.NewTicker(p.processExpiredSeenTxsInterval)
p.processCheckIfMinedTicker = time.NewTicker(p.processCheckIfMinedInterval)

p.logger.Info("Starting processor", slog.Duration("cacheExpiryTime", p.mapExpiryTime))

Expand Down Expand Up @@ -145,7 +147,7 @@ func (p *Processor) Shutdown() {
if err != nil {
p.logger.Error("Failed to unlock all hashes", slog.String("err", err.Error()))
}
p.processExpiredSeenTxsTicker.Stop()
p.processCheckIfMinedTicker.Stop()
p.processExpiredTxsTicker.Stop()
p.ProcessorResponseMap.Close()
}
Expand Down Expand Up @@ -177,7 +179,7 @@ func (p *Processor) processCheckIfMined() {

// Check transactions that have been seen on the network, but haven't been marked as mined
// The Items() method will return a copy of the map, so we can iterate over it without locking
for range p.processExpiredSeenTxsTicker.C {
for range p.processCheckIfMinedTicker.C {
expiredTransactionItems := p.ProcessorResponseMap.Items(filterFunc)
if len(expiredTransactionItems) == 0 {
continue
Expand Down
4 changes: 2 additions & 2 deletions metamorph/processor_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"time"
)

func WithProcessExpiredSeenTxsInterval(d time.Duration) func(*Processor) {
func WithProcessCheckIfMinedInterval(d time.Duration) func(*Processor) {
return func(p *Processor) {
p.processExpiredSeenTxsInterval = d
p.processCheckIfMinedInterval = d
}
}

Expand Down
8 changes: 4 additions & 4 deletions metamorph/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestNewProcessor(t *testing.T) {

processor, err := NewProcessor(tc.store, tc.pm, nil,
WithCacheExpiryTime(time.Second*5),
WithProcessExpiredSeenTxsInterval(time.Second*5),
WithProcessCheckIfMinedInterval(time.Second*5),
WithProcessorLogger(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: LogLevelDefault}))),
)
if tc.expectedErrorStr != "" || err != nil {
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestLoadUnmined(t *testing.T) {
}

processor, err := NewProcessor(mtmStore, pm, btxMock,
WithProcessExpiredSeenTxsInterval(time.Hour*24),
WithProcessCheckIfMinedInterval(time.Hour*24),
WithCacheExpiryTime(time.Hour*24),
WithNow(func() time.Time {
return storedAt.Add(1 * time.Hour)
Expand Down Expand Up @@ -813,7 +813,7 @@ func TestProcessCheckIfMined(t *testing.T) {

pm := p2p.NewPeerManagerMock()
processor, err := NewProcessor(metamorphStore, pm, btxMock,
WithProcessExpiredSeenTxsInterval(20*time.Millisecond),
WithProcessCheckIfMinedInterval(20*time.Millisecond),
WithProcessExpiredTxsInterval(time.Hour),
)
require.NoError(t, err)
Expand Down Expand Up @@ -862,7 +862,7 @@ func TestProcessExpiredTransactions(t *testing.T) {
}
pm := p2p.NewPeerManagerMock()
processor, err := NewProcessor(metamorphStore, pm, nil,
WithProcessExpiredSeenTxsInterval(time.Hour),
WithProcessCheckIfMinedInterval(time.Hour),
WithProcessExpiredTxsInterval(time.Millisecond*20),
WithNow(func() time.Time {
return time.Date(2033, 1, 1, 1, 0, 0, 0, time.UTC)
Expand Down

0 comments on commit fb293dd

Please sign in to comment.