Import btcdb repo into database directory.

This commit contains the entire btcdb repository along with several
changes needed to move all of the files into the database directory in
order to prepare it for merging.  This does NOT update btcd or any of the
other packages to use the new location as that will be done separately.

- All import paths in the old btcdb test files have been changed to the
  new location
- All references to btcdb as the package name have been chagned to
  database
- The coveralls badge has been removed since it unfortunately doesn't
  support coverage of sub-packages

This is ongoing work toward #214.
This commit is contained in:
Dave Collins 2015-01-27 11:45:10 -06:00
parent b284bf0f90
commit d574a3af6d
28 changed files with 226 additions and 283 deletions

28
.gitignore vendored
View file

@ -1,28 +0,0 @@
# Temp files
*~
# Log files
*.log
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe

View file

@ -1,15 +0,0 @@
language: go
go: release
before_install:
- gocleandeps=c16c849abae90c23419d
- git clone https://gist.github.com/$gocleandeps.git
- goclean=71d0380287747d956a26
- git clone https://gist.github.com/$goclean.git
install:
- go get -d -t -v ./...
- bash $gocleandeps/gocleandeps.sh
script:
- export PATH=$PATH:$HOME/gopath/bin
- bash $goclean/goclean.sh
after_success:
- goveralls -coverprofile=profile.cov -service=travis-ci

13
LICENSE
View file

@ -1,13 +0,0 @@
Copyright (c) 2013-2014 Conformal Systems LLC.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View file

@ -1,42 +1,40 @@
btcdb
=====
database
========
[![Build Status](http://img.shields.io/travis/btcsuite/btcdb.svg)]
(https://travis-ci.org/btcsuite/btcdb) [![Coverage Status]
(https://img.shields.io/coveralls/btcsuite/btcdb.svg)]
(https://coveralls.io/r/btcsuite/btcdb) [![ISC License]
[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)]
(https://travis-ci.org/btcsuite/btcd) [![ISC License]
(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
Package btcdb provides a database interface for the bitcoin block chain and
Package database provides a database interface for the bitcoin block chain and
transactions.
## Documentation
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)]
(http://godoc.org/github.com/btcsuite/btcdb)
(http://godoc.org/github.com/btcsuite/btcd/database)
Full `go doc` style documentation for the project can be viewed online without
installing this package by using the GoDoc site
[here](http://godoc.org/github.com/btcsuite/btcdb).
[here](http://godoc.org/github.com/btcsuite/btcd/database).
You can also view the documentation locally once the package is installed with
the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to
http://localhost:6060/pkg/github.com/btcsuite/btcdb
http://localhost:6060/pkg/github.com/btcsuite/btcd/database
## Installation
```bash
$ go get github.com/btcsuite/btcdb
$ go get github.com/btcsuite/btcd/database
```
## Examples
* [CreateDB Example]
(http://godoc.org/github.com/btcsuite/btcdb#example-CreateDB)
(http://godoc.org/github.com/btcsuite/btcd/database#example-CreateDB)
Demonstrates creating a new database and inserting the genesis block into it.
* [NewestSha Example]
(http://godoc.org/github.com/btcsuite/btcdb#example-Db--NewestSha)
(http://godoc.org/github.com/btcsuite/btcd/database#example-Db--NewestSha)
Demonstrates querying the database for the most recent best block height and
hash.
@ -65,4 +63,5 @@ signature perform the following:
## License
Package btcdb is licensed under the [copyfree](http://copyfree.org) ISC License.
Package database is licensed under the [copyfree](http://copyfree.org) ISC
License.

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcdb_test
package database_test
import (
"compress/bzip2"
@ -14,9 +14,9 @@ import (
"strings"
"testing"
"github.com/btcsuite/btcdb"
_ "github.com/btcsuite/btcdb/ldb"
_ "github.com/btcsuite/btcdb/memdb"
"github.com/btcsuite/btcd/database"
_ "github.com/btcsuite/btcd/database/ldb"
_ "github.com/btcsuite/btcd/database/memdb"
"github.com/btcsuite/btcnet"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
@ -53,10 +53,10 @@ func fileExists(name string) bool {
// openDB is used to open an existing database based on the database type and
// name.
func openDB(dbType, dbName string) (btcdb.Db, error) {
func openDB(dbType, dbName string) (database.Db, error) {
// Handle memdb specially since it has no files on disk.
if dbType == "memdb" {
db, err := btcdb.OpenDB(dbType)
db, err := database.OpenDB(dbType)
if err != nil {
return nil, fmt.Errorf("error opening db: %v", err)
}
@ -64,7 +64,7 @@ func openDB(dbType, dbName string) (btcdb.Db, error) {
}
dbPath := filepath.Join(testDbRoot, dbName)
db, err := btcdb.OpenDB(dbType, dbPath)
db, err := database.OpenDB(dbType, dbPath)
if err != nil {
return nil, fmt.Errorf("error opening db: %v", err)
}
@ -76,11 +76,11 @@ func openDB(dbType, dbName string) (btcdb.Db, error) {
// should invoke when done testing to clean up. The close flag indicates
// whether or not the teardown function should sync and close the database
// during teardown.
func createDB(dbType, dbName string, close bool) (btcdb.Db, func(), error) {
func createDB(dbType, dbName string, close bool) (database.Db, func(), error) {
// Handle memory database specially since it doesn't need the disk
// specific handling.
if dbType == "memdb" {
db, err := btcdb.CreateDB(dbType)
db, err := database.CreateDB(dbType)
if err != nil {
return nil, nil, fmt.Errorf("error creating db: %v", err)
}
@ -108,7 +108,7 @@ func createDB(dbType, dbName string, close bool) (btcdb.Db, func(), error) {
// Create a new database to store the accepted blocks into.
dbPath := filepath.Join(testDbRoot, dbName)
_ = os.RemoveAll(dbPath)
db, err := btcdb.CreateDB(dbType, dbPath)
db, err := database.CreateDB(dbType, dbPath)
if err != nil {
return nil, nil, fmt.Errorf("error creating db: %v", err)
}
@ -132,7 +132,7 @@ func createDB(dbType, dbName string, close bool) (btcdb.Db, func(), error) {
// setupDB is used to create a new db instance with the genesis block already
// inserted. In addition to the new db instance, it returns a teardown function
// the caller should invoke when done testing to clean up.
func setupDB(dbType, dbName string) (btcdb.Db, func(), error) {
func setupDB(dbType, dbName string) (database.Db, func(), error) {
db, teardown, err := createDB(dbType, dbName, true)
if err != nil {
return nil, nil, err

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcdb
package database
import (
"errors"

View file

@ -2,13 +2,13 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcdb_test
package database_test
import (
"fmt"
"testing"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
)
var (
@ -21,7 +21,7 @@ var (
// testNewestShaEmpty ensures that NewestSha returns the values expected by
// the interface contract.
func testNewestShaEmpty(t *testing.T, db btcdb.Db) {
func testNewestShaEmpty(t *testing.T, db database.Db) {
sha, height, err := db.NewestSha()
if err != nil {
t.Errorf("NewestSha error %v", err)
@ -37,7 +37,7 @@ func testNewestShaEmpty(t *testing.T, db btcdb.Db) {
// TestEmptyDB tests that empty databases are handled properly.
func TestEmptyDB(t *testing.T) {
for _, dbType := range btcdb.SupportedDBs() {
for _, dbType := range database.SupportedDBs() {
// Ensure NewestSha returns expected values for a newly created
// db.
db, teardown, err := createDB(dbType, "emptydb", false)
@ -66,7 +66,7 @@ func TestEmptyDB(t *testing.T) {
// TestAddDuplicateDriver ensures that adding a duplicate driver does not
// overwrite an existing one.
func TestAddDuplicateDriver(t *testing.T) {
supportedDBs := btcdb.SupportedDBs()
supportedDBs := database.SupportedDBs()
if len(supportedDBs) == 0 {
t.Errorf("TestAddDuplicateDriver: No backends to test")
return
@ -77,7 +77,7 @@ func TestAddDuplicateDriver(t *testing.T) {
// driver function and intentionally returns a failure that can be
// detected if the interface allows a duplicate driver to overwrite an
// existing one.
bogusCreateDB := func(args ...interface{}) (btcdb.Db, error) {
bogusCreateDB := func(args ...interface{}) (database.Db, error) {
return nil, fmt.Errorf("duplicate driver allowed for database "+
"type [%v]", dbType)
}
@ -85,12 +85,12 @@ func TestAddDuplicateDriver(t *testing.T) {
// Create a driver that tries to replace an existing one. Set its
// create and open functions to a function that causes a test failure if
// they are invoked.
driver := btcdb.DriverDB{
driver := database.DriverDB{
DbType: dbType,
CreateDB: bogusCreateDB,
OpenDB: bogusCreateDB,
}
btcdb.AddDBDriver(driver)
database.AddDBDriver(driver)
// Ensure creating a database of the type that we tried to replace
// doesn't fail (if it does, it indicates the driver was erroneously
@ -112,22 +112,22 @@ func TestCreateOpenFail(t *testing.T) {
dbType := "createopenfail"
openError := fmt.Errorf("failed to create or open database for "+
"database type [%v]", dbType)
bogusCreateDB := func(args ...interface{}) (btcdb.Db, error) {
bogusCreateDB := func(args ...interface{}) (database.Db, error) {
return nil, openError
}
// Create and add driver that intentionally fails when created or opened
// to ensure errors on database open and create are handled properly.
driver := btcdb.DriverDB{
driver := database.DriverDB{
DbType: dbType,
CreateDB: bogusCreateDB,
OpenDB: bogusCreateDB,
}
btcdb.AddDBDriver(driver)
database.AddDBDriver(driver)
// Ensure creating a database with the new type fails with the expected
// error.
_, err := btcdb.CreateDB(dbType, "createfailtest")
_, err := database.CreateDB(dbType, "createfailtest")
if err != openError {
t.Errorf("TestCreateOpenFail: expected error not received - "+
"got: %v, want %v", err, openError)
@ -136,7 +136,7 @@ func TestCreateOpenFail(t *testing.T) {
// Ensure opening a database with the new type fails with the expected
// error.
_, err = btcdb.OpenDB(dbType, "openfailtest")
_, err = database.OpenDB(dbType, "openfailtest")
if err != openError {
t.Errorf("TestCreateOpenFail: expected error not received - "+
"got: %v, want %v", err, openError)
@ -150,28 +150,28 @@ func TestCreateOpenUnsupported(t *testing.T) {
// Ensure creating a database with an unsupported type fails with the
// expected error.
dbType := "unsupported"
_, err := btcdb.CreateDB(dbType, "unsupportedcreatetest")
if err != btcdb.ErrDbUnknownType {
_, err := database.CreateDB(dbType, "unsupportedcreatetest")
if err != database.ErrDbUnknownType {
t.Errorf("TestCreateOpenUnsupported: expected error not "+
"received - got: %v, want %v", err, btcdb.ErrDbUnknownType)
"received - got: %v, want %v", err, database.ErrDbUnknownType)
return
}
// Ensure opening a database with the new type fails with the expected
// error.
_, err = btcdb.OpenDB(dbType, "unsupportedopentest")
if err != btcdb.ErrDbUnknownType {
_, err = database.OpenDB(dbType, "unsupportedopentest")
if err != database.ErrDbUnknownType {
t.Errorf("TestCreateOpenUnsupported: expected error not "+
"received - got: %v, want %v", err, btcdb.ErrDbUnknownType)
"received - got: %v, want %v", err, database.ErrDbUnknownType)
return
}
}
// TestInterface performs tests for the various interfaces of btcdb which
// require state in the database for each supported database type (those loaded
// in common_test.go that is).
// TestInterface performs tests for the various interfaces of the database
// package which require state in the database for each supported database
// type (those loaded in common_test.go that is).
func TestInterface(t *testing.T) {
for _, dbType := range btcdb.SupportedDBs() {
for _, dbType := range database.SupportedDBs() {
if _, exists := ignoreDbTypes[dbType]; !exists {
testInterface(t, dbType)
}

31
database/doc.go Normal file
View file

@ -0,0 +1,31 @@
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
/*
Package database provides a database interface for the Bitcoin block chain.
As of July 2014, there are over 309,000 blocks in the Bitcoin block chain and
and over 42 million transactions (which turns out to be over 21GB of data).
This package provides a database layer to store and retrieve this data in a
fairly simple and efficient manner. The use of this should not require specific
knowledge of the database backend.
Basic Design
The basic design of this package is to provide two classes of items in a
database; blocks and transactions (tx) where the block number increases
monotonically. Each transaction belongs to a single block although a block can
have a variable number of transactions. Along with these two items, several
convenience functions for dealing with the database are provided as well as
functions to query specific items that may be present in a block or tx.
Usage
At the highest level, the use of this packages just requires that you import it,
setup a database, insert some data into it, and optionally, query the data back.
The first block inserted into the database will be treated as the genesis block.
Every subsequent block insert requires the referenced parent block to already
exist.
*/
package database

View file

@ -2,13 +2,13 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcdb_test
package database_test
import (
"fmt"
"github.com/btcsuite/btcdb"
_ "github.com/btcsuite/btcdb/memdb"
"github.com/btcsuite/btcd/database"
_ "github.com/btcsuite/btcd/database/memdb"
"github.com/btcsuite/btcnet"
"github.com/btcsuite/btcutil"
)
@ -20,8 +20,8 @@ func ExampleCreateDB() {
// Ordinarily this would be whatever driver(s) your application
// requires.
// import (
// "github.com/btcsuite/btcdb"
// _ "github.com/btcsuite/btcdb/memdb"
// "github.com/btcsuite/btcd/database"
// _ "github.com/btcsuite/btcd/database/memdb"
// )
// Create a database and schedule it to be closed on exit. This example
@ -29,7 +29,7 @@ func ExampleCreateDB() {
// the disk. Typically, you would specify a persistent database driver
// such as "leveldb" and give it a database name as the second
// parameter.
db, err := btcdb.CreateDB("memdb")
db, err := database.CreateDB("memdb")
if err != nil {
fmt.Println(err)
return
@ -51,8 +51,8 @@ func ExampleCreateDB() {
}
// exampleLoadDB is used in the example to elide the setup code.
func exampleLoadDB() (btcdb.Db, error) {
db, err := btcdb.CreateDB("memdb")
func exampleLoadDB() (database.Db, error) {
db, err := database.CreateDB("memdb")
if err != nil {
return nil, err
}

View file

@ -2,13 +2,13 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcdb_test
package database_test
import (
"reflect"
"testing"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
"github.com/davecgh/go-spew/spew"
@ -25,7 +25,7 @@ import (
type testContext struct {
t *testing.T
dbType string
db btcdb.Db
db database.Db
blockHeight int64
blockHash *btcwire.ShaHash
block *btcutil.Block
@ -523,8 +523,8 @@ func testIntegrity(tc *testContext) bool {
return true
}
// testInterface tests performs tests for the various interfaces of btcdb which
// require state in the database for the given database type.
// testInterface tests performs tests for the various interfaces of the database
// package which require state in the database for the given database type.
func testInterface(t *testing.T, dbType string) {
db, teardown, err := setupDB(dbType, "interface")
if err != nil {

View file

@ -8,7 +8,7 @@ import (
"bytes"
"encoding/binary"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
"github.com/btcsuite/goleveldb/leveldb"
@ -40,7 +40,7 @@ func (db *LevelDb) fetchBlockBySha(sha *btcwire.ShaHash) (blk *btcutil.Block, er
}
// FetchBlockHeightBySha returns the block height for the given hash. This is
// part of the btcdb.Db interface implementation.
// part of the database.Db interface implementation.
func (db *LevelDb) FetchBlockHeightBySha(sha *btcwire.ShaHash) (int64, error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
@ -77,7 +77,7 @@ func (db *LevelDb) getBlkLoc(sha *btcwire.ShaHash) (int64, error) {
data, err := db.lDb.Get(key, db.ro)
if err != nil {
if err == leveldb.ErrNotFound {
err = btcdb.ErrBlockShaMissing
err = database.ErrBlockShaMissing
}
return 0, err
}
@ -151,7 +151,7 @@ func (db *LevelDb) insertBlockData(sha *btcwire.ShaHash, prevSha *btcwire.ShaHas
if err != nil {
// check current block count
// if count != 0 {
// err = btcdb.PrevShaMissing
// err = database.PrevShaMissing
// return
// }
oBlkHeight = -1
@ -206,7 +206,7 @@ func (db *LevelDb) blkExistsSha(sha *btcwire.ShaHash) (bool, error) {
switch err {
case nil:
return true, nil
case leveldb.ErrNotFound, btcdb.ErrBlockShaMissing:
case leveldb.ErrNotFound, database.ErrBlockShaMissing:
return false, nil
}
return false, err
@ -247,7 +247,7 @@ func (db *LevelDb) FetchHeightRange(startHeight, endHeight int64) (rshalist []bt
defer db.dbLock.Unlock()
var endidx int64
if endHeight == btcdb.AllShas {
if endHeight == database.AllShas {
endidx = startHeight + 500
} else {
endidx = endHeight
@ -302,7 +302,7 @@ func (db *LevelDb) fetchAddrIndexTip() (*btcwire.ShaHash, int64, error) {
data, err := db.lDb.Get(addrIndexMetaDataKey, db.ro)
if err != nil {
return &btcwire.ShaHash{}, -1, btcdb.ErrAddrIndexDoesNotExist
return &btcwire.ShaHash{}, -1, database.ErrAddrIndexDoesNotExist
}
var blkSha btcwire.ShaHash
@ -322,7 +322,7 @@ func (db *LevelDb) FetchAddrIndexTip() (*btcwire.ShaHash, int64, error) {
defer db.dbLock.Unlock()
if db.lastAddrIndexBlkIdx == -1 {
return &btcwire.ShaHash{}, -1, btcdb.ErrAddrIndexDoesNotExist
return &btcwire.ShaHash{}, -1, database.ErrAddrIndexDoesNotExist
}
sha := db.lastAddrIndexBlkSha

View file

@ -8,7 +8,7 @@ import (
"os"
"testing"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcwire"
)
@ -21,7 +21,7 @@ func TestEmptyDB(t *testing.T) {
dbnamever := dbname + ".ver"
_ = os.RemoveAll(dbname)
_ = os.RemoveAll(dbnamever)
db, err := btcdb.CreateDB("leveldb", dbname)
db, err := database.CreateDB("leveldb", dbname)
if err != nil {
t.Errorf("Failed to open test database %v", err)
return
@ -42,7 +42,7 @@ func TestEmptyDB(t *testing.T) {
t.Errorf("Close: unexpected error: %v", err)
}
db, err = btcdb.OpenDB("leveldb", dbname)
db, err = database.OpenDB("leveldb", dbname)
if err != nil {
t.Errorf("Failed to open test database %v", err)
return

View file

@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.
/*
Package ldb implements an instance of btcdb backed by leveldb.
Package ldb implements an instance of the database package backed by leveldb.
Database version number is stored in a flat file <dbname>.ver
Currently a single (littlendian) integer in the file. If there is

View file

@ -10,7 +10,7 @@ import (
"path/filepath"
"testing"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
)
@ -22,7 +22,7 @@ func Test_dupTx(t *testing.T) {
dbnamever := dbname + ".ver"
_ = os.RemoveAll(dbname)
_ = os.RemoveAll(dbnamever)
db, err := btcdb.CreateDB("leveldb", dbname)
db, err := database.CreateDB("leveldb", dbname)
if err != nil {
t.Errorf("Failed to open test database %v", err)
return
@ -154,7 +154,7 @@ out:
listReply = db.FetchUnSpentTxByShaList(fetchList)
for _, lr := range listReply {
if lr.Err != btcdb.ErrTxShaMissing {
if lr.Err != database.ErrTxShaMissing {
t.Errorf("sha %v spent %v err %v\n", lr.Sha,
lr.TxSpent, lr.Err)
}

View file

@ -10,8 +10,8 @@ import (
"path/filepath"
"testing"
"github.com/btcsuite/btcdb"
_ "github.com/btcsuite/btcdb/ldb"
"github.com/btcsuite/btcd/database"
_ "github.com/btcsuite/btcd/database/ldb"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
)
@ -48,7 +48,7 @@ func testUnspentInsert(t *testing.T) {
dbnamever := dbname + ".ver"
_ = os.RemoveAll(dbname)
_ = os.RemoveAll(dbnamever)
db, err := btcdb.CreateDB("leveldb", dbname)
db, err := database.CreateDB("leveldb", dbname)
if err != nil {
t.Errorf("Failed to open test database %v", err)
return
@ -96,7 +96,7 @@ endtest:
txOutList = append(txOutList, &txshaname)
}
txneededmap := map[btcwire.ShaHash]*btcdb.TxListReply{}
txneededmap := map[btcwire.ShaHash]*database.TxListReply{}
txlist := db.FetchUnSpentTxByShaList(txneededList)
for _, txe := range txlist {
if txe.Err != nil {
@ -122,7 +122,7 @@ endtest:
break endtest
}
txlookupmap := map[btcwire.ShaHash]*btcdb.TxListReply{}
txlookupmap := map[btcwire.ShaHash]*database.TxListReply{}
txlist = db.FetchTxByShaList(txlookupList)
for _, txe := range txlist {
if txe.Err != nil {
@ -158,7 +158,7 @@ endtest:
break endtest
}
txlookupmap = map[btcwire.ShaHash]*btcdb.TxListReply{}
txlookupmap = map[btcwire.ShaHash]*database.TxListReply{}
txlist = db.FetchUnSpentTxByShaList(txlookupList)
for _, txe := range txlist {
if txe.Err != nil {
@ -180,7 +180,7 @@ endtest:
t.Errorf("failed to insert block %v err %v", height, err)
break endtest
}
txlookupmap = map[btcwire.ShaHash]*btcdb.TxListReply{}
txlookupmap = map[btcwire.ShaHash]*database.TxListReply{}
txlist = db.FetchTxByShaList(txlookupList)
for _, txe := range txlist {
if txe.Err != nil {

View file

@ -11,7 +11,7 @@ import (
"strconv"
"sync"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btclog"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
@ -60,13 +60,13 @@ type LevelDb struct {
txSpentUpdateMap map[btcwire.ShaHash]*spentTxUpdate
}
var self = btcdb.DriverDB{DbType: "leveldb", CreateDB: CreateDB, OpenDB: OpenDB}
var self = database.DriverDB{DbType: "leveldb", CreateDB: CreateDB, OpenDB: OpenDB}
func init() {
btcdb.AddDBDriver(self)
database.AddDBDriver(self)
}
// parseArgs parses the arguments from the btcdb Open/Create methods.
// parseArgs parses the arguments from the database package Open/Create methods.
func parseArgs(funcName string, args ...interface{}) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("Invalid arguments to ldb.%s -- "+
@ -81,13 +81,13 @@ func parseArgs(funcName string, args ...interface{}) (string, error) {
}
// OpenDB opens an existing database for use.
func OpenDB(args ...interface{}) (btcdb.Db, error) {
func OpenDB(args ...interface{}) (database.Db, error) {
dbpath, err := parseArgs("OpenDB", args...)
if err != nil {
return nil, err
}
log = btcdb.GetLog()
log = database.GetLog()
db, err := openDB(dbpath, false)
if err != nil {
@ -159,7 +159,7 @@ blocknarrow:
// CurrentDBVersion is the database version.
var CurrentDBVersion int32 = 1
func openDB(dbpath string, create bool) (pbdb btcdb.Db, err error) {
func openDB(dbpath string, create bool) (pbdb database.Db, err error) {
var db LevelDb
var tlDb *leveldb.DB
var dbversion int32
@ -184,7 +184,7 @@ func openDB(dbpath string, create bool) (pbdb btcdb.Db, err error) {
} else {
_, err = os.Stat(dbpath)
if err != nil {
err = btcdb.ErrDbDoesNotExist
err = database.ErrDbDoesNotExist
return
}
}
@ -247,13 +247,13 @@ func openDB(dbpath string, create bool) (pbdb btcdb.Db, err error) {
}
// CreateDB creates, initializes and opens a database for use.
func CreateDB(args ...interface{}) (btcdb.Db, error) {
func CreateDB(args ...interface{}) (database.Db, error) {
dbpath, err := parseArgs("Create", args...)
if err != nil {
return nil, err
}
log = btcdb.GetLog()
log = database.GetLog()
// No special setup needed, just OpenBB
db, err := openDB(dbpath, true)
@ -694,7 +694,7 @@ func (db *LevelDb) processBatches() error {
return nil
}
// RollbackClose this is part of the btcdb.Db interface and should discard
// RollbackClose this is part of the database.Db interface and should discard
// recent changes to the db and the close the db. This currently just does
// a clean shutdown.
func (db *LevelDb) RollbackClose() error {

View file

@ -15,7 +15,7 @@ import (
"strings"
"testing"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcnet"
"github.com/btcsuite/btcscript"
"github.com/btcsuite/btcutil"
@ -29,7 +29,7 @@ var network = btcwire.MainNet
// the `cleanUpFunc` *must* be called after each test to maintain db
// consistency across tests.
type testDb struct {
db btcdb.Db
db database.Db
blocks []*btcutil.Block
dbName string
dbNameVer string
@ -42,7 +42,7 @@ func setUpTestDb(t *testing.T) (*testDb, error) {
dbnamever := dbname + ".ver"
_ = os.RemoveAll(dbname)
_ = os.RemoveAll(dbnamever)
db, err := btcdb.CreateDB("leveldb", dbname)
db, err := database.CreateDB("leveldb", dbname)
if err != nil {
return nil, err
}
@ -74,10 +74,10 @@ func TestOperational(t *testing.T) {
// testAddrIndexOperations ensures that all normal operations concerning
// the optional address index function correctly.
func testAddrIndexOperations(t *testing.T, db btcdb.Db, newestBlock *btcutil.Block, newestSha *btcwire.ShaHash, newestBlockIdx int64) {
func testAddrIndexOperations(t *testing.T, db database.Db, newestBlock *btcutil.Block, newestSha *btcwire.ShaHash, newestBlockIdx int64) {
// Metadata about the current addr index state should be unset.
sha, height, err := db.FetchAddrIndexTip()
if err != btcdb.ErrAddrIndexDoesNotExist {
if err != database.ErrAddrIndexDoesNotExist {
t.Fatalf("Address index metadata shouldn't be in db, hasn't been built up yet.")
}
@ -104,7 +104,7 @@ func testAddrIndexOperations(t *testing.T, db btcdb.Db, newestBlock *btcutil.Blo
}
// Simple test to index outputs(s) of the first tx.
testIndex := make(btcdb.BlockAddrIndex)
testIndex := make(database.BlockAddrIndex)
testTx, err := newestBlock.Tx(0)
if err != nil {
t.Fatalf("Block has no transactions, unable to test addr "+
@ -160,7 +160,7 @@ func testAddrIndexOperations(t *testing.T, db btcdb.Db, newestBlock *btcutil.Blo
db.Close()
// Re-Open, tip still should be updated to current height and sha.
db, err = btcdb.OpenDB("leveldb", "tstdbop1")
db, err = database.OpenDB("leveldb", "tstdbop1")
if err != nil {
t.Fatalf("Unable to re-open created db, err %v", err)
}
@ -184,13 +184,13 @@ func testAddrIndexOperations(t *testing.T, db btcdb.Db, newestBlock *btcutil.Blo
}
// Tip should be blanked out.
if _, _, err := db.FetchAddrIndexTip(); err != btcdb.ErrAddrIndexDoesNotExist {
if _, _, err := db.FetchAddrIndexTip(); err != database.ErrAddrIndexDoesNotExist {
t.Fatalf("Address index was not fully deleted.")
}
}
func assertAddrIndexTipIsUpdated(db btcdb.Db, t *testing.T, newestSha *btcwire.ShaHash, newestBlockIdx int64) {
func assertAddrIndexTipIsUpdated(db database.Db, t *testing.T, newestSha *btcwire.ShaHash, newestBlockIdx int64) {
// Safe to ignore error, since height will be < 0 in "error" case.
sha, height, _ := db.FetchAddrIndexTip()
if newestBlockIdx != height {
@ -337,7 +337,7 @@ func testBackout(t *testing.T) {
// db was closed at height 120, so no cleanup is possible.
// reopen db
testDb.db, err = btcdb.OpenDB("leveldb", testDb.dbName)
testDb.db, err = database.OpenDB("leveldb", testDb.dbName)
if err != nil {
t.Errorf("Failed to open test database %v", err)
return
@ -465,7 +465,7 @@ func loadBlocks(t *testing.T, file string) (blocks []*btcutil.Block, err error)
return
}
func testFetchHeightRange(t *testing.T, db btcdb.Db, blocks []*btcutil.Block) {
func testFetchHeightRange(t *testing.T, db database.Db, blocks []*btcutil.Block) {
var testincrement int64 = 50
var testcnt int64 = 100
@ -486,7 +486,7 @@ func testFetchHeightRange(t *testing.T, db btcdb.Db, blocks []*btcutil.Block) {
endheight := startheight + testcnt
if endheight > nBlocks {
endheight = btcdb.AllShas
endheight = database.AllShas
}
shalist, err := db.FetchHeightRange(startheight, endheight)
@ -494,7 +494,7 @@ func testFetchHeightRange(t *testing.T, db btcdb.Db, blocks []*btcutil.Block) {
t.Errorf("FetchHeightRange: unexpected failure looking up shas %v", err)
}
if endheight == btcdb.AllShas {
if endheight == database.AllShas {
if int64(len(shalist)) != nBlocks-startheight {
t.Errorf("FetchHeightRange: expected A %v shas, got %v", nBlocks-startheight, len(shalist))
}
@ -549,7 +549,7 @@ func TestLimitAndSkipFetchTxsForAddr(t *testing.T) {
// Create and insert an address index for out test addr.
txLoc, _ := testBlock.TxLoc()
index := make(btcdb.BlockAddrIndex)
index := make(database.BlockAddrIndex)
for i := range testBlock.Transactions() {
var hash160 [ripemd160.Size]byte
scriptAddr := targetAddr.ScriptAddress()

View file

@ -9,7 +9,7 @@ import (
"encoding/binary"
"errors"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
"github.com/btcsuite/goleveldb/leveldb"
@ -129,7 +129,7 @@ func (db *LevelDb) getTxFullySpent(txsha *btcwire.ShaHash) ([]*spentTx, error) {
key := shaSpentTxToKey(txsha)
buf, err := db.lDb.Get(key, db.ro)
if err == leveldb.ErrNotFound {
return badTxList, btcdb.ErrTxShaMissing
return badTxList, database.ErrTxShaMissing
} else if err != nil {
return badTxList, err
}
@ -198,13 +198,13 @@ func (db *LevelDb) existsTxSha(txSha *btcwire.ShaHash) (bool, error) {
}
// FetchTxByShaList returns the most recent tx of the name fully spent or not
func (db *LevelDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListReply {
func (db *LevelDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*database.TxListReply {
db.dbLock.Lock()
defer db.dbLock.Unlock()
// until the fully spent separation of tx is complete this is identical
// to FetchUnSpentTxByShaList
replies := make([]*btcdb.TxListReply, len(txShaList))
replies := make([]*database.TxListReply, len(txShaList))
for i, txsha := range txShaList {
tx, blockSha, height, txspent, err := db.fetchTxDataBySha(txsha)
btxspent := []bool{}
@ -216,7 +216,7 @@ func (db *LevelDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxLis
btxspent[idx] = (txspent[byteidx] & (byte(1) << byteoff)) != 0
}
}
if err == btcdb.ErrTxShaMissing {
if err == database.ErrTxShaMissing {
// if the unspent pool did not have the tx,
// look in the fully spent pool (only last instance)
@ -235,7 +235,7 @@ func (db *LevelDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxLis
}
}
}
txlre := btcdb.TxListReply{Sha: txsha, Tx: tx, BlkSha: blockSha, Height: height, TxSpent: btxspent, Err: err}
txlre := database.TxListReply{Sha: txsha, Tx: tx, BlkSha: blockSha, Height: height, TxSpent: btxspent, Err: err}
replies[i] = &txlre
}
return replies
@ -243,11 +243,11 @@ func (db *LevelDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxLis
// FetchUnSpentTxByShaList given a array of ShaHash, look up the transactions
// and return them in a TxListReply array.
func (db *LevelDb) FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListReply {
func (db *LevelDb) FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*database.TxListReply {
db.dbLock.Lock()
defer db.dbLock.Unlock()
replies := make([]*btcdb.TxListReply, len(txShaList))
replies := make([]*database.TxListReply, len(txShaList))
for i, txsha := range txShaList {
tx, blockSha, height, txspent, err := db.fetchTxDataBySha(txsha)
btxspent := []bool{}
@ -259,7 +259,7 @@ func (db *LevelDb) FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*btcd
btxspent[idx] = (txspent[byteidx] & (byte(1) << byteoff)) != 0
}
}
txlre := btcdb.TxListReply{Sha: txsha, Tx: tx, BlkSha: blockSha, Height: height, TxSpent: btxspent, Err: err}
txlre := database.TxListReply{Sha: txsha, Tx: tx, BlkSha: blockSha, Height: height, TxSpent: btxspent, Err: err}
replies[i] = &txlre
}
return replies
@ -274,7 +274,7 @@ func (db *LevelDb) fetchTxDataBySha(txsha *btcwire.ShaHash) (rtx *btcwire.MsgTx,
blkHeight, txOff, txLen, txspent, err = db.getTxData(txsha)
if err != nil {
if err == leveldb.ErrNotFound {
err = btcdb.ErrTxShaMissing
err = database.ErrTxShaMissing
}
return
}
@ -290,7 +290,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
blksha, blkbuf, err = db.getBlkByHeight(blkHeight)
if err != nil {
if err == leveldb.ErrNotFound {
err = btcdb.ErrTxShaMissing
err = database.ErrTxShaMissing
}
return
}
@ -299,7 +299,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
// txsha, blksha, blkHeight, txOff, txLen)
if len(blkbuf) < txOff+txLen {
err = btcdb.ErrTxShaMissing
err = database.ErrTxShaMissing
return
}
rbuf := bytes.NewReader(blkbuf[txOff : txOff+txLen])
@ -316,7 +316,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
}
// FetchTxBySha returns some data for the given Tx Sha.
func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, error) {
func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*database.TxListReply, error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
@ -327,22 +327,22 @@ func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, e
if txerr == nil {
replylen++
} else {
if txerr != btcdb.ErrTxShaMissing {
return []*btcdb.TxListReply{}, txerr
if txerr != database.ErrTxShaMissing {
return []*database.TxListReply{}, txerr
}
}
sTxList, fSerr := db.getTxFullySpent(txsha)
if fSerr != nil {
if fSerr != btcdb.ErrTxShaMissing {
return []*btcdb.TxListReply{}, fSerr
if fSerr != database.ErrTxShaMissing {
return []*database.TxListReply{}, fSerr
}
} else {
replylen += len(sTxList)
}
replies := make([]*btcdb.TxListReply, replylen)
replies := make([]*database.TxListReply, replylen)
if fSerr == nil {
for _, stx := range sTxList {
@ -350,7 +350,7 @@ func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, e
stx.blkHeight, stx.txoff, stx.txlen, []byte{})
if err != nil {
if err != leveldb.ErrNotFound {
return []*btcdb.TxListReply{}, err
return []*database.TxListReply{}, err
}
continue
}
@ -358,7 +358,7 @@ func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, e
for i := range btxspent {
btxspent[i] = true
}
txlre := btcdb.TxListReply{Sha: txsha, Tx: tx, BlkSha: blksha, Height: stx.blkHeight, TxSpent: btxspent, Err: nil}
txlre := database.TxListReply{Sha: txsha, Tx: tx, BlkSha: blksha, Height: stx.blkHeight, TxSpent: btxspent, Err: nil}
replies[replycnt] = &txlre
replycnt++
}
@ -370,7 +370,7 @@ func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, e
byteoff := uint(idx % 8)
btxspent[idx] = (txspent[byteidx] & (byte(1) << byteoff)) != 0
}
txlre := btcdb.TxListReply{Sha: txsha, Tx: tx, BlkSha: blksha, Height: height, TxSpent: btxspent, Err: nil}
txlre := database.TxListReply{Sha: txsha, Tx: tx, BlkSha: blksha, Height: height, TxSpent: btxspent, Err: nil}
replies[replycnt] = &txlre
replycnt++
}
@ -423,7 +423,7 @@ func bytesPrefix(prefix []byte) *util.Range {
// caller wishes to seek forward in the results some amount, the 'seek'
// represents how many results to skip.
func (db *LevelDb) FetchTxsForAddr(addr btcutil.Address, skip int,
limit int) ([]*btcdb.TxListReply, error) {
limit int) ([]*database.TxListReply, error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
@ -448,7 +448,7 @@ func (db *LevelDb) FetchTxsForAddr(addr btcutil.Address, skip int,
hash160 := addr.AddressPubKeyHash().Hash160()
addrKey = hash160[:]
default:
return nil, btcdb.ErrUnsupportedAddressType
return nil, database.ErrUnsupportedAddressType
}
// Create the prefix for our search.
@ -456,7 +456,7 @@ func (db *LevelDb) FetchTxsForAddr(addr btcutil.Address, skip int,
copy(addrPrefix[:2], addrIndexKeyPrefix)
copy(addrPrefix[2:], addrKey)
var replies []*btcdb.TxListReply
var replies []*database.TxListReply
iter := db.lDb.NewIterator(bytesPrefix(addrPrefix), nil)
for skip != 0 && iter.Next() {
@ -477,7 +477,7 @@ func (db *LevelDb) FetchTxsForAddr(addr btcutil.Address, skip int,
}
txSha, _ := tx.TxSha()
txReply := &btcdb.TxListReply{Sha: &txSha, Tx: tx,
txReply := &database.TxListReply{Sha: &txSha, Tx: tx,
BlkSha: blkSha, Height: blkHeight, TxSpent: []bool{}, Err: err}
replies = append(replies, txReply)
@ -502,7 +502,7 @@ func (db *LevelDb) FetchTxsForAddr(addr btcutil.Address, skip int,
// append-only list for the stored value. However, this add unnecessary
// overhead when storing and retrieving since the entire list must
// be fetched each time.
func (db *LevelDb) UpdateAddrIndexForBlock(blkSha *btcwire.ShaHash, blkHeight int64, addrIndex btcdb.BlockAddrIndex) error {
func (db *LevelDb) UpdateAddrIndexForBlock(blkSha *btcwire.ShaHash, blkHeight int64, addrIndex database.BlockAddrIndex) error {
db.dbLock.Lock()
defer db.dbLock.Unlock()

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcdb
package database
import (
"errors"

View file

@ -3,8 +3,8 @@
// license that can be found in the LICENSE file.
/*
Package memdb implements an instance of btcdb that uses memory for the block
storage.
Package memdb implements an instance of the database package that uses memory
for the block storage.
This is primary used for testing purposes as normal operations require a
persistent block storage mechanism which this is not.

View file

@ -7,18 +7,18 @@ package memdb
import (
"fmt"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btclog"
)
var log = btclog.Disabled
func init() {
driver := btcdb.DriverDB{DbType: "memdb", CreateDB: CreateDB, OpenDB: OpenDB}
btcdb.AddDBDriver(driver)
driver := database.DriverDB{DbType: "memdb", CreateDB: CreateDB, OpenDB: OpenDB}
database.AddDBDriver(driver)
}
// parseArgs parses the arguments from the btcdb Open/Create methods.
// parseArgs parses the arguments from the database package Open/Create methods.
func parseArgs(funcName string, args ...interface{}) error {
if len(args) != 0 {
return fmt.Errorf("memdb.%s does not accept any arguments",
@ -29,7 +29,7 @@ func parseArgs(funcName string, args ...interface{}) error {
}
// OpenDB opens an existing database for use.
func OpenDB(args ...interface{}) (btcdb.Db, error) {
func OpenDB(args ...interface{}) (database.Db, error) {
if err := parseArgs("OpenDB", args...); err != nil {
return nil, err
}
@ -39,11 +39,11 @@ func OpenDB(args ...interface{}) (btcdb.Db, error) {
}
// CreateDB creates, initializes, and opens a database for use.
func CreateDB(args ...interface{}) (btcdb.Db, error) {
func CreateDB(args ...interface{}) (database.Db, error) {
if err := parseArgs("CreateDB", args...); err != nil {
return nil, err
}
log = btcdb.GetLog()
log = database.GetLog()
return newMemDb(), nil
}

View file

@ -10,7 +10,7 @@ import (
"math"
"sync"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
)
@ -73,7 +73,7 @@ func isFullySpent(txD *tTxInsertData) bool {
return true
}
// MemDb is a concrete implementation of the btcdb.Db interface which provides
// MemDb is a concrete implementation of the database.Db interface which provides
// a memory-only database. Since it is memory-only, it is obviously not
// persistent and is mostly only useful for testing purposes.
type MemDb struct {
@ -133,7 +133,7 @@ func (db *MemDb) removeTx(msgTx *btcwire.MsgTx, txHash *btcwire.ShaHash) {
}
// Close cleanly shuts down database. This is part of the btcdb.Db interface
// Close cleanly shuts down database. This is part of the database.Db interface
// implementation.
//
// All data is purged upon close with this implementation since it is a
@ -155,7 +155,7 @@ func (db *MemDb) Close() error {
// DropAfterBlockBySha removes any blocks from the database after the given
// block. This is different than a simple truncate since the spend information
// for each block must also be unwound. This is part of the btcdb.Db interface
// for each block must also be unwound. This is part of the database.Db interface
// implementation.
func (db *MemDb) DropAfterBlockBySha(sha *btcwire.ShaHash) error {
db.Lock()
@ -196,7 +196,7 @@ func (db *MemDb) DropAfterBlockBySha(sha *btcwire.ShaHash) error {
}
// ExistsSha returns whether or not the given block hash is present in the
// database. This is part of the btcdb.Db interface implementation.
// database. This is part of the database.Db interface implementation.
func (db *MemDb) ExistsSha(sha *btcwire.ShaHash) (bool, error) {
db.Lock()
defer db.Unlock()
@ -213,7 +213,7 @@ func (db *MemDb) ExistsSha(sha *btcwire.ShaHash) (bool, error) {
}
// FetchBlockBySha returns a btcutil.Block. The implementation may cache the
// underlying data if desired. This is part of the btcdb.Db interface
// underlying data if desired. This is part of the database.Db interface
// implementation.
//
// This implementation does not use any additional cache since the entire
@ -236,7 +236,7 @@ func (db *MemDb) FetchBlockBySha(sha *btcwire.ShaHash) (*btcutil.Block, error) {
}
// FetchBlockHeightBySha returns the block height for the given hash. This is
// part of the btcdb.Db interface implementation.
// part of the database.Db interface implementation.
func (db *MemDb) FetchBlockHeightBySha(sha *btcwire.ShaHash) (int64, error) {
db.Lock()
defer db.Unlock()
@ -254,7 +254,7 @@ func (db *MemDb) FetchBlockHeightBySha(sha *btcwire.ShaHash) (int64, error) {
// FetchBlockHeaderBySha returns a btcwire.BlockHeader for the given sha. The
// implementation may cache the underlying data if desired. This is part of the
// btcdb.Db interface implementation.
// database.Db interface implementation.
//
// This implementation does not use any additional cache since the entire
// database is already in memory.
@ -274,7 +274,7 @@ func (db *MemDb) FetchBlockHeaderBySha(sha *btcwire.ShaHash) (*btcwire.BlockHead
}
// FetchBlockShaByHeight returns a block hash based on its height in the block
// chain. This is part of the btcdb.Db interface implementation.
// chain. This is part of the database.Db interface implementation.
func (db *MemDb) FetchBlockShaByHeight(height int64) (*btcwire.ShaHash, error) {
db.Lock()
defer db.Unlock()
@ -302,7 +302,7 @@ func (db *MemDb) FetchBlockShaByHeight(height int64) (*btcwire.ShaHash, error) {
// FetchHeightRange looks up a range of blocks by the start and ending heights.
// Fetch is inclusive of the start height and exclusive of the ending height.
// To fetch all hashes from the start height until no more are present, use the
// special id `AllShas'. This is part of the btcdb.Db interface implementation.
// special id `AllShas'. This is part of the database.Db interface implementation.
func (db *MemDb) FetchHeightRange(startHeight, endHeight int64) ([]btcwire.ShaHash, error) {
db.Lock()
defer db.Unlock()
@ -313,7 +313,7 @@ func (db *MemDb) FetchHeightRange(startHeight, endHeight int64) ([]btcwire.ShaHa
// When the user passes the special AllShas id, adjust the end height
// accordingly.
if endHeight == btcdb.AllShas {
if endHeight == database.AllShas {
endHeight = int64(len(db.blocks))
}
@ -348,7 +348,7 @@ func (db *MemDb) FetchHeightRange(startHeight, endHeight int64) ([]btcwire.ShaHa
}
// ExistsTxSha returns whether or not the given transaction hash is present in
// the database and is not fully spent. This is part of the btcdb.Db interface
// the database and is not fully spent. This is part of the database.Db interface
// implementation.
func (db *MemDb) ExistsTxSha(sha *btcwire.ShaHash) (bool, error) {
db.Lock()
@ -367,11 +367,11 @@ func (db *MemDb) ExistsTxSha(sha *btcwire.ShaHash) (bool, error) {
// FetchTxBySha returns some data for the given transaction hash. The
// implementation may cache the underlying data if desired. This is part of the
// btcdb.Db interface implementation.
// database.Db interface implementation.
//
// This implementation does not use any additional cache since the entire
// database is already in memory.
func (db *MemDb) FetchTxBySha(txHash *btcwire.ShaHash) ([]*btcdb.TxListReply, error) {
func (db *MemDb) FetchTxBySha(txHash *btcwire.ShaHash) ([]*database.TxListReply, error) {
db.Lock()
defer db.Unlock()
@ -383,11 +383,11 @@ func (db *MemDb) FetchTxBySha(txHash *btcwire.ShaHash) ([]*btcdb.TxListReply, er
if !exists {
log.Warnf("FetchTxBySha: requested hash of %s does not exist",
txHash)
return nil, btcdb.ErrTxShaMissing
return nil, database.ErrTxShaMissing
}
txHashCopy := *txHash
replyList := make([]*btcdb.TxListReply, len(txns))
replyList := make([]*database.TxListReply, len(txns))
for i, txD := range txns {
msgBlock := db.blocks[txD.blockHeight]
blockSha, err := msgBlock.BlockSha()
@ -397,7 +397,7 @@ func (db *MemDb) FetchTxBySha(txHash *btcwire.ShaHash) ([]*btcdb.TxListReply, er
spentBuf := make([]bool, len(txD.spentBuf))
copy(spentBuf, txD.spentBuf)
reply := btcdb.TxListReply{
reply := database.TxListReply{
Sha: &txHashCopy,
Tx: msgBlock.Transactions[txD.offset],
BlkSha: &blockSha,
@ -422,16 +422,16 @@ func (db *MemDb) FetchTxBySha(txHash *btcwire.ShaHash) ([]*btcdb.TxListReply, er
// will indicate the transaction does not exist.
//
// This function must be called with the db lock held.
func (db *MemDb) fetchTxByShaList(txShaList []*btcwire.ShaHash, includeSpent bool) []*btcdb.TxListReply {
replyList := make([]*btcdb.TxListReply, 0, len(txShaList))
func (db *MemDb) fetchTxByShaList(txShaList []*btcwire.ShaHash, includeSpent bool) []*database.TxListReply {
replyList := make([]*database.TxListReply, 0, len(txShaList))
for i, hash := range txShaList {
// Every requested entry needs a response, so start with nothing
// more than a response with the requested hash marked missing.
// The reply will be updated below with the appropriate
// information if the transaction exists.
reply := btcdb.TxListReply{
reply := database.TxListReply{
Sha: txShaList[i],
Err: btcdb.ErrTxShaMissing,
Err: database.ErrTxShaMissing,
}
replyList = append(replyList, &reply)
@ -480,7 +480,7 @@ func (db *MemDb) fetchTxByShaList(txShaList []*btcwire.ShaHash, includeSpent boo
// FetchTxByShaList returns a TxListReply given an array of transaction hashes.
// The implementation may cache the underlying data if desired. This is part of
// the btcdb.Db interface implementation.
// the database.Db interface implementation.
//
// This implementation does not use any additional cache since the entire
// database is already in memory.
@ -491,14 +491,14 @@ func (db *MemDb) fetchTxByShaList(txShaList []*btcwire.ShaHash, includeSpent boo
// increased number of transaction fetches, this function is typically more
// expensive than the unspent counterpart, however the specific performance
// details depend on the concrete implementation. The implementation may cache
// the underlying data if desired. This is part of the btcdb.Db interface
// the underlying data if desired. This is part of the database.Db interface
// implementation.
//
// To fetch all versions of a specific transaction, call FetchTxBySha.
//
// This implementation does not use any additional cache since the entire
// database is already in memory.
func (db *MemDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListReply {
func (db *MemDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*database.TxListReply {
db.Lock()
defer db.Unlock()
@ -508,7 +508,7 @@ func (db *MemDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListR
// FetchUnSpentTxByShaList returns a TxListReply given an array of transaction
// hashes. Any transactions which are fully spent will indicate they do not
// exist by setting the Err field to TxShaMissing. The implementation may cache
// the underlying data if desired. This is part of the btcdb.Db interface
// the underlying data if desired. This is part of the database.Db interface
// implementation.
//
// To obtain results which do contain the most recent version of a fully spent
@ -517,7 +517,7 @@ func (db *MemDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListR
//
// This implementation does not use any additional cache since the entire
// database is already in memory.
func (db *MemDb) FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListReply {
func (db *MemDb) FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*database.TxListReply {
db.Lock()
defer db.Unlock()
@ -527,7 +527,7 @@ func (db *MemDb) FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.
// InsertBlock inserts raw block and transaction data from a block into the
// database. The first block inserted into the database will be treated as the
// genesis block. Every subsequent block insert requires the referenced parent
// block to already exist. This is part of the btcdb.Db interface
// block to already exist. This is part of the database.Db interface
// implementation.
func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
db.Lock()
@ -548,7 +548,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
msgBlock := block.MsgBlock()
if _, exists := db.blocksBySha[msgBlock.Header.PrevBlock]; !exists {
if len(db.blocks) > 0 {
return 0, btcdb.ErrPrevShaMissing
return 0, database.ErrPrevShaMissing
}
}
@ -599,7 +599,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
log.Warnf("InsertBlock: requested hash "+
" of %s does not exist in-flight",
tx.Sha())
return 0, btcdb.ErrTxShaMissing
return 0, database.ErrTxShaMissing
}
} else {
originTxns, exists := db.txns[prevOut.Hash]
@ -607,14 +607,14 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
log.Warnf("InsertBlock: requested hash "+
"of %s by %s does not exist",
prevOut.Hash, tx.Sha())
return 0, btcdb.ErrTxShaMissing
return 0, database.ErrTxShaMissing
}
originTxD := originTxns[len(originTxns)-1]
if prevOut.Index > uint32(len(originTxD.spentBuf)) {
log.Warnf("InsertBlock: requested hash "+
"of %s with index %d does not "+
"exist", tx.Sha(), prevOut.Index)
return 0, btcdb.ErrTxShaMissing
return 0, database.ErrTxShaMissing
}
}
}
@ -624,7 +624,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
inFlightIndex < i {
log.Warnf("Block contains duplicate transaction %s",
tx.Sha())
return 0, btcdb.ErrDuplicateSha
return 0, database.ErrDuplicateSha
}
// Prevent duplicate transactions unless the old one is fully
@ -634,7 +634,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
if !isFullySpent(txD) {
log.Warnf("Attempt to insert duplicate "+
"transaction %s", tx.Sha())
return 0, btcdb.ErrDuplicateSha
return 0, database.ErrDuplicateSha
}
}
}
@ -674,7 +674,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
// NewestSha returns the hash and block height of the most recent (end) block of
// the block chain. It will return the zero hash, -1 for the block height, and
// no error (nil) if there are not any blocks in the database yet. This is part
// of the btcdb.Db interface implementation.
// of the database.Db interface implementation.
func (db *MemDb) NewestSha() (*btcwire.ShaHash, int64, error) {
db.Lock()
defer db.Unlock()
@ -699,32 +699,32 @@ func (db *MemDb) NewestSha() (*btcwire.ShaHash, int64, error) {
}
// FetchAddrIndexTip isn't currently implemented. This is a part of the
// btcdb.Db interface implementation.
// database.Db interface implementation.
func (db *MemDb) FetchAddrIndexTip() (*btcwire.ShaHash, int64, error) {
return nil, 0, btcdb.ErrNotImplemented
return nil, 0, database.ErrNotImplemented
}
// UpdateAddrIndexForBlock isn't currently implemented. This is a part of the
// btcdb.Db interface implementation.
// database.Db interface implementation.
func (db *MemDb) UpdateAddrIndexForBlock(*btcwire.ShaHash, int64,
btcdb.BlockAddrIndex) error {
return btcdb.ErrNotImplemented
database.BlockAddrIndex) error {
return database.ErrNotImplemented
}
// FetchTxsForAddr isn't currently implemented. This is a part of the btcdb.Db
// FetchTxsForAddr isn't currently implemented. This is a part of the database.Db
// interface implementation.
func (db *MemDb) FetchTxsForAddr(btcutil.Address, int, int) ([]*btcdb.TxListReply, error) {
return nil, btcdb.ErrNotImplemented
func (db *MemDb) FetchTxsForAddr(btcutil.Address, int, int) ([]*database.TxListReply, error) {
return nil, database.ErrNotImplemented
}
// DeleteAddrIndex isn't currently implemented. This is a part of the btcdb.Db
// DeleteAddrIndex isn't currently implemented. This is a part of the database.Db
// interface implementation.
func (db *MemDb) DeleteAddrIndex() error {
return btcdb.ErrNotImplemented
return database.ErrNotImplemented
}
// RollbackClose discards the recent database changes to the previously saved
// data at last Sync and closes the database. This is part of the btcdb.Db
// data at last Sync and closes the database. This is part of the database.Db
// interface implementation.
//
// The database is completely purged on close with this implementation since the
@ -737,7 +737,7 @@ func (db *MemDb) RollbackClose() error {
}
// Sync verifies that the database is coherent on disk and no outstanding
// transactions are in flight. This is part of the btcdb.Db interface
// transactions are in flight. This is part of the database.Db interface
// implementation.
//
// This implementation does not write any data to disk, so this function only

View file

@ -8,8 +8,8 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcdb"
"github.com/btcsuite/btcdb/memdb"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcd/database/memdb"
"github.com/btcsuite/btcnet"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
@ -20,7 +20,7 @@ import (
// and does not panic or otherwise misbehave for functions which do not return
// errors.
func TestClosed(t *testing.T) {
db, err := btcdb.CreateDB("memdb")
db, err := database.CreateDB("memdb")
if err != nil {
t.Errorf("Failed to open test database %v", err)
return
@ -74,7 +74,7 @@ func TestClosed(t *testing.T) {
"got: %d, want: %d", len(reply), len(requestHashes))
}
for i, txLR := range reply {
wantReply := &btcdb.TxListReply{
wantReply := &database.TxListReply{
Sha: requestHashes[i],
Err: memdb.ErrDbClosed,
}
@ -90,7 +90,7 @@ func TestClosed(t *testing.T) {
"got: %d, want: %d", len(reply), len(requestHashes))
}
for i, txLR := range reply {
wantReply := &btcdb.TxListReply{
wantReply := &database.TxListReply{
Sha: requestHashes[i],
Err: memdb.ErrDbClosed,
}

31
doc.go
View file

@ -1,31 +0,0 @@
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
/*
Package btcdb provides a database interface for the Bitcoin block chain.
As of July 2014, there are over 309,000 blocks in the Bitcoin block chain and
and over 42 million transactions (which turns out to be over 21GB of data).
btcdb provides a database layer to store and retrieve this data in a fairly
simple and efficient manner. The use of this should not require specific
knowledge of the database backend.
Basic Design
The basic design of btcdb is to provide two classes of items in a database;
blocks and transactions (tx) where the block number increases monotonically.
Each transaction belongs to a single block although a block can have a variable
number of transactions. Along with these two items, several convenience
functions for dealing with the database are provided as well as functions to
query specific items that may be present in a block or tx.
Usage
At the highest level, the use of this packages just requires that you import it,
setup a database, insert some data into it, and optionally, query the data back.
The first block inserted into the database will be treated as the genesis block.
Every subsequent block insert requires the referenced parent block to already
exist.
*/
package btcdb