github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/blockchain/chain_test.go (about)

     1  // Copyright (c) 2013-2014 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  	"testing"
    10  
    11  	"github.com/dashpay/godash/blockchain"
    12  	"github.com/dashpay/godash/chaincfg"
    13  	"github.com/dashpay/godash/wire"
    14  	"github.com/dashpay/godashutil"
    15  )
    16  
    17  // TestHaveBlock tests the HaveBlock API to ensure proper functionality.
    18  func TestHaveBlock(t *testing.T) {
    19  	// Load up blocks such that there is a side chain.
    20  	// (genesis block) -> 1 -> 2 -> 3 -> 4
    21  	//                          \-> 3a
    22  	testFiles := []string{
    23  		"blk_0_to_4.dat.bz2",
    24  		"blk_3A.dat.bz2",
    25  	}
    26  
    27  	var blocks []*godashutil.Block
    28  	for _, file := range testFiles {
    29  		blockTmp, err := loadBlocks(file)
    30  		if err != nil {
    31  			t.Errorf("Error loading file: %v\n", err)
    32  			return
    33  		}
    34  		for _, block := range blockTmp {
    35  			blocks = append(blocks, block)
    36  		}
    37  	}
    38  
    39  	// Create a new database and chain instance to run tests against.
    40  	chain, teardownFunc, err := chainSetup("haveblock")
    41  	if err != nil {
    42  		t.Errorf("Failed to setup chain instance: %v", err)
    43  		return
    44  	}
    45  	defer teardownFunc()
    46  
    47  	// Since we're not dealing with the real block chain, disable
    48  	// checkpoints and set the coinbase maturity to 1.
    49  	chain.DisableCheckpoints(true)
    50  	blockchain.TstSetCoinbaseMaturity(1)
    51  
    52  	for i := 1; i < len(blocks); i++ {
    53  		isOrphan, err := chain.ProcessBlock(blocks[i], blockchain.BFNone)
    54  		if err != nil {
    55  			t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
    56  			return
    57  		}
    58  		if isOrphan {
    59  			t.Errorf("ProcessBlock incorrectly returned block %v "+
    60  				"is an orphan\n", i)
    61  			return
    62  		}
    63  	}
    64  
    65  	// Insert an orphan block.
    66  	isOrphan, err := chain.ProcessBlock(godashutil.NewBlock(&Block100000),
    67  		blockchain.BFNone)
    68  	if err != nil {
    69  		t.Errorf("Unable to process block: %v", err)
    70  		return
    71  	}
    72  	if !isOrphan {
    73  		t.Errorf("ProcessBlock indicated block is an not orphan when " +
    74  			"it should be\n")
    75  		return
    76  	}
    77  
    78  	tests := []struct {
    79  		hash string
    80  		want bool
    81  	}{
    82  		// Genesis block should be present (in the main chain).
    83  		{hash: chaincfg.MainNetParams.GenesisHash.String(), want: true},
    84  
    85  		// Block 3a should be present (on a side chain).
    86  		{hash: "00000000474284d20067a4d33f6a02284e6ef70764a3a26d6a5b9df52ef663dd", want: true},
    87  
    88  		// Block 100000 should be present (as an orphan).
    89  		{hash: "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506", want: true},
    90  
    91  		// Random hashes should not be available.
    92  		{hash: "123", want: false},
    93  	}
    94  
    95  	for i, test := range tests {
    96  		hash, err := wire.NewShaHashFromStr(test.hash)
    97  		if err != nil {
    98  			t.Errorf("NewShaHashFromStr: %v", err)
    99  			continue
   100  		}
   101  
   102  		result, err := chain.HaveBlock(hash)
   103  		if err != nil {
   104  			t.Errorf("HaveBlock #%d unexpected error: %v", i, err)
   105  			return
   106  		}
   107  		if result != test.want {
   108  			t.Errorf("HaveBlock #%d got %v want %v", i, result,
   109  				test.want)
   110  			continue
   111  		}
   112  	}
   113  }