diff --git a/account.go b/account.go index 90cb1e2..f35f34b 100644 --- a/account.go +++ b/account.go @@ -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. diff --git a/acctmgr.go b/acctmgr.go index 853a42b..58334f3 100644 --- a/acctmgr.go +++ b/acctmgr.go @@ -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 } diff --git a/createtx.go b/createtx.go index 3bb615b..e80f6f6 100644 --- a/createtx.go +++ b/createtx.go @@ -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 { diff --git a/rpcserver.go b/rpcserver.go index 39de197..af1a9a3 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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