Skip to content

Commit

Permalink
fix unit tests and add lock to a map
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Apr 25, 2024
1 parent 300cf75 commit f4b86ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
35 changes: 25 additions & 10 deletions blockpoller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math"
"sync"
"time"

"github.com/streamingfast/bstream"
Expand Down Expand Up @@ -41,7 +42,8 @@ type BlockPoller struct {

optimisticallyPolledBlocks map[uint64]*BlockItem

fetching bool
fetching bool
optimisticallyPolledBlocksLock sync.Mutex
}

func New(
Expand Down Expand Up @@ -109,18 +111,20 @@ func (p *BlockPoller) run(resolvedStartBlock bstream.BlockRef, numberOfBlockToFe

for {
requestedBlockItem := p.requestBlock(blockToFetch, numberOfBlockToFetch)
fetchedBlockItem := <-requestedBlockItem

if fetchedBlockItem.skipped {
p.logger.Info("block was skipped", zap.Uint64("block_num", fetchedBlockItem.blockNumber))
blockToFetch++
continue
fetchedBlockItem, ok := <-requestedBlockItem
if !ok {
p.logger.Info("requested block channel was closed, quitting")
return nil
}
if !fetchedBlockItem.skipped {
fetchedBlock = fetchedBlockItem.block
break
}

fetchedBlock = fetchedBlockItem.block
break
}
p.logger.Info("block was skipped", zap.Uint64("block_num", fetchedBlockItem.blockNumber))
blockToFetch++

}
}

if err != nil {
Expand Down Expand Up @@ -236,7 +240,9 @@ func (p *BlockPoller) loadNextBlocks(requestedBlock uint64, numberOfBlockToFetch
done := make(chan interface{}, 1)
go func() {
for blockItem := range nailer.Out {
p.optimisticallyPolledBlocksLock.Lock()
p.optimisticallyPolledBlocks[blockItem.blockNumber] = blockItem
p.optimisticallyPolledBlocksLock.Unlock()
}
close(done)
}()
Expand Down Expand Up @@ -281,7 +287,16 @@ func (p *BlockPoller) requestBlock(blockNumber uint64, numberOfBlockToFetch int)

go func(requestedBlock chan *BlockItem) {
for {

if p.IsTerminating() {
close(requestedBlock)
p.logger.Info("block poller is terminating")
return
}

p.optimisticallyPolledBlocksLock.Lock()
blockItem, found := p.optimisticallyPolledBlocks[blockNumber]
p.optimisticallyPolledBlocksLock.Unlock()
if !found {
if !p.fetching {
go func() {
Expand Down
7 changes: 4 additions & 3 deletions blockpoller/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ func TestForkHandler_run(t *testing.T) {
blockFetcher := newTestBlockFetcher(t, tt.blocks)
blockFinalizer := newTestBlockFinalizer(t, tt.expectFireBlock)

f := New(blockFetcher, blockFinalizer)
f.forkDB = forkable.NewForkDB()
poller := New(blockFetcher, blockFinalizer)
poller.fetchBlockRetryCount = 0
poller.forkDB = forkable.NewForkDB()

err := f.run(tt.startBlock, 1)
err := poller.run(tt.startBlock, 1)
if !errors.Is(err, TestErrCompleteDone) {
require.NoError(t, err)
}
Expand Down

0 comments on commit f4b86ff

Please sign in to comment.