2015-08-26 06:03:18 +02:00
|
|
|
// Copyright (c) 2014-2016 The btcsuite developers
|
2014-07-08 08:41:39 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
package blockchain_test
|
2014-07-08 08:41:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-01-08 12:08:11 +01:00
|
|
|
"math/big"
|
2015-08-26 06:03:18 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-01-08 12:08:11 +01:00
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2015-02-06 06:18:27 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2015-08-26 11:54:55 +02:00
|
|
|
"github.com/btcsuite/btcd/database"
|
|
|
|
_ "github.com/btcsuite/btcd/database/ffldb"
|
2015-01-15 17:23:47 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2014-07-08 08:41:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// This example demonstrates how to create a new chain instance and use
|
|
|
|
// ProcessBlock to attempt to attempt add a block to the chain. As the package
|
|
|
|
// overview documentation describes, this includes all of the Bitcoin consensus
|
|
|
|
// rules. This example intentionally attempts to insert a duplicate genesis
|
|
|
|
// block to illustrate how an invalid block is handled.
|
2014-07-08 08:49:48 +02:00
|
|
|
func ExampleBlockChain_ProcessBlock() {
|
2014-07-08 08:41:39 +02:00
|
|
|
// Create a new database to store the accepted blocks into. Typically
|
2015-08-26 06:03:18 +02:00
|
|
|
// this would be opening an existing database and would not be deleting
|
|
|
|
// and creating a new database like this, but it is done here so this is
|
|
|
|
// a complete working example and does not leave temporary files laying
|
|
|
|
// around.
|
|
|
|
dbPath := filepath.Join(os.TempDir(), "exampleprocessblock")
|
|
|
|
_ = os.RemoveAll(dbPath)
|
|
|
|
db, err := database.Create("ffldb", dbPath, chaincfg.MainNetParams.Net)
|
2014-07-08 08:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to create database: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
defer os.RemoveAll(dbPath)
|
2014-07-08 08:41:39 +02:00
|
|
|
defer db.Close()
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Create a new BlockChain instance using the underlying database for
|
|
|
|
// the main bitcoin network. This example does not demonstrate some
|
|
|
|
// of the other available configuration options such as specifying a
|
|
|
|
// notification callback and signature cache.
|
|
|
|
chain, err := blockchain.New(&blockchain.Config{
|
|
|
|
DB: db,
|
|
|
|
ChainParams: &chaincfg.MainNetParams,
|
|
|
|
})
|
2014-07-08 08:41:39 +02:00
|
|
|
if err != nil {
|
2015-08-26 06:03:18 +02:00
|
|
|
fmt.Printf("Failed to create chain instance: %v\n", err)
|
2014-07-08 08:41:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-03 22:29:25 +02:00
|
|
|
// Create a new median time source that is required by the upcoming
|
|
|
|
// call to ProcessBlock. Ordinarily this would also add time values
|
|
|
|
// obtained from other peers on the network so the local time is
|
|
|
|
// adjusted to be in agreement with other peers.
|
2015-01-30 21:54:30 +01:00
|
|
|
timeSource := blockchain.NewMedianTime()
|
2014-10-03 22:29:25 +02:00
|
|
|
|
2014-07-08 08:41:39 +02:00
|
|
|
// Process a block. For this example, we are going to intentionally
|
|
|
|
// cause an error by trying to process the genesis block which already
|
|
|
|
// exists.
|
2015-08-26 06:03:18 +02:00
|
|
|
genesisBlock := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
|
2015-01-30 21:54:30 +01:00
|
|
|
isOrphan, err := chain.ProcessBlock(genesisBlock, timeSource, blockchain.BFNone)
|
2014-07-08 08:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to process block: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2014-08-28 16:23:46 +02:00
|
|
|
fmt.Printf("Block accepted. Is it an orphan?: %v", isOrphan)
|
2014-07-08 08:41:39 +02:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Failed to process block: already have block 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
|
|
|
|
}
|
2014-07-08 09:15:41 +02:00
|
|
|
|
2014-07-08 14:57:22 +02:00
|
|
|
// 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.
|
2014-07-08 09:15:41 +02:00
|
|
|
func ExampleCompactToBig() {
|
|
|
|
// Convert the bits from block 300000 in the main block chain.
|
|
|
|
bits := uint32(419465580)
|
2015-01-30 21:54:30 +01:00
|
|
|
targetDifficulty := blockchain.CompactToBig(bits)
|
2014-07-08 09:15:41 +02:00
|
|
|
|
|
|
|
// Display it in hex.
|
|
|
|
fmt.Printf("%064x\n", targetDifficulty.Bytes())
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// 0000000000000000896c00000000000000000000000000000000000000000000
|
|
|
|
}
|
2014-07-08 14:57:22 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2015-01-30 21:54:30 +01:00
|
|
|
bits := blockchain.BigToCompact(targetDifficulty)
|
2014-07-08 14:57:22 +02:00
|
|
|
|
|
|
|
fmt.Println(bits)
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// 419465580
|
|
|
|
}
|