mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[NOD-1498] Consensus State Store (#992)
* [NOD-1420] Start working on ConsensusStateManager. Might be redundant due to recent changes * [NOD-1420] Convert model to externalapi in utxo_algerbra helpers * [NOD-1420] Add UTXO-diff algebra * [NOD-1420] Prepare skeleton of calculateAcceptanceDataAndMultiset * [NOD-1420] Added skeleton for AddBlockToVirtual * [NOD-1420] Implement PopulateTransactionWithUTXOEntries * [NOD-1420] Implement restorePastUTXO * [NOD-1420] Implement finality check * [NOD-1420] Move handling of tips to consensusStateManager * [NOD-1420] Implement calculateAcceptanceDataAndMultiset * [NOD-1420] Start implementing resolveBlockStatus * [NOD-1420] Implement resolveBlockStatus * [NOD-1420] Update related fields in end of resolveSingleBlockStatus * [NOD-1420] Start working on selectVirtualParents * [NOD-1420] Implemented BlockHeap * [NOD-1420] Implement selectVirtualParents * [NOD-1420] Implement updateVirtual * [NOD-1420] Added comments where they were missing * [NOD-1420] Place all consensusStateManager functions in correct files * [NOD-1420] Return the missing outpoints from populateTransactionWithUTXOEntriesFromVirtualOrDiff * [NOD-1420] Outpoint.ID -> TransactionID * [NOD-1420] Fix Stringer tests * [NOD-1420] Copy hash.FromString into utils * [NOD-1420] SetParents should return an error * [NOD-1420] Remove all reachabilityManager references from consensusStateManager * [NOD-1420] Remove VirtualData. Get the info from the stores where needed * [NOD-1420] Invert parameters to IsAncestorOf * [NOD-1420] Use model.AcceptanceData * [NOD-1420] Don't return accumulatedMassBefore in error cases * [NOD-1420] Don't expect store functions to return nil when the requested data was found - instead add HasXXX functions * [NOD-1420] addTransactionToMultiset sets isCoinbase properly * [NOD-1420] expected hash string length is externalapi.DomainHashSize * 2 * [NOD-1420] Rename reachabilityTree -> reachabilityManager + updateReindexRoot if isNextVirtualSelectedParent * [NOD-1420] ValidateCoinbaseTransaction in csm.verifyAndBuildUTXO * [NOD-1420] Re-write HAsUTXODiffChild * [NOD-1420] delete past_utxo.go.bak * [NOD-1420] Implement validateCoinbaseTransaction in CSM * [NOD-1420] Imlemented missing functionality in ValidateTransactionAndPopulateWithConsensusData * [NOD-1420] Moved merge depth logic to MergeDepthManager * [NOD-1420] Add logs * [NOD-1498] Implement tips-related methods of consensusStateStore * [NOD-1498] Implement consensusStateStore virtualDiffParents functionality * [NOD-1498] Implement ConsensusStateStore UTXO-Set part * [NOD-1498] Implement rest of consensusStateStore methods * [NOD-1498] Use io.ReadFull instead of r.Read * [NOD-1498] Added comments * [NOD-1498] Move utxo serialization to protobufs * [NOD-1498] Add comments * [NOD-1498] Minor fixes in ConsensusStateStore * [NOD-1498] Use empty bucket key + simplify serializeUTXOEntry
This commit is contained in:
parent
c7f2de73df
commit
8dc246a2a7
@ -10,7 +10,7 @@ func utxoCollectionToDBUTXOCollection(utxoCollection model.UTXOCollection) []*Db
|
||||
for outpoint, entry := range utxoCollection {
|
||||
items[i] = &DbUtxoCollectionItem{
|
||||
Outpoint: DomainOutpointToDbOutpoint(&outpoint),
|
||||
UtxoEntry: utxoEntryToDBUTXOEntry(entry),
|
||||
UtxoEntry: UTXOEntryToDBUTXOEntry(entry),
|
||||
}
|
||||
i++
|
||||
}
|
||||
@ -26,7 +26,7 @@ func dbUTXOCollectionToUTXOCollection(items []*DbUtxoCollectionItem) (model.UTXO
|
||||
return nil, err
|
||||
}
|
||||
|
||||
collection[*outpoint] = dbUTXOEntryToUTXOEntry(item.UtxoEntry)
|
||||
collection[*outpoint] = DBUTXOEntryToUTXOEntry(item.UtxoEntry)
|
||||
}
|
||||
return collection, nil
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
)
|
||||
|
||||
func utxoEntryToDBUTXOEntry(utxoEntry *externalapi.UTXOEntry) *DbUtxoEntry {
|
||||
// UTXOEntryToDBUTXOEntry converts UTXOEntry to DbUtxoEntry
|
||||
func UTXOEntryToDBUTXOEntry(utxoEntry *externalapi.UTXOEntry) *DbUtxoEntry {
|
||||
return &DbUtxoEntry{
|
||||
Amount: utxoEntry.Amount,
|
||||
ScriptPublicKey: utxoEntry.ScriptPublicKey,
|
||||
@ -13,7 +14,8 @@ func utxoEntryToDBUTXOEntry(utxoEntry *externalapi.UTXOEntry) *DbUtxoEntry {
|
||||
}
|
||||
}
|
||||
|
||||
func dbUTXOEntryToUTXOEntry(dbUtxoEntry *DbUtxoEntry) *externalapi.UTXOEntry {
|
||||
// DBUTXOEntryToUTXOEntry convert DbUtxoEntry ro UTXOEntry
|
||||
func DBUTXOEntryToUTXOEntry(dbUtxoEntry *DbUtxoEntry) *externalapi.UTXOEntry {
|
||||
return &externalapi.UTXOEntry{
|
||||
Amount: dbUtxoEntry.Amount,
|
||||
ScriptPublicKey: dbUtxoEntry.ScriptPublicKey,
|
||||
|
@ -7,6 +7,9 @@ import (
|
||||
|
||||
// consensusStateStore represents a store for the current consensus state
|
||||
type consensusStateStore struct {
|
||||
stagedTips []*externalapi.DomainHash
|
||||
stagedVirtualDiffParents []*externalapi.DomainHash
|
||||
stagedVirtualUTXODiff *model.UTXODiff
|
||||
}
|
||||
|
||||
// New instantiates a new ConsensusStateStore
|
||||
@ -15,43 +18,32 @@ func New() model.ConsensusStateStore {
|
||||
}
|
||||
|
||||
func (c consensusStateStore) Discard() {
|
||||
panic("implement me")
|
||||
c.stagedTips = nil
|
||||
c.stagedVirtualUTXODiff = nil
|
||||
c.stagedVirtualDiffParents = nil
|
||||
}
|
||||
|
||||
func (c consensusStateStore) Commit(dbTx model.DBTransaction) error {
|
||||
panic("implement me")
|
||||
err := c.commitTips(dbTx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.commitVirtualDiffParents(dbTx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.commitVirtualUTXODiff(dbTx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c consensusStateStore) IsStaged() bool {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) StageVirtualUTXODiff(virtualUTXODiff *model.UTXODiff) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) UTXOByOutpoint(dbContext model.DBReader, outpoint *externalapi.DomainOutpoint) (*externalapi.UTXOEntry, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) HasUTXOByOutpoint(dbContext model.DBReader, outpoint *externalapi.DomainOutpoint) (bool, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) StageVirtualDiffParents(virtualDiffParents []*externalapi.DomainHash) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) VirtualDiffParents(dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) Tips(dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) StageTips(tipHashes []*externalapi.DomainHash) error {
|
||||
panic("implement me")
|
||||
return c.stagedTips != nil ||
|
||||
c.stagedVirtualDiffParents != nil ||
|
||||
c.stagedVirtualUTXODiff != nil
|
||||
}
|
||||
|
||||
func (c consensusStateStore) VirtualUTXOSetIterator(dbContext model.DBReader) (model.ReadOnlyUTXOSetIterator, error) {
|
||||
|
39
domain/consensus/datastructures/consensusstatestore/tips.go
Normal file
39
domain/consensus/datastructures/consensusstatestore/tips.go
Normal file
@ -0,0 +1,39 @@
|
||||
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/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
)
|
||||
|
||||
var tipsKey = dbkeys.MakeBucket().Key([]byte("tips"))
|
||||
|
||||
func (c consensusStateStore) Tips(dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
|
||||
if c.stagedTips != nil {
|
||||
return c.stagedTips, nil
|
||||
}
|
||||
|
||||
tipsBytes, err := dbContext.Get(tipsKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hashes.DeserializeHashSlice(tipsBytes)
|
||||
}
|
||||
|
||||
func (c consensusStateStore) StageTips(tipHashes []*externalapi.DomainHash) {
|
||||
c.stagedTips = tipHashes
|
||||
}
|
||||
|
||||
func (c consensusStateStore) commitTips(dbTx model.DBTransaction) error {
|
||||
tipsBytes := hashes.SerializeHashSlice(c.stagedTips)
|
||||
|
||||
err := dbTx.Put(tipsKey, tipsBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.stagedTips = nil
|
||||
return nil
|
||||
}
|
94
domain/consensus/datastructures/consensusstatestore/utxo.go
Normal file
94
domain/consensus/datastructures/consensusstatestore/utxo.go
Normal file
@ -0,0 +1,94 @@
|
||||
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/kaspanet/kaspad/infrastructure/db/database"
|
||||
)
|
||||
|
||||
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) {
|
||||
c.stagedVirtualUTXODiff = virtualUTXODiff
|
||||
}
|
||||
|
||||
func (c consensusStateStore) commitVirtualUTXODiff(dbTx model.DBTransaction) error {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
c.stagedVirtualUTXODiff = nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c consensusStateStore) UTXOByOutpoint(dbContext model.DBReader, outpoint *externalapi.DomainOutpoint) (
|
||||
*externalapi.UTXOEntry, error) {
|
||||
|
||||
if _, ok := c.stagedVirtualUTXODiff.ToRemove[*outpoint]; ok {
|
||||
return nil, database.ErrNotFound
|
||||
}
|
||||
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) HasUTXOByOutpoint(dbContext model.DBReader, outpoint *externalapi.DomainOutpoint) (bool, error) {
|
||||
if _, ok := c.stagedVirtualUTXODiff.ToRemove[*outpoint]; ok {
|
||||
return false, database.ErrNotFound
|
||||
}
|
||||
if _, ok := c.stagedVirtualUTXODiff.ToAdd[*outpoint]; ok {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
key, err := utxoKey(outpoint)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return dbContext.Has(key)
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package consensusstatestore
|
||||
|
||||
import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/database/serialization"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
)
|
||||
|
||||
func serializeOutpoint(outpoint *externalapi.DomainOutpoint) ([]byte, error) {
|
||||
return proto.Marshal(serialization.DomainOutpointToDbOutpoint(outpoint))
|
||||
}
|
||||
|
||||
func serializeUTXOEntry(entry *externalapi.UTXOEntry) ([]byte, error) {
|
||||
return proto.Marshal(serialization.UTXOEntryToDBUTXOEntry(entry))
|
||||
}
|
||||
|
||||
func deserializeOutpoint(outpointBytes []byte) (*externalapi.DomainOutpoint, error) {
|
||||
dbOutpoint := &serialization.DbOutpoint{}
|
||||
err := proto.Unmarshal(outpointBytes, dbOutpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return serialization.DbOutpointToDomainOutpoint(dbOutpoint)
|
||||
}
|
||||
|
||||
func deserializeUTXOEntry(entryBytes []byte) (*externalapi.UTXOEntry, error) {
|
||||
dbEntry := &serialization.DbUtxoEntry{}
|
||||
err := proto.Unmarshal(entryBytes, dbEntry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return serialization.DBUTXOEntryToUTXOEntry(dbEntry), nil
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
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/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
)
|
||||
|
||||
var virtualDiffParentsKey = dbkeys.MakeBucket().Key([]byte("virtual-diff-parents"))
|
||||
|
||||
func (c consensusStateStore) VirtualDiffParents(dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
|
||||
if c.stagedVirtualDiffParents != nil {
|
||||
return c.stagedVirtualDiffParents, nil
|
||||
}
|
||||
|
||||
virtualDiffParentsBytes, err := dbContext.Get(virtualDiffParentsKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hashes.DeserializeHashSlice(virtualDiffParentsBytes)
|
||||
}
|
||||
|
||||
func (c consensusStateStore) StageVirtualDiffParents(tipHashes []*externalapi.DomainHash) {
|
||||
c.stagedVirtualDiffParents = tipHashes
|
||||
}
|
||||
|
||||
func (c consensusStateStore) commitVirtualDiffParents(dbTx model.DBTransaction) error {
|
||||
virtualDiffParentsBytes := hashes.SerializeHashSlice(c.stagedVirtualDiffParents)
|
||||
|
||||
err := dbTx.Put(virtualDiffParentsKey, virtualDiffParentsBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.stagedVirtualDiffParents = nil
|
||||
return nil
|
||||
}
|
@ -12,9 +12,9 @@ type ConsensusStateStore interface {
|
||||
HasUTXOByOutpoint(dbContext DBReader, outpoint *externalapi.DomainOutpoint) (bool, error)
|
||||
VirtualUTXOSetIterator(dbContext DBReader) (ReadOnlyUTXOSetIterator, error)
|
||||
|
||||
StageVirtualDiffParents(virtualDiffParents []*externalapi.DomainHash) error
|
||||
StageVirtualDiffParents(virtualDiffParents []*externalapi.DomainHash)
|
||||
VirtualDiffParents(dbContext DBReader) ([]*externalapi.DomainHash, error)
|
||||
|
||||
StageTips(tipHashes []*externalapi.DomainHash)
|
||||
Tips(dbContext DBReader) ([]*externalapi.DomainHash, error)
|
||||
StageTips(tipHashes []*externalapi.DomainHash) error
|
||||
}
|
||||
|
@ -85,10 +85,7 @@ func (csm *consensusStateManager) addTip(newTipHash *externalapi.DomainHash) (ne
|
||||
}
|
||||
}
|
||||
|
||||
err = csm.consensusStateStore.StageTips(newTips)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
csm.consensusStateStore.StageTips(newTips)
|
||||
|
||||
return newTips, nil
|
||||
}
|
||||
|
@ -69,5 +69,7 @@ func (csm *consensusStateManager) updateVirtualDiffParents(
|
||||
}
|
||||
}
|
||||
|
||||
return csm.consensusStateStore.StageVirtualDiffParents(newVirtualDiffParents)
|
||||
csm.consensusStateStore.StageVirtualDiffParents(newVirtualDiffParents)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
41
domain/consensus/utils/hashes/hash_array.go
Normal file
41
domain/consensus/utils/hashes/hash_array.go
Normal file
@ -0,0 +1,41 @@
|
||||
package hashes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// DeserializeHashSlice decodes an array of DomainHashes from byte-slice representation
|
||||
func DeserializeHashSlice(hashesBytes []byte) ([]*externalapi.DomainHash, error) {
|
||||
if len(hashesBytes)%externalapi.DomainHashSize != 0 {
|
||||
return nil, errors.Errorf("serialized hashes length is %d bytes, while it should be a multiple of %d",
|
||||
len(hashesBytes), externalapi.DomainHashSize)
|
||||
}
|
||||
|
||||
hashes := make([]*externalapi.DomainHash, 0, len(hashesBytes)/externalapi.DomainHashSize)
|
||||
|
||||
for i := 0; i < len(hashesBytes); i += externalapi.DomainHashSize {
|
||||
hashBytes := hashesBytes[i : i+externalapi.DomainHashSize]
|
||||
hash, err := FromBytes(hashBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hashes = append(hashes, hash)
|
||||
}
|
||||
|
||||
return hashes, nil
|
||||
}
|
||||
|
||||
// SerializeHashSlice encodes a slice of DomainHashes into a byte slice
|
||||
func SerializeHashSlice(hashes []*externalapi.DomainHash) []byte {
|
||||
hashesBytes := make([][]byte, 0, len(hashes))
|
||||
|
||||
for _, hash := range hashes {
|
||||
hashesBytes = append(hashesBytes, hash[:])
|
||||
}
|
||||
|
||||
return bytes.Join(hashesBytes, []byte{})
|
||||
}
|
@ -49,17 +49,9 @@ func serializeOutpoint(w io.Writer, outpoint *externalapi.DomainOutpoint) error
|
||||
}
|
||||
|
||||
func serializeUTXOEntry(w io.Writer, entry *externalapi.UTXOEntry) error {
|
||||
buf := [8 + 1 + 8]byte{}
|
||||
// Encode the blueScore.
|
||||
binary.LittleEndian.PutUint64(buf[:8], entry.BlockBlueScore)
|
||||
|
||||
buf[8] = serializeUTXOEntryFlags(entry)
|
||||
|
||||
binary.LittleEndian.PutUint64(buf[9:], entry.Amount)
|
||||
|
||||
_, err := w.Write(buf[:])
|
||||
err := writeElements(w, entry.BlockBlueScore, entry.Amount, entry.IsCoinbase)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
return err
|
||||
}
|
||||
|
||||
count := uint64(len(entry.ScriptPublicKey))
|
||||
@ -75,13 +67,3 @@ func serializeUTXOEntry(w io.Writer, entry *externalapi.UTXOEntry) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func serializeUTXOEntryFlags(entry *externalapi.UTXOEntry) uint8 {
|
||||
var serializedFlags uint8 = 0
|
||||
|
||||
if entry.IsCoinbase {
|
||||
serializedFlags |= 1
|
||||
}
|
||||
|
||||
return serializedFlags
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user