nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: kaspanet/kaspad
Pull request #1403 (2021-01-12 14:39:11)

Delete existing UTXOSet when commiting VirtualUTXOSet.


Patch:
diff --git a/domain/consensus/database/transaction.go b/domain/consensus/database/transaction.go
index 9f17bc54..1ecbfa80 100644
--- a/domain/consensus/database/transaction.go
+++ b/domain/consensus/database/transaction.go
@@ -9,6 +9,22 @@ type dbTransaction struct {
 	transaction database.Transaction
 }
 
+func (d *dbTransaction) Get(key model.DBKey) ([]byte, error) {
+	return d.transaction.Get(dbKeyToDatabaseKey(key))
+}
+
+func (d *dbTransaction) Has(key model.DBKey) (bool, error) {
+	return d.transaction.Has(dbKeyToDatabaseKey(key))
+}
+
+func (d *dbTransaction) Cursor(bucket model.DBBucket) (model.DBCursor, error) {
+	cursor, err := d.transaction.Cursor(dbBucketToDatabaseBucket(bucket))
+	if err != nil {
+		return nil, err
+	}
+	return newDBCursor(cursor), nil
+}
+
 func (d *dbTransaction) Put(key model.DBKey, value []byte) error {
 	return d.transaction.Put(dbKeyToDatabaseKey(key), value)
 }
diff --git a/domain/consensus/model/database.go b/domain/consensus/model/database.go
index f5b2f47a..7133d99e 100644
--- a/domain/consensus/model/database.go
+++ b/domain/consensus/model/database.go
@@ -58,6 +58,7 @@ type DBWriter interface {
 // access that requires an open database transaction
 type DBTransaction interface {
 	DBWriter
+	DBReader
 
 	// Rollback rolls back whatever changes were made to the
 	// database within this transaction.


Test patch:
diff --git a/domain/consensus/datastructures/consensusstatestore/utxo.go b/domain/consensus/datastructures/consensusstatestore/utxo.go
index 5959bfb1..8c1dc285 100644
--- a/domain/consensus/datastructures/consensusstatestore/utxo.go
+++ b/domain/consensus/datastructures/consensusstatestore/utxo.go
@@ -85,6 +85,23 @@ func (css *consensusStateStore) commitVirtualUTXOSet(dbTx model.DBTransaction) e
 		return nil
 	}
 
+	// Clear the existing virtual utxo set in database before adding the new one
+	cursor, err := dbTx.Cursor(utxoSetBucket)
+	if err != nil {
+		return err
+	}
+	for cursor.Next() {
+		key, err := cursor.Key()
+		if err != nil {
+			return err
+		}
+		err = dbTx.Delete(key)
+		if err != nil {
+			return err
+		}
+	}
+
+	// Now put the new virtualUTXOSet into the database
 	css.virtualUTXOSetCache.Clear()
 	iterator := css.virtualUTXOSetStaging.Iterator()
 	for iterator.Next() {