mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-03 04:36:51 +00:00

* Replaced the content of MsgIBDRootUTXOSetChunk with pairs of outpoint-utxo entry pairs. * Rename utxoIter to utxoIterator. * Add a big stinky TODO on an assert. * Replace pruningStore staging with a UTXO set iterator. * Reimplement receiveAndInsertIBDRootUTXOSet. * Extract OutpointAndUTXOEntryPairsToDomainOutpointAndUTXOEntryPairs into domainconverters.go. * Pass the outpoint and utxy entry pairs to the pruning store. * Implement InsertCandidatePruningPointUTXOs. * Implement ClearCandidatePruningPointUTXOs. * Implement UpdateCandidatePruningPointMultiset. * Use the candidate pruning point multiset in updatePruningPoint. * Implement CandidatePruningPointUTXOIterator. * Use the pruning point utxo set iterator for StageVirtualUTXOSet. * Defer ClearCandidatePruningPointUTXOs. * Implement OverwriteVirtualUTXOSet. * Implement CommitCandidatePruningPointUTXOSet. * Implement BeginOverwritingVirtualUTXOSet and FinishOverwritingVirtualUTXOSet. * Implement overwriteVirtualUTXOSetAndCommitPruningPointUTXOSet. * Rename ClearCandidatePruningPointUTXOs to ClearCandidatePruningPointData. * Add missing methods to dbManager. * Implement PruningPointUTXOs. * Implement RecoverUTXOIfRequired. * Delete the utxoserialization package. * Fix compilation errors in TestValidateAndInsertPruningPoint. * Switch order of operations in the if statements in PruningPointUTXOs so that Next() wouldn't be unnecessarily called. * Fix missing pruning point utxo set staging and bad slice length. * Fix no default multiset in InsertCandidatePruningPointUTXOs. * Make go vet happy. * Rename candidateXXX to importedXXX. * Do some more renaming. * Rename some more. * Fix bad MsgIBDRootNotFound logic. * Fix an error message. * Simplify receiveIBDRootBlock. * Fix error message in receiveAndInsertIBDRootUTXOSet. * Do some more renaming. * Fix merge errors. * Fix a bug caused by calling iterator.First() unnecessarily. * Remove databaseContext from stores and don't use a transaction in ClearXXX functions. * Simplify receiveAndInsertIBDRootUTXOSet. * Fix offset count in PruningPointUTXOs(). * Fix readOnlyUTXOIteratorWithDiff.First(). * Split handleRequestIBDRootUTXOSetAndBlockFlow into smaller methods. * Rename IbdRootNotFound to UnexpectedPruningPoint. * Rename requestIBDRootHash to requestPruningPointHash. * Rename IBDRootHash to PruningPointHash. * Rename RequestIBDRootUTXOSetAndBlock to RequestPruningPointUTXOSetAndBlock. * Rename IBDRootUTXOSetChunk to PruningPointUTXOSetChunk. * Rename RequestNextIBDRootUTXOSetChunk to RequestNextPruningPointUTXOSetChunk. * Rename DoneIBDRootUTXOSetChunks to DonePruningPointUTXOSetChunks. * Rename remaining references to IBD root. * Fix an error message. * Add a check for HadStartedImportingPruningPointUTXOSet in commitVirtualUTXODiff. * Add a check for HadStartedImportingPruningPointUTXOSet in ImportPruningPointUTXOSetIntoVirtualUTXOSet. * Move FinishImportingPruningPointUTXOSet closer to HadStartedImportingPruningPointUTXOSet. * Remove reference to pruningStore in utxoSetIterator. * Pointerify utxoSetIterator receivers. * Fix bad insert in CommitImportedPruningPointUTXOSet. * Rename commitImportedPruningPointUTXOSetAll to applyImportedPruningPointUTXOSet. * Simplify PruningPointUTXOs. * Add populateTransactionWithUTXOEntriesFromUTXOSet. * Fix a TODO comment. * Rename InsertImportedPruningPointUTXOs to AppendImportedPruningPointUTXOs. * Extract handleRequestPruningPointUTXOSetAndBlockMessage to a separate method. * Rename stuff in readOnlyUTXOIteratorWithDiff.First(). * Address toAddIterator in readOnlyUTXOIteratorWithDiff.First(). * Call First() before any full iteration on ReadOnlyUTXOSetIterator. * Call First() before any full iteration on a database Cursor. * Put StartImportingPruningPointUTXOSet inside the pruning point transaction. * Make serializeOutpoint and serializeUTXOEntry free functions in pruningStore. * Fix readOnlyUTXOIteratorWithDiff.First(). * Fix bad validations in importPruningPoint. * Remove superfluous call to validateBlockTransactionsAgainstPastUTXO.
162 lines
5.3 KiB
Go
162 lines
5.3 KiB
Go
package consensusstatemanager_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/testapi"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/multiset"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
|
"github.com/kaspanet/kaspad/domain/dagconfig"
|
|
)
|
|
|
|
func TestUTXOCommitment(t *testing.T) {
|
|
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
|
params.BlockCoinbaseMaturity = 0
|
|
factory := consensus.NewFactory()
|
|
|
|
consensus, teardown, err := factory.NewTestConsensus(params, false, "TestUTXOCommitment")
|
|
if err != nil {
|
|
t.Fatalf("Error setting up consensus: %+v", err)
|
|
}
|
|
defer teardown(false)
|
|
|
|
// Build the following DAG:
|
|
// G <- A <- B <- C <- E
|
|
// <- D <-
|
|
// Where block D has a non-coinbase transaction
|
|
genesisHash := params.GenesisHash
|
|
|
|
// Block A:
|
|
blockAHash, _, err := consensus.AddBlock([]*externalapi.DomainHash{genesisHash}, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Error creating block A: %+v", err)
|
|
}
|
|
checkBlockUTXOCommitment(t, consensus, blockAHash, "A")
|
|
// Block B:
|
|
blockBHash, _, err := consensus.AddBlock([]*externalapi.DomainHash{blockAHash}, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Error creating block B: %+v", err)
|
|
}
|
|
blockB, err := consensus.GetBlock(blockBHash)
|
|
if err != nil {
|
|
t.Fatalf("Error getting block B: %+v", err)
|
|
}
|
|
checkBlockUTXOCommitment(t, consensus, blockBHash, "B")
|
|
// Block C:
|
|
blockCHash, _, err := consensus.AddBlock([]*externalapi.DomainHash{blockBHash}, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Error creating block C: %+v", err)
|
|
}
|
|
checkBlockUTXOCommitment(t, consensus, blockCHash, "C")
|
|
// Block D:
|
|
blockDTransaction, err := testutils.CreateTransaction(
|
|
blockB.Transactions[transactionhelper.CoinbaseTransactionIndex])
|
|
if err != nil {
|
|
t.Fatalf("Error creating transaction: %+v", err)
|
|
}
|
|
blockDHash, _, err := consensus.AddBlock([]*externalapi.DomainHash{blockBHash}, nil,
|
|
[]*externalapi.DomainTransaction{blockDTransaction})
|
|
if err != nil {
|
|
t.Fatalf("Error creating block D: %+v", err)
|
|
}
|
|
checkBlockUTXOCommitment(t, consensus, blockDHash, "D")
|
|
// Block E:
|
|
blockEHash, _, err := consensus.AddBlock([]*externalapi.DomainHash{blockCHash, blockDHash}, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Error creating block E: %+v", err)
|
|
}
|
|
checkBlockUTXOCommitment(t, consensus, blockEHash, "E")
|
|
})
|
|
}
|
|
|
|
func checkBlockUTXOCommitment(t *testing.T, consensus testapi.TestConsensus, blockHash *externalapi.DomainHash, blockName string) {
|
|
block, err := consensus.GetBlock(blockHash)
|
|
if err != nil {
|
|
t.Fatalf("Error getting block %s: %+v", blockName, err)
|
|
}
|
|
|
|
// Get the past UTXO set of block
|
|
csm := consensus.ConsensusStateManager()
|
|
utxoSetIterator, err := csm.RestorePastUTXOSetIterator(blockHash)
|
|
if err != nil {
|
|
t.Fatalf("Error restoring past UTXO of block %s: %+v", blockName, err)
|
|
}
|
|
|
|
// Build a Multiset
|
|
ms := multiset.New()
|
|
for ok := utxoSetIterator.First(); ok; ok = utxoSetIterator.Next() {
|
|
outpoint, entry, err := utxoSetIterator.Get()
|
|
if err != nil {
|
|
t.Fatalf("Error getting from UTXOSet iterator: %+v", err)
|
|
}
|
|
err = consensus.ConsensusStateManager().AddUTXOToMultiset(ms, entry, outpoint)
|
|
if err != nil {
|
|
t.Fatalf("Error adding utxo to multiset: %+v", err)
|
|
}
|
|
}
|
|
|
|
// Turn the multiset into a UTXO commitment
|
|
utxoCommitment := ms.Hash()
|
|
|
|
// Make sure that the two commitments are equal
|
|
if !utxoCommitment.Equal(block.Header.UTXOCommitment()) {
|
|
t.Fatalf("TestUTXOCommitment: calculated UTXO commitment for block %s and "+
|
|
"actual UTXO commitment don't match. Want: %s, got: %s", blockName,
|
|
utxoCommitment, block.Header.UTXOCommitment())
|
|
}
|
|
}
|
|
|
|
func TestPastUTXOMultiset(t *testing.T) {
|
|
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
|
factory := consensus.NewFactory()
|
|
|
|
consensus, teardown, err := factory.NewTestConsensus(params, false, "TestUTXOCommitment")
|
|
if err != nil {
|
|
t.Fatalf("Error setting up consensus: %+v", err)
|
|
}
|
|
defer teardown(false)
|
|
|
|
// Build a short chain
|
|
currentHash := params.GenesisHash
|
|
for i := 0; i < 3; i++ {
|
|
currentHash, _, err = consensus.AddBlock([]*externalapi.DomainHash{currentHash}, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Error creating block A: %+v", err)
|
|
}
|
|
}
|
|
|
|
// Save the current tip's hash to be used lated
|
|
testedBlockHash := currentHash
|
|
|
|
// Take testedBlock's multiset and hash
|
|
firstMultiset, err := consensus.MultisetStore().Get(consensus.DatabaseContext(), testedBlockHash)
|
|
if err != nil {
|
|
return
|
|
}
|
|
firstMultisetHash := firstMultiset.Hash()
|
|
|
|
// Add another block on top of testedBlock
|
|
_, _, err = consensus.AddBlock([]*externalapi.DomainHash{testedBlockHash}, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Error creating block A: %+v", err)
|
|
}
|
|
|
|
// Take testedBlock's multiset and hash again
|
|
secondMultiset, err := consensus.MultisetStore().Get(consensus.DatabaseContext(), testedBlockHash)
|
|
if err != nil {
|
|
return
|
|
}
|
|
secondMultisetHash := secondMultiset.Hash()
|
|
|
|
// Make sure the multiset hasn't changed
|
|
if !firstMultisetHash.Equal(secondMultisetHash) {
|
|
t.Fatalf("TestPastUTXOMultiSet: selectedParentMultiset appears to have changed!")
|
|
}
|
|
})
|
|
}
|