mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

This commit modifies the way initial database creation is handled. Previously, the genesis for the main chain was inserted automatically upon creation of the database. However, that approach caused an issue since other networks such as the test network don't use the same genesis block as the main network. The new approach introduced by this commit is to leave it up to the caller to insert the desired genesis block. In order to support this, the InsertBlock function has been modified to allow the first (and only the first) block to be inserted without having an existing parent. Also, the NewestSha function has been modified to return a zero hash, -1 for the height, and no error when the database does not yet have any blocks. This allows the caller to determine the difference between no blocks and only the genesis block (in which case the return values would be the genesis hash and 0 for the height).
60 lines
2.1 KiB
Go
60 lines
2.1 KiB
Go
// Copyright (c) 2013 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 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).
|
|
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 used although currently only db_sqlite is
|
|
provided.
|
|
|
|
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 (although many of these are in
|
|
the sqlite3 subpackage).
|
|
|
|
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. In a more concrete example:
|
|
|
|
// Import packages.
|
|
import (
|
|
"github.com/conformal/btcdb"
|
|
_ "github.com/conformal/btcdb/sqlite3"
|
|
"github.com/conformal/btcutil"
|
|
"github.com/conformal/btcwire"
|
|
)
|
|
|
|
// Create a database and schedule it to be closed on exit.
|
|
dbName := "example.db"
|
|
db, err := btcdb.CreateDB("sqlite", dbName)
|
|
if err != nil {
|
|
// Log and handle the error
|
|
}
|
|
defer db.Close()
|
|
|
|
|
|
// Insert the main network genesis block.
|
|
pver := btcwire.ProtocolVersion
|
|
genesis := btcutil.NewBlock(&btcwire.GenesisBlock, pver)
|
|
newHeight, err := db.InsertBlock(block)
|
|
if err != nil {
|
|
// Log and handle the error
|
|
}
|
|
*/
|
|
package btcdb
|