Remove virtual diff parents (#1550)

* resolveSingleBlockStatus: If the block being resolved is not going to be the next selectedTip - set it's diffParent to be the old selectedTip

* resolveSingleBlockStatus: If the block being resolved is going to be the next selectedTip - set it as old selectedTip's diffChild

* Remove any mentions of virtualDiffParents

* If block is genesis - don't do all the mumbo-jumbo with oldSelectedTip

* Check an unchecked error

* Write a better log message
This commit is contained in:
Svarog 2021-02-23 17:19:35 +02:00 committed by GitHub
parent 2adb4f5d0f
commit fb6c9c8f21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 103 additions and 384 deletions

View File

@ -1520,53 +1520,6 @@ func (x *DbTips) GetTips() []*DbHash {
return nil return nil
} }
type DbVirtualDiffParents struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
VirtualDiffParents []*DbHash `protobuf:"bytes,1,rep,name=virtualDiffParents,proto3" json:"virtualDiffParents,omitempty"`
}
func (x *DbVirtualDiffParents) Reset() {
*x = DbVirtualDiffParents{}
if protoimpl.UnsafeEnabled {
mi := &file_dbobjects_proto_msgTypes[25]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *DbVirtualDiffParents) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*DbVirtualDiffParents) ProtoMessage() {}
func (x *DbVirtualDiffParents) ProtoReflect() protoreflect.Message {
mi := &file_dbobjects_proto_msgTypes[25]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use DbVirtualDiffParents.ProtoReflect.Descriptor instead.
func (*DbVirtualDiffParents) Descriptor() ([]byte, []int) {
return file_dbobjects_proto_rawDescGZIP(), []int{25}
}
func (x *DbVirtualDiffParents) GetVirtualDiffParents() []*DbHash {
if x != nil {
return x.VirtualDiffParents
}
return nil
}
type DbBlockCount struct { type DbBlockCount struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
@ -1578,7 +1531,7 @@ type DbBlockCount struct {
func (x *DbBlockCount) Reset() { func (x *DbBlockCount) Reset() {
*x = DbBlockCount{} *x = DbBlockCount{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_dbobjects_proto_msgTypes[26] mi := &file_dbobjects_proto_msgTypes[25]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@ -1591,7 +1544,7 @@ func (x *DbBlockCount) String() string {
func (*DbBlockCount) ProtoMessage() {} func (*DbBlockCount) ProtoMessage() {}
func (x *DbBlockCount) ProtoReflect() protoreflect.Message { func (x *DbBlockCount) ProtoReflect() protoreflect.Message {
mi := &file_dbobjects_proto_msgTypes[26] mi := &file_dbobjects_proto_msgTypes[25]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@ -1604,7 +1557,7 @@ func (x *DbBlockCount) ProtoReflect() protoreflect.Message {
// Deprecated: Use DbBlockCount.ProtoReflect.Descriptor instead. // Deprecated: Use DbBlockCount.ProtoReflect.Descriptor instead.
func (*DbBlockCount) Descriptor() ([]byte, []int) { func (*DbBlockCount) Descriptor() ([]byte, []int) {
return file_dbobjects_proto_rawDescGZIP(), []int{26} return file_dbobjects_proto_rawDescGZIP(), []int{25}
} }
func (x *DbBlockCount) GetCount() uint64 { func (x *DbBlockCount) GetCount() uint64 {
@ -1625,7 +1578,7 @@ type DbBlockHeaderCount struct {
func (x *DbBlockHeaderCount) Reset() { func (x *DbBlockHeaderCount) Reset() {
*x = DbBlockHeaderCount{} *x = DbBlockHeaderCount{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_dbobjects_proto_msgTypes[27] mi := &file_dbobjects_proto_msgTypes[26]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@ -1638,7 +1591,7 @@ func (x *DbBlockHeaderCount) String() string {
func (*DbBlockHeaderCount) ProtoMessage() {} func (*DbBlockHeaderCount) ProtoMessage() {}
func (x *DbBlockHeaderCount) ProtoReflect() protoreflect.Message { func (x *DbBlockHeaderCount) ProtoReflect() protoreflect.Message {
mi := &file_dbobjects_proto_msgTypes[27] mi := &file_dbobjects_proto_msgTypes[26]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@ -1651,7 +1604,7 @@ func (x *DbBlockHeaderCount) ProtoReflect() protoreflect.Message {
// Deprecated: Use DbBlockHeaderCount.ProtoReflect.Descriptor instead. // Deprecated: Use DbBlockHeaderCount.ProtoReflect.Descriptor instead.
func (*DbBlockHeaderCount) Descriptor() ([]byte, []int) { func (*DbBlockHeaderCount) Descriptor() ([]byte, []int) {
return file_dbobjects_proto_rawDescGZIP(), []int{27} return file_dbobjects_proto_rawDescGZIP(), []int{26}
} }
func (x *DbBlockHeaderCount) GetCount() uint64 { func (x *DbBlockHeaderCount) GetCount() uint64 {
@ -1891,21 +1844,15 @@ var file_dbobjects_proto_rawDesc = []byte{
0x62, 0x54, 0x69, 0x70, 0x73, 0x12, 0x29, 0x0a, 0x04, 0x74, 0x69, 0x70, 0x73, 0x18, 0x01, 0x20, 0x62, 0x54, 0x69, 0x70, 0x73, 0x12, 0x29, 0x0a, 0x04, 0x74, 0x69, 0x70, 0x73, 0x18, 0x01, 0x20,
0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74, 0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x2e, 0x44, 0x62, 0x48, 0x61, 0x73, 0x68, 0x52, 0x04, 0x74, 0x69, 0x70, 0x73, 0x69, 0x6f, 0x6e, 0x2e, 0x44, 0x62, 0x48, 0x61, 0x73, 0x68, 0x52, 0x04, 0x74, 0x69, 0x70, 0x73,
0x22, 0x5d, 0x0a, 0x14, 0x44, 0x62, 0x56, 0x69, 0x72, 0x74, 0x75, 0x61, 0x6c, 0x44, 0x69, 0x66, 0x22, 0x24, 0x0a, 0x0c, 0x44, 0x62, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x43, 0x6f, 0x75, 0x6e, 0x74,
0x66, 0x50, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x45, 0x0a, 0x12, 0x76, 0x69, 0x72, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52,
0x75, 0x61, 0x6c, 0x44, 0x69, 0x66, 0x66, 0x50, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x2a, 0x0a, 0x12, 0x44, 0x62, 0x42, 0x6c, 0x6f, 0x63,
0x20, 0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x6b, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05,
0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x44, 0x62, 0x48, 0x61, 0x73, 0x68, 0x52, 0x12, 0x76, 0x69, 0x72, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05, 0x63, 0x6f, 0x75,
0x74, 0x75, 0x61, 0x6c, 0x44, 0x69, 0x66, 0x66, 0x50, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x6e, 0x74, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x24, 0x0a, 0x0c, 0x44, 0x62, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x2f, 0x6b, 0x61, 0x73, 0x70, 0x61, 0x6e, 0x65, 0x74, 0x2f, 0x6b, 0x61, 0x73, 0x70, 0x61, 0x64,
0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05, 0x2f, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x62, 0x06,
0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x2a, 0x0a, 0x12, 0x44, 0x62, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x63,
0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e,
0x74, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
0x6b, 0x61, 0x73, 0x70, 0x61, 0x6e, 0x65, 0x74, 0x2f, 0x6b, 0x61, 0x73, 0x70, 0x61, 0x64, 0x2f,
0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@ -1920,7 +1867,7 @@ func file_dbobjects_proto_rawDescGZIP() []byte {
return file_dbobjects_proto_rawDescData return file_dbobjects_proto_rawDescData
} }
var file_dbobjects_proto_msgTypes = make([]protoimpl.MessageInfo, 28) var file_dbobjects_proto_msgTypes = make([]protoimpl.MessageInfo, 27)
var file_dbobjects_proto_goTypes = []interface{}{ var file_dbobjects_proto_goTypes = []interface{}{
(*DbBlock)(nil), // 0: serialization.DbBlock (*DbBlock)(nil), // 0: serialization.DbBlock
(*DbBlockHeader)(nil), // 1: serialization.DbBlockHeader (*DbBlockHeader)(nil), // 1: serialization.DbBlockHeader
@ -1947,9 +1894,8 @@ var file_dbobjects_proto_goTypes = []interface{}{
(*DbReachabilityInterval)(nil), // 22: serialization.DbReachabilityInterval (*DbReachabilityInterval)(nil), // 22: serialization.DbReachabilityInterval
(*DbUtxoDiff)(nil), // 23: serialization.DbUtxoDiff (*DbUtxoDiff)(nil), // 23: serialization.DbUtxoDiff
(*DbTips)(nil), // 24: serialization.DbTips (*DbTips)(nil), // 24: serialization.DbTips
(*DbVirtualDiffParents)(nil), // 25: serialization.DbVirtualDiffParents (*DbBlockCount)(nil), // 25: serialization.DbBlockCount
(*DbBlockCount)(nil), // 26: serialization.DbBlockCount (*DbBlockHeaderCount)(nil), // 26: serialization.DbBlockHeaderCount
(*DbBlockHeaderCount)(nil), // 27: serialization.DbBlockHeaderCount
} }
var file_dbobjects_proto_depIdxs = []int32{ var file_dbobjects_proto_depIdxs = []int32{
1, // 0: serialization.DbBlock.header:type_name -> serialization.DbBlockHeader 1, // 0: serialization.DbBlock.header:type_name -> serialization.DbBlockHeader
@ -1988,12 +1934,11 @@ var file_dbobjects_proto_depIdxs = []int32{
18, // 33: serialization.DbUtxoDiff.toAdd:type_name -> serialization.DbUtxoCollectionItem 18, // 33: serialization.DbUtxoDiff.toAdd:type_name -> serialization.DbUtxoCollectionItem
18, // 34: serialization.DbUtxoDiff.toRemove:type_name -> serialization.DbUtxoCollectionItem 18, // 34: serialization.DbUtxoDiff.toRemove:type_name -> serialization.DbUtxoCollectionItem
2, // 35: serialization.DbTips.tips:type_name -> serialization.DbHash 2, // 35: serialization.DbTips.tips:type_name -> serialization.DbHash
2, // 36: serialization.DbVirtualDiffParents.virtualDiffParents:type_name -> serialization.DbHash 36, // [36:36] is the sub-list for method output_type
37, // [37:37] is the sub-list for method output_type 36, // [36:36] is the sub-list for method input_type
37, // [37:37] is the sub-list for method input_type 36, // [36:36] is the sub-list for extension type_name
37, // [37:37] is the sub-list for extension type_name 36, // [36:36] is the sub-list for extension extendee
37, // [37:37] is the sub-list for extension extendee 0, // [0:36] is the sub-list for field type_name
0, // [0:37] is the sub-list for field type_name
} }
func init() { file_dbobjects_proto_init() } func init() { file_dbobjects_proto_init() }
@ -2303,18 +2248,6 @@ func file_dbobjects_proto_init() {
} }
} }
file_dbobjects_proto_msgTypes[25].Exporter = func(v interface{}, i int) interface{} { file_dbobjects_proto_msgTypes[25].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*DbVirtualDiffParents); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_dbobjects_proto_msgTypes[26].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*DbBlockCount); i { switch v := v.(*DbBlockCount); i {
case 0: case 0:
return &v.state return &v.state
@ -2326,7 +2259,7 @@ func file_dbobjects_proto_init() {
return nil return nil
} }
} }
file_dbobjects_proto_msgTypes[27].Exporter = func(v interface{}, i int) interface{} { file_dbobjects_proto_msgTypes[26].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*DbBlockHeaderCount); i { switch v := v.(*DbBlockHeaderCount); i {
case 0: case 0:
return &v.state return &v.state
@ -2345,7 +2278,7 @@ func file_dbobjects_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_dbobjects_proto_rawDesc, RawDescriptor: file_dbobjects_proto_rawDesc,
NumEnums: 0, NumEnums: 0,
NumMessages: 28, NumMessages: 27,
NumExtensions: 0, NumExtensions: 0,
NumServices: 0, NumServices: 0,
}, },

View File

@ -143,10 +143,6 @@ message DbTips {
repeated DbHash tips = 1; repeated DbHash tips = 1;
} }
message DbVirtualDiffParents {
repeated DbHash virtualDiffParents = 1;
}
message DbBlockCount { message DbBlockCount {
uint64 count = 1; uint64 count = 1;
} }

View File

@ -1,17 +0,0 @@
package serialization
import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
)
// VirtualDiffParentsToDBHeaderVirtualDiffParents converts a slice of hashes to DbVirtualDiffParents
func VirtualDiffParentsToDBHeaderVirtualDiffParents(tips []*externalapi.DomainHash) *DbVirtualDiffParents {
return &DbVirtualDiffParents{
VirtualDiffParents: DomainHashesToDbHashes(tips),
}
}
// DBVirtualDiffParentsToVirtualDiffParents converts DbHeaderTips to a slice of hashes
func DBVirtualDiffParentsToVirtualDiffParents(dbVirtualDiffParents *DbVirtualDiffParents) ([]*externalapi.DomainHash, error) {
return DbHashesToDomainHashes(dbVirtualDiffParents.VirtualDiffParents)
}

View File

@ -8,14 +8,12 @@ import (
// consensusStateStore represents a store for the current consensus state // consensusStateStore represents a store for the current consensus state
type consensusStateStore struct { type consensusStateStore struct {
tipsStaging []*externalapi.DomainHash tipsStaging []*externalapi.DomainHash
virtualDiffParentsStaging []*externalapi.DomainHash virtualUTXODiffStaging externalapi.UTXODiff
virtualUTXODiffStaging externalapi.UTXODiff
virtualUTXOSetCache *utxolrucache.LRUCache virtualUTXOSetCache *utxolrucache.LRUCache
tipsCache []*externalapi.DomainHash tipsCache []*externalapi.DomainHash
virtualDiffParentsCache []*externalapi.DomainHash
} }
// New instantiates a new ConsensusStateStore // New instantiates a new ConsensusStateStore
@ -28,7 +26,6 @@ func New(utxoSetCacheSize int, preallocate bool) model.ConsensusStateStore {
func (css *consensusStateStore) Discard() { func (css *consensusStateStore) Discard() {
css.tipsStaging = nil css.tipsStaging = nil
css.virtualUTXODiffStaging = nil css.virtualUTXODiffStaging = nil
css.virtualDiffParentsStaging = nil
} }
func (css *consensusStateStore) Commit(dbTx model.DBTransaction) error { func (css *consensusStateStore) Commit(dbTx model.DBTransaction) error {
@ -36,10 +33,6 @@ func (css *consensusStateStore) Commit(dbTx model.DBTransaction) error {
if err != nil { if err != nil {
return err return err
} }
err = css.commitVirtualDiffParents(dbTx)
if err != nil {
return err
}
err = css.commitVirtualUTXODiff(dbTx) err = css.commitVirtualUTXODiff(dbTx)
if err != nil { if err != nil {
@ -53,6 +46,5 @@ func (css *consensusStateStore) Commit(dbTx model.DBTransaction) error {
func (css *consensusStateStore) IsStaged() bool { func (css *consensusStateStore) IsStaged() bool {
return css.tipsStaging != nil || return css.tipsStaging != nil ||
css.virtualDiffParentsStaging != nil ||
css.virtualUTXODiffStaging != nil css.virtualUTXODiffStaging != nil
} }

View File

@ -1,74 +0,0 @@
package consensusstatestore
import (
"github.com/golang/protobuf/proto"
"github.com/kaspanet/kaspad/domain/consensus/database"
"github.com/kaspanet/kaspad/domain/consensus/database/serialization"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
)
var virtualDiffParentsKey = database.MakeBucket(nil).Key([]byte("virtual-diff-parents"))
func (css *consensusStateStore) VirtualDiffParents(dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
if css.virtualDiffParentsStaging != nil {
return externalapi.CloneHashes(css.virtualDiffParentsStaging), nil
}
if css.virtualDiffParentsCache != nil {
return externalapi.CloneHashes(css.virtualDiffParentsCache), nil
}
virtualDiffParentsBytes, err := dbContext.Get(virtualDiffParentsKey)
if err != nil {
return nil, err
}
virtualDiffParents, err := css.deserializeVirtualDiffParents(virtualDiffParentsBytes)
if err != nil {
return nil, err
}
css.virtualDiffParentsCache = virtualDiffParents
return externalapi.CloneHashes(virtualDiffParents), nil
}
func (css *consensusStateStore) StageVirtualDiffParents(tipHashes []*externalapi.DomainHash) {
css.virtualDiffParentsStaging = externalapi.CloneHashes(tipHashes)
}
func (css *consensusStateStore) commitVirtualDiffParents(dbTx model.DBTransaction) error {
if css.virtualDiffParentsStaging == nil {
return nil
}
virtualDiffParentsBytes, err := css.serializeVirtualDiffParents(css.virtualDiffParentsStaging)
if err != nil {
return err
}
err = dbTx.Put(virtualDiffParentsKey, virtualDiffParentsBytes)
if err != nil {
return err
}
css.virtualDiffParentsCache = css.virtualDiffParentsStaging
// Note: we don't discard the staging here since that's
// being done at the end of Commit()
return nil
}
func (css *consensusStateStore) serializeVirtualDiffParents(virtualDiffParentsBytes []*externalapi.DomainHash) ([]byte, error) {
virtualDiffParents := serialization.VirtualDiffParentsToDBHeaderVirtualDiffParents(virtualDiffParentsBytes)
return proto.Marshal(virtualDiffParents)
}
func (css *consensusStateStore) deserializeVirtualDiffParents(virtualDiffParentsBytes []byte) ([]*externalapi.DomainHash,
error) {
dbVirtualDiffParents := &serialization.DbVirtualDiffParents{}
err := proto.Unmarshal(virtualDiffParentsBytes, dbVirtualDiffParents)
if err != nil {
return nil, err
}
return serialization.DBVirtualDiffParentsToVirtualDiffParents(dbVirtualDiffParents)
}

View File

@ -14,9 +14,6 @@ type ConsensusStateStore interface {
VirtualUTXOs(dbContext DBReader, VirtualUTXOs(dbContext DBReader,
fromOutpoint *externalapi.DomainOutpoint, limit int) ([]*externalapi.OutpointAndUTXOEntryPair, error) fromOutpoint *externalapi.DomainOutpoint, limit int) ([]*externalapi.OutpointAndUTXOEntryPair, error)
StageVirtualDiffParents(virtualDiffParents []*externalapi.DomainHash)
VirtualDiffParents(dbContext DBReader) ([]*externalapi.DomainHash, error)
StageTips(tipHashes []*externalapi.DomainHash) StageTips(tipHashes []*externalapi.DomainHash)
Tips(dbContext DBReader) ([]*externalapi.DomainHash, error) Tips(dbContext DBReader) ([]*externalapi.DomainHash, error)

View File

@ -77,14 +77,8 @@ func (csm *consensusStateManager) importPruningPoint(newPruningPoint *externalap
return err return err
} }
log.Debugf("Deleting all existing virtual diff parents")
csm.consensusStateStore.StageVirtualDiffParents(nil)
log.Debugf("Updating the new pruning point to be the new virtual diff parent with an empty diff") log.Debugf("Updating the new pruning point to be the new virtual diff parent with an empty diff")
err = csm.stageDiff(newPruningPointHash, utxo.NewUTXODiff(), nil) csm.stageDiff(newPruningPointHash, utxo.NewUTXODiff(), nil)
if err != nil {
return err
}
log.Debugf("Staging the new pruning point %s", newPruningPointHash) log.Debugf("Staging the new pruning point %s", newPruningPointHash)
csm.pruningStore.StagePruningPoint(newPruningPointHash) csm.pruningStore.StagePruningPoint(newPruningPointHash)

View File

@ -38,8 +38,7 @@ func (csm *consensusStateManager) calculateMultiset(
isCoinbase := i == 0 isCoinbase := i == 0
log.Tracef("Is transaction %s a coinbase transaction: %t", transactionID, isCoinbase) log.Tracef("Is transaction %s a coinbase transaction: %t", transactionID, isCoinbase)
var err error err := addTransactionToMultiset(ms, transaction, blockGHOSTDAGData.BlueScore(), isCoinbase)
err = addTransactionToMultiset(ms, transaction, blockGHOSTDAGData.BlueScore(), isCoinbase)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,14 +1,18 @@
package consensusstatemanager package consensusstatemanager
import ( import (
"fmt"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi" "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors" "github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
"github.com/kaspanet/kaspad/infrastructure/logger"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func (csm *consensusStateManager) resolveBlockStatus(blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) { func (csm *consensusStateManager) resolveBlockStatus(blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) {
log.Debugf("resolveBlockStatus start for block %s", blockHash) onEnd := logger.LogAndMeasureExecutionTime(log, fmt.Sprintf("resolveBlockStatus for %s", blockHash))
defer log.Debugf("resolveBlockStatus end for block %s", blockHash) defer onEnd()
log.Debugf("Getting a list of all blocks in the selected "+ log.Debugf("Getting a list of all blocks in the selected "+
"parent chain of %s that have no yet resolved their status", blockHash) "parent chain of %s that have no yet resolved their status", blockHash)
@ -55,7 +59,8 @@ func (csm *consensusStateManager) resolveBlockStatus(blockHash *externalapi.Doma
csm.blockStatusStore.Stage(unverifiedBlockHash, blockStatus) csm.blockStatusStore.Stage(unverifiedBlockHash, blockStatus)
selectedParentStatus = blockStatus selectedParentStatus = blockStatus
log.Debugf("Block %s status resolved to `%s`, finished %d/%d of unverified blocks", unverifiedBlockHash, blockStatus, len(unverifiedBlocks)-i, len(unverifiedBlocks)) log.Debugf("Block %s status resolved to `%s`, finished %d/%d of unverified blocks",
unverifiedBlockHash, blockStatus, len(unverifiedBlocks)-i, len(unverifiedBlocks))
} }
return blockStatus, nil return blockStatus, nil
@ -120,8 +125,8 @@ func (csm *consensusStateManager) getUnverifiedChainBlocks(
} }
func (csm *consensusStateManager) resolveSingleBlockStatus(blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) { func (csm *consensusStateManager) resolveSingleBlockStatus(blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) {
log.Debugf("resolveSingleBlockStatus start for block %s", blockHash) onEnd := logger.LogAndMeasureExecutionTime(log, fmt.Sprintf("resolveSingleBlockStatus for %s", blockHash))
defer log.Debugf("resolveSingleBlockStatus end for block %s", blockHash) defer onEnd()
log.Tracef("Calculating pastUTXO and acceptance data and multiset for block %s", blockHash) log.Tracef("Calculating pastUTXO and acceptance data and multiset for block %s", blockHash)
pastUTXODiff, acceptanceData, multiset, err := csm.CalculatePastUTXOAndAcceptanceData(blockHash) pastUTXODiff, acceptanceData, multiset, err := csm.CalculatePastUTXOAndAcceptanceData(blockHash)
@ -151,72 +156,63 @@ func (csm *consensusStateManager) resolveSingleBlockStatus(blockHash *externalap
log.Tracef("Staging the multiset of block %s", blockHash) log.Tracef("Staging the multiset of block %s", blockHash)
csm.multisetStore.Stage(blockHash, multiset) csm.multisetStore.Stage(blockHash, multiset)
log.Tracef("Staging the utxoDiff of block %s", blockHash) if csm.genesisHash.Equal(blockHash) {
err = csm.stageDiff(blockHash, pastUTXODiff, nil) log.Tracef("Staging the utxoDiff of genesis")
csm.stageDiff(blockHash, pastUTXODiff, nil)
return externalapi.StatusUTXOValid, nil
}
oldSelectedTip, err := csm.selectedTip()
if err != nil { if err != nil {
return 0, err return 0, err
} }
log.Tracef("Remove block ancestors from virtual diff parents and assign %s as their diff child", blockHash) isNewSelectedTip, err := csm.isNewSelectedTip(blockHash, oldSelectedTip)
err = csm.removeAncestorsFromVirtualDiffParentsAndAssignDiffChild(blockHash, pastUTXODiff)
if err != nil { if err != nil {
return 0, err return 0, err
} }
oldSelectedTipUTXOSet, err := csm.restorePastUTXO(oldSelectedTip)
if err != nil {
return 0, err
}
if isNewSelectedTip {
log.Debugf("Block %s is the new SelectedTip, therefore setting it as old selectedTip's diffChild", blockHash)
oldSelectedTipUTXOSet, err := pastUTXODiff.DiffFrom(oldSelectedTipUTXOSet.ToImmutable())
if err != nil {
return 0, err
}
csm.stageDiff(oldSelectedTip, oldSelectedTipUTXOSet, blockHash)
log.Tracef("Staging the utxoDiff of block %s", blockHash)
csm.stageDiff(blockHash, pastUTXODiff, nil)
} else {
log.Debugf("Block %s is not the new SelectedTip, therefore setting old selectedTip as it's diffChild", blockHash)
pastUTXODiff, err = oldSelectedTipUTXOSet.DiffFrom(pastUTXODiff)
if err != nil {
return 0, err
}
log.Tracef("Staging the utxoDiff of block %s", blockHash)
csm.stageDiff(blockHash, pastUTXODiff, oldSelectedTip)
}
return externalapi.StatusUTXOValid, nil return externalapi.StatusUTXOValid, nil
} }
func (csm *consensusStateManager) removeAncestorsFromVirtualDiffParentsAndAssignDiffChild( func (csm *consensusStateManager) isNewSelectedTip(blockHash, oldSelectedTip *externalapi.DomainHash) (bool, error) {
blockHash *externalapi.DomainHash, pastUTXODiff externalapi.UTXODiff) error { newSelectedTip, err := csm.ghostdagManager.ChooseSelectedParent(blockHash, oldSelectedTip)
log.Tracef("removeAncestorsFromVirtualDiffParentsAndAssignDiffChild start for block %s", blockHash)
defer log.Tracef("removeAncestorsFromVirtualDiffParentsAndAssignDiffChild end for block %s", blockHash)
if blockHash.Equal(csm.genesisHash) {
log.Tracef("Genesis block doesn't have ancestors to remove from the virtual diff parents")
return nil
}
virtualDiffParents, err := csm.consensusStateStore.VirtualDiffParents(csm.databaseContext)
if err != nil { if err != nil {
return err return false, err
} }
for _, virtualDiffParent := range virtualDiffParents { return blockHash.Equal(newSelectedTip), nil
if virtualDiffParent.Equal(blockHash) { }
log.Tracef("Skipping updating virtual diff parent %s "+
"because it was updated before.", virtualDiffParent) func (csm *consensusStateManager) selectedTip() (*externalapi.DomainHash, error) {
continue virtualGHOSTDAGData, err := csm.ghostdagDataStore.Get(csm.databaseContext, model.VirtualBlockHash)
} if err != nil {
return nil, err
isAncestorOfBlock, err := csm.dagTopologyManager.IsAncestorOf(virtualDiffParent, blockHash) }
if err != nil {
return err return virtualGHOSTDAGData.SelectedParent(), nil
}
if !isAncestorOfBlock {
log.Tracef("Skipping block %s because it's not an "+
"ancestor of %s", virtualDiffParent, blockHash)
continue
}
// parents that didn't have a utxo-diff child until now were actually virtual's diffParents.
// Update them to have the new block as their utxo-diff child
log.Tracef("Updating %s to be the diff child of %s", blockHash, virtualDiffParent)
currentDiff, err := csm.utxoDiffStore.UTXODiff(csm.databaseContext, virtualDiffParent)
if err != nil {
return err
}
newDiff, err := pastUTXODiff.DiffFrom(currentDiff)
if err != nil {
return err
}
err = csm.stageDiff(virtualDiffParent, newDiff, blockHash)
if err != nil {
return err
}
}
return nil
} }

View File

@ -60,8 +60,8 @@ func (csm *consensusStateManager) updateVirtual(newBlockHash *externalapi.Domain
log.Debugf("Staging new UTXO diff for the virtual block") log.Debugf("Staging new UTXO diff for the virtual block")
csm.consensusStateStore.StageVirtualUTXODiff(virtualUTXODiff) csm.consensusStateStore.StageVirtualUTXODiff(virtualUTXODiff)
log.Debugf("Updating the virtual diff parents after adding %s to the DAG", newBlockHash) log.Debugf("Updating the selected tip's utxo-diff after adding %s to the DAG", newBlockHash)
err = csm.updateVirtualDiffParents(virtualUTXODiff) err = csm.updateSelectedTipUTXODiff(virtualUTXODiff)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -86,32 +86,27 @@ func (csm *consensusStateManager) updateVirtual(newBlockHash *externalapi.Domain
return selectedParentChainChanges, virtualUTXODiff, nil return selectedParentChainChanges, virtualUTXODiff, nil
} }
func (csm *consensusStateManager) updateVirtualDiffParents(virtualUTXODiff externalapi.UTXODiff) error { func (csm *consensusStateManager) updateSelectedTipUTXODiff(virtualUTXODiff externalapi.UTXODiff) error {
log.Debugf("updateVirtualDiffParents start") onEnd := logger.LogAndMeasureExecutionTime(log, "updateSelectedTipUTXODiff")
defer log.Debugf("updateVirtualDiffParents end") defer onEnd()
virtualDiffParents, err := csm.consensusStateStore.VirtualDiffParents(csm.databaseContext) selectedTip, err := csm.selectedTip()
if err != nil { if err != nil {
return err return err
} }
for _, virtualDiffParent := range virtualDiffParents { log.Debugf("Calculating new UTXO diff for virtual diff parent %s", selectedTip)
log.Debugf("Calculating new UTXO diff for virtual diff parent %s", virtualDiffParent) selectedTipUTXODiff, err := csm.utxoDiffStore.UTXODiff(csm.databaseContext, selectedTip)
virtualDiffParentUTXODiff, err := csm.utxoDiffStore.UTXODiff(csm.databaseContext, virtualDiffParent) if err != nil {
if err != nil { return err
return err
}
newDiff, err := virtualUTXODiff.DiffFrom(virtualDiffParentUTXODiff)
if err != nil {
return err
}
log.Debugf("Staging new UTXO diff for virtual diff parent %s", virtualDiffParent)
err = csm.stageDiff(virtualDiffParent, newDiff, nil)
if err != nil {
return err
}
} }
newDiff, err := virtualUTXODiff.DiffFrom(selectedTipUTXODiff)
if err != nil {
return err
}
log.Debugf("Staging new UTXO diff for virtual diff parent %s", selectedTip)
csm.stageDiff(selectedTip, newDiff, nil)
return nil return nil
} }

View File

@ -2,81 +2,14 @@ package consensusstatemanager
import ( import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi" "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/pkg/errors"
) )
func (csm *consensusStateManager) stageDiff(blockHash *externalapi.DomainHash, func (csm *consensusStateManager) stageDiff(blockHash *externalapi.DomainHash,
utxoDiff externalapi.UTXODiff, utxoDiffChild *externalapi.DomainHash) error { utxoDiff externalapi.UTXODiff, utxoDiffChild *externalapi.DomainHash) {
log.Debugf("stageDiff start for block %s", blockHash) log.Debugf("stageDiff start for block %s", blockHash)
defer log.Debugf("stageDiff end for block %s", blockHash) defer log.Debugf("stageDiff end for block %s", blockHash)
log.Debugf("Staging block %s as the diff child of %s", utxoDiffChild, blockHash) log.Debugf("Staging block %s as the diff child of %s", utxoDiffChild, blockHash)
csm.utxoDiffStore.Stage(blockHash, utxoDiff, utxoDiffChild) csm.utxoDiffStore.Stage(blockHash, utxoDiff, utxoDiffChild)
if utxoDiffChild == nil {
log.Debugf("Adding block %s to the virtual diff parents", blockHash)
return csm.addToVirtualDiffParents(blockHash)
}
log.Debugf("Removing block %s from the virtual diff parents", blockHash)
return csm.removeFromVirtualDiffParents(blockHash)
}
func (csm *consensusStateManager) addToVirtualDiffParents(blockHash *externalapi.DomainHash) error {
log.Debugf("addToVirtualDiffParents start for block %s", blockHash)
defer log.Debugf("addToVirtualDiffParents end for block %s", blockHash)
var oldVirtualDiffParents []*externalapi.DomainHash
if !blockHash.Equal(csm.genesisHash) {
var err error
oldVirtualDiffParents, err = csm.consensusStateStore.VirtualDiffParents(csm.databaseContext)
if err != nil {
return err
}
}
isInVirtualDiffParents := false
for _, diffParent := range oldVirtualDiffParents {
if diffParent.Equal(blockHash) {
isInVirtualDiffParents = true
break
}
}
if isInVirtualDiffParents {
log.Debugf("Block %s is already a virtual diff parent, so there's no need to add it", blockHash)
return nil
}
newVirtualDiffParents := append([]*externalapi.DomainHash{blockHash}, oldVirtualDiffParents...)
log.Debugf("Staging virtual diff parents after adding %s to it", blockHash)
csm.consensusStateStore.StageVirtualDiffParents(newVirtualDiffParents)
return nil
}
func (csm *consensusStateManager) removeFromVirtualDiffParents(blockHash *externalapi.DomainHash) error {
log.Debugf("removeFromVirtualDiffParents start for block %s", blockHash)
defer log.Debugf("removeFromVirtualDiffParents end for block %s", blockHash)
oldVirtualDiffParents, err := csm.consensusStateStore.VirtualDiffParents(csm.databaseContext)
if err != nil {
return err
}
newVirtualDiffParents := make([]*externalapi.DomainHash, 0, len(oldVirtualDiffParents)-1)
for _, diffParent := range oldVirtualDiffParents {
if !diffParent.Equal(blockHash) {
newVirtualDiffParents = append(newVirtualDiffParents, diffParent)
}
}
if len(newVirtualDiffParents) != len(oldVirtualDiffParents)-1 {
return errors.Errorf("expected to remove one member from virtual diff parents and "+
"have a length of %d but got length of %d", len(oldVirtualDiffParents)-1, len(newVirtualDiffParents))
}
log.Debugf("Staging virtual diff parents after removing %s from it", blockHash)
csm.consensusStateStore.StageVirtualDiffParents(newVirtualDiffParents)
return nil
} }

View File

@ -256,11 +256,6 @@ func (pm *pruningManager) deletePastBlocks(pruningPoint *externalapi.DomainHash)
return err return err
} }
err = pm.pruneVirtualDiffParents(pruningPoint, virtualParents)
if err != nil {
return err
}
return nil return nil
} }
@ -292,26 +287,6 @@ func (pm *pruningManager) deleteBlocksDownward(queue model.BlockHeap) error {
return nil return nil
} }
func (pm *pruningManager) pruneVirtualDiffParents(pruningPoint *externalapi.DomainHash, virtualParents []*externalapi.DomainHash) error {
virtualDiffParents, err := pm.consensusStateStore.VirtualDiffParents(pm.databaseContext)
if err != nil {
return err
}
validVirtualDiffParents := make([]*externalapi.DomainHash, 0, len(virtualParents))
for _, parent := range virtualDiffParents {
isInPruningFutureOrInVirtualPast, err := pm.isInPruningFutureOrInVirtualPast(parent, pruningPoint, virtualParents)
if err != nil {
return err
}
if isInPruningFutureOrInVirtualPast {
validVirtualDiffParents = append(validVirtualDiffParents, parent)
}
}
pm.consensusStateStore.StageVirtualDiffParents(validVirtualDiffParents)
return nil
}
func (pm *pruningManager) pruneTips(pruningPoint *externalapi.DomainHash, virtualParents []*externalapi.DomainHash) ( func (pm *pruningManager) pruneTips(pruningPoint *externalapi.DomainHash, virtualParents []*externalapi.DomainHash) (
prunedTips []*externalapi.DomainHash, err error) { prunedTips []*externalapi.DomainHash, err error) {