Skip to content

Commit

Permalink
fixed bug with block rollback; re-orgs should work now
Browse files Browse the repository at this point in the history
  • Loading branch information
BrannonKing committed Aug 27, 2021
1 parent 1834d95 commit 7998772
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
6 changes: 4 additions & 2 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,10 @@ func (b *BlockChain) disconnectBlock(node *blockNode, block *btcutil.Block, view
return err
}

if err = b.claimTrie.ResetHeight(node.parent.height); err != nil {
return err
if b.claimTrie != nil {
if err = b.claimTrie.ResetHeight(node.parent.height); err != nil {
return err
}
}

// Prune fully spent entries and mark all entries in the view unmodified
Expand Down
3 changes: 2 additions & 1 deletion claimtrie/claimtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,13 @@ func (ct *ClaimTrie) ResetHeight(height int32) error {
}

passedHashFork := ct.height >= param.ActiveParams.AllClaimsInMerkleForkHeight && height < param.ActiveParams.AllClaimsInMerkleForkHeight
ct.height = height
hash, err := ct.blockRepo.Get(height)
if err != nil {
return err
}

ct.height = height // keep this before the rebuild

if passedHashFork {
names = nil // force them to reconsider all names
}
Expand Down
7 changes: 6 additions & 1 deletion claimtrie/claimtrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,14 @@ func BenchmarkClaimTrie_AppendBlock(b *testing.B) {
b.StopTimer()
ht := ct.height
h1 = *ct.MerkleHash()
ct.Close()
b.Logf("Running AppendBlock bench with %d names in %f sec. Height: %d, Hash: %s",
b.N, time.Since(start).Seconds(), ht, h1.String())

// a very important test of the functionality:
for ct.height > 0 {
err = ct.ResetHeight(ct.height - 1)
r.NoError(err)
}
}

func randomName() []byte {
Expand Down
2 changes: 1 addition & 1 deletion claimtrie/merkletrie/collapsedtrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestNilNameHandling(t *testing.T) {
}

func TestCollapsedTriePerformance(t *testing.T) {
inserts := 10000 // increase this to 1M for more interesting results
inserts := 100000 // increase this to 1M for more interesting results
data := make([][]byte, inserts)
rand.Seed(42)
for i := 0; i < inserts; i++ {
Expand Down
8 changes: 1 addition & 7 deletions claimtrie/merkletrie/ramtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@ func (rt *RamTrie) SetRoot(h *chainhash.Hash) error {
return nil
}

// should technically clear the old trie first:
if rt.Nodes > 1 {
rt.Root = &collapsedVertex{key: make(KeyType, 0)}
rt.Nodes = 1
runtime.GC()
}

// should technically clear the old trie first, but this is abused for partial rebuilds so don't
return ErrFullRebuildRequired
}

Expand Down
2 changes: 1 addition & 1 deletion claimtrie/node/noderepo/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (repo *Pebble) DropChanges(name []byte, finalHeight int32) error {
return errors.Wrapf(err, "in load changes for %s", name)
}
i := 0
for ; i < len(changes); i++ {
for ; i < len(changes); i++ { // assuming changes are ordered by height
if changes[i].Height > finalHeight {
break
}
Expand Down
1 change: 1 addition & 0 deletions goclean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
set -ex

env GORACE="halt_on_error=1" go test -race -tags="rpctest" -covermode atomic -coverprofile=profile.cov ./...
go test -bench=. -benchtime=4000x ./claimtrie/

# Automatic checks
golangci-lint run --deadline=10m --disable-all \
Expand Down

0 comments on commit 7998772

Please sign in to comment.