-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(lib/blocktree): removes the inconsistency to choose a deepest leaf #2094
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c87313f
fix: removes the inconsistency to choose a deepest leaf
EclesioMeloJunior 1fe6740
chore: add test to currentDeepestNode
EclesioMeloJunior de90ca3
chore: removing prints
EclesioMeloJunior 41146d6
chore: right identation in the comment block
EclesioMeloJunior 2d94bc4
chore: fix typo
EclesioMeloJunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ func createTestBlockTree(t *testing.T, header *types.Header, number int) (*Block | |
hash := header.Hash() | ||
err := bt.AddBlock(header, time.Unix(0, at)) | ||
require.NoError(t, err) | ||
|
||
previousHash = hash | ||
|
||
isBranch := r.Intn(2) | ||
|
@@ -88,8 +89,10 @@ func createTestBlockTree(t *testing.T, header *types.Header, number int) (*Block | |
hash := header.Hash() | ||
err := bt.AddBlock(header, time.Unix(0, at)) | ||
require.NoError(t, err) | ||
|
||
previousHash = hash | ||
at += int64(r.Intn(8)) | ||
|
||
} | ||
} | ||
|
||
|
@@ -474,6 +477,111 @@ func TestBlockTree_GetHashByNumber(t *testing.T) { | |
require.Error(t, err) | ||
} | ||
|
||
func TestBlockTree_AllLeavesHasSameNumberAndArrivalTime_DeepestBlockHash_ShouldHasConsistentOutput(t *testing.T) { | ||
bt := NewBlockTreeFromRoot(testHeader) | ||
previousHash := testHeader.Hash() | ||
|
||
branches := []testBranch{} | ||
|
||
const fixedArrivalTime = 99 | ||
const deep = 8 | ||
|
||
// create a base tree with a fixed amount of blocks | ||
// and all block with the same arrival time | ||
|
||
/** | ||
base tree and nodes representation, all with the same arraival time and all | ||
the leaves has the same number (8) the numbers in the right represents the order | ||
the nodes are inserted into the blocktree. | ||
|
||
a -> b -> c -> d -> e -> f -> g -> h (1) | ||
| | | | | |> h (7) | ||
| | | | |> g -> h (6) | ||
| | | |> f -> g -> h (5) | ||
| | |> e -> f -> g -> h (4) | ||
| |> d -> e -> f -> g -> h (3) | ||
|> c -> d -> e -> f -> g -> h (2) | ||
**/ | ||
|
||
for i := 1; i <= deep; i++ { | ||
header := &types.Header{ | ||
ParentHash: previousHash, | ||
Number: big.NewInt(int64(i)), | ||
Digest: types.NewDigest(), | ||
} | ||
|
||
hash := header.Hash() | ||
|
||
err := bt.AddBlock(header, time.Unix(0, fixedArrivalTime)) | ||
require.NoError(t, err) | ||
|
||
previousHash = hash | ||
|
||
// the last block on the base tree should not generates a branch | ||
if i < deep { | ||
branches = append(branches, testBranch{ | ||
hash: hash, | ||
number: bt.getNode(hash).number, | ||
}) | ||
} | ||
} | ||
|
||
// create all the branch nodes with the same arraival time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo here too |
||
for _, branch := range branches { | ||
previousHash = branch.hash | ||
|
||
for i := int(branch.number.Uint64()); i < deep; i++ { | ||
header := &types.Header{ | ||
ParentHash: previousHash, | ||
Number: big.NewInt(int64(i) + 1), | ||
StateRoot: common.Hash{0x1}, | ||
Digest: types.NewDigest(), | ||
} | ||
|
||
hash := header.Hash() | ||
err := bt.AddBlock(header, time.Unix(0, fixedArrivalTime)) | ||
require.NoError(t, err) | ||
|
||
previousHash = hash | ||
} | ||
} | ||
|
||
// check all leaves has the same number and timestamps | ||
leaves := bt.leaves.nodes() | ||
for idx := 0; idx < len(leaves)-2; idx++ { | ||
curr := leaves[idx] | ||
next := leaves[idx+1] | ||
|
||
require.Equal(t, curr.number, next.number) | ||
require.Equal(t, curr.arrivalTime, next.arrivalTime) | ||
} | ||
|
||
require.Len(t, leaves, deep) | ||
|
||
// expects currentDeepestLeaf nil till call deepestLeaf() function | ||
require.Nil(t, bt.leaves.currentDeepestLeaf) | ||
deepestLeaf := bt.deepestLeaf() | ||
|
||
require.Equal(t, deepestLeaf, bt.leaves.currentDeepestLeaf) | ||
require.Contains(t, leaves, deepestLeaf) | ||
|
||
// adding a new node with a greater number, should update the currentDeepestLeaf | ||
header := &types.Header{ | ||
ParentHash: previousHash, | ||
Number: big.NewInt(int64(deepestLeaf.number.Uint64() + 1)), | ||
StateRoot: common.Hash{0x1}, | ||
Digest: types.NewDigest(), | ||
} | ||
|
||
hash := header.Hash() | ||
err := bt.AddBlock(header, time.Unix(0, fixedArrivalTime)) | ||
require.NoError(t, err) | ||
|
||
deepestLeaf = bt.deepestLeaf() | ||
require.Equal(t, hash, deepestLeaf.hash) | ||
require.Equal(t, hash, bt.leaves.currentDeepestLeaf.hash) | ||
} | ||
|
||
func TestBlockTree_DeepCopy(t *testing.T) { | ||
bt, _ := createFlatTree(t, 8) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo at arraival