From 09186a414a37ce7b90a0beb0705cde82783a75bd Mon Sep 17 00:00:00 2001 From: msutton Date: Fri, 8 Jul 2022 17:23:59 +0300 Subject: [PATCH] Make sure the block at the split point is reversed to new chain as well --- .../resolve_virtual_test.go | 56 +++++++++++-------- .../reverse_utxo_diffs.go | 12 ++-- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/domain/consensus/processes/consensusstatemanager/resolve_virtual_test.go b/domain/consensus/processes/consensusstatemanager/resolve_virtual_test.go index 8d8dceedc..1822e5aae 100644 --- a/domain/consensus/processes/consensusstatemanager/resolve_virtual_test.go +++ b/domain/consensus/processes/consensusstatemanager/resolve_virtual_test.go @@ -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") } diff --git a/domain/consensus/processes/consensusstatemanager/reverse_utxo_diffs.go b/domain/consensus/processes/consensusstatemanager/reverse_utxo_diffs.go index b8f388d20..49d2b24bd 100644 --- a/domain/consensus/processes/consensusstatemanager/reverse_utxo_diffs.go +++ b/domain/consensus/processes/consensusstatemanager/reverse_utxo_diffs.go @@ -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