Skip to content

Commit

Permalink
All headtracker test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
yongkangc committed Jul 12, 2023
1 parent 3e814e6 commit 46d01bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func NewBlocks(t *testing.T, numHashes int) *Blocks {
if i > 0 {
parent := heads[i-1]
heads[i].Parent = parent
heads[i].Block.PreviousBlockhash = parent.BlockHash().Hash
}
}

Expand All @@ -128,6 +129,7 @@ func (b *Blocks) NewHead(number uint64) *types.Head {

head := Head(number)
head.Parent = parent
head.Block.PreviousBlockhash = parent.BlockHash().Hash

return head
}
Expand All @@ -142,6 +144,7 @@ func (b *Blocks) ForkAt(t *testing.T, blockNum int64, numHashes int) *Blocks {
forked.Heads[i] = b.Heads[i]
}

forked.Heads[blockNum].Block.PreviousBlockhash = b.Heads[blockNum].Block.PreviousBlockhash
forked.Heads[blockNum].Parent = b.Heads[blockNum].Parent
return forked
}
Expand Down
29 changes: 28 additions & 1 deletion pkg/solana/headtracker/head_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T)
chchHeaders := make(chan testutils.RawSub[*types.Head], 1)
checker := commonmocks.NewHeadTrackable[*types.Head, types.Hash](t)
ht := createHeadTrackerWithChecker(t, cfg, client, headSaver, checker)

cfg.SetHeadTrackerSamplingInterval(2500 * time.Millisecond)
cfg.SetHeadTrackerMaxBufferSize(100)

client.On("SubscribeNewHead", mock.Anything, mock.Anything).
Return(
Expand Down Expand Up @@ -461,6 +463,16 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T)
headSeq.Append(blocksForked.Head(4))
headSeq.Append(blocksForked.Head(5)) // Now the new chain is longer

// Print out all the block and the parent block
for i := 0; i < len(headSeq.Heads); i++ {
h := headSeq.Heads[i]
if h.Parent == nil {
fmt.Println("headSeq h.BlockNumber, hash:", h.BlockNumber(), h.BlockHash(), "Parent: nil")
} else {
fmt.Println("headSeq", h.BlockNumber(), h.BlockHash(), "Parent: ", h.Parent.BlockNumber(), h.Parent.BlockHash())
}
}

lastLongestChainAwaiter := cltest.NewAwaiter()

// the callback is only called for head number 5 because of head sampling
Expand All @@ -472,6 +484,8 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T)
assert.Equal(t, int64(5), h.BlockNumber())
assert.Equal(t, blocksForked.Head(5).BlockHash(), h.BlockHash())

fmt.Println("\n\n H Parent: ", h.Parent) // Why is the parent nil?

// This is the new longest chain, check that it came with its parents
if !assert.NotNil(t, h.Parent) {
return
Expand All @@ -488,7 +502,20 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T)
if !assert.NotNil(t, h.Parent.Parent.Parent.Parent) {
return
}
assert.Equal(t, h.Parent.Parent.Parent.Parent.BlockHash(), blocksForked.Head(1).BlockHash())
// h is 5, h.parent.parent.parent.parent is 1
fmt.Println("Inconsistency of block number:", h.Parent.Parent.Parent.Parent.BlockNumber())
// Print out all parent block number
fmt.Println("h Blocknumber", h.BlockNumber())
fmt.Println("Parent 1 Blocknumber", h.Parent.BlockNumber())
fmt.Println("Parent 2 Blocknumber", h.Parent.Parent.BlockNumber())
fmt.Println("Parent 3 Blocknumber", h.Parent.Parent.Parent.BlockNumber(), h.Parent.Parent.Parent.BlockHash())
fmt.Println("Parent 4 Blocknumber, hash", h.Parent.Parent.Parent.Parent.BlockNumber(), h.Parent.Parent.Parent.Parent.BlockHash())

// Why does it have 2 parents?
// 5->4->3->2->2->1
// Why is 2 he parent of 2

assert.Equal(t, blocksForked.Head(1).BlockHash(), h.Parent.Parent.Parent.Parent.BlockHash())
lastLongestChainAwaiter.ItHappened()
}).Return().Once()

Expand Down

0 comments on commit 46d01bb

Please sign in to comment.