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 *wallet.Wallet
TxStore *txstore.Store TxStore *txstore.Store
lockedOutpoints map[btcwire.OutPoint]struct{} 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. // 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) wlt := new(wallet.Wallet)
txs := txstore.New() txs := txstore.New()
a := &Account{ a := newAccount(name, wlt, txs)
name: name,
Wallet: wlt,
TxStore: txs,
lockedOutpoints: map[btcwire.OutPoint]struct{}{},
}
walletPath := accountFilename("wallet.bin", name, netdir) walletPath := accountFilename("wallet.bin", name, netdir)
txstorePath := accountFilename("tx.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 // Create new account and begin managing with the global account
// manager. Registering will fail if the new account can not be // manager. Registering will fail if the new account can not be
// written immediately to disk. // written immediately to disk.
a := &Account{ a := newAccount("", wlt, txstore.New())
Wallet: wlt,
TxStore: txstore.New(),
lockedOutpoints: map[btcwire.OutPoint]struct{}{},
}
if err := am.RegisterNewAccount(a); err != nil { if err := am.RegisterNewAccount(a); err != nil {
return err return err
} }

View file

@ -22,7 +22,6 @@ import (
"fmt" "fmt"
badrand "math/rand" badrand "math/rand"
"sort" "sort"
"sync"
"time" "time"
"github.com/conformal/btcchain" "github.com/conformal/btcchain"
@ -61,19 +60,9 @@ var ErrNonPositiveAmount = errors.New("amount is not positive")
// negative. // negative.
var ErrNegativeFee = errors.New("fee is 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. // measured in satoshis) added to transactions requiring a fee.
const minTxFee = 10000 const defaultFeeIncrement = 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,
}
type CreatedTx struct { type CreatedTx struct {
tx *btcutil.Tx tx *btcutil.Tx
@ -305,7 +294,7 @@ func (a *Account) txToPairs(pairs map[string]btcutil.Amount,
if !cfg.DisallowFree { if !cfg.DisallowFree {
noFeeAllowed = allowFree(bs.Height, inputs, msgtx.SerializeSize()) 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 fee = minFee
} else { } else {
selectedInputs = inputs 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. // and none of the outputs contain a value less than 1 bitcent.
// Otherwise, the fee will be calculated using TxFeeIncrement, // Otherwise, the fee will be calculated using TxFeeIncrement,
// incrementing the fee for each kilobyte of transaction. // 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() txLen := tx.SerializeSize()
TxFeeIncrement.Lock()
incr := TxFeeIncrement.i
TxFeeIncrement.Unlock()
fee := btcutil.Amount(int64(1+txLen/1000) * int64(incr)) fee := btcutil.Amount(int64(1+txLen/1000) * int64(incr))
if allowFree && txLen < 1000 { if allowFree && txLen < 1000 {

View file

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