From e82c97be3bc4778ba382e6f4b5c32b646b27fc39 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 29 Jul 2013 12:35:01 -0500 Subject: [PATCH] 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. --- README.md | 29 +++++++++++++++++++++++------ doc.go | 29 +++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0860666f..a5395400 100644 --- a/README.md +++ b/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/btcutil" "github.com/conformal/btcwire" + "os" ) func main() { - // First, we create a new database to store the accepted blocks into. - // Typically this would be opening an existing database, but we create - // a new db here so this is a complete working example. - db, err := btcdb.CreateDB("sqlite", "example.db") + // Create a new database to store the accepted blocks into. Typically + // this would be opening an existing database, but we create a new db + // here so this is a complete working example. Also, typically the + // 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 { fmt.Printf("Failed to create database: %v\n", err) return } + defer os.Remove(dbName) // Ignore error. 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 // the main bitcoin network and ignore notifications. 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 // cause an error by trying to process the genesis block which already // exists. - block := btcutil.NewBlock(&btcwire.GenesisBlock, btcwire.ProtocolVersion) - err = chain.ProcessBlock(block) + err = chain.ProcessBlock(genesisBlock) if err != nil { fmt.Printf("Failed to process block: %v\n", err) return diff --git a/doc.go b/doc.go index 7effc092..4d068260 100644 --- a/doc.go +++ b/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/btcutil" "github.com/conformal/btcwire" + "os" ) func main() { - // First, we create a new database to store the accepted blocks into. - // Typically this would be opening an existing database, but we create - // a new db here so this is a complete working example. - db, err := btcdb.CreateDB("sqlite", "example.db") + // Create a new database to store the accepted blocks into. Typically + // this would be opening an existing database, but we create a new db + // here so this is a complete working example. Also, typically the + // 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 { fmt.Printf("Failed to create database: %v\n", err) return } + defer os.Remove(dbName) // Ignore error. 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 // the main bitcoin network and ignore notifications. 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 // cause an error by trying to process the genesis block which already // exists. - block := btcutil.NewBlock(&btcwire.GenesisBlock, btcwire.ProtocolVersion) - err = chain.ProcessBlock(block) + err = chain.ProcessBlock(genesisBlock) if err != nil { fmt.Printf("Failed to process block: %v\n", err) return