diff --git a/pkg/solana/logpoller/loader.go b/pkg/solana/logpoller/loader.go index e717002b9..12940367c 100644 --- a/pkg/solana/logpoller/loader.go +++ b/pkg/solana/logpoller/loader.go @@ -323,7 +323,7 @@ type orderedParser struct { func newOrderedParser(parser ProgramEventProcessor, lggr logger.Logger) *orderedParser { op := &orderedParser{ parser: parser, - blocks: &blockHeap{}, + blocks: NewBlockHeap(), ready: make([]uint64, 0), expect: make(map[uint64]int), actual: make(map[uint64][]ProgramEvent), @@ -335,8 +335,6 @@ func newOrderedParser(parser ProgramEventProcessor, lggr logger.Logger) *ordered Close: op.close, }.NewServiceEngine(lggr) - heap.Init(op.blocks) - return op } @@ -487,6 +485,12 @@ var ( errExpectationsNotSet = errors.New("expectations not set") ) +func NewBlockHeap() *blockHeap { + h := &blockHeap{} + heap.Init(h) + return h +} + type blockHeap []uint64 func (h blockHeap) Len() int { return len(h) } diff --git a/pkg/solana/logpoller/loader_test.go b/pkg/solana/logpoller/loader_test.go index b4a0b6215..1b7fd7933 100644 --- a/pkg/solana/logpoller/loader_test.go +++ b/pkg/solana/logpoller/loader_test.go @@ -1,9 +1,11 @@ package logpoller_test import ( + "container/heap" "context" "crypto/rand" "reflect" + "slices" "sync" "sync/atomic" "testing" @@ -19,7 +21,7 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/utils/tests" "github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller" - mocks "github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller/mocks" + "github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller/mocks" ) var ( @@ -110,6 +112,44 @@ func TestEncodedLogCollector_ParseSingleEvent(t *testing.T) { }) } +func TestBlockHeap(t *testing.T) { + testBlocks := []uint64{53, 19, 105, 21, 6, 9} + expected := make([]uint64, len(testBlocks)) + copy(expected, testBlocks) + slices.Sort(expected) + + blocks := logpoller.NewBlockHeap() + for _, b := range testBlocks { + heap.Push(blocks, b) + } + + // Current iteration pattern: fails + for i, b := range *blocks { + assert.Equal(t, expected[i], b) + } + + // Suggested iteration pattern + for _, e := range expected { + block := (*blocks)[0] + assert.Equal(t, e, block) + // + // If waiting on information for block + // break + // + // If expectations 0 + // heap.Pop(blocks) + // continue + // + // If expectations met + // ExpectBlock(block) + // heap.Pop(blocks) + // + assert.Equal(t, e, heap.Pop(blocks)) + + // Else: return + } +} + func TestEncodedLogCollector_MultipleEventOrdered(t *testing.T) { t.Parallel()