Update doc.go and README.md examples to use memdb.

Since the code is only intended to be example code, there is no reason to
use a database which actually writes to disk.
This commit is contained in:
Dave Collins 2014-07-07 22:52:04 -05:00
parent 73228aaebe
commit 5a4242cb03
2 changed files with 12 additions and 24 deletions

View file

@ -82,27 +82,21 @@ intentionally causes an error by attempting to process a duplicate block.
"fmt" "fmt"
"github.com/conformal/btcchain" "github.com/conformal/btcchain"
"github.com/conformal/btcdb" "github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb" _ "github.com/conformal/btcdb/memdb"
"github.com/conformal/btcnet" "github.com/conformal/btcnet"
"github.com/conformal/btcutil" "github.com/conformal/btcutil"
"os"
) )
func main() { func main() {
// Create a new database to store the accepted blocks into. Typically // Create a new database to store the accepted blocks into. Typically
// this would be opening an existing database, but we create a new db // this would be opening an existing database and would not use memdb
// here so this is a complete working example. Also, typically the // which is a memory-only database backend, but we create a new db
// calls to os.Remove would not be used either, but again, we want // here so this is a complete working example.
// a complete working example here, so we make sure to remove the db, err := btcdb.CreateDB("memdb")
// database.
dbName := "exampledb"
_ = os.RemoveAll(dbName)
db, err := btcdb.CreateDB("leveldb", 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.RemoveAll(dbName) // Ignore error.
defer db.Close() defer db.Close()
// Insert the main network genesis block. This is part of the initial // Insert the main network genesis block. This is part of the initial
@ -122,7 +116,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.
err = chain.ProcessBlock(genesisBlock, false) _, err = chain.ProcessBlock(genesisBlock, btcchain.BFNone)
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

18
doc.go
View file

@ -72,27 +72,21 @@ intentionally causes an error by attempting to process a duplicate block.
"fmt" "fmt"
"github.com/conformal/btcchain" "github.com/conformal/btcchain"
"github.com/conformal/btcdb" "github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb" _ "github.com/conformal/btcdb/memdb"
"github.com/conformal/btcnet" "github.com/conformal/btcnet"
"github.com/conformal/btcutil" "github.com/conformal/btcutil"
"os"
) )
func main() { func main() {
// Create a new database to store the accepted blocks into. Typically // Create a new database to store the accepted blocks into. Typically
// this would be opening an existing database, but we create a new db // this would be opening an existing database and would not use memdb
// here so this is a complete working example. Also, typically the // which is a memory-only database backend, but we create a new db
// calls to os.Remove would not be used either, but again, we want // here so this is a complete working example.
// a complete working example here, so we make sure to remove the db, err := btcdb.CreateDB("memdb")
// database.
dbName := "exampledb"
_ = os.RemoveAll(dbName)
db, err := btcdb.CreateDB("leveldb", 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.RemoveAll(dbName) // Ignore error.
defer db.Close() defer db.Close()
// Insert the main network genesis block. This is part of the initial // Insert the main network genesis block. This is part of the initial
@ -107,7 +101,7 @@ intentionally causes an error by attempting to process a duplicate block.
// 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, btcnet.MainNetParams, nil) chain := btcchain.New(db, &btcnet.MainNetParams, nil)
// 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