github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/host/announce.go (about)

     1  package host
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  )
     8  
     9  var (
    10  	// errAnnWalletLocked is returned during a host announcement if the wallet
    11  	// is locked.
    12  	errAnnWalletLocked = errors.New("cannot announce the host while the wallet is locked")
    13  
    14  	// errUnknownAddress is returned if the host is unable to determine a
    15  	// public address for itself to use in the announcement.
    16  	errUnknownAddress = errors.New("host cannot announce, does not seem to have a valid address.")
    17  )
    18  
    19  // announce creates an announcement transaction and submits it to the network.
    20  func (h *Host) announce(addr modules.NetAddress) error {
    21  	// The wallet needs to be unlocked to add fees to the transaction, and the
    22  	// host needs to have an active unlock hash that renters can make payment
    23  	// to.
    24  	if !h.wallet.Unlocked() {
    25  		return errAnnWalletLocked
    26  	}
    27  	err := h.checkUnlockHash()
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	// Create the announcement that's going to be added to the arbitrary data
    33  	// field of the transaction.
    34  	signedAnnouncement, err := modules.CreateAnnouncement(addr, h.publicKey, h.secretKey)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	// Create a transaction, with a fee, that contains the full announcement.
    40  	txnBuilder := h.wallet.StartTransaction()
    41  	_, fee := h.tpool.FeeEstimation()
    42  	err = txnBuilder.FundSiacoins(fee)
    43  	if err != nil {
    44  		txnBuilder.Drop()
    45  		return err
    46  	}
    47  	_ = txnBuilder.AddMinerFee(fee)
    48  	_ = txnBuilder.AddArbitraryData(signedAnnouncement)
    49  	txnSet, err := txnBuilder.Sign(true)
    50  	if err != nil {
    51  		txnBuilder.Drop()
    52  		return err
    53  	}
    54  
    55  	// Add the transactions to the transaction pool.
    56  	err = h.tpool.AcceptTransactionSet(txnSet)
    57  	if err != nil {
    58  		txnBuilder.Drop()
    59  		return err
    60  	}
    61  	h.announced = true
    62  	h.log.Printf("INFO: Successfully announced as %v", addr)
    63  	return nil
    64  }
    65  
    66  // Announce creates a host announcement transaction, adding information to the
    67  // arbitrary data, signing the transaction, and submitting it to the
    68  // transaction pool.
    69  func (h *Host) Announce() error {
    70  	h.mu.Lock()
    71  	defer h.mu.Unlock()
    72  	h.resourceLock.RLock()
    73  	defer h.resourceLock.RUnlock()
    74  	if h.closed {
    75  		return errHostClosed
    76  	}
    77  
    78  	// Determine whether to use the settings.NetAddress or autoAddress.
    79  	if h.settings.NetAddress != "" {
    80  		return h.announce(h.settings.NetAddress)
    81  	}
    82  	if h.autoAddress == "" {
    83  		return errUnknownAddress
    84  	}
    85  	return h.announce(h.autoAddress)
    86  }
    87  
    88  // AnnounceAddress submits a host announcement to the blockchain to announce a
    89  // specific address. No checks for validity are performed on the address.
    90  func (h *Host) AnnounceAddress(addr modules.NetAddress) error {
    91  	h.mu.Lock()
    92  	defer h.mu.Unlock()
    93  	h.resourceLock.RLock()
    94  	defer h.resourceLock.RUnlock()
    95  	if h.closed {
    96  		return errHostClosed
    97  	}
    98  
    99  	return h.announce(addr)
   100  }