mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-24 14:35:53 +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())
|
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) {
|
func ConvertStringToDomainHash(stringDomainHash string) (*externalapi.DomainHash, error) {
|
||||||
return externalapi.NewDomainHashFromString(stringDomainHash)
|
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 {
|
func ConvertTXIDToString(txID *externalapi.DomainTransactionID) string {
|
||||||
return hex.EncodeToString(txID.ByteSlice())
|
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) {
|
func ConvertStringTXID(stringDomainTransactionID string) (*externalapi.DomainTransactionID, error) {
|
||||||
return externalapi.NewDomainTransactionIDFromString(stringDomainTransactionID)
|
return externalapi.NewDomainTransactionIDFromString(stringDomainTransactionID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ func newTXIndexStore(database database.Database) *txIndexStore {
|
|||||||
return &txIndexStore{
|
return &txIndexStore{
|
||||||
database: database,
|
database: database,
|
||||||
toAdd: make(map[externalapi.DomainTransactionID]*externalapi.DomainHash),
|
toAdd: make(map[externalapi.DomainTransactionID]*externalapi.DomainHash),
|
||||||
|
toRemove: make(map[externalapi.DomainTransactionID]*externalapi.DomainHash),
|
||||||
virtualParents: nil,
|
virtualParents: nil,
|
||||||
pruningPoint: 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.toAdd = make(map[externalapi.DomainTransactionID]*externalapi.DomainHash)
|
||||||
|
tis.toRemove = make(map[externalapi.DomainTransactionID]*externalapi.DomainHash)
|
||||||
tis.virtualParents = nil
|
tis.virtualParents = nil
|
||||||
tis.pruningPoint = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tis *txIndexStore) commit() error {
|
func (tis *txIndexStore) commit() error {
|
||||||
@ -109,47 +110,45 @@ func (tis *txIndexStore) commit() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = dbTransaction.Put(pruningPointKey, tis.pruningPoint.ByteSlice())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = dbTransaction.Commit()
|
err = dbTransaction.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tis.discard()
|
tis.discardAllButPruningPoint()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tis *txIndexStore) updateAndCommitVirtualParentsWithoutTransaction(virtualParents []*externalapi.DomainHash) error {
|
func (tis *txIndexStore) commitVirtualParentsWithoutTransaction(virtualParents []*externalapi.DomainHash) error {
|
||||||
serializeParentHashes := serializeHashes(virtualParents)
|
serializeParentHashes := serializeHashes(virtualParents)
|
||||||
return tis.database.Put(virtualParentsKey, serializeParentHashes)
|
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) {
|
func (tis *txIndexStore) updateVirtualParents(virtualParents []*externalapi.DomainHash) {
|
||||||
tis.virtualParents = virtualParents
|
tis.virtualParents = virtualParents
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tis *txIndexStore) CommitWithoutTransaction() error {
|
func (tis *txIndexStore) updateAndCommitPruningPointWithoutTransaction(pruningPoint *externalapi.DomainHash) error {
|
||||||
for txID := range tis.toRemove { //safer to remove first
|
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)
|
key := tis.convertTxIDToKey(txAcceptedIndexBucket, txID)
|
||||||
err := tis.database.Delete(key)
|
err := tis.database.Put(key, blockHash.ByteSlice())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for txID, blockHash := range tis.toAdd {
|
for txID := range tis.toRemove { //safer to remove first
|
||||||
key := tis.convertTxIDToKey(txAcceptedIndexBucket, txID)
|
key := tis.convertTxIDToKey(txAcceptedIndexBucket, txID)
|
||||||
err := tis.database.Put(key, blockHash.ByteSlice())
|
err := tis.database.Delete(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -70,28 +70,34 @@ func (ti *TXIndex) Reset() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ti.removeTXIDs(selectedParentChainChanges, 1000)
|
ti.removeTXIDs(selectedParentChainChanges, len(selectedParentChainChanges.Removed))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ti.addTXIDs(selectedParentChainChanges, 1000)
|
ti.addTXIDs(selectedParentChainChanges, len(selectedParentChainChanges.Added))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ti.store.CommitWithoutTransaction()
|
err = ti.store.commitTxIDsWithoutTransaction()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ti.store.updateAndCommitPruningPointWithoutTransaction(pruningPoint)
|
ti.store.updateAndCommitPruningPointWithoutTransaction(pruningPoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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) {
|
func (ti *TXIndex) isSynced() (bool, error) {
|
||||||
@ -145,14 +151,14 @@ func (ti *TXIndex) Update(virtualChangeSet *externalapi.VirtualChangeSet) (*TXAc
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ti.store.updateVirtualParents(virtualChangeSet.VirtualParents)
|
|
||||||
|
|
||||||
added, removed, _, _ := ti.store.stagedData()
|
added, removed, _, _ := ti.store.stagedData()
|
||||||
txIndexChanges := &TXAcceptanceChange{
|
txIndexChanges := &TXAcceptanceChange{
|
||||||
Added: added,
|
Added: added,
|
||||||
Removed: removed,
|
Removed: removed,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ti.store.updateVirtualParents(virtualChangeSet.VirtualParents)
|
||||||
|
|
||||||
err = ti.store.commit()
|
err = ti.store.commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -213,7 +219,7 @@ func (ti *TXIndex) removeTXIDs(selectedParentChainChanges *externalapi.SelectedC
|
|||||||
chainBlockAcceptanceData := chainBlocksAcceptanceData[i]
|
chainBlockAcceptanceData := chainBlocksAcceptanceData[i]
|
||||||
for _, blockAcceptanceData := range chainBlockAcceptanceData {
|
for _, blockAcceptanceData := range chainBlockAcceptanceData {
|
||||||
for _, transactionAcceptanceData := range blockAcceptanceData.TransactionAcceptanceData {
|
for _, transactionAcceptanceData := range blockAcceptanceData.TransactionAcceptanceData {
|
||||||
if transactionAcceptanceData.IsAccepted && transactionAcceptanceData.Transaction.ID != nil{
|
if transactionAcceptanceData.IsAccepted && transactionAcceptanceData.Transaction.ID != nil {
|
||||||
ti.store.remove(*transactionAcceptanceData.Transaction.ID, removedChainBlock)
|
ti.store.remove(*transactionAcceptanceData.Transaction.ID, removedChainBlock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user