-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blockchain: better Ancestor with skiplists
On startup, Ancestor call was taking a lot of time when the node was loading the blockindex onto memory. This change speeds up the Ancestor function significantly and speeds up the node during startup. On testnet3 at blockheight ~2,500,000, the startup was around 30seconds on current main and was 5 seconds with this change. Below is a benchstat result showing the significant speedup. goos: darwin goarch: arm64 pkg: github.com/utreexo/utreexod/blockchain │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ Ancestor-8 120819.301µ ± 5% 7.013µ ± 19% -99.99% (p=0.000 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ Ancestor-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ Ancestor-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal
- Loading branch information
1 parent
7bbd9b0
commit 126d920
Showing
3 changed files
with
94 additions
and
2 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2023 The utreexo developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package blockchain | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
func TestAncestor(t *testing.T) { | ||
height := 500_000 | ||
blockNodes := chainedNodes(nil, height) | ||
|
||
for i, blockNode := range blockNodes { | ||
// Grab a random node that's a child of this node | ||
// and try to fetch the current blockNode with Ancestor. | ||
randNode := blockNodes[rand.Intn(height-i)+i] | ||
got := randNode.Ancestor(blockNode.height) | ||
|
||
// See if we got the right one. | ||
if got.hash != blockNode.hash { | ||
t.Fatalf("expected ancestor at height %d "+ | ||
"but got a node at height %d", | ||
blockNode.height, got.height) | ||
} | ||
|
||
// Gensis doesn't have ancestors so skip the check below. | ||
if blockNode.height == 0 { | ||
continue | ||
} | ||
|
||
// The ancestors are deterministic so check that this node's | ||
// ancestor is the correct one. | ||
if blockNode.ancestor.height != getAncestorHeight(blockNode.height) { | ||
t.Fatalf("expected anestor at height %d, but it was at %d", | ||
getAncestorHeight(blockNode.height), | ||
blockNode.ancestor.height) | ||
} | ||
} | ||
} |