Skip to content

Commit

Permalink
Updated waddrmgr to manage account names
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed Mar 17, 2015
1 parent 85fe722 commit fbd7aea
Show file tree
Hide file tree
Showing 10 changed files with 1,846 additions and 279 deletions.
38 changes: 14 additions & 24 deletions createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (u ByAmount) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
// to addr or as a fee for the miner are sent to a newly generated
// address. InsufficientFundsError is returned if there are not enough
// eligible unspent outputs to create the transaction.
func (w *Wallet) txToPairs(pairs map[string]btcutil.Amount, minconf int) (*CreatedTx, error) {
func (w *Wallet) txToPairs(pairs map[string]btcutil.Amount, account uint32, minconf int) (*CreatedTx, error) {

// Address manager must be unlocked to compose transaction. Grab
// the unlock if possible (to prevent future unlocks), or return the
Expand All @@ -145,12 +145,12 @@ func (w *Wallet) txToPairs(pairs map[string]btcutil.Amount, minconf int) (*Creat
return nil, err
}

eligible, err := w.findEligibleOutputs(minconf, bs)
eligible, err := w.findEligibleOutputs(account, minconf, bs)
if err != nil {
return nil, err
}

return createTx(eligible, pairs, bs, w.FeeIncrement, w.Manager, w.changeAddress)
return createTx(eligible, pairs, bs, w.FeeIncrement, w.Manager, account, w.NewChangeAddress)
}

// createTx selects inputs (from the given slice of eligible utxos)
Expand All @@ -164,7 +164,8 @@ func createTx(
bs *waddrmgr.BlockStamp,
feeIncrement btcutil.Amount,
mgr *waddrmgr.Manager,
changeAddress func(*waddrmgr.BlockStamp) (btcutil.Address, error)) (
account uint32,
changeAddress func(account uint32) (btcutil.Address, error)) (
*CreatedTx, error) {

msgtx := wire.NewMsgTx()
Expand Down Expand Up @@ -220,7 +221,7 @@ func createTx(
change := totalAdded - minAmount - feeEst
if change > 0 {
if changeAddr == nil {
changeAddr, err = changeAddress(bs)
changeAddr, err = changeAddress(account)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -293,23 +294,6 @@ func addChange(msgtx *wire.MsgTx, change btcutil.Amount, changeAddr btcutil.Addr
return int(r), nil
}

// changeAddress obtains a new btcutil.Address to be used as a change
// transaction output. It will also mark the KeyStore as dirty and
// tells chainSvr to watch that address.
func (w *Wallet) changeAddress(bs *waddrmgr.BlockStamp) (btcutil.Address, error) {
changeAddrs, err := w.Manager.NextInternalAddresses(0, 1)
if err != nil {
return nil, fmt.Errorf("failed to get change address: %s", err)
}
changeAddr := changeAddrs[0].Address()
err = w.chainSvr.NotifyReceived([]btcutil.Address{changeAddr})
if err != nil {
return nil, fmt.Errorf("cannot request updates for "+
"change address: %v", err)
}
return changeAddr, nil
}

// addOutputs adds the given address/amount pairs as outputs to msgtx,
// returning their total amount.
func addOutputs(msgtx *wire.MsgTx, pairs map[string]btcutil.Amount) (btcutil.Amount, error) {
Expand All @@ -335,7 +319,7 @@ func addOutputs(msgtx *wire.MsgTx, pairs map[string]btcutil.Amount) (btcutil.Amo
return minAmount, nil
}

func (w *Wallet) findEligibleOutputs(minconf int, bs *waddrmgr.BlockStamp) ([]txstore.Credit, error) {
func (w *Wallet) findEligibleOutputs(account uint32, minconf int, bs *waddrmgr.BlockStamp) ([]txstore.Credit, error) {
unspent, err := w.TxStore.UnspentOutputs()
if err != nil {
return nil, err
Expand Down Expand Up @@ -365,7 +349,13 @@ func (w *Wallet) findEligibleOutputs(minconf int, bs *waddrmgr.BlockStamp) ([]tx
continue
}

eligible = append(eligible, unspent[i])
creditAccount, err := w.CreditAccount(unspent[i])
if err != nil {
continue
}
if creditAccount == account {
eligible = append(eligible, unspent[i])
}
}
}
return eligible, nil
Expand Down
10 changes: 6 additions & 4 deletions createtx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,17 @@ func TestCreateTx(t *testing.T) {
cfg = &config{DisallowFree: false}
bs := &waddrmgr.BlockStamp{Height: 11111}
mgr := newManager(t, txInfo.privKeys, bs)
account := uint32(0)
changeAddr, _ := btcutil.DecodeAddress("muqW4gcixv58tVbSKRC5q6CRKy8RmyLgZ5", activeNet.Params)
var tstChangeAddress = func(bs *waddrmgr.BlockStamp) (btcutil.Address, error) {
var tstChangeAddress = func(account uint32) (btcutil.Address, error) {
return changeAddr, nil
}

// Pick all utxos from txInfo as eligible input.
eligible := eligibleInputsFromTx(t, txInfo.hex, []uint32{1, 2, 3, 4, 5})
// Now create a new TX sending 25e6 satoshis to the following addresses:
outputs := map[string]btcutil.Amount{outAddr1: 15e6, outAddr2: 10e6}
tx, err := createTx(eligible, outputs, bs, defaultFeeIncrement, mgr, tstChangeAddress)
tx, err := createTx(eligible, outputs, bs, defaultFeeIncrement, mgr, account, tstChangeAddress)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -124,12 +125,13 @@ func TestCreateTxInsufficientFundsError(t *testing.T) {
outputs := map[string]btcutil.Amount{outAddr1: 10, outAddr2: 1e9}
eligible := eligibleInputsFromTx(t, txInfo.hex, []uint32{1})
bs := &waddrmgr.BlockStamp{Height: 11111}
account := uint32(0)
changeAddr, _ := btcutil.DecodeAddress("muqW4gcixv58tVbSKRC5q6CRKy8RmyLgZ5", activeNet.Params)
var tstChangeAddress = func(bs *waddrmgr.BlockStamp) (btcutil.Address, error) {
var tstChangeAddress = func(account uint32) (btcutil.Address, error) {
return changeAddr, nil
}

_, err := createTx(eligible, outputs, bs, defaultFeeIncrement, nil, tstChangeAddress)
_, err := createTx(eligible, outputs, bs, defaultFeeIncrement, nil, account, tstChangeAddress)

if err == nil {
t.Error("Expected InsufficientFundsError, got no error")
Expand Down
Loading

0 comments on commit fbd7aea

Please sign in to comment.