github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/blockchain/example_test.go (about)

     1  // Copyright (c) 2014-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package blockchain_test
     7  
     8  import (
     9  	"fmt"
    10  	"math/big"
    11  	"os"
    12  	"path/filepath"
    13  
    14  	"github.com/BlockABC/godash/blockchain"
    15  	"github.com/BlockABC/godash/chaincfg"
    16  	"github.com/BlockABC/godash/database"
    17  	_ "github.com/BlockABC/godash/database/ffldb"
    18  	"github.com/BlockABC/godashutil"
    19  )
    20  
    21  // This example demonstrates how to create a new chain instance and use
    22  // ProcessBlock to attempt to attempt add a block to the chain.  As the package
    23  // overview documentation describes, this includes all of the Bitcoin consensus
    24  // rules.  This example intentionally attempts to insert a duplicate genesis
    25  // block to illustrate how an invalid block is handled.
    26  func ExampleBlockChain_ProcessBlock() {
    27  	// Create a new database to store the accepted blocks into.  Typically
    28  	// this would be opening an existing database and would not be deleting
    29  	// and creating a new database like this, but it is done here so this is
    30  	// a complete working example and does not leave temporary files laying
    31  	// around.
    32  	dbPath := filepath.Join(os.TempDir(), "exampleprocessblock")
    33  	_ = os.RemoveAll(dbPath)
    34  	db, err := database.Create("ffldb", dbPath, chaincfg.MainNetParams.Net)
    35  	if err != nil {
    36  		fmt.Printf("Failed to create database: %v\n", err)
    37  		return
    38  	}
    39  	defer os.RemoveAll(dbPath)
    40  	defer db.Close()
    41  
    42  	// Create a new BlockChain instance using the underlying database for
    43  	// the main bitcoin network.  This example does not demonstrate some
    44  	// of the other available configuration options such as specifying a
    45  	// notification callback and signature cache.  Also, the caller would
    46  	// ordinarily keep a reference to the median time source and add time
    47  	// values obtained from other peers on the network so the local time is
    48  	// adjusted to be in agreement with other peers.
    49  	chain, err := blockchain.New(&blockchain.Config{
    50  		DB:          db,
    51  		ChainParams: &chaincfg.MainNetParams,
    52  		TimeSource:  blockchain.NewMedianTime(),
    53  	})
    54  	if err != nil {
    55  		fmt.Printf("Failed to create chain instance: %v\n", err)
    56  		return
    57  	}
    58  
    59  	// Process a block.  For this example, we are going to intentionally
    60  	// cause an error by trying to process the genesis block which already
    61  	// exists.
    62  	genesisBlock := godashutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
    63  	isOrphan, err := chain.ProcessBlock(genesisBlock, blockchain.BFNone)
    64  	if err != nil {
    65  		fmt.Printf("Failed to process block: %v\n", err)
    66  		return
    67  	}
    68  	fmt.Printf("Block accepted. Is it an orphan?: %v", isOrphan)
    69  
    70  	// Output:
    71  	// Failed to process block: already have block 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
    72  }
    73  
    74  // This example demonstrates how to convert the compact "bits" in a block header
    75  // which represent the target difficulty to a big integer and display it using
    76  // the typical hex notation.
    77  func ExampleCompactToBig() {
    78  	// Convert the bits from block 300000 in the main block chain.
    79  	bits := uint32(419465580)
    80  	targetDifficulty := blockchain.CompactToBig(bits)
    81  
    82  	// Display it in hex.
    83  	fmt.Printf("%064x\n", targetDifficulty.Bytes())
    84  
    85  	// Output:
    86  	// 0000000000000000896c00000000000000000000000000000000000000000000
    87  }
    88  
    89  // This example demonstrates how to convert a target difficulty into the compact
    90  // "bits" in a block header which represent that target difficulty .
    91  func ExampleBigToCompact() {
    92  	// Convert the target difficulty from block 300000 in the main block
    93  	// chain to compact form.
    94  	t := "0000000000000000896c00000000000000000000000000000000000000000000"
    95  	targetDifficulty, success := new(big.Int).SetString(t, 16)
    96  	if !success {
    97  		fmt.Println("invalid target difficulty")
    98  		return
    99  	}
   100  	bits := blockchain.BigToCompact(targetDifficulty)
   101  
   102  	fmt.Println(bits)
   103  
   104  	// Output:
   105  	// 419465580
   106  }