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
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/blockchain"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
|
|
|
"github.com/lbryio/lbcd/database"
|
|
|
|
_ "github.com/lbryio/lbcd/database/ffldb"
|
|
|
|
btcutil "github.com/lbryio/lbcutil"
|
2014-07-08 08:41:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// This example demonstrates how to create a new chain instance and use
|
2018-01-31 09:40:46 +01:00
|
|
|
// ProcessBlock to attempt to add a block to the chain. As the package
|
2014-07-08 08:41:39 +02:00
|
|
|
// 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
|
2016-07-14 02:36:36 +02:00
|
|
|
// notification callback and signature cache. Also, the caller would
|
|
|
|
// ordinarily keep a reference to the median time source and add time
|
|
|
|
// values obtained from other peers on the network so the local time is
|
|
|
|
// adjusted to be in agreement with other peers.
|
2015-08-26 06:03:18 +02:00
|
|
|
chain, err := blockchain.New(&blockchain.Config{
|
|
|
|
DB: db,
|
|
|
|
ChainParams: &chaincfg.MainNetParams,
|
2016-07-14 02:36:36 +02:00
|
|
|
TimeSource: blockchain.NewMedianTime(),
|
2015-08-26 06:03:18 +02:00
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2016-10-13 02:43:01 +02:00
|
|
|
isMainChain, isOrphan, err := chain.ProcessBlock(genesisBlock,
|
|
|
|
blockchain.BFNone)
|
2014-07-08 08:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to process block: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2016-10-13 02:43:01 +02:00
|
|
|
fmt.Printf("Block accepted. Is it on the main chain?: %v", isMainChain)
|
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
|
|
|
|
}
|