Move fee increment to Account structure.

When a BIP0032 wallet is implemented and multiple address chains can
be supported by a single keystore, the Account structure will
represent a single wallet (and be renamed to reflect that change),
rather than keeping the collection of Account structs as currently
managed by the AccountManager.  In preperation for this, and to remove
a global variable, move the fee increment for created transactions to
this structure.  When setting the fee, look it up from the default
account.
This commit is contained in:
Josh Rickmar 2014-07-07 09:35:54 -05:00
parent 0abe6e32bf
commit 2d9fb71afd
4 changed files with 39 additions and 43 deletions

View file

@ -38,6 +38,17 @@ type Account struct {
*wallet.Wallet
TxStore *txstore.Store
lockedOutpoints map[btcwire.OutPoint]struct{}
FeeIncrement btcutil.Amount
}
func newAccount(name string, w *wallet.Wallet, txs *txstore.Store) *Account {
return &Account{
name: name,
Wallet: w,
TxStore: txs,
lockedOutpoints: map[btcwire.OutPoint]struct{}{},
FeeIncrement: defaultFeeIncrement,
}
}
// Lock locks the underlying wallet for an account.

View file

@ -221,12 +221,7 @@ func openSavedAccount(name string, cfg *config) (*Account, error) {
wlt := new(wallet.Wallet)
txs := txstore.New()
a := &Account{
name: name,
Wallet: wlt,
TxStore: txs,
lockedOutpoints: map[btcwire.OutPoint]struct{}{},
}
a := newAccount(name, wlt, txs)
walletPath := accountFilename("wallet.bin", name, netdir)
txstorePath := accountFilename("tx.bin", name, netdir)
@ -839,11 +834,7 @@ func (am *AccountManager) CreateEncryptedWallet(passphrase []byte) error {
// Create new account and begin managing with the global account
// manager. Registering will fail if the new account can not be
// written immediately to disk.
a := &Account{
Wallet: wlt,
TxStore: txstore.New(),
lockedOutpoints: map[btcwire.OutPoint]struct{}{},
}
a := newAccount("", wlt, txstore.New())
if err := am.RegisterNewAccount(a); err != nil {
return err
}

View file

@ -22,7 +22,6 @@ import (
"fmt"
badrand "math/rand"
"sort"
"sync"
"time"
"github.com/conformal/btcchain"
@ -61,19 +60,9 @@ var ErrNonPositiveAmount = errors.New("amount is not positive")
// negative.
var ErrNegativeFee = errors.New("fee is negative")
// minTxFee is the default minimum transation fee (0.0001 BTC,
// defaultFeeIncrement is the default minimum transation fee (0.0001 BTC,
// measured in satoshis) added to transactions requiring a fee.
const minTxFee = 10000
// TxFeeIncrement represents the global transaction fee per KB of Tx
// added to newly-created transactions and sent as a reward to the block
// miner. i is measured in satoshis.
var TxFeeIncrement = struct {
sync.Mutex
i btcutil.Amount
}{
i: minTxFee,
}
const defaultFeeIncrement = 10000
type CreatedTx struct {
tx *btcutil.Tx
@ -305,7 +294,7 @@ func (a *Account) txToPairs(pairs map[string]btcutil.Amount,
if !cfg.DisallowFree {
noFeeAllowed = allowFree(bs.Height, inputs, msgtx.SerializeSize())
}
if minFee := minimumFee(msgtx, noFeeAllowed); fee < minFee {
if minFee := minimumFee(a.FeeIncrement, msgtx, noFeeAllowed); fee < minFee {
fee = minFee
} else {
selectedInputs = inputs
@ -351,11 +340,8 @@ func (a *Account) txToPairs(pairs map[string]btcutil.Amount,
// and none of the outputs contain a value less than 1 bitcent.
// Otherwise, the fee will be calculated using TxFeeIncrement,
// incrementing the fee for each kilobyte of transaction.
func minimumFee(tx *btcwire.MsgTx, allowFree bool) btcutil.Amount {
func minimumFee(incr btcutil.Amount, tx *btcwire.MsgTx, allowFree bool) btcutil.Amount {
txLen := tx.SerializeSize()
TxFeeIncrement.Lock()
incr := TxFeeIncrement.i
TxFeeIncrement.Unlock()
fee := btcutil.Amount(int64(1+txLen/1000) * int64(incr))
if allowFree && txLen < 1000 {

View file

@ -1334,22 +1334,26 @@ func GetInfo(icmd btcjson.Cmd) (interface{}, error) {
return nil, err
}
var balance btcutil.Amount
accounts, err := AcctMgr.ListAccounts(1)
if err != nil {
return nil, err
}
for _, v := range accounts {
balance += v
var balance, feeIncr btcutil.Amount
accounts := AcctMgr.AllAccounts()
for _, a := range accounts {
bal, err := a.CalculateBalance(1)
if err == nil {
balance += bal
}
// For now we assume all transactions can only be created
// with the default account (as this is the only account
// that's usable), so use it for the fee increment.
if a.name == "" {
feeIncr = a.FeeIncrement
}
}
info.WalletVersion = int32(wallet.VersCurrent.Uint32())
info.Balance = balance.ToUnit(btcutil.AmountBTC)
// Keypool times are not tracked. set to current time.
info.KeypoolOldest = time.Now().Unix()
info.KeypoolSize = int32(cfg.KeypoolSize)
TxFeeIncrement.Lock()
info.PaytxFee = float64(TxFeeIncrement.i) / btcutil.SatoshiPerBitcoin
TxFeeIncrement.Unlock()
info.PaytxFee = feeIncr.ToUnit(btcutil.AmountBTC)
// We don't set the following since they don't make much sense in the
// wallet architecture:
// - unlocked_until
@ -2306,10 +2310,14 @@ func SetTxFee(icmd btcjson.Cmd) (interface{}, error) {
return nil, ErrNeedPositiveAmount
}
// Set global tx fee.
TxFeeIncrement.Lock()
TxFeeIncrement.i = btcutil.Amount(cmd.Amount)
TxFeeIncrement.Unlock()
// Lookup default account (which realistically is the only account
// that transactions can be made with at the moment) and set its
// fee increment field.
a, err := AcctMgr.Account("")
if err != nil {
return nil, ErrNoAccounts
}
a.FeeIncrement = btcutil.Amount(cmd.Amount)
// A boolean true result is returned upon success.
return true, nil