Kirill aea3baf897
[NOD-1320] Flush UTXOs to disk (#902)
* [NOD-1320] Flush UTXOs to disk.

* [NOD-1320] Minor improvements and fixes.

* FullUTXOSet: change size type from int64 to uint64.
* Rename FullUTXOSet.size to FullUTXOSet.estimatedSize
* Fill NewFullUTXOSetFromContext with db context on virtual block
  creation.
* Typo fixes.

* [NOD-1320] Stylystic fixes.

* [NOD-1320] Update tests. Improvements and fixes.

* Update blockdag/dag tests: prepare DB for tests.
* Update blockdag/utxoset tests: prepare DB for tests.
* Update blockdag/test_utils utils.
* Update blockdag/common tests.
* FullUTXOSet: remove embedded utxoCollection type. Rename
  utxoCollection to utxoCache.
* Fix blockdag/block_utxo genesisPastUTXO func.
* Minor fixes and improvements.
2020-09-27 13:16:11 +03:00

61 lines
1.4 KiB
Go

package dbaccess
import (
"github.com/kaspanet/kaspad/infrastructure/db/database"
)
var (
utxoBucket = database.MakeBucket([]byte("utxo"))
)
func utxoKey(outpointKey []byte) *database.Key {
return utxoBucket.Key(outpointKey)
}
// AddToUTXOSet adds the given outpoint-utxoEntry pair to
// the database's UTXO set.
func AddToUTXOSet(context Context, outpointKey []byte, utxoEntry []byte) error {
accessor, err := context.accessor()
if err != nil {
return err
}
key := utxoKey(outpointKey)
return accessor.Put(key, utxoEntry)
}
// RemoveFromUTXOSet removes the given outpoint from the
// database's UTXO set.
func RemoveFromUTXOSet(context Context, outpointKey []byte) error {
accessor, err := context.accessor()
if err != nil {
return err
}
key := utxoKey(outpointKey)
return accessor.Delete(key)
}
// GetFromUTXOSet return the given outpoint from the
// database's UTXO set.
func GetFromUTXOSet(context Context, outpointKey []byte) ([]byte, error) {
accessor, err := context.accessor()
if err != nil {
return nil, err
}
key := utxoKey(outpointKey)
return accessor.Get(key)
}
// UTXOSetCursor opens a cursor over all the UTXO entries
// that have been previously added to the database.
func UTXOSetCursor(context Context) (database.Cursor, error) {
accessor, err := context.accessor()
if err != nil {
return nil, err
}
return accessor.Cursor(utxoBucket)
}