2014-01-09 06:54:52 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-05-29 02:07:21 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
2013-06-01 00:09:26 +02:00
|
|
|
Package btcdb provides a database interface for the Bitcoin block chain.
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-06-01 00:09:26 +02:00
|
|
|
As of May 2013, there are over 235,000 blocks in the Bitcoin block chain and
|
|
|
|
and over 17 million transactions (which turns out to be over 11GB of data).
|
2013-05-29 02:07:21 +02:00
|
|
|
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
|
2013-10-22 22:25:33 +02:00
|
|
|
knowledge of the database backend.
|
2013-05-29 02:07:21 +02:00
|
|
|
|
|
|
|
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
|
2013-10-22 22:25:33 +02:00
|
|
|
that may be present in a block or tx.
|
2013-05-29 02:07:21 +02:00
|
|
|
|
|
|
|
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,
|
2013-07-25 23:44:18 +02:00
|
|
|
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. In a more concrete example:
|
|
|
|
|
|
|
|
// Import packages.
|
|
|
|
import (
|
|
|
|
"github.com/conformal/btcdb"
|
2013-10-22 22:25:33 +02:00
|
|
|
_ "github.com/conformal/btcdb/ldb"
|
2013-07-25 23:44:18 +02:00
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-05-31 20:50:23 +02:00
|
|
|
// Create a database and schedule it to be closed on exit.
|
2013-07-25 23:44:18 +02:00
|
|
|
dbName := "example.db"
|
2013-10-22 22:25:33 +02:00
|
|
|
db, err := btcdb.CreateDB("leveldb", dbName)
|
2013-05-29 02:07:21 +02:00
|
|
|
if err != nil {
|
2013-05-31 20:50:23 +02:00
|
|
|
// Log and handle the error
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
2013-05-31 20:50:23 +02:00
|
|
|
defer db.Close()
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-07-25 23:44:18 +02:00
|
|
|
|
|
|
|
// Insert the main network genesis block.
|
2013-08-05 20:03:41 +02:00
|
|
|
genesis := btcutil.NewBlock(&btcwire.GenesisBlock)
|
2013-07-29 19:41:30 +02:00
|
|
|
newHeight, err := db.InsertBlock(genesis)
|
2013-05-29 02:07:21 +02:00
|
|
|
if err != nil {
|
2013-05-31 20:50:23 +02:00
|
|
|
// Log and handle the error
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
package btcdb
|