diff --git a/README.md b/README.md index a67182df..8bc2af0d 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,14 @@ is by no means exhaustive: * [CompactToBig Example] (http://godoc.org/github.com/conformal/btcchain#example-CompactToBig) - Demonstrates how to convert the "bits" in a block header which represent the - target difficulty to a big integer and display it using the typical hex - notation. + Demonstrates how to convert the compact "bits" in a block header which + represent the target difficulty to a big integer and display it using the + typical hex notation. + +* [BigToCompact Example] + (http://godoc.org/github.com/conformal/btcchain#example-BigToCompact) + Demonstrates how to convert how to convert a target difficulty into the + compact "bits" in a block header which represent that target difficulty. ## TODO diff --git a/example_test.go b/example_test.go index 0ee7a583..ac1b3f8e 100644 --- a/example_test.go +++ b/example_test.go @@ -11,6 +11,7 @@ import ( _ "github.com/conformal/btcdb/memdb" "github.com/conformal/btcnet" "github.com/conformal/btcutil" + "math/big" ) // This example demonstrates how to create a new chain instance and use @@ -58,9 +59,9 @@ func ExampleBlockChain_ProcessBlock() { // Failed to process block: already have block 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f } -// This example demonstrates how to convert the "bits" in a block header which -// represent the target difficulty to a big integer and display it using the -// typical hex notation.. +// This example demonstrates how to convert the compact "bits" in a block header +// which represent the target difficulty to a big integer and display it using +// the typical hex notation. func ExampleCompactToBig() { // Convert the bits from block 300000 in the main block chain. bits := uint32(419465580) @@ -72,3 +73,22 @@ func ExampleCompactToBig() { // Output: // 0000000000000000896c00000000000000000000000000000000000000000000 } + +// This example demonstrates how to convert a target difficulty into the compact +// "bits" in a block header which represent that target difficulty . +func ExampleBigToCompact() { + // Convert the target difficulty from block 300000 in the main block + // chain to compact form. + t := "0000000000000000896c00000000000000000000000000000000000000000000" + targetDifficulty, success := new(big.Int).SetString(t, 16) + if !success { + fmt.Println("invalid target difficulty") + return + } + bits := btcchain.BigToCompact(targetDifficulty) + + fmt.Println(bits) + + // Output: + // 419465580 +}