diff --git a/data/txDupCache.go b/data/txDupCache.go index 834bdfc533..026a168578 100644 --- a/data/txDupCache.go +++ b/data/txDupCache.go @@ -111,15 +111,14 @@ type txSaltedCache struct { ctx context.Context } -func makeSaltedCache(ctx context.Context, size int, refreshInterval time.Duration) *txSaltedCache { - c := &txSaltedCache{ - digestCache: digestCache{ - cur: map[crypto.Digest]struct{}{}, - maxSize: size, - }, - ctx: ctx, +func makeSaltedCache(size int) *txSaltedCache { + return &txSaltedCache{ + digestCache: *makeDigestCache(size), } +} +func (c *txSaltedCache) start(ctx context.Context, refreshInterval time.Duration) { + c.ctx = ctx if refreshInterval != 0 { go c.salter(refreshInterval) } @@ -127,8 +126,6 @@ func makeSaltedCache(ctx context.Context, size int, refreshInterval time.Duratio c.mu.Lock() defer c.mu.Unlock() c.moreSalt() - - return c } // salter is a goroutine refreshing the cache by schedule diff --git a/data/txDupCache_test.go b/data/txDupCache_test.go index ed3cb5bce2..e0bfac25a5 100644 --- a/data/txDupCache_test.go +++ b/data/txDupCache_test.go @@ -119,7 +119,8 @@ func TestTxHandlerSaltedCacheBasic(t *testing.T) { t.Parallel() const size = 20 - cache := makeSaltedCache(context.Background(), size, 0) + cache := makeSaltedCache(size) + cache.start(context.Background(), 0) require.Zero(t, cache.Len()) // add some unique random @@ -202,7 +203,8 @@ func TestTxHandlerSaltedCacheScheduled(t *testing.T) { const size = 20 updateInterval := 1000 * time.Microsecond - cache := makeSaltedCache(context.Background(), size, updateInterval) + cache := makeSaltedCache(size) + cache.start(context.Background(), updateInterval) require.Zero(t, cache.Len()) // add some unique random @@ -226,7 +228,8 @@ func TestTxHandlerSaltedCacheManual(t *testing.T) { t.Parallel() const size = 20 - cache := makeSaltedCache(context.Background(), 2*size, 0) + cache := makeSaltedCache(2 * size) + cache.start(context.Background(), 0) require.Zero(t, cache.Len()) // add some unique random @@ -290,7 +293,9 @@ func (m digestCacheMaker) make(size int) cachePusher { return &digestCachePusher{c: makeDigestCache(size)} } func (m saltedCacheMaker) make(size int) cachePusher { - return &saltedCachePusher{c: makeSaltedCache(context.Background(), size, 0)} + scp := &saltedCachePusher{c: makeSaltedCache(size)} + scp.c.start(context.Background(), 0) + return scp } type digestCachePusher struct { diff --git a/data/txHandler.go b/data/txHandler.go index 372c6aaaab..118f0979b3 100644 --- a/data/txHandler.go +++ b/data/txHandler.go @@ -123,7 +123,6 @@ func MakeTxHandler(opts TxHandlerOpts) *TxHandler { return nil } - ctx, ctxCancel := context.WithCancel(context.Background()) handler := &TxHandler{ txPool: opts.TxPool, genesisID: opts.GenesisID, @@ -133,22 +132,17 @@ func MakeTxHandler(opts TxHandlerOpts) *TxHandler { backlogQueue: make(chan *txBacklogMsg, txBacklogSize), postVerificationQueue: make(chan *txBacklogMsg, txBacklogSize), net: opts.Net, - msgCache: makeSaltedCache(ctx, 2*txBacklogSize, 60*time.Second), + msgCache: makeSaltedCache(2 * txBacklogSize), txCanonicalCache: makeDigestCache(2 * txBacklogSize), cacheConfig: txHandlerConfig{opts.Config.TxFilterRawMsgEnabled(), opts.Config.TxFilterCanonicalEnabled()}, - ctx: ctx, - ctxCancel: ctxCancel, } return handler } // Start enables the processing of incoming messages at the transaction handler func (handler *TxHandler) Start() { - if handler.ctx.Err() != nil { - // existing context has gone, create a new one - handler.ctx, handler.ctxCancel = context.WithCancel(context.Background()) - handler.msgCache = makeSaltedCache(handler.ctx, 2*txBacklogSize, 60*time.Second) - } + handler.ctx, handler.ctxCancel = context.WithCancel(context.Background()) + handler.msgCache.start(handler.ctx, 60*time.Second) handler.net.RegisterHandlers([]network.TaggedMessageHandler{ {Tag: protocol.TxnTag, MessageHandler: network.HandlerFunc(handler.processIncomingTxn)}, }) diff --git a/data/txHandler_test.go b/data/txHandler_test.go index 5efe1b3a30..dd9f5fea1f 100644 --- a/data/txHandler_test.go +++ b/data/txHandler_test.go @@ -725,12 +725,14 @@ func makeTestTxHandlerOrphanedWithContext(ctx context.Context, backlogSize int, if cacheSize <= 0 { cacheSize = txBacklogSize } - return &TxHandler{ + handler := &TxHandler{ backlogQueue: make(chan *txBacklogMsg, backlogSize), - msgCache: makeSaltedCache(ctx, cacheSize, refreshInterval), + msgCache: makeSaltedCache(cacheSize), txCanonicalCache: makeDigestCache(cacheSize), cacheConfig: txHandlerConfig{true, true}, } + handler.msgCache.start(ctx, refreshInterval) + return handler } func makeTestTxHandler(dl *Ledger, cfg config.Local) *TxHandler {