Add BigToCompact example.

This commit is contained in:
Dave Collins 2014-07-08 07:57:22 -05:00
parent 1a2baa3099
commit 01fa7fa069
2 changed files with 31 additions and 6 deletions

View file

@ -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

View file

@ -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
}