mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
Update examples for recent btcdb changes.
Since creating a new database with btcdb no longer automatically inserts the main network genesis block, update the examples accordingly.
This commit is contained in:
parent
761f666c44
commit
e82c97be3b
2 changed files with 46 additions and 12 deletions
29
README.md
29
README.md
|
@ -82,19 +82,37 @@ intentionally causes an error by attempting to process a duplicate block.
|
||||||
_ "github.com/conformal/btcdb/sqlite3"
|
_ "github.com/conformal/btcdb/sqlite3"
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// First, we create a new database to store the accepted blocks into.
|
// Create a new database to store the accepted blocks into. Typically
|
||||||
// Typically this would be opening an existing database, but we create
|
// this would be opening an existing database, but we create a new db
|
||||||
// a new db here so this is a complete working example.
|
// here so this is a complete working example. Also, typically the
|
||||||
db, err := btcdb.CreateDB("sqlite", "example.db")
|
// calls to os.Remove would not be used either, but again, we want
|
||||||
|
// a complete working example here, so we make sure to remove the
|
||||||
|
// database.
|
||||||
|
dbName := "example.db"
|
||||||
|
_ = os.Remove(dbName)
|
||||||
|
db, err := btcdb.CreateDB("sqlite", dbName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to create database: %v\n", err)
|
fmt.Printf("Failed to create database: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer os.Remove(dbName) // Ignore error.
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
// Insert the main network genesis block. This is part of the initial
|
||||||
|
// database setup. Like above, this typically would not be needed when
|
||||||
|
// opening an existing database.
|
||||||
|
pver := btcwire.ProtocolVersion
|
||||||
|
genesisBlock := btcutil.NewBlock(&btcwire.GenesisBlock, pver)
|
||||||
|
_, err = db.InsertBlock(genesisBlock)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to insert genesis block: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new BlockChain instance using the underlying database for
|
// Create a new BlockChain instance using the underlying database for
|
||||||
// the main bitcoin network and ignore notifications.
|
// the main bitcoin network and ignore notifications.
|
||||||
chain := btcchain.New(db, btcwire.MainNet, nil)
|
chain := btcchain.New(db, btcwire.MainNet, nil)
|
||||||
|
@ -102,8 +120,7 @@ intentionally causes an error by attempting to process a duplicate block.
|
||||||
// Process a block. For this example, we are going to intentionally
|
// Process a block. For this example, we are going to intentionally
|
||||||
// cause an error by trying to process the genesis block which already
|
// cause an error by trying to process the genesis block which already
|
||||||
// exists.
|
// exists.
|
||||||
block := btcutil.NewBlock(&btcwire.GenesisBlock, btcwire.ProtocolVersion)
|
err = chain.ProcessBlock(genesisBlock)
|
||||||
err = chain.ProcessBlock(block)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to process block: %v\n", err)
|
fmt.Printf("Failed to process block: %v\n", err)
|
||||||
return
|
return
|
||||||
|
|
29
doc.go
29
doc.go
|
@ -75,19 +75,37 @@ intentionally causes an error by attempting to process a duplicate block.
|
||||||
_ "github.com/conformal/btcdb/sqlite3"
|
_ "github.com/conformal/btcdb/sqlite3"
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// First, we create a new database to store the accepted blocks into.
|
// Create a new database to store the accepted blocks into. Typically
|
||||||
// Typically this would be opening an existing database, but we create
|
// this would be opening an existing database, but we create a new db
|
||||||
// a new db here so this is a complete working example.
|
// here so this is a complete working example. Also, typically the
|
||||||
db, err := btcdb.CreateDB("sqlite", "example.db")
|
// calls to os.Remove would not be used either, but again, we want
|
||||||
|
// a complete working example here, so we make sure to remove the
|
||||||
|
// database.
|
||||||
|
dbName := "example.db"
|
||||||
|
_ = os.Remove(dbName)
|
||||||
|
db, err := btcdb.CreateDB("sqlite", dbName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to create database: %v\n", err)
|
fmt.Printf("Failed to create database: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer os.Remove(dbName) // Ignore error.
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
// Insert the main network genesis block. This is part of the initial
|
||||||
|
// database setup. Like above, this typically would not be needed when
|
||||||
|
// opening an existing database.
|
||||||
|
pver := btcwire.ProtocolVersion
|
||||||
|
genesisBlock := btcutil.NewBlock(&btcwire.GenesisBlock, pver)
|
||||||
|
_, err = db.InsertBlock(genesisBlock)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to insert genesis block: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new BlockChain instance using the underlying database for
|
// Create a new BlockChain instance using the underlying database for
|
||||||
// the main bitcoin network and ignore notifications.
|
// the main bitcoin network and ignore notifications.
|
||||||
chain := btcchain.New(db, btcwire.MainNet, nil)
|
chain := btcchain.New(db, btcwire.MainNet, nil)
|
||||||
|
@ -95,8 +113,7 @@ intentionally causes an error by attempting to process a duplicate block.
|
||||||
// Process a block. For this example, we are going to intentionally
|
// Process a block. For this example, we are going to intentionally
|
||||||
// cause an error by trying to process the genesis block which already
|
// cause an error by trying to process the genesis block which already
|
||||||
// exists.
|
// exists.
|
||||||
block := btcutil.NewBlock(&btcwire.GenesisBlock, btcwire.ProtocolVersion)
|
err = chain.ProcessBlock(genesisBlock)
|
||||||
err = chain.ProcessBlock(block)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to process block: %v\n", err)
|
fmt.Printf("Failed to process block: %v\n", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Add table
Reference in a new issue