mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-23 23:36:56 +00:00

* Revert "[NOD-1500] Delete integration tests" This reverts commit fcb57a206690a884fa6afb69d5d493282954a8bf. * [NOD-1518] hashserialization -> consenusserialization * [NOD-1518] Fix add genesis to virtual * [NOD-1518] Fix a bug in SerializeCoinbasePayload. * [NOD-1518] Fix a loop error and make pastMedianTime behave correctly everywhere on genesis. * [NOD-1518] Fix another bug and an infinite loop. * [NOD-1518] Fix uninitialized slice. * [NOD-1518] Fix bad should-commit checks and another infinite loop. * [NOD-1518] Fix nil serialization. * [NOD-1518] Rename blockHash to currentBlockHash. * [NOD-1518] Move the check whether stagedVirtualUTXOSet != nil to the top of commitVirtualUTXODiff. * [NOD-1518] Simplify utxoDiffStore.Commit. * [NOD-1518] Unextract resolveBlockStatusAndCheckFinality. * [NOD-1518] Move no-transactions logic into CalculateIDMerkleRoot. * [NOD-1518] Remove redundant is-staged check. * [NOD-1518] Fix merge errors. * [NOD-1518] Don't write anything if utxoDiffChild is nil. * [NOD-1518] Stage virtualAcceptanceData and virtualMultiset. * [NOD-1518] Fix bugs in getBlockTemplate and submitBlock. * [NOD-1518] Fix bad validation order in validateHeaderInContext. * [NOD-1518] Fix bug in Next(). * [NOD-1518] Fix nil dereference of subnetworks in AddressCache. * [NOD-1518] Fix multisetStore.Get returning a pointer to a multiset that is changed in place. * [NOD-1518] Break on genesis in countSubtrees. * [NOD-1518] Fix createBlockLocator. * [NOD-1518] Fix MsgTxToDomainTransaction. * [NOD-1518] Set MaxTxVersion to 1. * [NOD-1518] Fix missing error handling, bug in MsgTxToDomainTransaction, and bad subnetwork equality check. * [NOD-1518] Fix bug in hasUTXOByOutpointFromStagedVirtualUTXODiff. * [NOD-1518] Remove irrelevant comments. * [NOD-1518] Generate transactions with sufficient fee in tx_relay_test. * [NOD-1518] Fix broken RPC handlers. * [NOD-1518] Fix merge errors. * [NOD-1518] Fix bad exists check in restorePastUTXO and missing genesis check in CalculatePastUTXOAndAcceptanceData. * [NOD-1518] Add a comment. * [NOD-1518] Use a regular mutex instead of a read-write mutex in consensus to avoid dealing with sneaky not-actually-read functions. * [NOD-1518] Fix a deadlock in GetVirtualSelectedParent. * [NOD-1518] Fix missing handler registration for CmdHeader. * [NOD-1518] Fix processHeader calling OnNewBlock and LogBlock. Also fix conversion errors in IBDRootUTXOSetAndBlock. * [NOD-1518] Fix bad Command() in MsgIBDRootUTXOSetAndBlock. * [NOD-1518] Fix bad SyncStateMissingUTXOSet logic in resolveSyncState. * [NOD-1518] Rename mode to syncState. * [NOD-1518] Fix headers-only blocks coming in after the consensus thinks it's synced. * [NOD-1518] Fix selectedChildIterator.Next not ignoring virtual, infinite loop in HashSet.Length(). * [NOD-1518] Fix not-properly wrapped IBD blocks. * [NOD-1518] Fix bad conversion in RequestIBDBlocks. * [NOD-1518] Fix bad string for CmdRequestHeaders. * [NOD-1518] Fix bad string for CmdDoneHeaders. * [NOD-1518] Fix bad Command() for MsgIBDRootNotFound. * [NOD-1518] Fix bad areHeaderTipsSyncedMaxTimeDifference value. * [NOD-1518] Add missing string for CmdRequestIBDBlocks. * [NOD-1518] Fix bad check for SyncStateMissingBlockBodies. * [NOD-1518] Fix bad timeout durations in tests. * [NOD-1518] Fix IBD blocks not calling OnNewBlock. * [NOD-1518] Change when IBD finishes. * [NOD-1518] Properly clone utxoDiffChild. * [NOD-1518] Fix merge errors. * [NOD-1518] Move call to LogBlock to into OnNewBlock. * [NOD-1518] Return "not implemented" in unimplemented RPC handlers. * [NOD-1518] Extract cloning of hashes to a method over DomainHash. * [NOD-1518] Use isHeaderOnlyBlock. * [NOD-1518] Use constants.TransactionVersion. * [NOD-1518] Break immediately if we reached the virtual in SelectedChildIterator. * [NOD-1518] Don't stage nil utxoDiffChild. * [NOD-1518] Properly check the genesis hash in CalculatePastUTXOAndAcceptanceData. * [NOD-1518] Explain why we break on current == nil in countSubtrees. * [NOD-1518] Add a comment explaining why we check against StatusValid in resolveSyncState. Co-authored-by: Mike Zak <feanorr@gmail.com> Co-authored-by: Ori Newman <orinewman1@gmail.com>
262 lines
6.7 KiB
Go
262 lines
6.7 KiB
Go
package consensusstatestore
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/dbkeys"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
var utxoSetBucket = dbkeys.MakeBucket([]byte("virtual-utxo-set"))
|
|
|
|
func utxoKey(outpoint *externalapi.DomainOutpoint) (model.DBKey, error) {
|
|
serializedOutpoint, err := serializeOutpoint(outpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return utxoSetBucket.Key(serializedOutpoint), nil
|
|
}
|
|
|
|
func (c *consensusStateStore) StageVirtualUTXODiff(virtualUTXODiff *model.UTXODiff) error {
|
|
if c.stagedVirtualUTXOSet != nil {
|
|
return errors.New("cannot stage virtual UTXO diff while virtual UTXO set is staged")
|
|
}
|
|
|
|
c.stagedVirtualUTXODiff = c.cloneUTXODiff(virtualUTXODiff)
|
|
return nil
|
|
}
|
|
|
|
func (c *consensusStateStore) commitVirtualUTXODiff(dbTx model.DBTransaction) error {
|
|
if c.stagedVirtualUTXODiff == nil {
|
|
return nil
|
|
}
|
|
|
|
for toRemoveOutpoint := range c.stagedVirtualUTXODiff.ToRemove {
|
|
dbKey, err := utxoKey(&toRemoveOutpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbTx.Delete(dbKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for toAddOutpoint, toAddEntry := range c.stagedVirtualUTXODiff.ToAdd {
|
|
dbKey, err := utxoKey(&toAddOutpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serializedEntry, err := serializeUTXOEntry(toAddEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbTx.Put(dbKey, serializedEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *consensusStateStore) commitVirtualUTXOSet(dbTx model.DBTransaction) error {
|
|
if c.stagedVirtualUTXOSet == nil {
|
|
return nil
|
|
}
|
|
|
|
for outpoint, utxoEntry := range c.stagedVirtualUTXOSet {
|
|
dbKey, err := utxoKey(&outpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serializedEntry, err := serializeUTXOEntry(utxoEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbTx.Put(dbKey, serializedEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *consensusStateStore) UTXOByOutpoint(dbContext model.DBReader, outpoint *externalapi.DomainOutpoint) (
|
|
*externalapi.UTXOEntry, error) {
|
|
|
|
if c.stagedVirtualUTXOSet != nil {
|
|
return c.utxoByOutpointFromStagedVirtualUTXOSet(outpoint)
|
|
}
|
|
|
|
return c.utxoByOutpointFromStagedVirtualUTXODiff(dbContext, outpoint)
|
|
}
|
|
|
|
func (c *consensusStateStore) utxoByOutpointFromStagedVirtualUTXODiff(dbContext model.DBReader,
|
|
outpoint *externalapi.DomainOutpoint) (
|
|
*externalapi.UTXOEntry, error) {
|
|
|
|
if c.stagedVirtualUTXODiff != nil {
|
|
if _, ok := c.stagedVirtualUTXODiff.ToRemove[*outpoint]; ok {
|
|
return nil, errors.Errorf("outpoint was not found")
|
|
}
|
|
if utxoEntry, ok := c.stagedVirtualUTXODiff.ToAdd[*outpoint]; ok {
|
|
return utxoEntry, nil
|
|
}
|
|
}
|
|
|
|
key, err := utxoKey(outpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
serializedUTXOEntry, err := dbContext.Get(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return deserializeUTXOEntry(serializedUTXOEntry)
|
|
}
|
|
|
|
func (c *consensusStateStore) utxoByOutpointFromStagedVirtualUTXOSet(outpoint *externalapi.DomainOutpoint) (
|
|
*externalapi.UTXOEntry, error) {
|
|
if utxoEntry, ok := c.stagedVirtualUTXOSet[*outpoint]; ok {
|
|
return utxoEntry, nil
|
|
}
|
|
|
|
return nil, errors.Errorf("outpoint was not found")
|
|
}
|
|
|
|
func (c *consensusStateStore) HasUTXOByOutpoint(dbContext model.DBReader, outpoint *externalapi.DomainOutpoint) (bool, error) {
|
|
if c.stagedVirtualUTXOSet != nil {
|
|
return c.hasUTXOByOutpointFromStagedVirtualUTXOSet(outpoint), nil
|
|
}
|
|
|
|
return c.hasUTXOByOutpointFromStagedVirtualUTXODiff(dbContext, outpoint)
|
|
}
|
|
|
|
func (c *consensusStateStore) hasUTXOByOutpointFromStagedVirtualUTXODiff(dbContext model.DBReader,
|
|
outpoint *externalapi.DomainOutpoint) (bool, error) {
|
|
|
|
if c.stagedVirtualUTXODiff != nil {
|
|
if _, ok := c.stagedVirtualUTXODiff.ToRemove[*outpoint]; ok {
|
|
return false, nil
|
|
}
|
|
if _, ok := c.stagedVirtualUTXODiff.ToAdd[*outpoint]; ok {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
key, err := utxoKey(outpoint)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return dbContext.Has(key)
|
|
}
|
|
|
|
func (c *consensusStateStore) hasUTXOByOutpointFromStagedVirtualUTXOSet(outpoint *externalapi.DomainOutpoint) bool {
|
|
_, ok := c.stagedVirtualUTXOSet[*outpoint]
|
|
return ok
|
|
}
|
|
|
|
func (c *consensusStateStore) VirtualUTXOSetIterator(dbContext model.DBReader) (model.ReadOnlyUTXOSetIterator, error) {
|
|
cursor, err := dbContext.Cursor(utxoSetBucket)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return newUTXOSetIterator(cursor), nil
|
|
}
|
|
|
|
type utxoSetIterator struct {
|
|
cursor model.DBCursor
|
|
}
|
|
|
|
func newUTXOSetIterator(cursor model.DBCursor) model.ReadOnlyUTXOSetIterator {
|
|
return &utxoSetIterator{cursor: cursor}
|
|
}
|
|
|
|
func (u utxoSetIterator) Next() bool {
|
|
return u.cursor.Next()
|
|
}
|
|
|
|
func (u utxoSetIterator) Get() (outpoint *externalapi.DomainOutpoint, utxoEntry *externalapi.UTXOEntry, err error) {
|
|
key, err := u.cursor.Key()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
utxoEntryBytes, err := u.cursor.Value()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
outpoint, err = deserializeOutpoint(key.Suffix())
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
utxoEntry, err = deserializeUTXOEntry(utxoEntryBytes)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return outpoint, utxoEntry, nil
|
|
}
|
|
|
|
func (c *consensusStateStore) StageVirtualUTXOSet(virtualUTXOSetIterator model.ReadOnlyUTXOSetIterator) error {
|
|
if c.stagedVirtualUTXODiff != nil {
|
|
return errors.New("cannot stage virtual UTXO set while virtual UTXO diff is staged")
|
|
}
|
|
|
|
c.stagedVirtualUTXOSet = make(model.UTXOCollection)
|
|
for virtualUTXOSetIterator.Next() {
|
|
outpoint, entry, err := virtualUTXOSetIterator.Get()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, exists := c.stagedVirtualUTXOSet[*outpoint]; exists {
|
|
return errors.Errorf("outpoint %s is found more than once in the given iterator", outpoint)
|
|
}
|
|
c.stagedVirtualUTXOSet[*outpoint] = entry
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *consensusStateStore) cloneUTXODiff(diff *model.UTXODiff) *model.UTXODiff {
|
|
utxoDiffCopy := &model.UTXODiff{
|
|
ToAdd: make(model.UTXOCollection, len(diff.ToAdd)),
|
|
ToRemove: make(model.UTXOCollection, len(diff.ToRemove)),
|
|
}
|
|
|
|
for outpoint, entry := range diff.ToAdd {
|
|
scriptPublicKeyCopy := make([]byte, len(entry.ScriptPublicKey))
|
|
copy(scriptPublicKeyCopy, entry.ScriptPublicKey)
|
|
utxoDiffCopy.ToAdd[outpoint] = cloneUTXOEntry(entry)
|
|
}
|
|
|
|
for outpoint, entry := range diff.ToRemove {
|
|
scriptPublicKeyCopy := make([]byte, len(entry.ScriptPublicKey))
|
|
copy(scriptPublicKeyCopy, entry.ScriptPublicKey)
|
|
utxoDiffCopy.ToRemove[outpoint] = cloneUTXOEntry(entry)
|
|
}
|
|
|
|
return diff
|
|
}
|
|
|
|
func cloneUTXOEntry(entry *externalapi.UTXOEntry) *externalapi.UTXOEntry {
|
|
scriptPublicKeyCopy := make([]byte, len(entry.ScriptPublicKey))
|
|
copy(scriptPublicKeyCopy, entry.ScriptPublicKey)
|
|
return &externalapi.UTXOEntry{
|
|
Amount: entry.Amount,
|
|
ScriptPublicKey: scriptPublicKeyCopy,
|
|
BlockBlueScore: entry.BlockBlueScore,
|
|
IsCoinbase: entry.IsCoinbase,
|
|
}
|
|
}
|