gitlab.com/jokerrs1/Sia@v1.3.2/modules/host/negotiatesettings.go (about)

     1  package host
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  
     7  	"github.com/NebulousLabs/Sia/build"
     8  	"github.com/NebulousLabs/Sia/crypto"
     9  	"github.com/NebulousLabs/Sia/modules"
    10  )
    11  
    12  // capacity returns the amount of storage still available on the machine. The
    13  // amount can be negative if the total capacity was reduced to below the active
    14  // capacity.
    15  func (h *Host) capacity() (total, remaining uint64) {
    16  	// Total storage can be computed by summing the size of all the storage
    17  	// folders.
    18  	sfs := h.StorageFolders()
    19  	for _, sf := range sfs {
    20  		total += sf.Capacity
    21  		remaining += sf.CapacityRemaining
    22  	}
    23  	return total, remaining
    24  }
    25  
    26  // externalSettings compiles and returns the external settings for the host.
    27  func (h *Host) externalSettings() modules.HostExternalSettings {
    28  	// Increment the revision number for the external settings
    29  	h.revisionNumber++
    30  
    31  	totalStorage, remainingStorage := h.capacity()
    32  	var netAddr modules.NetAddress
    33  	if h.settings.NetAddress != "" {
    34  		netAddr = h.settings.NetAddress
    35  	} else {
    36  		netAddr = h.autoAddress
    37  	}
    38  
    39  	// Calculate contract price
    40  	_, maxFee := h.tpool.FeeEstimation()
    41  	contractPrice := maxFee.Mul64(10e3) // estimated size of txns host needs to fund
    42  	if contractPrice.Cmp(h.settings.MinContractPrice) < 0 {
    43  		contractPrice = h.settings.MinContractPrice
    44  	}
    45  
    46  	return modules.HostExternalSettings{
    47  		AcceptingContracts:   h.settings.AcceptingContracts,
    48  		MaxDownloadBatchSize: h.settings.MaxDownloadBatchSize,
    49  		MaxDuration:          h.settings.MaxDuration,
    50  		MaxReviseBatchSize:   h.settings.MaxReviseBatchSize,
    51  		NetAddress:           netAddr,
    52  		RemainingStorage:     remainingStorage,
    53  		SectorSize:           modules.SectorSize,
    54  		TotalStorage:         totalStorage,
    55  		UnlockHash:           h.unlockHash,
    56  		WindowSize:           h.settings.WindowSize,
    57  
    58  		Collateral:    h.settings.Collateral,
    59  		MaxCollateral: h.settings.MaxCollateral,
    60  
    61  		ContractPrice:          contractPrice,
    62  		DownloadBandwidthPrice: h.settings.MinDownloadBandwidthPrice,
    63  		StoragePrice:           h.settings.MinStoragePrice,
    64  		UploadBandwidthPrice:   h.settings.MinUploadBandwidthPrice,
    65  
    66  		RevisionNumber: h.revisionNumber,
    67  		Version:        build.Version,
    68  	}
    69  }
    70  
    71  // managedRPCSettings is an rpc that returns the host's settings.
    72  func (h *Host) managedRPCSettings(conn net.Conn) error {
    73  	// Set the negotiation deadline.
    74  	conn.SetDeadline(time.Now().Add(modules.NegotiateSettingsTime))
    75  
    76  	// The revision number is updated so that the renter can be certain that
    77  	// they have the most recent copy of the settings. The revision number and
    78  	// signature can be compared against other settings objects that the renter
    79  	// may have, and if the new revision number is not higher the renter can
    80  	// suspect foul play. Largely, the revision number is in place to enable
    81  	// renters to share host settings with each other, a feature that has not
    82  	// yet been implemented.
    83  	//
    84  	// While updating the revision number, also grab the secret key and
    85  	// external settings.
    86  	var hes modules.HostExternalSettings
    87  	var secretKey crypto.SecretKey
    88  	h.mu.Lock()
    89  	secretKey = h.secretKey
    90  	hes = h.externalSettings()
    91  	h.mu.Unlock()
    92  
    93  	// Write the settings to the renter. If the write fails, return a
    94  	// connection error.
    95  	err := crypto.WriteSignedObject(conn, hes, secretKey)
    96  	if err != nil {
    97  		return ErrorConnection("failed WriteSignedObject during RPCSettings: " + err.Error())
    98  	}
    99  	return nil
   100  }