Make sure the block at the split point is reversed to new chain as well

This commit is contained in:
msutton 2022-07-08 17:23:59 +03:00
parent 9c732e6878
commit 09186a414a
2 changed files with 39 additions and 29 deletions

View File

@ -3,6 +3,7 @@ package consensusstatemanager_test
import (
"fmt"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/testapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"testing"
@ -229,7 +230,8 @@ func TestResolveVirtualMess(t *testing.T) {
hashes := []*externalapi.DomainHash{consensusConfig.GenesisHash}
blocks := make(map[externalapi.DomainHash]string)
blocks[*consensusConfig.GenesisHash] = "g"
fmt.Printf("%s\n\n", consensusConfig.GenesisHash)
blocks[*model.VirtualBlockHash] = "v"
//fmt.Printf("%s\n\n", consensusConfig.GenesisHash)
// Create a chain of blocks
const initialChainLength = 6
@ -238,17 +240,17 @@ func TestResolveVirtualMess(t *testing.T) {
previousBlockHash, _, err = tc.AddBlock([]*externalapi.DomainHash{previousBlockHash}, nil, nil)
blocks[*previousBlockHash] = fmt.Sprintf("A_%d", i)
hashes = append(hashes, previousBlockHash)
fmt.Printf("A_%d: %s\n", i, previousBlockHash)
//fmt.Printf("A_%d: %s\n", i, previousBlockHash)
if err != nil {
t.Fatalf("Error mining block no. %d in initial chain: %+v", i, err)
}
}
fmt.Printf("\n")
//fmt.Printf("\n")
// Mine a chain with more blocks, to re-organize the DAG
const reorgChainLength = initialChainLength + 1
const reorgChainLength = 20 // initialChainLength + 1
previousBlockHash = consensusConfig.GenesisHash
for i := 0; i < reorgChainLength; i++ {
previousBlock, _, err := tc.BuildBlockWithParents([]*externalapi.DomainHash{previousBlockHash}, nil, nil)
@ -258,7 +260,7 @@ func TestResolveVirtualMess(t *testing.T) {
previousBlockHash = consensushashing.BlockHash(previousBlock)
blocks[*previousBlockHash] = fmt.Sprintf("B_%d", i)
hashes = append(hashes, previousBlockHash)
fmt.Printf("B_%d: %s\n", i, previousBlockHash)
//fmt.Printf("B_%d: %s\n", i, previousBlockHash)
// Do not UTXO validate in order to resolve virtual later
err = tc.ValidateAndInsertBlock(previousBlock, false)
@ -267,44 +269,52 @@ func TestResolveVirtualMess(t *testing.T) {
}
}
fmt.Printf("\n")
//fmt.Printf("\n")
//printUtxoDiffChildren(t, hashes, tc, blocks)
// Resolve one step
_, err = tc.ResolveVirtualWithMaxParam(2)
_, err = tc.ResolveVirtualWithMaxParam(3)
if err != nil {
printUtxoDiffChildren(t, hashes, tc, blocks)
t.Fatalf("Error resolving virtual in re-org chain: %+v", err)
}
// Resolve one more step
isCompletelyResolved, err := tc.ResolveVirtualWithMaxParam(2)
isCompletelyResolved, err := tc.ResolveVirtualWithMaxParam(3)
if err != nil {
t.Fatalf("Error resolving virtual in re-org chain: %+v", err)
}
// Complete resolving virtual
for !isCompletelyResolved {
isCompletelyResolved, err = tc.ResolveVirtualWithMaxParam(2)
isCompletelyResolved, err = tc.ResolveVirtualWithMaxParam(3)
if err != nil {
t.Fatalf("Error resolving virtual in re-org chain: %+v", err)
}
}
fmt.Printf("Block\t\tDiff child\n")
stagingArea := model.NewStagingArea()
for _, block := range hashes {
hasUTXODiffChild, err := tc.UTXODiffStore().HasUTXODiffChild(tc.DatabaseContext(), stagingArea, block)
//printUtxoDiffChildren(t, hashes, tc, blocks)
})
}
func printUtxoDiffChildren(t *testing.T, hashes []*externalapi.DomainHash, tc testapi.TestConsensus, blocks map[externalapi.DomainHash]string) {
fmt.Printf("\n===============================\nBlock\t\tDiff child\n")
stagingArea := model.NewStagingArea()
for _, block := range hashes {
hasUTXODiffChild, err := tc.UTXODiffStore().HasUTXODiffChild(tc.DatabaseContext(), stagingArea, block)
if err != nil {
t.Fatalf("Error while reading utxo diff store: %+v", err)
}
if hasUTXODiffChild {
utxoDiffChild, err := tc.UTXODiffStore().UTXODiffChild(tc.DatabaseContext(), stagingArea, block)
if err != nil {
t.Fatalf("Error while reading utxo diff store: %+v", err)
}
if hasUTXODiffChild {
utxoDiffChild, err := tc.UTXODiffStore().UTXODiffChild(tc.DatabaseContext(), stagingArea, block)
if err != nil {
t.Fatalf("Error while reading utxo diff store: %+v", err)
}
fmt.Printf("%s\t\t\t%s\n", blocks[*block], blocks[*utxoDiffChild])
} else {
fmt.Printf("%s\n", blocks[*block])
}
fmt.Printf("%s\t\t\t%s\n", blocks[*block], blocks[*utxoDiffChild])
} else {
fmt.Printf("%s\n", blocks[*block])
}
})
}
fmt.Printf("\n===============================\n")
}

View File

@ -56,12 +56,6 @@ func (csm *consensusStateManager) ReverseUTXODiffs(tipHash *externalapi.DomainHa
return err
}
// We stop reversing when current's UTXODiffChild is not current's SelectedParent
if !currentBlockGHOSTDAGData.SelectedParent().Equal(currentBlockUTXODiffChild) {
log.Debugf("Block %s's UTXODiffChild is not it's selected parent - finish reversing", currentBlock)
break
}
currentUTXODiff := previousUTXODiff.Reversed()
// retrieve current utxoDiff for Bi, to be used by next block
@ -75,6 +69,12 @@ func (csm *consensusStateManager) ReverseUTXODiffs(tipHash *externalapi.DomainHa
return err
}
// We stop reversing when current's UTXODiffChild is not current's SelectedParent
if !currentBlockGHOSTDAGData.SelectedParent().Equal(currentBlockUTXODiffChild) {
log.Debugf("Block %s's UTXODiffChild is not it's selected parent - finish reversing", currentBlock)
break
}
previousBlock = currentBlock
previousBlockGHOSTDAGData = currentBlockGHOSTDAGData