Make maximum keypool size a config option.

This commit is contained in:
Josh Rickmar 2014-01-15 17:29:01 -05:00
parent 87b6335cc2
commit f0c649b7ac
6 changed files with 23 additions and 16 deletions

View file

@ -548,7 +548,7 @@ func (a *Account) NewAddress() (btcutil.Address, error) {
} }
// Get next address from wallet. // Get next address from wallet.
addr, err := a.NextChainedAddress(&bs) addr, err := a.NextChainedAddress(&bs, cfg.KeypoolSize)
if err != nil { if err != nil {
a.mtx.Unlock() a.mtx.Unlock()
return nil, err return nil, err

View file

@ -189,7 +189,7 @@ func (store *AccountStore) CreateEncryptedWallet(name, desc string, passphrase [
} }
// Create new wallet in memory. // Create new wallet in memory.
wlt, err := wallet.NewWallet(name, desc, passphrase, cfg.Net(), &bs) wlt, err := wallet.NewWallet(name, desc, passphrase, cfg.Net(), &bs, cfg.KeypoolSize)
if err != nil { if err != nil {
return err return err
} }

View file

@ -32,6 +32,7 @@ const (
defaultConfigFilename = "btcwallet.conf" defaultConfigFilename = "btcwallet.conf"
defaultBtcNet = btcwire.TestNet3 defaultBtcNet = btcwire.TestNet3
defaultLogLevel = "info" defaultLogLevel = "info"
defaultKeypoolSize = 100
) )
var ( var (
@ -58,6 +59,7 @@ type config struct {
RPCCert string `long:"rpccert" description:"File containing the certificate file"` RPCCert string `long:"rpccert" description:"File containing the certificate file"`
RPCKey string `long:"rpckey" description:"File containing the certificate key"` RPCKey string `long:"rpckey" description:"File containing the certificate key"`
MainNet bool `long:"mainnet" description:"Use the main Bitcoin network (default testnet3)"` MainNet bool `long:"mainnet" description:"Use the main Bitcoin network (default testnet3)"`
KeypoolSize uint `short:"k" long:"keypoolsize" description:"Maximum number of addresses in keypool"`
Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"` Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
ProxyUser string `long:"proxyuser" description:"Username for proxy server"` ProxyUser string `long:"proxyuser" description:"Username for proxy server"`
ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"` ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
@ -137,11 +139,12 @@ func normalizeAddress(addr, defaultPort string) string {
func loadConfig() (*config, []string, error) { func loadConfig() (*config, []string, error) {
// Default config. // Default config.
cfg := config{ cfg := config{
DebugLevel: defaultLogLevel, DebugLevel: defaultLogLevel,
ConfigFile: defaultConfigFile, ConfigFile: defaultConfigFile,
DataDir: defaultDataDir, DataDir: defaultDataDir,
RPCKey: defaultRPCKeyFile, RPCKey: defaultRPCKeyFile,
RPCCert: defaultRPCCertFile, RPCCert: defaultRPCCertFile,
KeypoolSize: defaultKeypoolSize,
} }
// A config file in the current directory takes precedence. // A config file in the current directory takes precedence.

View file

@ -245,7 +245,7 @@ func (a *Account) txToPairs(pairs map[string]int64, minconf int) (*CreatedTx, er
// Get a new change address if one has not already been found. // Get a new change address if one has not already been found.
if changeAddr == nil { if changeAddr == nil {
changeAddr, err = a.NextChainedAddress(&bs) changeAddr, err = a.NextChainedAddress(&bs, cfg.KeypoolSize)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get next address: %s", err) return nil, fmt.Errorf("failed to get next address: %s", err)
} }

View file

@ -12,6 +12,9 @@
; directory for mainnet and testnet wallets, respectively. ; directory for mainnet and testnet wallets, respectively.
; datadir=~/.btcwallet ; datadir=~/.btcwallet
; Maximum number of addresses to generate for the keypool
; keypoolsize=100
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------

View file

@ -46,9 +46,6 @@ const (
// Maximum length in bytes of a comment that can have a size represented // Maximum length in bytes of a comment that can have a size represented
// as a uint16. // as a uint16.
maxCommentLen = (1 << 16) - 1 maxCommentLen = (1 << 16) - 1
// Number of addresses to extend keypool by.
nKeypoolIncrement = 100
) )
const ( const (
@ -455,7 +452,7 @@ type Wallet struct {
addrCommentMap map[btcutil.AddressPubKeyHash]comment addrCommentMap map[btcutil.AddressPubKeyHash]comment
txCommentMap map[transactionHashKey]comment txCommentMap map[transactionHashKey]comment
// These are not serialized. // The rest of the fields in this struct are not serialized.
secret struct { secret struct {
sync.Mutex sync.Mutex
key []byte key []byte
@ -469,7 +466,9 @@ type Wallet struct {
// desc's binary representation must not exceed 32 and 256 bytes, // desc's binary representation must not exceed 32 and 256 bytes,
// respectively. All address private keys are encrypted with passphrase. // respectively. All address private keys are encrypted with passphrase.
// The wallet is returned unlocked. // The wallet is returned unlocked.
func NewWallet(name, desc string, passphrase []byte, net btcwire.BitcoinNet, createdAt *BlockStamp) (*Wallet, error) { func NewWallet(name, desc string, passphrase []byte, net btcwire.BitcoinNet,
createdAt *BlockStamp, keypoolSize uint) (*Wallet, error) {
// Check sizes of inputs. // Check sizes of inputs.
if len([]byte(name)) > 32 { if len([]byte(name)) > 32 {
return nil, errors.New("name exceeds 32 byte maximum size") return nil, errors.New("name exceeds 32 byte maximum size")
@ -546,7 +545,7 @@ func NewWallet(name, desc string, passphrase []byte, net btcwire.BitcoinNet, cre
w.chainIdxMap[rootKeyChainIdx] = w.keyGenerator.address(net) w.chainIdxMap[rootKeyChainIdx] = w.keyGenerator.address(net)
// Fill keypool. // Fill keypool.
if err := w.extendKeypool(nKeypoolIncrement, aeskey, createdAt); err != nil { if err := w.extendKeypool(keypoolSize, aeskey, createdAt); err != nil {
return nil, err return nil, err
} }
@ -796,7 +795,9 @@ func (w *Wallet) Version() (string, int) {
// NextChainedAddress attempts to get the next chained address, // NextChainedAddress attempts to get the next chained address,
// refilling the keypool if necessary. // refilling the keypool if necessary.
func (w *Wallet) NextChainedAddress(bs *BlockStamp) (*btcutil.AddressPubKeyHash, error) { func (w *Wallet) NextChainedAddress(bs *BlockStamp,
keypoolSize uint) (*btcutil.AddressPubKeyHash, error) {
// Attempt to get address hash of next chained address. // Attempt to get address hash of next chained address.
next160, ok := w.chainIdxMap[w.highestUsed+1] next160, ok := w.chainIdxMap[w.highestUsed+1]
if !ok { if !ok {
@ -810,7 +811,7 @@ func (w *Wallet) NextChainedAddress(bs *BlockStamp) (*btcutil.AddressPubKeyHash,
copy(aeskey, w.secret.key) copy(aeskey, w.secret.key)
w.secret.Unlock() w.secret.Unlock()
err := w.extendKeypool(nKeypoolIncrement, aeskey, bs) err := w.extendKeypool(keypoolSize, aeskey, bs)
if err != nil { if err != nil {
return nil, err return nil, err
} }