mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-11 00:26:44 +00:00

* [NOD-375] Move to pkg/errors * [NOD-375] Fix tests * [NOD-375] Make AreErrorsEqual a shared function
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
// Copyright (c) 2015-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package ffldb
|
|
|
|
import (
|
|
"github.com/daglabs/btcd/database"
|
|
"github.com/daglabs/btcd/wire"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
dbType = "ffldb"
|
|
)
|
|
|
|
// parseArgs parses the arguments from the database Open/Create methods.
|
|
func parseArgs(funcName string, args ...interface{}) (string, wire.BitcoinNet, error) {
|
|
if len(args) != 2 {
|
|
return "", 0, errors.Errorf("invalid arguments to %s.%s -- "+
|
|
"expected database path and block network", dbType,
|
|
funcName)
|
|
}
|
|
|
|
dbPath, ok := args[0].(string)
|
|
if !ok {
|
|
return "", 0, errors.Errorf("first argument to %s.%s is invalid -- "+
|
|
"expected database path string", dbType, funcName)
|
|
}
|
|
|
|
network, ok := args[1].(wire.BitcoinNet)
|
|
if !ok {
|
|
return "", 0, errors.Errorf("second argument to %s.%s is invalid -- "+
|
|
"expected block network", dbType, funcName)
|
|
}
|
|
|
|
return dbPath, network, nil
|
|
}
|
|
|
|
// openDBDriver is the callback provided during driver registration that opens
|
|
// an existing database for use.
|
|
func openDBDriver(args ...interface{}) (database.DB, error) {
|
|
dbPath, network, err := parseArgs("Open", args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return openDB(dbPath, network, false)
|
|
}
|
|
|
|
// createDBDriver is the callback provided during driver registration that
|
|
// creates, initializes, and opens a database for use.
|
|
func createDBDriver(args ...interface{}) (database.DB, error) {
|
|
dbPath, network, err := parseArgs("Create", args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return openDB(dbPath, network, true)
|
|
}
|