mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-24 06:25:55 +00:00
linting and deal with pruning point commitment
This commit is contained in:
parent
b4754d4b23
commit
560a1a99b1
@ -18,18 +18,17 @@ func ConvertDomainHashToString(blockHash *externalapi.DomainHash) string {
|
||||
return hex.EncodeToString(blockHash.ByteSlice())
|
||||
}
|
||||
|
||||
// ConvertStringDomainHashToDomainHash converts the given string to a domainHash
|
||||
// ConvertStringToDomainHash converts the given string to a domainHash
|
||||
func ConvertStringToDomainHash(stringDomainHash string) (*externalapi.DomainHash, error) {
|
||||
return externalapi.NewDomainHashFromString(stringDomainHash)
|
||||
}
|
||||
|
||||
// ConvertDomainHashToString converts the given DomainHash to a string
|
||||
// ConvertTXIDToString converts the given DomainHash to a string
|
||||
func ConvertTXIDToString(txID *externalapi.DomainTransactionID) string {
|
||||
return hex.EncodeToString(txID.ByteSlice())
|
||||
}
|
||||
|
||||
// ConvertStringDomainHashToDomainHash converts the given string to a domainHash
|
||||
// ConvertStringTXID converts the given string to a domainHash
|
||||
func ConvertStringTXID(stringDomainTransactionID string) (*externalapi.DomainTransactionID, error) {
|
||||
return externalapi.NewDomainTransactionIDFromString(stringDomainTransactionID)
|
||||
}
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ func newTXIndexStore(database database.Database) *txIndexStore {
|
||||
return &txIndexStore{
|
||||
database: database,
|
||||
toAdd: make(map[externalapi.DomainTransactionID]*externalapi.DomainHash),
|
||||
toRemove: make(map[externalapi.DomainTransactionID]*externalapi.DomainHash),
|
||||
virtualParents: nil,
|
||||
pruningPoint: nil,
|
||||
}
|
||||
@ -72,10 +73,10 @@ func (tis *txIndexStore) remove(txID externalapi.DomainTransactionID, blockHash
|
||||
}
|
||||
}
|
||||
|
||||
func (tis *txIndexStore) discard() {
|
||||
func (tis *txIndexStore) discardAllButPruningPoint() {
|
||||
tis.toAdd = make(map[externalapi.DomainTransactionID]*externalapi.DomainHash)
|
||||
tis.toRemove = make(map[externalapi.DomainTransactionID]*externalapi.DomainHash)
|
||||
tis.virtualParents = nil
|
||||
tis.pruningPoint = nil
|
||||
}
|
||||
|
||||
func (tis *txIndexStore) commit() error {
|
||||
@ -109,47 +110,45 @@ func (tis *txIndexStore) commit() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = dbTransaction.Put(pruningPointKey, tis.pruningPoint.ByteSlice())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = dbTransaction.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tis.discard()
|
||||
tis.discardAllButPruningPoint()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tis *txIndexStore) updateAndCommitVirtualParentsWithoutTransaction(virtualParents []*externalapi.DomainHash) error {
|
||||
func (tis *txIndexStore) commitVirtualParentsWithoutTransaction(virtualParents []*externalapi.DomainHash) error {
|
||||
serializeParentHashes := serializeHashes(virtualParents)
|
||||
return tis.database.Put(virtualParentsKey, serializeParentHashes)
|
||||
}
|
||||
|
||||
func (tis *txIndexStore) updateAndCommitPruningPointWithoutTransaction(pruningPoint *externalapi.DomainHash) error {
|
||||
return tis.database.Put(pruningPointKey, pruningPoint.ByteSlice())
|
||||
|
||||
}
|
||||
|
||||
func (tis *txIndexStore) updateVirtualParents(virtualParents []*externalapi.DomainHash) {
|
||||
tis.virtualParents = virtualParents
|
||||
}
|
||||
|
||||
func (tis *txIndexStore) CommitWithoutTransaction() error {
|
||||
for txID := range tis.toRemove { //safer to remove first
|
||||
func (tis *txIndexStore) updateAndCommitPruningPointWithoutTransaction(pruningPoint *externalapi.DomainHash) error {
|
||||
tis.pruningPoint = pruningPoint
|
||||
|
||||
return tis.database.Put(pruningPointKey, pruningPoint.ByteSlice())
|
||||
}
|
||||
|
||||
func (tis *txIndexStore) commitTxIDsWithoutTransaction() error {
|
||||
for txID, blockHash := range tis.toAdd {
|
||||
delete(tis.toRemove, txID) //adding takes precedence
|
||||
key := tis.convertTxIDToKey(txAcceptedIndexBucket, txID)
|
||||
err := tis.database.Delete(key)
|
||||
err := tis.database.Put(key, blockHash.ByteSlice())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for txID, blockHash := range tis.toAdd {
|
||||
for txID := range tis.toRemove { //safer to remove first
|
||||
key := tis.convertTxIDToKey(txAcceptedIndexBucket, txID)
|
||||
err := tis.database.Put(key, blockHash.ByteSlice())
|
||||
err := tis.database.Delete(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -70,28 +70,34 @@ func (ti *TXIndex) Reset() error {
|
||||
return err
|
||||
}
|
||||
|
||||
ti.removeTXIDs(selectedParentChainChanges, 1000)
|
||||
ti.removeTXIDs(selectedParentChainChanges, len(selectedParentChainChanges.Removed))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ti.addTXIDs(selectedParentChainChanges, 1000)
|
||||
ti.addTXIDs(selectedParentChainChanges, len(selectedParentChainChanges.Added))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ti.store.CommitWithoutTransaction()
|
||||
err = ti.store.commitTxIDsWithoutTransaction()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ti.store.updateAndCommitPruningPointWithoutTransaction(pruningPoint)
|
||||
ti.store.updateAndCommitPruningPointWithoutTransaction(pruningPoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ti.store.updateAndCommitVirtualParentsWithoutTransaction(virtualInfo.ParentHashes)
|
||||
ti.store.commitVirtualParentsWithoutTransaction(virtualInfo.ParentHashes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ti.store.discardAllButPruningPoint()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ti *TXIndex) isSynced() (bool, error) {
|
||||
@ -145,14 +151,14 @@ func (ti *TXIndex) Update(virtualChangeSet *externalapi.VirtualChangeSet) (*TXAc
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ti.store.updateVirtualParents(virtualChangeSet.VirtualParents)
|
||||
|
||||
added, removed, _, _ := ti.store.stagedData()
|
||||
txIndexChanges := &TXAcceptanceChange{
|
||||
Added: added,
|
||||
Removed: removed,
|
||||
}
|
||||
|
||||
ti.store.updateVirtualParents(virtualChangeSet.VirtualParents)
|
||||
|
||||
err = ti.store.commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user