Import btcchain repo into blockchain directory.

This commit contains the entire btcchain repository along with several
changes needed to move all of the files into the blockchain directory in
order to prepare it for merging.  This does NOT update btcd or any of the
other packages to use the new location as that will be done separately.

- All import paths in the old btcchain test files have been changed to
  the new location
- All references to btcchain as the package name have been changed to
  blockchain
This commit is contained in:
Dave Collins 2015-01-30 14:54:30 -06:00
parent 3f177c9895
commit b69a849114
43 changed files with 195 additions and 410 deletions

28
.gitignore vendored
View file

@ -1,28 +0,0 @@
# Temp files
*~
# Log files
*.log
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe

View file

@ -1,17 +0,0 @@
language: go
go:
- release
- tip
sudo: false
before_install:
- gotools=golang.org/x/tools
- if [ "$TRAVIS_GO_VERSION" = "release" ]; then gotools=code.google.com/p/go.tools; fi
install:
- go get -d -t -v ./...
- go get -v $gotools/cmd/cover
- go get -v $gotools/cmd/vet
- go get -v github.com/bradfitz/goimports
- go get -v github.com/golang/lint/golint
script:
- export PATH=$PATH:$HOME/gopath/bin
- ./goclean.sh

13
LICENSE
View file

@ -1,13 +0,0 @@
Copyright (c) 2013-2014 Conformal Systems LLC.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View file

@ -1,42 +1,40 @@
btcchain
========
blockchain
==========
[![Build Status](http://img.shields.io/travis/btcsuite/btcchain.svg)]
(https://travis-ci.org/btcsuite/btcchain) [![ISC License]
[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)]
(https://travis-ci.org/btcsuite/btcd) [![ISC License]
(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
Package btcchain implements bitcoin block handling and chain selection rules.
Package blockchain implements bitcoin block handling and chain selection rules.
The test coverage is currently only around 60%, but will be increasing over
time. See `test_coverage.txt` for the gocov coverage report. Alternatively, if
you are running a POSIX OS, you can run the `cov_report.sh` script for a
real-time report. Package btcchain is licensed under the liberal ISC license.
real-time report. Package blockchain is licensed under the liberal ISC license.
There is an associated blog post about the release of this package
[here](https://blog.conformal.com/btcchain-the-bitcoin-chain-package-from-bctd/).
This package is one of the core packages from btcd, an alternative full-node
implementation of bitcoin which is under active development by Conformal.
Although it was primarily written for btcd, this package has intentionally been
designed so it can be used as a standalone package for any projects needing to
handle processing of blocks into the bitcoin block chain.
This package has intentionally been designed so it can be used as a standalone
package for any projects needing to handle processing of blocks into the bitcoin
block chain.
## Documentation
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)]
(http://godoc.org/github.com/btcsuite/btcchain)
(http://godoc.org/github.com/btcsuite/btcd/blockchain)
Full `go doc` style documentation for the project can be viewed online without
installing this package by using the GoDoc site here:
http://godoc.org/github.com/btcsuite/btcchain
http://godoc.org/github.com/btcsuite/btcd/blockchain
You can also view the documentation locally once the package is installed with
the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to
http://localhost:6060/pkg/github.com/btcsuite/btcchain
http://localhost:6060/pkg/github.com/btcsuite/btcd/blockchain
## Installation
```bash
$ go get github.com/btcsuite/btcchain
$ go get github.com/btcsuite/btcd/blockchain
```
## Bitcoin Chain Processing Overview
@ -77,27 +75,23 @@ is by no means exhaustive:
## Examples
* [ProcessBlock Example]
(http://godoc.org/github.com/btcsuite/btcchain#example-BlockChain-ProcessBlock)
(http://godoc.org/github.com/btcsuite/btcd/blockchain#example-BlockChain-ProcessBlock)
Demonstrates how to create a new chain instance and use ProcessBlock to
attempt to attempt add a block to the chain. This example intentionally
attempts to insert a duplicate genesis block to illustrate how an invalid
block is handled.
* [CompactToBig Example]
(http://godoc.org/github.com/btcsuite/btcchain#example-CompactToBig)
(http://godoc.org/github.com/btcsuite/btcd/blockchain#example-CompactToBig)
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.
* [BigToCompact Example]
(http://godoc.org/github.com/btcsuite/btcchain#example-BigToCompact)
(http://godoc.org/github.com/btcsuite/btcd/blockchain#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
- Increase test coverage
## GPG Verification Key
All official release tags are signed by Conformal so users can ensure the code
@ -121,5 +115,5 @@ signature perform the following:
## License
Package btcchain is licensed under the [copyfree](http://copyfree.org) ISC
Package blockchain is licensed under the [copyfree](http://copyfree.org) ISC
License.

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"github.com/btcsuite/btcwire"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"container/list"

View file

@ -2,12 +2,12 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"testing"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcnet"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
@ -46,12 +46,12 @@ func TestHaveBlock(t *testing.T) {
// Since we're not dealing with the real block chain, disable
// checkpoints and set the coinbase maturity to 1.
chain.DisableCheckpoints(true)
btcchain.TstSetCoinbaseMaturity(1)
blockchain.TstSetCoinbaseMaturity(1)
timeSource := btcchain.NewMedianTime()
timeSource := blockchain.NewMedianTime()
for i := 1; i < len(blocks); i++ {
isOrphan, err := chain.ProcessBlock(blocks[i], timeSource,
btcchain.BFNone)
blockchain.BFNone)
if err != nil {
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
return
@ -65,7 +65,7 @@ func TestHaveBlock(t *testing.T) {
// Insert an orphan block.
isOrphan, err := chain.ProcessBlock(btcutil.NewBlock(&Block100000),
timeSource, btcchain.BFNone)
timeSource, blockchain.BFNone)
if err != nil {
t.Errorf("Unable to process block: %v", err)
return

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"compress/bzip2"
@ -13,7 +13,7 @@ import (
"path/filepath"
"strings"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/database"
_ "github.com/btcsuite/btcd/database/ldb"
_ "github.com/btcsuite/btcd/database/memdb"
@ -54,7 +54,7 @@ func isSupportedDbType(dbType string) bool {
// chainSetup is used to create a new db and chain instance with the genesis
// block already inserted. In addition to the new chain instnce, it returns
// a teardown function the caller should invoke when done testing to clean up.
func chainSetup(dbName string) (*btcchain.BlockChain, func(), error) {
func chainSetup(dbName string) (*blockchain.BlockChain, func(), error) {
if !isSupportedDbType(testDbType) {
return nil, nil, fmt.Errorf("unsupported db type %v", testDbType)
}
@ -116,12 +116,12 @@ func chainSetup(dbName string) (*btcchain.BlockChain, func(), error) {
return nil, nil, err
}
chain := btcchain.New(db, &btcnet.MainNetParams, nil)
chain := blockchain.New(db, &btcnet.MainNetParams, nil)
return chain, teardown, nil
}
// loadTxStore returns a transaction store loaded from a file.
func loadTxStore(filename string) (btcchain.TxStore, error) {
func loadTxStore(filename string) (blockchain.TxStore, error) {
// The txstore file format is:
// <num tx data entries> <tx length> <serialized tx> <blk height>
// <num spent bits> <spent bits>
@ -150,10 +150,10 @@ func loadTxStore(filename string) (btcchain.TxStore, error) {
return nil, err
}
txStore := make(btcchain.TxStore)
txStore := make(blockchain.TxStore)
var uintBuf uint32
for height := uint32(0); height < numItems; height++ {
txD := btcchain.TxData{}
txD := blockchain.TxData{}
// Serialized transaction length.
err = binary.Read(r, binary.LittleEndian, &uintBuf)

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

View file

@ -2,13 +2,13 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"math/big"
"testing"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
)
func TestBigToCompact(t *testing.T) {
@ -22,7 +22,7 @@ func TestBigToCompact(t *testing.T) {
for x, test := range tests {
n := big.NewInt(test.in)
r := btcchain.BigToCompact(n)
r := blockchain.BigToCompact(n)
if r != test.out {
t.Errorf("TestBigToCompact test #%d failed: got %d want %d\n",
x, r, test.out)
@ -40,7 +40,7 @@ func TestCompactToBig(t *testing.T) {
}
for x, test := range tests {
n := btcchain.CompactToBig(test.in)
n := blockchain.CompactToBig(test.in)
want := big.NewInt(test.out)
if n.Cmp(want) != 0 {
t.Errorf("TestCompactToBig test #%d failed: got %d want %d\n",
@ -61,7 +61,7 @@ func TestCalcWork(t *testing.T) {
for x, test := range tests {
bits := uint32(test.in)
r := btcchain.CalcWork(bits)
r := blockchain.CalcWork(bits)
if r.Int64() != test.out {
t.Errorf("TestCalcWork test #%d failed: got %v want %d\n",
x, r.Int64(), test.out)

View file

@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.
/*
Package btcchain implements bitcoin block handling and chain selection rules.
Package blockchain implements bitcoin block handling and chain selection rules.
The bitcoin block handling and chain selection rules are an integral, and quite
likely the most important, part of bitcoin. Unfortunately, at the time of
@ -64,11 +64,11 @@ is by no means exhaustive:
Errors
Errors returned by this package are either the raw errors provided by underlying
calls or of type btcchain.RuleError. This allows the caller to differentiate
calls or of type blockchain.RuleError. This allows the caller to differentiate
between unexpected errors, such as database errors, versus errors due to rule
violations through type assertions. In addition, callers can programmatically
determine the specific rule violation by examining the ErrorCode field of the
type asserted btcchain.RuleError.
type asserted blockchain.RuleError.
Bitcoin Improvement Proposals
@ -78,4 +78,4 @@ This package includes spec changes outlined by the following BIPs:
BIP0030 (https://en.bitcoin.it/wiki/BIP_0030)
BIP0034 (https://en.bitcoin.it/wiki/BIP_0034)
*/
package btcchain
package blockchain

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

97
blockchain/error_test.go Normal file
View file

@ -0,0 +1,97 @@
// Copyright (c) 2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain_test
import (
"testing"
"github.com/btcsuite/btcd/blockchain"
)
// TestErrorCodeStringer tests the stringized output for the ErrorCode type.
func TestErrorCodeStringer(t *testing.T) {
tests := []struct {
in blockchain.ErrorCode
want string
}{
{blockchain.ErrDuplicateBlock, "ErrDuplicateBlock"},
{blockchain.ErrBlockTooBig, "ErrBlockTooBig"},
{blockchain.ErrBlockVersionTooOld, "ErrBlockVersionTooOld"},
{blockchain.ErrInvalidTime, "ErrInvalidTime"},
{blockchain.ErrTimeTooOld, "ErrTimeTooOld"},
{blockchain.ErrTimeTooNew, "ErrTimeTooNew"},
{blockchain.ErrDifficultyTooLow, "ErrDifficultyTooLow"},
{blockchain.ErrUnexpectedDifficulty, "ErrUnexpectedDifficulty"},
{blockchain.ErrHighHash, "ErrHighHash"},
{blockchain.ErrBadMerkleRoot, "ErrBadMerkleRoot"},
{blockchain.ErrBadCheckpoint, "ErrBadCheckpoint"},
{blockchain.ErrForkTooOld, "ErrForkTooOld"},
{blockchain.ErrCheckpointTimeTooOld, "ErrCheckpointTimeTooOld"},
{blockchain.ErrNoTransactions, "ErrNoTransactions"},
{blockchain.ErrTooManyTransactions, "ErrTooManyTransactions"},
{blockchain.ErrNoTxInputs, "ErrNoTxInputs"},
{blockchain.ErrNoTxOutputs, "ErrNoTxOutputs"},
{blockchain.ErrTxTooBig, "ErrTxTooBig"},
{blockchain.ErrBadTxOutValue, "ErrBadTxOutValue"},
{blockchain.ErrDuplicateTxInputs, "ErrDuplicateTxInputs"},
{blockchain.ErrBadTxInput, "ErrBadTxInput"},
{blockchain.ErrBadCheckpoint, "ErrBadCheckpoint"},
{blockchain.ErrMissingTx, "ErrMissingTx"},
{blockchain.ErrUnfinalizedTx, "ErrUnfinalizedTx"},
{blockchain.ErrDuplicateTx, "ErrDuplicateTx"},
{blockchain.ErrOverwriteTx, "ErrOverwriteTx"},
{blockchain.ErrImmatureSpend, "ErrImmatureSpend"},
{blockchain.ErrDoubleSpend, "ErrDoubleSpend"},
{blockchain.ErrSpendTooHigh, "ErrSpendTooHigh"},
{blockchain.ErrBadFees, "ErrBadFees"},
{blockchain.ErrTooManySigOps, "ErrTooManySigOps"},
{blockchain.ErrFirstTxNotCoinbase, "ErrFirstTxNotCoinbase"},
{blockchain.ErrMultipleCoinbases, "ErrMultipleCoinbases"},
{blockchain.ErrBadCoinbaseScriptLen, "ErrBadCoinbaseScriptLen"},
{blockchain.ErrBadCoinbaseValue, "ErrBadCoinbaseValue"},
{blockchain.ErrMissingCoinbaseHeight, "ErrMissingCoinbaseHeight"},
{blockchain.ErrBadCoinbaseHeight, "ErrBadCoinbaseHeight"},
{blockchain.ErrScriptMalformed, "ErrScriptMalformed"},
{blockchain.ErrScriptValidation, "ErrScriptValidation"},
{0xffff, "Unknown ErrorCode (65535)"},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.String()
if result != test.want {
t.Errorf("String #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}
// TestRuleError tests the error output for the RuleError type.
func TestRuleError(t *testing.T) {
tests := []struct {
in blockchain.RuleError
want string
}{
{
blockchain.RuleError{Description: "duplicate block"},
"duplicate block",
},
{
blockchain.RuleError{Description: "human-readable error"},
"human-readable error",
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("Error #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}

View file

@ -2,13 +2,13 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"fmt"
"math/big"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/database"
_ "github.com/btcsuite/btcd/database/memdb"
"github.com/btcsuite/btcnet"
@ -44,18 +44,18 @@ func ExampleBlockChain_ProcessBlock() {
// Create a new BlockChain instance using the underlying database for
// the main bitcoin network and ignore notifications.
chain := btcchain.New(db, &btcnet.MainNetParams, nil)
chain := blockchain.New(db, &btcnet.MainNetParams, nil)
// 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.
timeSource := btcchain.NewMedianTime()
timeSource := blockchain.NewMedianTime()
// Process a block. For this example, we are going to intentionally
// cause an error by trying to process the genesis block which already
// exists.
isOrphan, err := chain.ProcessBlock(genesisBlock, timeSource, btcchain.BFNone)
isOrphan, err := chain.ProcessBlock(genesisBlock, timeSource, blockchain.BFNone)
if err != nil {
fmt.Printf("Failed to process block: %v\n", err)
return
@ -72,7 +72,7 @@ func ExampleBlockChain_ProcessBlock() {
func ExampleCompactToBig() {
// Convert the bits from block 300000 in the main block chain.
bits := uint32(419465580)
targetDifficulty := btcchain.CompactToBig(bits)
targetDifficulty := blockchain.CompactToBig(bits)
// Display it in hex.
fmt.Printf("%064x\n", targetDifficulty.Bytes())
@ -92,7 +92,7 @@ func ExampleBigToCompact() {
fmt.Println("invalid target difficulty")
return
}
bits := btcchain.BigToCompact(targetDifficulty)
bits := blockchain.BigToCompact(targetDifficulty)
fmt.Println(bits)

View file

@ -3,13 +3,14 @@
// license that can be found in the LICENSE file.
/*
This test file is part of the btcchain package rather than than the
btcchain_test package so it can bridge access to the internals to properly test
cases which are either not possible or can't reliably be tested via the public
interface. The functions are only exported while the tests are being run.
This test file is part of the blockchain package rather than than the
blockchain_test package so it can bridge access to the internals to properly
test cases which are either not possible or can't reliably be tested via the
public interface. The functions are only exported while the tests are being
run.
*/
package btcchain
package blockchain
import (
"sort"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"errors"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"math"

View file

@ -2,14 +2,14 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"strconv"
"testing"
"time"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
)
// TestMedianTime tests the medianTime implementation.
@ -55,11 +55,11 @@ func TestMedianTime(t *testing.T) {
}
// Modify the max number of allowed median time entries for these tests.
btcchain.TstSetMaxMedianTimeEntries(10)
defer btcchain.TstSetMaxMedianTimeEntries(200)
blockchain.TstSetMaxMedianTimeEntries(10)
defer blockchain.TstSetMaxMedianTimeEntries(200)
for i, test := range tests {
filter := btcchain.NewMedianTime()
filter := blockchain.NewMedianTime()
for j, offset := range test.in {
id := strconv.Itoa(j)
now := time.Unix(time.Now().Unix(), 0)

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"math"

View file

@ -2,19 +2,19 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"testing"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcutil"
)
// TestMerkle tests the BuildMerkleTreeStore API.
func TestMerkle(t *testing.T) {
block := btcutil.NewBlock(&Block100000)
merkles := btcchain.BuildMerkleTreeStore(block.Transactions())
merkles := blockchain.BuildMerkleTreeStore(block.Transactions())
calculatedMerkleRoot := merkles[len(merkles)-1]
wantMerkle := &Block100000.Header.MerkleRoot
if !wantMerkle.IsEqual(calculatedMerkleRoot) {

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"compress/bzip2"
@ -13,7 +13,7 @@ import (
"strings"
"testing"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
)
@ -56,12 +56,12 @@ func TestReorganization(t *testing.T) {
// Since we're not dealing with the real block chain, disable
// checkpoints and set the coinbase maturity to 1.
chain.DisableCheckpoints(true)
btcchain.TstSetCoinbaseMaturity(1)
blockchain.TstSetCoinbaseMaturity(1)
timeSource := btcchain.NewMedianTime()
timeSource := blockchain.NewMedianTime()
expectedOrphans := map[int]struct{}{5: struct{}{}, 6: struct{}{}}
for i := 1; i < len(blocks); i++ {
isOrphan, err := chain.ProcessBlock(blocks[i], timeSource, btcchain.BFNone)
isOrphan, err := chain.ProcessBlock(blocks[i], timeSource, blockchain.BFNone)
if err != nil {
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
return

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

View file

@ -2,14 +2,14 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"fmt"
"runtime"
"testing"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
)
// TestCheckBlockScripts ensures that validating the all of the scripts in a
@ -35,7 +35,7 @@ func TestCheckBlockScripts(t *testing.T) {
return
}
if err := btcchain.TstCheckBlockScripts(blocks[0], txStore); err != nil {
if err := blockchain.TstCheckBlockScripts(blocks[0], txStore); err != nil {
t.Errorf("Transaction script validation failed: %v\n",
err)
return

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"time"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"reflect"
@ -10,7 +10,7 @@ import (
"testing"
"time"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
)
// TestTimeSorter tests the timeSorter implementation.
@ -42,7 +42,7 @@ func TestTimeSorter(t *testing.T) {
for i, test := range tests {
result := make([]time.Time, len(test.in))
copy(result, test.in)
sort.Sort(btcchain.TstTimeSorter(result))
sort.Sort(blockchain.TstTimeSorter(result))
if !reflect.DeepEqual(result, test.want) {
t.Errorf("timeSorter #%d got %v want %v", i, result,
test.want)

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"fmt"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain
package blockchain
import (
"encoding/binary"

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
package blockchain_test
import (
"math"
@ -10,7 +10,7 @@ import (
"testing"
"time"
"github.com/btcsuite/btcchain"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcnet"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwire"
@ -46,8 +46,8 @@ func TestCheckConnectBlock(t *testing.T) {
func TestCheckBlockSanity(t *testing.T) {
powLimit := btcnet.MainNetParams.PowLimit
block := btcutil.NewBlock(&Block100000)
timeSource := btcchain.NewMedianTime()
err := btcchain.CheckBlockSanity(block, powLimit, timeSource)
timeSource := blockchain.NewMedianTime()
err := blockchain.CheckBlockSanity(block, powLimit, timeSource)
if err != nil {
t.Errorf("CheckBlockSanity: %v", err)
}
@ -56,7 +56,7 @@ func TestCheckBlockSanity(t *testing.T) {
// second fails.
timestamp := block.MsgBlock().Header.Timestamp
block.MsgBlock().Header.Timestamp = timestamp.Add(time.Nanosecond)
err = btcchain.CheckBlockSanity(block, powLimit, timeSource)
err = blockchain.CheckBlockSanity(block, powLimit, timeSource)
if err == nil {
t.Errorf("CheckBlockSanity: error is nil when it shouldn't be")
}
@ -73,11 +73,11 @@ func TestCheckSerializedHeight(t *testing.T) {
coinbaseTx.AddTxIn(btcwire.NewTxIn(coinbaseOutpoint, nil))
// Expected rule errors.
missingHeightError := btcchain.RuleError{
ErrorCode: btcchain.ErrMissingCoinbaseHeight,
missingHeightError := blockchain.RuleError{
ErrorCode: blockchain.ErrMissingCoinbaseHeight,
}
badHeightError := btcchain.RuleError{
ErrorCode: btcchain.ErrBadCoinbaseHeight,
badHeightError := blockchain.RuleError{
ErrorCode: blockchain.ErrBadCoinbaseHeight,
}
tests := []struct {
@ -109,15 +109,15 @@ func TestCheckSerializedHeight(t *testing.T) {
msgTx.TxIn[0].SignatureScript = test.sigScript
tx := btcutil.NewTx(msgTx)
err := btcchain.TstCheckSerializedHeight(tx, test.wantHeight)
err := blockchain.TstCheckSerializedHeight(tx, test.wantHeight)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("checkSerializedHeight #%d wrong error type "+
"got: %v <%T>, want: %T", i, err, err, test.err)
continue
}
if rerr, ok := err.(btcchain.RuleError); ok {
trerr := test.err.(btcchain.RuleError)
if rerr, ok := err.(blockchain.RuleError); ok {
trerr := test.err.(blockchain.RuleError)
if rerr.ErrorCode != trerr.ErrorCode {
t.Errorf("checkSerializedHeight #%d wrong "+
"error code got: %v, want: %v", i,

View file

@ -1,17 +0,0 @@
#!/bin/sh
# This script uses gocov to generate a test coverage report.
# The gocov tool my be obtained with the following command:
# go get github.com/axw/gocov/gocov
#
# It will be installed to $GOPATH/bin, so ensure that location is in your $PATH.
# Check for gocov.
type gocov >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo >&2 "This script requires the gocov tool."
echo >&2 "You may obtain it with the following command:"
echo >&2 "go get github.com/axw/gocov/gocov"
exit 1
fi
gocov test | gocov report

View file

@ -1,97 +0,0 @@
// Copyright (c) 2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
import (
"testing"
"github.com/btcsuite/btcchain"
)
// TestErrorCodeStringer tests the stringized output for the ErrorCode type.
func TestErrorCodeStringer(t *testing.T) {
tests := []struct {
in btcchain.ErrorCode
want string
}{
{btcchain.ErrDuplicateBlock, "ErrDuplicateBlock"},
{btcchain.ErrBlockTooBig, "ErrBlockTooBig"},
{btcchain.ErrBlockVersionTooOld, "ErrBlockVersionTooOld"},
{btcchain.ErrInvalidTime, "ErrInvalidTime"},
{btcchain.ErrTimeTooOld, "ErrTimeTooOld"},
{btcchain.ErrTimeTooNew, "ErrTimeTooNew"},
{btcchain.ErrDifficultyTooLow, "ErrDifficultyTooLow"},
{btcchain.ErrUnexpectedDifficulty, "ErrUnexpectedDifficulty"},
{btcchain.ErrHighHash, "ErrHighHash"},
{btcchain.ErrBadMerkleRoot, "ErrBadMerkleRoot"},
{btcchain.ErrBadCheckpoint, "ErrBadCheckpoint"},
{btcchain.ErrForkTooOld, "ErrForkTooOld"},
{btcchain.ErrCheckpointTimeTooOld, "ErrCheckpointTimeTooOld"},
{btcchain.ErrNoTransactions, "ErrNoTransactions"},
{btcchain.ErrTooManyTransactions, "ErrTooManyTransactions"},
{btcchain.ErrNoTxInputs, "ErrNoTxInputs"},
{btcchain.ErrNoTxOutputs, "ErrNoTxOutputs"},
{btcchain.ErrTxTooBig, "ErrTxTooBig"},
{btcchain.ErrBadTxOutValue, "ErrBadTxOutValue"},
{btcchain.ErrDuplicateTxInputs, "ErrDuplicateTxInputs"},
{btcchain.ErrBadTxInput, "ErrBadTxInput"},
{btcchain.ErrBadCheckpoint, "ErrBadCheckpoint"},
{btcchain.ErrMissingTx, "ErrMissingTx"},
{btcchain.ErrUnfinalizedTx, "ErrUnfinalizedTx"},
{btcchain.ErrDuplicateTx, "ErrDuplicateTx"},
{btcchain.ErrOverwriteTx, "ErrOverwriteTx"},
{btcchain.ErrImmatureSpend, "ErrImmatureSpend"},
{btcchain.ErrDoubleSpend, "ErrDoubleSpend"},
{btcchain.ErrSpendTooHigh, "ErrSpendTooHigh"},
{btcchain.ErrBadFees, "ErrBadFees"},
{btcchain.ErrTooManySigOps, "ErrTooManySigOps"},
{btcchain.ErrFirstTxNotCoinbase, "ErrFirstTxNotCoinbase"},
{btcchain.ErrMultipleCoinbases, "ErrMultipleCoinbases"},
{btcchain.ErrBadCoinbaseScriptLen, "ErrBadCoinbaseScriptLen"},
{btcchain.ErrBadCoinbaseValue, "ErrBadCoinbaseValue"},
{btcchain.ErrMissingCoinbaseHeight, "ErrMissingCoinbaseHeight"},
{btcchain.ErrBadCoinbaseHeight, "ErrBadCoinbaseHeight"},
{btcchain.ErrScriptMalformed, "ErrScriptMalformed"},
{btcchain.ErrScriptValidation, "ErrScriptValidation"},
{0xffff, "Unknown ErrorCode (65535)"},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.String()
if result != test.want {
t.Errorf("String #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}
// TestRuleError tests the error output for the RuleError type.
func TestRuleError(t *testing.T) {
tests := []struct {
in btcchain.RuleError
want string
}{
{
btcchain.RuleError{Description: "duplicate block"},
"duplicate block",
},
{
btcchain.RuleError{Description: "human-readable error"},
"human-readable error",
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("Error #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}

View file

@ -1,38 +0,0 @@
#!/bin/bash
# The script does automatic checking on a Go package and its sub-packages, including:
# 1. gofmt (http://golang.org/cmd/gofmt/)
# 2. goimports (https://github.com/bradfitz/goimports)
# 3. golint (https://github.com/golang/lint)
# 4. go vet (http://golang.org/cmd/vet)
# 5. test coverage (http://blog.golang.org/cover)
set -e
# Automatic checks
test -z "$(gofmt -l -w . | tee /dev/stderr)"
test -z "$(goimports -l -w . | tee /dev/stderr)"
test -z "$(golint . | tee /dev/stderr)"
go vet ./...
env GORACE="halt_on_error=1" go test -v -race ./...
# Run test coverage on each subdirectories and merge the coverage profile.
echo "mode: count" > profile.cov
# Standard go tooling behavior is to ignore dirs with leading underscores.
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d);
do
if ls $dir/*.go &> /dev/null; then
go test -covermode=count -coverprofile=$dir/profile.tmp $dir
if [ -f $dir/profile.tmp ]; then
cat $dir/profile.tmp | tail -n +2 >> profile.cov
rm $dir/profile.tmp
fi
fi
done
go tool cover -func profile.cov
# To submit the test coverage result to coveralls.io,
# use goveralls (https://github.com/mattn/goveralls)
# goveralls -coverprofile=profile.cov -service=travis-ci

View file

@ -1,97 +0,0 @@
github.com/conformal/btcchain/validate.go checkSerializedHeight 100.00% (17/17)
github.com/conformal/btcchain/chain.go BlockChain.removeOrphanBlock 100.00% (16/16)
github.com/conformal/btcchain/txlookup.go disconnectTransactions 100.00% (13/13)
github.com/conformal/btcchain/difficulty.go CompactToBig 100.00% (12/12)
github.com/conformal/btcchain/validate.go CountSigOps 100.00% (9/9)
github.com/conformal/btcchain/chain.go New 100.00% (8/8)
github.com/conformal/btcchain/validate.go BlockChain.CheckConnectBlock 100.00% (7/7)
github.com/conformal/btcchain/difficulty.go ShaHashToBig 100.00% (5/5)
github.com/conformal/btcchain/merkle.go HashMerkleBranches 100.00% (5/5)
github.com/conformal/btcchain/chain.go BlockChain.IsKnownOrphan 100.00% (5/5)
github.com/conformal/btcchain/difficulty.go CalcWork 100.00% (5/5)
github.com/conformal/btcchain/merkle.go nextPowerOfTwo 100.00% (4/4)
github.com/conformal/btcchain/process.go BlockChain.blockExists 100.00% (3/3)
github.com/conformal/btcchain/chain.go newBlockNode 100.00% (3/3)
github.com/conformal/btcchain/error.go ErrorCode.String 100.00% (3/3)
github.com/conformal/btcchain/checkpoints.go newShaHashFromStr 100.00% (2/2)
github.com/conformal/btcchain/log.go init 100.00% (1/1)
github.com/conformal/btcchain/scriptval.go newTxValidator 100.00% (1/1)
github.com/conformal/btcchain/scriptval.go txValidator.sendResult 100.00% (1/1)
github.com/conformal/btcchain/timesorter.go timeSorter.Less 100.00% (1/1)
github.com/conformal/btcchain/log.go DisableLog 100.00% (1/1)
github.com/conformal/btcchain/chain.go BlockChain.HaveBlock 100.00% (1/1)
github.com/conformal/btcchain/error.go ruleError 100.00% (1/1)
github.com/conformal/btcchain/checkpoints.go BlockChain.DisableCheckpoints 100.00% (1/1)
github.com/conformal/btcchain/validate.go CheckBlockSanity 100.00% (1/1)
github.com/conformal/btcchain/error.go RuleError.Error 100.00% (1/1)
github.com/conformal/btcchain/timesorter.go timeSorter.Len 100.00% (1/1)
github.com/conformal/btcchain/timesorter.go timeSorter.Swap 100.00% (1/1)
github.com/conformal/btcchain/txlookup.go fetchTxStoreMain 95.65% (22/23)
github.com/conformal/btcchain/merkle.go BuildMerkleTreeStore 93.33% (14/15)
github.com/conformal/btcchain/chain.go BlockChain.getReorganizeNodes 92.86% (13/14)
github.com/conformal/btcchain/scriptval.go txValidator.Validate 88.46% (23/26)
github.com/conformal/btcchain/txlookup.go BlockChain.fetchTxStore 86.96% (20/23)
github.com/conformal/btcchain/chain.go BlockChain.connectBestChain 85.71% (30/35)
github.com/conformal/btcchain/validate.go BlockChain.checkBIP0030 85.71% (12/14)
github.com/conformal/btcchain/validate.go IsCoinBase 85.71% (6/7)
github.com/conformal/btcchain/chain.go BlockChain.reorganizeChain 85.29% (29/34)
github.com/conformal/btcchain/process.go BlockChain.processOrphans 84.21% (16/19)
github.com/conformal/btcchain/scriptval.go checkBlockScripts 83.33% (15/18)
github.com/conformal/btcchain/chain.go BlockChain.connectBlock 83.33% (10/12)
github.com/conformal/btcchain/chain.go BlockChain.calcPastMedianTime 82.35% (14/17)
github.com/conformal/btcchain/chain.go BlockChain.isMajorityVersion 80.00% (8/10)
github.com/conformal/btcchain/chain.go BlockChain.addOrphanBlock 77.78% (14/18)
github.com/conformal/btcchain/chain.go BlockChain.getPrevNodeFromBlock 77.78% (7/9)
github.com/conformal/btcchain/chain.go BlockChain.GenerateInitialIndex 77.27% (17/22)
github.com/conformal/btcchain/chain.go BlockChain.disconnectBlock 76.92% (10/13)
github.com/conformal/btcchain/txlookup.go BlockChain.fetchInputTransactions 75.00% (18/24)
github.com/conformal/btcchain/difficulty.go BigToCompact 75.00% (12/16)
github.com/conformal/btcchain/validate.go isTransactionSpent 75.00% (3/4)
github.com/conformal/btcchain/validate.go BlockChain.checkConnectBlock 71.15% (37/52)
github.com/conformal/btcchain/validate.go checkBlockSanity 67.44% (29/43)
github.com/conformal/btcchain/validate.go CalcBlockSubsidy 66.67% (2/3)
github.com/conformal/btcchain/validate.go isNullOutpoint 66.67% (2/3)
github.com/conformal/btcchain/validate.go CheckTransactionInputs 63.64% (28/44)
github.com/conformal/btcchain/txlookup.go connectTransactions 61.54% (8/13)
github.com/conformal/btcchain/validate.go CheckTransactionSanity 61.11% (22/36)
github.com/conformal/btcchain/validate.go isBIP0030Node 60.00% (3/5)
github.com/conformal/btcchain/validate.go checkProofOfWork 56.25% (9/16)
github.com/conformal/btcchain/accept.go BlockChain.maybeAcceptBlock 55.07% (38/69)
github.com/conformal/btcchain/process.go BlockChain.ProcessBlock 52.27% (23/44)
github.com/conformal/btcchain/chain.go BlockChain.loadBlockNode 52.00% (13/25)
github.com/conformal/btcchain/scriptval.go txValidator.validateHandler 50.00% (16/32)
github.com/conformal/btcchain/chain.go BlockChain.getPrevNodeFromNode 50.00% (4/8)
github.com/conformal/btcchain/checkpoints.go BlockChain.LatestCheckpoint 50.00% (2/4)
github.com/conformal/btcchain/notifications.go BlockChain.sendNotification 50.00% (2/4)
github.com/conformal/btcchain/chain.go BlockChain.pruneBlockNodes 41.18% (7/17)
github.com/conformal/btcchain/validate.go IsFinalizedTransaction 28.57% (4/14)
github.com/conformal/btcchain/checkpoints.go BlockChain.verifyCheckpoint 22.22% (2/9)
github.com/conformal/btcchain/difficulty.go BlockChain.calcNextRequiredDifficulty 11.11% (4/36)
github.com/conformal/btcchain/checkpoints.go BlockChain.findPreviousCheckpoint 4.88% (2/41)
github.com/conformal/btcchain/blocklocator.go BlockChain.BlockLocatorFromHash 0.00% (0/39)
github.com/conformal/btcchain/checkpoints.go BlockChain.IsCheckpointCandidate 0.00% (0/32)
github.com/conformal/btcchain/validate.go CountP2SHSigOps 0.00% (0/26)
github.com/conformal/btcchain/chain.go BlockChain.removeBlockNode 0.00% (0/12)
github.com/conformal/btcchain/difficulty.go BlockChain.calcEasiestDifficulty 0.00% (0/12)
github.com/conformal/btcchain/chain.go BlockChain.GetOrphanRoot 0.00% (0/11)
github.com/conformal/btcchain/scriptval.go ValidateTransactionScripts 0.00% (0/11)
github.com/conformal/btcchain/difficulty.go BlockChain.findPrevTestNetDifficulty 0.00% (0/11)
github.com/conformal/btcchain/log.go SetLogWriter 0.00% (0/10)
github.com/conformal/btcchain/chain.go BlockChain.IsCurrent 0.00% (0/9)
github.com/conformal/btcchain/chain.go removeChildNode 0.00% (0/8)
github.com/conformal/btcchain/txlookup.go BlockChain.FetchTransactionStore 0.00% (0/6)
github.com/conformal/btcchain/blocklocator.go BlockChain.LatestBlockLocator 0.00% (0/6)
github.com/conformal/btcchain/checkpoints.go isNonstandardTransaction 0.00% (0/5)
github.com/conformal/btcchain/checkpoints.go BlockChain.Checkpoints 0.00% (0/3)
github.com/conformal/btcchain/notifications.go NotificationType.String 0.00% (0/3)
github.com/conformal/btcchain/chain.go addChildrenWork 0.00% (0/3)
github.com/conformal/btcchain/log.go logClosure.String 0.00% (0/1)
github.com/conformal/btcchain/chain.go BlockChain.CalcPastMedianTime 0.00% (0/1)
github.com/conformal/btcchain/chain.go BlockChain.DisableVerify 0.00% (0/1)
github.com/conformal/btcchain/validate.go CheckProofOfWork 0.00% (0/1)
github.com/conformal/btcchain/log.go newLogClosure 0.00% (0/1)
github.com/conformal/btcchain/difficulty.go BlockChain.CalcNextRequiredDifficulty 0.00% (0/1)
github.com/conformal/btcchain/log.go UseLogger 0.00% (0/1)
github.com/conformal/btcchain ------------------------------------- 56.65% (699/1234)