From d76627e6d55d153d72f64c9480b19212d63650d6 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 12 Aug 2016 19:27:51 -0500 Subject: [PATCH] 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. --- glide.lock | 4 +- rpc/legacyrpc/methods.go | 2 +- rpc/rpcserver/server.go | 4 +- votingpool/example_test.go | 2 +- votingpool/factory_test.go | 2 +- wallet/createtx.go | 3 +- wallet/wallet.go | 13 +++--- wtxmgr/example_test.go | 3 +- wtxmgr/tx.go | 15 ++++--- wtxmgr/tx_test.go | 88 ++++++++++++++++++++------------------ 10 files changed, 72 insertions(+), 64 deletions(-) diff --git a/glide.lock b/glide.lock index ebae116..c44db77 100644 --- a/glide.lock +++ b/glide.lock @@ -1,10 +1,10 @@ hash: 0cff4a723566a3e33910785c104bddde49d1e0ba2d51f3833599a423140f5163 -updated: 2016-08-08T14:07:48.6427885-05:00 +updated: 2016-08-12T09:43:45.22287-05:00 imports: - name: github.com/boltdb/bolt version: 831b652a7f8dbefaf94da0eb66abd46c0c4bcf23 - name: github.com/btcsuite/btcd - version: bd4e64d1d43bad445dd8e6577907c0c265cd83c2 + version: a7b35d9f9e24f8cebf3dc30cf083668700690095 subpackages: - blockchain - btcec diff --git a/rpc/legacyrpc/methods.go b/rpc/legacyrpc/methods.go index 67ba669..17f1d78 100644 --- a/rpc/legacyrpc/methods.go +++ b/rpc/legacyrpc/methods.go @@ -894,7 +894,7 @@ func getTransaction(icmd interface{}, w *wallet.Wallet) (interface{}, error) { ret.Fee = feeF64 } - credCat := wallet.RecvCategory(details, syncBlock.Height).String() + credCat := wallet.RecvCategory(details, syncBlock.Height, w.ChainParams()).String() for _, cred := range details.Credits { // Change is ignored. if cred.Change { diff --git a/rpc/rpcserver/server.go b/rpc/rpcserver/server.go index c6e2e26..5f3222e 100644 --- a/rpc/rpcserver/server.go +++ b/rpc/rpcserver/server.go @@ -25,7 +25,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" - "github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "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) { continue } + target := int32(s.wallet.ChainParams().CoinbaseMaturity) if !req.IncludeImmatureCoinbases && output.FromCoinBase && - !confirmed(blockchain.CoinbaseMaturity, output.Height, syncBlock.Height) { + !confirmed(target, output.Height, syncBlock.Height) { continue } diff --git a/votingpool/example_test.go b/votingpool/example_test.go index 5ac1c64..6d438ce 100644 --- a/votingpool/example_test.go +++ b/votingpool/example_test.go @@ -340,7 +340,7 @@ func exampleCreateTxStore() (*wtxmgr.Store, func(), error) { if err != nil { return nil, nil, err } - s, err := wtxmgr.Open(wtxmgrNamespace) + s, err := wtxmgr.Open(wtxmgrNamespace, &chaincfg.MainNetParams) if err != nil { return nil, nil, err } diff --git a/votingpool/factory_test.go b/votingpool/factory_test.go index d8fc0e7..21f0d74 100644 --- a/votingpool/factory_test.go +++ b/votingpool/factory_test.go @@ -154,7 +154,7 @@ func TstCreateTxStore(t *testing.T) (store *wtxmgr.Store, tearDown func()) { if err != nil { t.Fatalf("Failed to create txstore: %v", err) } - s, err := wtxmgr.Open(wtxmgrNamespace) + s, err := wtxmgr.Open(wtxmgrNamespace, &chaincfg.MainNetParams) if err != nil { t.Fatalf("Failed to open txstore: %v", err) } diff --git a/wallet/createtx.go b/wallet/createtx.go index d072796..b167909 100644 --- a/wallet/createtx.go +++ b/wallet/createtx.go @@ -8,7 +8,6 @@ import ( "fmt" "sort" - "github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" @@ -188,7 +187,7 @@ func (w *Wallet) findEligibleOutputs(account uint32, minconf int32, bs *waddrmgr continue } if output.FromCoinBase { - const target = blockchain.CoinbaseMaturity + target := int32(w.chainParams.CoinbaseMaturity) if !confirmed(target, output.Height, bs.Height) { continue } diff --git a/wallet/wallet.go b/wallet/wallet.go index 280fe3a..4f6f2a0 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -701,7 +701,7 @@ func (w *Wallet) CalculateAccountBalances(account uint32, confirms int32) (Balan bals.Total += output.Amount if output.FromCoinBase { - const target = blockchain.CoinbaseMaturity + target := int32(w.chainParams.CoinbaseMaturity) if !confirmed(target, output.Height, syncBlock.Height) { 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 // 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 confirmed(blockchain.CoinbaseMaturity, details.Block.Height, syncHeight) { + if confirmed(int32(net.CoinbaseMaturity), details.Block.Height, + syncHeight) { return CreditGenerate } return CreditImmature @@ -843,7 +844,7 @@ func ListTransactions(details *wtxmgr.TxDetails, addrMgr *waddrmgr.Manager, txHashStr := details.Hash.String() received := details.Received.Unix() generated := blockchain.IsCoinBaseTx(&details.MsgTx) - recvCat := RecvCategory(details, syncHeight).String() + recvCat := RecvCategory(details, syncHeight, net).String() send := len(details.Debits) != 0 @@ -1340,7 +1341,7 @@ func (w *Wallet) ListUnspent(minconf, maxconf int32, // Only mature coinbase outputs are included. if output.FromCoinBase { - const target = blockchain.CoinbaseMaturity + target := int32(w.chainParams.CoinbaseMaturity) if !confirmed(target, output.Height, syncBlock.Height) { continue } @@ -2132,7 +2133,7 @@ func Open(db walletdb.DB, pubPass []byte, cbs *waddrmgr.OpenCallbacks, params *c return nil, err } } - txMgr, err := wtxmgr.Open(txMgrNS) + txMgr, err := wtxmgr.Open(txMgrNS, params) if err != nil { return nil, err } diff --git a/wtxmgr/example_test.go b/wtxmgr/example_test.go index a20266d..34016af 100644 --- a/wtxmgr/example_test.go +++ b/wtxmgr/example_test.go @@ -7,6 +7,7 @@ package wtxmgr_test import ( "fmt" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcwallet/wtxmgr" @@ -161,7 +162,7 @@ func Example_basicUsage() { fmt.Println(err) return } - s, err := wtxmgr.Open(ns) + s, err := wtxmgr.Open(ns, &chaincfg.TestNet3Params) if err != nil { fmt.Println(err) return diff --git a/wtxmgr/tx.go b/wtxmgr/tx.go index 458ebc5..940bba3 100644 --- a/wtxmgr/tx.go +++ b/wtxmgr/tx.go @@ -9,6 +9,7 @@ import ( "time" "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" @@ -130,7 +131,8 @@ type Credit struct { // Store implements a transaction store for storing and managing wallet // transactions. type Store struct { - namespace walletdb.Namespace + namespace walletdb.Namespace + chainParams *chaincfg.Params // Event callbacks. These execute in the same goroutine as the wtxmgr // caller. @@ -140,13 +142,13 @@ type Store struct { // Open opens the wallet transaction store from a walletdb namespace. If the // store does not exist, ErrNoExist is returned. Existing stores will be // 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. err := openStore(namespace) if err != nil { 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. @@ -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 // minConf confirmations and any (unspent) immature coinbase credit. + coinbaseMaturity := int32(s.chainParams.CoinbaseMaturity) stopConf := minConf - if blockchain.CoinbaseMaturity > stopConf { - stopConf = blockchain.CoinbaseMaturity + if coinbaseMaturity > stopConf { + stopConf = coinbaseMaturity } lastHeight := syncHeight - stopConf blockIt := makeReverseBlockIterator(ns) @@ -898,7 +901,7 @@ func (s *Store) balance(ns walletdb.Bucket, minConf int32, syncHeight int32) (bt } confs := syncHeight - block.Height + 1 if confs < minConf || (blockchain.IsCoinBaseTx(&rec.MsgTx) && - confs < blockchain.CoinbaseMaturity) { + confs < coinbaseMaturity) { bal -= amt } } diff --git a/wtxmgr/tx_test.go b/wtxmgr/tx_test.go index 3a2af3d..ef23897 100644 --- a/wtxmgr/tx_test.go +++ b/wtxmgr/tx_test.go @@ -13,7 +13,7 @@ import ( "testing" "time" - "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" @@ -80,7 +80,7 @@ func testStore() (*Store, func(), error) { if err != nil { return nil, teardown, err } - s, err := Open(ns) + s, err := Open(ns, &chaincfg.TestNet3Params) return s, teardown, err } @@ -665,6 +665,8 @@ func TestCoinbases(t *testing.T) { t.Fatal(err) } + coinbaseMaturity := int32(chaincfg.TestNet3Params.CoinbaseMaturity) + // Balance should be 0 if the coinbase is immature, 50 BTC at and beyond // maturity. // @@ -679,67 +681,67 @@ func TestCoinbases(t *testing.T) { balTests := []balTest{ // Next block it is still immature { - height: b100.Height + blockchain.CoinbaseMaturity - 2, + height: b100.Height + coinbaseMaturity - 2, minConf: 0, bal: 0, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 2, - minConf: blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity - 2, + minConf: coinbaseMaturity, bal: 0, }, // Next block it matures { - height: b100.Height + blockchain.CoinbaseMaturity - 1, + height: b100.Height + coinbaseMaturity - 1, minConf: 0, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 1, + height: b100.Height + coinbaseMaturity - 1, minConf: 1, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 1, - minConf: blockchain.CoinbaseMaturity - 1, + height: b100.Height + coinbaseMaturity - 1, + minConf: coinbaseMaturity - 1, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 1, - minConf: blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity - 1, + minConf: coinbaseMaturity, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 1, - minConf: blockchain.CoinbaseMaturity + 1, + height: b100.Height + coinbaseMaturity - 1, + minConf: coinbaseMaturity + 1, bal: 0, }, // Matures at this block { - height: b100.Height + blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity, minConf: 0, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity, minConf: 1, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, - minConf: blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity, + minConf: coinbaseMaturity, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, - minConf: blockchain.CoinbaseMaturity + 1, + height: b100.Height + coinbaseMaturity, + minConf: coinbaseMaturity + 1, bal: 50e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, - minConf: blockchain.CoinbaseMaturity + 2, + height: b100.Height + coinbaseMaturity, + minConf: coinbaseMaturity + 2, bal: 0, }, } @@ -776,50 +778,50 @@ func TestCoinbases(t *testing.T) { balTests = []balTest{ // Next block it matures { - height: b100.Height + blockchain.CoinbaseMaturity - 1, + height: b100.Height + coinbaseMaturity - 1, minConf: 0, bal: 35e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 1, + height: b100.Height + coinbaseMaturity - 1, minConf: 1, bal: 30e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 1, - minConf: blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity - 1, + minConf: coinbaseMaturity, bal: 30e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity - 1, - minConf: blockchain.CoinbaseMaturity + 1, + height: b100.Height + coinbaseMaturity - 1, + minConf: coinbaseMaturity + 1, bal: 0, }, // Matures at this block { - height: b100.Height + blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity, minConf: 0, bal: 35e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity, minConf: 1, bal: 30e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, - minConf: blockchain.CoinbaseMaturity, + height: b100.Height + coinbaseMaturity, + minConf: coinbaseMaturity, bal: 30e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, - minConf: blockchain.CoinbaseMaturity + 1, + height: b100.Height + coinbaseMaturity, + minConf: coinbaseMaturity + 1, bal: 30e8, }, { - height: b100.Height + blockchain.CoinbaseMaturity, - minConf: blockchain.CoinbaseMaturity + 2, + height: b100.Height + coinbaseMaturity, + minConf: coinbaseMaturity + 2, bal: 0, }, } @@ -839,7 +841,7 @@ func TestCoinbases(t *testing.T) { // Mine the spending transaction in the block the coinbase matures. bMaturity := BlockMeta{ - Block: Block{Height: b100.Height + blockchain.CoinbaseMaturity}, + Block: Block{Height: b100.Height + coinbaseMaturity}, Time: time.Now(), } err = s.InsertTx(spenderARec, &bMaturity) @@ -866,17 +868,17 @@ func TestCoinbases(t *testing.T) { }, { height: bMaturity.Height, - minConf: blockchain.CoinbaseMaturity, + minConf: coinbaseMaturity, bal: 30e8, }, { height: bMaturity.Height, - minConf: blockchain.CoinbaseMaturity + 1, + minConf: coinbaseMaturity + 1, bal: 30e8, }, { height: bMaturity.Height, - minConf: blockchain.CoinbaseMaturity + 2, + minConf: coinbaseMaturity + 2, bal: 0, }, @@ -898,12 +900,12 @@ func TestCoinbases(t *testing.T) { }, { height: bMaturity.Height + 1, - minConf: blockchain.CoinbaseMaturity + 2, + minConf: coinbaseMaturity + 2, bal: 30e8, }, { height: bMaturity.Height + 1, - minConf: blockchain.CoinbaseMaturity + 3, + minConf: coinbaseMaturity + 3, bal: 0, }, } @@ -1101,9 +1103,11 @@ func TestMoveMultipleToSameBlock(t *testing.T) { t.Fatal(err) } + coinbaseMaturity := int32(chaincfg.TestNet3Params.CoinbaseMaturity) + // Mine both transactions in the block that matures the coinbase. bMaturity := BlockMeta{ - Block: Block{Height: b100.Height + blockchain.CoinbaseMaturity}, + Block: Block{Height: b100.Height + coinbaseMaturity}, Time: time.Now(), } err = s.InsertTx(spenderARec, &bMaturity)