Svarog 9a756939d8
[NOD-1412] Remove ffldb, remove dataStore from database, store blocks directly in levelDB (#939)
* [NOD-1412] Remove ffldb, and make ldb implement all the database
interfaces

* [NOD-1412] Removed any reference to dataStore and updated block dbaccess to work directly with key/values
2020-09-27 15:40:15 +03:00

64 lines
1.4 KiB
Go

package dbaccess
import (
"github.com/kaspanet/kaspad/infrastructure/db/database"
"github.com/kaspanet/kaspad/util/daghash"
"github.com/pkg/errors"
)
var (
blocksBucket = database.MakeBucket([]byte("blocks"))
)
func blockKey(hash *daghash.Hash) *database.Key {
return blocksBucket.Key(hash[:])
}
// StoreBlock stores the given block in the database.
func StoreBlock(context *TxContext, hash *daghash.Hash, blockBytes []byte) error {
accessor, err := context.accessor()
if err != nil {
return err
}
// Make sure that the block does not already exist.
exists, err := HasBlock(context, hash)
if err != nil {
return err
}
if exists {
return errors.Errorf("block %s already exists", hash)
}
// Write the block's bytes to the block store
err = accessor.Put(blockKey(hash), blockBytes)
if err != nil {
return err
}
return nil
}
// HasBlock returns whether the block of the given hash has been
// previously inserted into the database.
func HasBlock(context Context, hash *daghash.Hash) (bool, error) {
accessor, err := context.accessor()
if err != nil {
return false, err
}
return accessor.Has(blockKey(hash))
}
// FetchBlock returns the block of the given hash. Returns
// ErrNotFound if the block had not been previously inserted
// into the database.
func FetchBlock(context Context, hash *daghash.Hash) ([]byte, error) {
accessor, err := context.accessor()
if err != nil {
return nil, err
}
return accessor.Get(blockKey(hash))
}