_miningaddresses.go raw

   1  package mining
   2  
   3  import (
   4  	"github.com/p9c/p9/cmd/node/active"
   5  	"github.com/p9c/p9/pkg/btcaddr"
   6  	wm "github.com/p9c/p9/pkg/waddrmgr"
   7  	"github.com/p9c/p9/cmd/wallet"
   8  	"github.com/p9c/p9/pod/config"
   9  )
  10  
  11  // RefillMiningAddresses adds new addresses to the mining address pool for the miner
  12  // todo: make this remove ones that have been used or received a payment or mined
  13  func RefillMiningAddresses(w *wallet.Wallet, cfg *config.Config, stateCfg *active.Config) {
  14  	if w == nil {
  15  		D.Ln("trying to refill without a wallet")
  16  		return
  17  	}
  18  	if cfg == nil {
  19  		D.Ln("config is empty")
  20  		return
  21  	}
  22  	miningAddressLen := len(cfg.MiningAddrs.S())
  23  	toMake := 99 - miningAddressLen
  24  	if miningAddressLen >= 99 {
  25  		toMake = 0
  26  	}
  27  	if toMake < 1 {
  28  		D.Ln("not making any new addresses")
  29  		return
  30  	}
  31  	D.Ln("refilling mining addresses")
  32  	account, e := w.AccountNumber(
  33  		wm.KeyScopeBIP0044,
  34  		"default",
  35  	)
  36  	if e != nil {
  37  		E.Ln("error getting account number ", e)
  38  	}
  39  	for i := 0; i < toMake; i++ {
  40  		var addr btcaddr.Address
  41  		addr, e = w.NewAddress(
  42  			account, wm.KeyScopeBIP0044,
  43  			true,
  44  		)
  45  		if e == nil {
  46  			// add them to the configuration to be saved
  47  			cfg.MiningAddrs.Set(append(cfg.MiningAddrs.S(), addr.EncodeAddress()))
  48  			// add them to the active mining address list so they
  49  			// are ready to use
  50  			stateCfg.ActiveMiningAddrs = append(stateCfg.ActiveMiningAddrs, addr)
  51  		} else {
  52  			E.Ln("error adding new address ", e)
  53  		}
  54  	}
  55  	if podcfg.Save(cfg) {
  56  		D.Ln("saved config with new addresses")
  57  	} else {
  58  		E.Ln("error adding new addresses", e)
  59  	}
  60  }
  61