From 7ddbe68d4c6c5de34add978416cee43906266e71 Mon Sep 17 00:00:00 2001 From: Matheus Bach <35426162+matheusbach@users.noreply.github.com> Date: Tue, 14 Mar 2023 20:29:08 -0300 Subject: [PATCH] [fix] queue.Peek() now return last block hash instead first --- pkg/phantom/queue.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/phantom/queue.go b/pkg/phantom/queue.go index a571809..b825535 100644 --- a/pkg/phantom/queue.go +++ b/pkg/phantom/queue.go @@ -97,14 +97,19 @@ func (q *Queue) Pop() *chainhash.Hash { } func (q *Queue) Peek() *chainhash.Hash { - q.muxex.Lock() - - defer q.muxex.Unlock() - - if q.count == 0 { - return nil - } - return q.nodes[q.head] + q.muxex.Lock() + defer q.muxex.Unlock() + + if q.count == 0 { + return nil + } + + // Get the last element by accessing the element at the tail index + index := q.tail - 1 + if index < 0 { + index = len(q.nodes) - 1 + } + return q.nodes[index] } func (q *Queue) Len() int { @@ -113,4 +118,4 @@ func (q *Queue) Len() int { defer q.muxex.Unlock() return q.count -} \ No newline at end of file +}