Add BigToCompact example.
This commit is contained in:
parent
1a2baa3099
commit
01fa7fa069
2 changed files with 31 additions and 6 deletions
11
README.md
11
README.md
|
@ -84,9 +84,14 @@ is by no means exhaustive:
|
||||||
|
|
||||||
* [CompactToBig Example]
|
* [CompactToBig Example]
|
||||||
(http://godoc.org/github.com/conformal/btcchain#example-CompactToBig)
|
(http://godoc.org/github.com/conformal/btcchain#example-CompactToBig)
|
||||||
Demonstrates how to convert the "bits" in a block header which represent the
|
Demonstrates how to convert the compact "bits" in a block header which
|
||||||
target difficulty to a big integer and display it using the typical hex
|
represent the target difficulty to a big integer and display it using the
|
||||||
notation.
|
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
|
## TODO
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
_ "github.com/conformal/btcdb/memdb"
|
_ "github.com/conformal/btcdb/memdb"
|
||||||
"github.com/conformal/btcnet"
|
"github.com/conformal/btcnet"
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This example demonstrates how to create a new chain instance and use
|
// 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
|
// Failed to process block: already have block 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
|
||||||
}
|
}
|
||||||
|
|
||||||
// This example demonstrates how to convert the "bits" in a block header which
|
// This example demonstrates how to convert the compact "bits" in a block header
|
||||||
// represent the target difficulty to a big integer and display it using the
|
// which represent the target difficulty to a big integer and display it using
|
||||||
// typical hex notation..
|
// the typical hex notation.
|
||||||
func ExampleCompactToBig() {
|
func ExampleCompactToBig() {
|
||||||
// Convert the bits from block 300000 in the main block chain.
|
// Convert the bits from block 300000 in the main block chain.
|
||||||
bits := uint32(419465580)
|
bits := uint32(419465580)
|
||||||
|
@ -72,3 +73,22 @@ func ExampleCompactToBig() {
|
||||||
// Output:
|
// Output:
|
||||||
// 0000000000000000896c00000000000000000000000000000000000000000000
|
// 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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue