Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yongkangc committed Jul 13, 2023
1 parent 46d01bb commit 9e9aff2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 63 deletions.
57 changes: 1 addition & 56 deletions pkg/solana/headtracker/head_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package headtracker_test
import (
"context"
"errors"
"fmt"
"math"
"math/big"
"sync"
Expand Down Expand Up @@ -463,29 +462,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
checker.On("OnNewLongestChain", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
h := args.Get(1).(*types.Head)
fmt.Println("OnNewLongestChain", h.BlockNumber())

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 @@ -502,18 +488,6 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T)
if !assert.NotNil(t, h.Parent.Parent.Parent.Parent) {
return
}
// 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()
Expand Down Expand Up @@ -559,7 +533,6 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T)
}
}

// TODO: Fix this test later
func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingDisabled(t *testing.T) {
t.Parallel()
lggr, _ := logger.New()
Expand All @@ -586,7 +559,7 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingDisabled(t *testing.T
// ---------------------
blocks := cltest.NewBlocks(t, 10)

head0 := blocks.Head(0) // types.Head{Number: 0, Hash: utils.NewSolanaHash(), ParentHash: utils.NewSolanaHash(), Timestamp: time.Unix(0, 0)}
head0 := blocks.Head(0)
// Initial query
client.On("HeadByNumber", mock.Anything, (*big.Int)(nil)).Return(head0, nil)

Expand All @@ -611,21 +584,6 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingDisabled(t *testing.T
headSeq.Append(blocksForked.Head(4))
headSeq.Append(blocksForked.Head(5)) // Now the new chain is longer

// --------------------- Delete
// Print HeadSequence in a nice format
fmt.Println("headSeq", headSeq.Heads)

// Iterate over Head sequence, print head.BlockHash() and parent.BlockHash()
// Add new line after each head
for _, h := range headSeq.Heads {
if h.Parent == nil {
fmt.Printf("head %s, parent nil \n\n", h.BlockHash())
continue
}
fmt.Printf("head hash %s head number %d, parent hash %s parent number %d \n\n ", h.BlockHash(), h.BlockNumber(), h.Parent.BlockHash(), h.Parent.BlockNumber())
}
// --------------------- Delete

lastLongestChainAwaiter := cltest.NewAwaiter()

checker.On("OnNewLongestChain", mock.Anything, mock.Anything).
Expand All @@ -640,22 +598,13 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingDisabled(t *testing.T
h := args.Get(1).(*types.Head)
require.Equal(t, int64(1), h.BlockNumber())
require.Equal(t, blocks.Head(1).BlockHash(), h.BlockHash())

fmt.Println("good h number, hash", h.BlockNumber(), h.BlockHash())

}).Return().Once()

checker.On("OnNewLongestChain", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
h := args.Get(1).(*types.Head)
require.Equal(t, int64(3), h.BlockNumber())
require.Equal(t, blocks.Head(3).BlockHash(), h.BlockHash())

// Get parent parent parent
fmt.Println("problematic h number, hash", h.BlockNumber(), h.BlockHash())

// Get parent
fmt.Println("Parent of problematic h", h.Parent)
}).Return().Once()

checker.On("OnNewLongestChain", mock.Anything, mock.Anything).
Expand Down Expand Up @@ -753,10 +702,6 @@ func TestHeadTracker_Backfill(t *testing.T) {
h1 := cltest.Head(1)
h1.Block.PreviousBlockhash = head0.BlockHash().Hash

fmt.Println("\n\n head0 Blockhash \n", head0.BlockHash().Hash)
fmt.Println("\n\n h1 Blockhash \n", h1.GetParentHash())
fmt.Println("h1 Blockhash \n", h1.Block.PreviousBlockhash)

h8 := cltest.Head(8)

h9 := cltest.Head(9)
Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/headtracker/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (id ChainID) String() string {
case Localnet:
return "localnet"
default:
return "localnet"
return "unknown"
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/headtracker/types/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (h *Head) HasChainID() bool {
if h == nil {
return false
}
return h.ChainID().String() != "unknown" // TODO: Refactor this into a more coherent check
return h.ChainID().String() != "unknown"
}

func (h *Head) IsValid() bool {
Expand Down
26 changes: 21 additions & 5 deletions pkg/solana/headtracker/types/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,30 @@ func TestHead_EarliestHeadInChain(t *testing.T) {
}

func TestHead_GetParentHash(t *testing.T) {
blockResult := cltest.ConfigureBlockResult()
id := types.Mainnet

head := types.NewHead(3, blockResult,
types.NewHead(2, blockResult,
types.NewHead(1, blockResult, nil, id), id), id)
blockResult0 := cltest.ConfigureBlockResult()
h0 := types.NewHead(0, blockResult0, nil, id)

blockResult1 := cltest.ConfigureBlockResult()
blockResult1.ParentSlot = 0
blockResult1.PreviousBlockhash = blockResult0.Blockhash
h1 := types.NewHead(1, blockResult1, h0, id)

blockResult2 := cltest.ConfigureBlockResult()
blockResult2.ParentSlot = 1
blockResult2.PreviousBlockhash = blockResult1.Blockhash
h2 := types.NewHead(2, blockResult2, h1, id)

blockResult3 := cltest.ConfigureBlockResult()
blockResult3.ParentSlot = 2
blockResult3.PreviousBlockhash = blockResult2.Blockhash
h3 := types.NewHead(3, blockResult3, h2, id)

assert.Equal(t, head.Parent.BlockHash(), head.GetParentHash())
// h3 -> h2 -> h1 -> h0
assert.Equal(t, h2.BlockHash(), h3.GetParentHash())
assert.Equal(t, h1.BlockHash(), h2.GetParentHash())
assert.Equal(t, h0.BlockHash(), h1.GetParentHash())
}

func TestHead_GetParent(t *testing.T) {
Expand Down

0 comments on commit 9e9aff2

Please sign in to comment.