diff --git a/README.md b/README.md index 813eee02..3ec1b0f0 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,12 @@ $ go get github.com/conformal/btcdb * [CreateDB Example] (http://godoc.org/github.com/conformal/btcdb#example-CreateDB) - Demonstrates creating a new database and inserting the genesis - block into it. + Demonstrates creating a new database and inserting the genesis block into it. + +* [NewestSha Example] + (http://godoc.org/github.com/conformal/btcdb#example-CreateDB) + Demonstrates querying the database for the most recent best block height and + hash. ## TODO - Increase test coverage to 100% diff --git a/example_test.go b/example_test.go index 6c8e677f..79219a28 100644 --- a/example_test.go +++ b/example_test.go @@ -48,3 +48,46 @@ func ExampleCreateDB() { // Output: // New height: 0 } + +// exampleLoadDb is used in the example to elide the setup code. +func exampleLoadDb() (btcdb.Db, error) { + db, err := btcdb.CreateDB("memdb") + if err != nil { + return nil, err + } + + // Insert the main network genesis block. + genesis := btcutil.NewBlock(btcnet.MainNetParams.GenesisBlock) + _, err = db.InsertBlock(genesis) + if err != nil { + return nil, err + } + + return db, err +} + +// This example demonstrates querying the database for the most recent best +// block height and hash. +func ExampleDb_NewestSha() { + // Load a database for the purposes of this example and schedule it to + // be closed on exit. See the CreateDB example for more details on what + // this step is doing. + db, err := exampleLoadDb() + if err != nil { + fmt.Println(err) + return + } + defer db.Close() + + latestHash, latestHeight, err := db.NewestSha() + if err != nil { + fmt.Println(err) + return + } + fmt.Println("Latest hash:", latestHash) + fmt.Println("Latest height:", latestHeight) + + // Output: + // Latest hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f + // Latest height: 0 +}