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 ( import (
"fmt" "fmt"
"github.com/kaspanet/kaspad/domain/consensus/model" "github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/testapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing" "github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"testing" "testing"
@ -229,7 +230,8 @@ func TestResolveVirtualMess(t *testing.T) {
hashes := []*externalapi.DomainHash{consensusConfig.GenesisHash} hashes := []*externalapi.DomainHash{consensusConfig.GenesisHash}
blocks := make(map[externalapi.DomainHash]string) blocks := make(map[externalapi.DomainHash]string)
blocks[*consensusConfig.GenesisHash] = "g" 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 // Create a chain of blocks
const initialChainLength = 6 const initialChainLength = 6
@ -238,17 +240,17 @@ func TestResolveVirtualMess(t *testing.T) {
previousBlockHash, _, err = tc.AddBlock([]*externalapi.DomainHash{previousBlockHash}, nil, nil) previousBlockHash, _, err = tc.AddBlock([]*externalapi.DomainHash{previousBlockHash}, nil, nil)
blocks[*previousBlockHash] = fmt.Sprintf("A_%d", i) blocks[*previousBlockHash] = fmt.Sprintf("A_%d", i)
hashes = append(hashes, previousBlockHash) hashes = append(hashes, previousBlockHash)
fmt.Printf("A_%d: %s\n", i, previousBlockHash) //fmt.Printf("A_%d: %s\n", i, previousBlockHash)
if err != nil { if err != nil {
t.Fatalf("Error mining block no. %d in initial chain: %+v", i, err) 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 // Mine a chain with more blocks, to re-organize the DAG
const reorgChainLength = initialChainLength + 1 const reorgChainLength = 20 // initialChainLength + 1
previousBlockHash = consensusConfig.GenesisHash previousBlockHash = consensusConfig.GenesisHash
for i := 0; i < reorgChainLength; i++ { for i := 0; i < reorgChainLength; i++ {
previousBlock, _, err := tc.BuildBlockWithParents([]*externalapi.DomainHash{previousBlockHash}, nil, nil) previousBlock, _, err := tc.BuildBlockWithParents([]*externalapi.DomainHash{previousBlockHash}, nil, nil)
@ -258,7 +260,7 @@ func TestResolveVirtualMess(t *testing.T) {
previousBlockHash = consensushashing.BlockHash(previousBlock) previousBlockHash = consensushashing.BlockHash(previousBlock)
blocks[*previousBlockHash] = fmt.Sprintf("B_%d", i) blocks[*previousBlockHash] = fmt.Sprintf("B_%d", i)
hashes = append(hashes, previousBlockHash) 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 // Do not UTXO validate in order to resolve virtual later
err = tc.ValidateAndInsertBlock(previousBlock, false) err = tc.ValidateAndInsertBlock(previousBlock, false)
@ -267,29 +269,37 @@ func TestResolveVirtualMess(t *testing.T) {
} }
} }
fmt.Printf("\n") //fmt.Printf("\n")
//printUtxoDiffChildren(t, hashes, tc, blocks)
// Resolve one step // Resolve one step
_, err = tc.ResolveVirtualWithMaxParam(2) _, err = tc.ResolveVirtualWithMaxParam(3)
if err != nil { if err != nil {
printUtxoDiffChildren(t, hashes, tc, blocks)
t.Fatalf("Error resolving virtual in re-org chain: %+v", err) t.Fatalf("Error resolving virtual in re-org chain: %+v", err)
} }
// Resolve one more step // Resolve one more step
isCompletelyResolved, err := tc.ResolveVirtualWithMaxParam(2) isCompletelyResolved, err := tc.ResolveVirtualWithMaxParam(3)
if err != nil { if err != nil {
t.Fatalf("Error resolving virtual in re-org chain: %+v", err) t.Fatalf("Error resolving virtual in re-org chain: %+v", err)
} }
// Complete resolving virtual // Complete resolving virtual
for !isCompletelyResolved { for !isCompletelyResolved {
isCompletelyResolved, err = tc.ResolveVirtualWithMaxParam(2) isCompletelyResolved, err = tc.ResolveVirtualWithMaxParam(3)
if err != nil { if err != nil {
t.Fatalf("Error resolving virtual in re-org chain: %+v", err) t.Fatalf("Error resolving virtual in re-org chain: %+v", err)
} }
} }
fmt.Printf("Block\t\tDiff child\n") //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() stagingArea := model.NewStagingArea()
for _, block := range hashes { for _, block := range hashes {
hasUTXODiffChild, err := tc.UTXODiffStore().HasUTXODiffChild(tc.DatabaseContext(), stagingArea, block) hasUTXODiffChild, err := tc.UTXODiffStore().HasUTXODiffChild(tc.DatabaseContext(), stagingArea, block)
@ -306,5 +316,5 @@ func TestResolveVirtualMess(t *testing.T) {
fmt.Printf("%s\n", blocks[*block]) 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 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() currentUTXODiff := previousUTXODiff.Reversed()
// retrieve current utxoDiff for Bi, to be used by next block // retrieve current utxoDiff for Bi, to be used by next block
@ -75,6 +69,12 @@ func (csm *consensusStateManager) ReverseUTXODiffs(tipHash *externalapi.DomainHa
return err 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 previousBlock = currentBlock
previousBlockGHOSTDAGData = currentBlockGHOSTDAGData previousBlockGHOSTDAGData = currentBlockGHOSTDAGData