Update for recent chaincfg API changes. (#451)

Since the coinbase maturity is now allowed to be defined per chain and
the old blockchain.CoinbaseMaturity constant has been removed, this
updates the code accordingly.

Also, update glide.lock to use the required version of btcd.
This commit is contained in:
Dave Collins 2016-08-12 19:27:51 -05:00 committed by GitHub
parent 33a6c2d8b6
commit d76627e6d5
10 changed files with 72 additions and 64 deletions

4
glide.lock generated
View file

@ -1,10 +1,10 @@
hash: 0cff4a723566a3e33910785c104bddde49d1e0ba2d51f3833599a423140f5163 hash: 0cff4a723566a3e33910785c104bddde49d1e0ba2d51f3833599a423140f5163
updated: 2016-08-08T14:07:48.6427885-05:00 updated: 2016-08-12T09:43:45.22287-05:00
imports: imports:
- name: github.com/boltdb/bolt - name: github.com/boltdb/bolt
version: 831b652a7f8dbefaf94da0eb66abd46c0c4bcf23 version: 831b652a7f8dbefaf94da0eb66abd46c0c4bcf23
- name: github.com/btcsuite/btcd - name: github.com/btcsuite/btcd
version: bd4e64d1d43bad445dd8e6577907c0c265cd83c2 version: a7b35d9f9e24f8cebf3dc30cf083668700690095
subpackages: subpackages:
- blockchain - blockchain
- btcec - btcec

View file

@ -894,7 +894,7 @@ func getTransaction(icmd interface{}, w *wallet.Wallet) (interface{}, error) {
ret.Fee = feeF64 ret.Fee = feeF64
} }
credCat := wallet.RecvCategory(details, syncBlock.Height).String() credCat := wallet.RecvCategory(details, syncBlock.Height, w.ChainParams()).String()
for _, cred := range details.Credits { for _, cred := range details.Credits {
// Change is ignored. // Change is ignored.
if cred.Change { if cred.Change {

View file

@ -25,7 +25,6 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -341,8 +340,9 @@ func (s *walletServer) FundTransaction(ctx context.Context, req *pb.FundTransact
if !confirmed(req.RequiredConfirmations, output.Height, syncBlock.Height) { if !confirmed(req.RequiredConfirmations, output.Height, syncBlock.Height) {
continue continue
} }
target := int32(s.wallet.ChainParams().CoinbaseMaturity)
if !req.IncludeImmatureCoinbases && output.FromCoinBase && if !req.IncludeImmatureCoinbases && output.FromCoinBase &&
!confirmed(blockchain.CoinbaseMaturity, output.Height, syncBlock.Height) { !confirmed(target, output.Height, syncBlock.Height) {
continue continue
} }

View file

@ -340,7 +340,7 @@ func exampleCreateTxStore() (*wtxmgr.Store, func(), error) {
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
s, err := wtxmgr.Open(wtxmgrNamespace) s, err := wtxmgr.Open(wtxmgrNamespace, &chaincfg.MainNetParams)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

View file

@ -154,7 +154,7 @@ func TstCreateTxStore(t *testing.T) (store *wtxmgr.Store, tearDown func()) {
if err != nil { if err != nil {
t.Fatalf("Failed to create txstore: %v", err) t.Fatalf("Failed to create txstore: %v", err)
} }
s, err := wtxmgr.Open(wtxmgrNamespace) s, err := wtxmgr.Open(wtxmgrNamespace, &chaincfg.MainNetParams)
if err != nil { if err != nil {
t.Fatalf("Failed to open txstore: %v", err) t.Fatalf("Failed to open txstore: %v", err)
} }

View file

@ -8,7 +8,6 @@ import (
"fmt" "fmt"
"sort" "sort"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -188,7 +187,7 @@ func (w *Wallet) findEligibleOutputs(account uint32, minconf int32, bs *waddrmgr
continue continue
} }
if output.FromCoinBase { if output.FromCoinBase {
const target = blockchain.CoinbaseMaturity target := int32(w.chainParams.CoinbaseMaturity)
if !confirmed(target, output.Height, bs.Height) { if !confirmed(target, output.Height, bs.Height) {
continue continue
} }

View file

@ -701,7 +701,7 @@ func (w *Wallet) CalculateAccountBalances(account uint32, confirms int32) (Balan
bals.Total += output.Amount bals.Total += output.Amount
if output.FromCoinBase { if output.FromCoinBase {
const target = blockchain.CoinbaseMaturity target := int32(w.chainParams.CoinbaseMaturity)
if !confirmed(target, output.Height, syncBlock.Height) { if !confirmed(target, output.Height, syncBlock.Height) {
bals.ImmatureReward += output.Amount bals.ImmatureReward += output.Amount
} }
@ -811,9 +811,10 @@ func (c CreditCategory) String() string {
// //
// TODO: This is intended for use by the RPC server and should be moved out of // TODO: This is intended for use by the RPC server and should be moved out of
// this package at a later time. // this package at a later time.
func RecvCategory(details *wtxmgr.TxDetails, syncHeight int32) CreditCategory { func RecvCategory(details *wtxmgr.TxDetails, syncHeight int32, net *chaincfg.Params) CreditCategory {
if blockchain.IsCoinBaseTx(&details.MsgTx) { if blockchain.IsCoinBaseTx(&details.MsgTx) {
if confirmed(blockchain.CoinbaseMaturity, details.Block.Height, syncHeight) { if confirmed(int32(net.CoinbaseMaturity), details.Block.Height,
syncHeight) {
return CreditGenerate return CreditGenerate
} }
return CreditImmature return CreditImmature
@ -843,7 +844,7 @@ func ListTransactions(details *wtxmgr.TxDetails, addrMgr *waddrmgr.Manager,
txHashStr := details.Hash.String() txHashStr := details.Hash.String()
received := details.Received.Unix() received := details.Received.Unix()
generated := blockchain.IsCoinBaseTx(&details.MsgTx) generated := blockchain.IsCoinBaseTx(&details.MsgTx)
recvCat := RecvCategory(details, syncHeight).String() recvCat := RecvCategory(details, syncHeight, net).String()
send := len(details.Debits) != 0 send := len(details.Debits) != 0
@ -1340,7 +1341,7 @@ func (w *Wallet) ListUnspent(minconf, maxconf int32,
// Only mature coinbase outputs are included. // Only mature coinbase outputs are included.
if output.FromCoinBase { if output.FromCoinBase {
const target = blockchain.CoinbaseMaturity target := int32(w.chainParams.CoinbaseMaturity)
if !confirmed(target, output.Height, syncBlock.Height) { if !confirmed(target, output.Height, syncBlock.Height) {
continue continue
} }
@ -2132,7 +2133,7 @@ func Open(db walletdb.DB, pubPass []byte, cbs *waddrmgr.OpenCallbacks, params *c
return nil, err return nil, err
} }
} }
txMgr, err := wtxmgr.Open(txMgrNS) txMgr, err := wtxmgr.Open(txMgrNS, params)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -7,6 +7,7 @@ package wtxmgr_test
import ( import (
"fmt" "fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wtxmgr" "github.com/btcsuite/btcwallet/wtxmgr"
@ -161,7 +162,7 @@ func Example_basicUsage() {
fmt.Println(err) fmt.Println(err)
return return
} }
s, err := wtxmgr.Open(ns) s, err := wtxmgr.Open(ns, &chaincfg.TestNet3Params)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View file

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
@ -130,7 +131,8 @@ type Credit struct {
// Store implements a transaction store for storing and managing wallet // Store implements a transaction store for storing and managing wallet
// transactions. // transactions.
type Store struct { type Store struct {
namespace walletdb.Namespace namespace walletdb.Namespace
chainParams *chaincfg.Params
// Event callbacks. These execute in the same goroutine as the wtxmgr // Event callbacks. These execute in the same goroutine as the wtxmgr
// caller. // caller.
@ -140,13 +142,13 @@ type Store struct {
// Open opens the wallet transaction store from a walletdb namespace. If the // Open opens the wallet transaction store from a walletdb namespace. If the
// store does not exist, ErrNoExist is returned. Existing stores will be // store does not exist, ErrNoExist is returned. Existing stores will be
// upgraded to new database formats as necessary. // upgraded to new database formats as necessary.
func Open(namespace walletdb.Namespace) (*Store, error) { func Open(namespace walletdb.Namespace, chainParams *chaincfg.Params) (*Store, error) {
// Open the store, upgrading to the latest version as needed. // Open the store, upgrading to the latest version as needed.
err := openStore(namespace) err := openStore(namespace)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Store{namespace, nil}, nil // TODO: set callbacks return &Store{namespace, chainParams, nil}, nil // TODO: set callbacks
} }
// Create creates a new persistent transaction store in the walletdb namespace. // Create creates a new persistent transaction store in the walletdb namespace.
@ -856,9 +858,10 @@ func (s *Store) balance(ns walletdb.Bucket, minConf int32, syncHeight int32) (bt
// Decrement the balance for any unspent credit with less than // Decrement the balance for any unspent credit with less than
// minConf confirmations and any (unspent) immature coinbase credit. // minConf confirmations and any (unspent) immature coinbase credit.
coinbaseMaturity := int32(s.chainParams.CoinbaseMaturity)
stopConf := minConf stopConf := minConf
if blockchain.CoinbaseMaturity > stopConf { if coinbaseMaturity > stopConf {
stopConf = blockchain.CoinbaseMaturity stopConf = coinbaseMaturity
} }
lastHeight := syncHeight - stopConf lastHeight := syncHeight - stopConf
blockIt := makeReverseBlockIterator(ns) blockIt := makeReverseBlockIterator(ns)
@ -898,7 +901,7 @@ func (s *Store) balance(ns walletdb.Bucket, minConf int32, syncHeight int32) (bt
} }
confs := syncHeight - block.Height + 1 confs := syncHeight - block.Height + 1
if confs < minConf || (blockchain.IsCoinBaseTx(&rec.MsgTx) && if confs < minConf || (blockchain.IsCoinBaseTx(&rec.MsgTx) &&
confs < blockchain.CoinbaseMaturity) { confs < coinbaseMaturity) {
bal -= amt bal -= amt
} }
} }

View file

@ -13,7 +13,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
@ -80,7 +80,7 @@ func testStore() (*Store, func(), error) {
if err != nil { if err != nil {
return nil, teardown, err return nil, teardown, err
} }
s, err := Open(ns) s, err := Open(ns, &chaincfg.TestNet3Params)
return s, teardown, err return s, teardown, err
} }
@ -665,6 +665,8 @@ func TestCoinbases(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
coinbaseMaturity := int32(chaincfg.TestNet3Params.CoinbaseMaturity)
// Balance should be 0 if the coinbase is immature, 50 BTC at and beyond // Balance should be 0 if the coinbase is immature, 50 BTC at and beyond
// maturity. // maturity.
// //
@ -679,67 +681,67 @@ func TestCoinbases(t *testing.T) {
balTests := []balTest{ balTests := []balTest{
// Next block it is still immature // Next block it is still immature
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 2, height: b100.Height + coinbaseMaturity - 2,
minConf: 0, minConf: 0,
bal: 0, bal: 0,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 2, height: b100.Height + coinbaseMaturity - 2,
minConf: blockchain.CoinbaseMaturity, minConf: coinbaseMaturity,
bal: 0, bal: 0,
}, },
// Next block it matures // Next block it matures
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: 0, minConf: 0,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: 1, minConf: 1,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: blockchain.CoinbaseMaturity - 1, minConf: coinbaseMaturity - 1,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: blockchain.CoinbaseMaturity, minConf: coinbaseMaturity,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: blockchain.CoinbaseMaturity + 1, minConf: coinbaseMaturity + 1,
bal: 0, bal: 0,
}, },
// Matures at this block // Matures at this block
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: 0, minConf: 0,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: 1, minConf: 1,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: blockchain.CoinbaseMaturity, minConf: coinbaseMaturity,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: blockchain.CoinbaseMaturity + 1, minConf: coinbaseMaturity + 1,
bal: 50e8, bal: 50e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: blockchain.CoinbaseMaturity + 2, minConf: coinbaseMaturity + 2,
bal: 0, bal: 0,
}, },
} }
@ -776,50 +778,50 @@ func TestCoinbases(t *testing.T) {
balTests = []balTest{ balTests = []balTest{
// Next block it matures // Next block it matures
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: 0, minConf: 0,
bal: 35e8, bal: 35e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: 1, minConf: 1,
bal: 30e8, bal: 30e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: blockchain.CoinbaseMaturity, minConf: coinbaseMaturity,
bal: 30e8, bal: 30e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity - 1, height: b100.Height + coinbaseMaturity - 1,
minConf: blockchain.CoinbaseMaturity + 1, minConf: coinbaseMaturity + 1,
bal: 0, bal: 0,
}, },
// Matures at this block // Matures at this block
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: 0, minConf: 0,
bal: 35e8, bal: 35e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: 1, minConf: 1,
bal: 30e8, bal: 30e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: blockchain.CoinbaseMaturity, minConf: coinbaseMaturity,
bal: 30e8, bal: 30e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: blockchain.CoinbaseMaturity + 1, minConf: coinbaseMaturity + 1,
bal: 30e8, bal: 30e8,
}, },
{ {
height: b100.Height + blockchain.CoinbaseMaturity, height: b100.Height + coinbaseMaturity,
minConf: blockchain.CoinbaseMaturity + 2, minConf: coinbaseMaturity + 2,
bal: 0, bal: 0,
}, },
} }
@ -839,7 +841,7 @@ func TestCoinbases(t *testing.T) {
// Mine the spending transaction in the block the coinbase matures. // Mine the spending transaction in the block the coinbase matures.
bMaturity := BlockMeta{ bMaturity := BlockMeta{
Block: Block{Height: b100.Height + blockchain.CoinbaseMaturity}, Block: Block{Height: b100.Height + coinbaseMaturity},
Time: time.Now(), Time: time.Now(),
} }
err = s.InsertTx(spenderARec, &bMaturity) err = s.InsertTx(spenderARec, &bMaturity)
@ -866,17 +868,17 @@ func TestCoinbases(t *testing.T) {
}, },
{ {
height: bMaturity.Height, height: bMaturity.Height,
minConf: blockchain.CoinbaseMaturity, minConf: coinbaseMaturity,
bal: 30e8, bal: 30e8,
}, },
{ {
height: bMaturity.Height, height: bMaturity.Height,
minConf: blockchain.CoinbaseMaturity + 1, minConf: coinbaseMaturity + 1,
bal: 30e8, bal: 30e8,
}, },
{ {
height: bMaturity.Height, height: bMaturity.Height,
minConf: blockchain.CoinbaseMaturity + 2, minConf: coinbaseMaturity + 2,
bal: 0, bal: 0,
}, },
@ -898,12 +900,12 @@ func TestCoinbases(t *testing.T) {
}, },
{ {
height: bMaturity.Height + 1, height: bMaturity.Height + 1,
minConf: blockchain.CoinbaseMaturity + 2, minConf: coinbaseMaturity + 2,
bal: 30e8, bal: 30e8,
}, },
{ {
height: bMaturity.Height + 1, height: bMaturity.Height + 1,
minConf: blockchain.CoinbaseMaturity + 3, minConf: coinbaseMaturity + 3,
bal: 0, bal: 0,
}, },
} }
@ -1101,9 +1103,11 @@ func TestMoveMultipleToSameBlock(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
coinbaseMaturity := int32(chaincfg.TestNet3Params.CoinbaseMaturity)
// Mine both transactions in the block that matures the coinbase. // Mine both transactions in the block that matures the coinbase.
bMaturity := BlockMeta{ bMaturity := BlockMeta{
Block: Block{Height: b100.Height + blockchain.CoinbaseMaturity}, Block: Block{Height: b100.Height + coinbaseMaturity},
Time: time.Now(), Time: time.Now(),
} }
err = s.InsertTx(spenderARec, &bMaturity) err = s.InsertTx(spenderARec, &bMaturity)