github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/renter/contractor/contracts.go (about)

     1  package contractor
     2  
     3  import (
     4  	"SiaPrime/modules"
     5  	"SiaPrime/types"
     6  )
     7  
     8  // contractEndHeight returns the height at which the Contractor's contracts
     9  // end. If there are no contracts, it returns zero.
    10  func (c *Contractor) contractEndHeight() types.BlockHeight {
    11  	return c.currentPeriod + c.allowance.Period + c.allowance.RenewWindow
    12  }
    13  
    14  // managedCancelContract cancels a contract by setting its utility fields to
    15  // false and locking the utilities. The contract can still be used for
    16  // downloads after this but it won't be used for uploads or renewals.
    17  func (c *Contractor) managedCancelContract(cid types.FileContractID) error {
    18  	return c.managedUpdateContractUtility(cid, modules.ContractUtility{
    19  		GoodForRenew:  false,
    20  		GoodForUpload: false,
    21  		Locked:        true,
    22  	})
    23  }
    24  
    25  // managedContractUtility returns the ContractUtility for a contract with a given id.
    26  func (c *Contractor) managedContractUtility(id types.FileContractID) (modules.ContractUtility, bool) {
    27  	rc, exists := c.staticContracts.View(id)
    28  	if !exists {
    29  		return modules.ContractUtility{}, false
    30  	}
    31  	return rc.Utility, true
    32  }
    33  
    34  // ContractByPublicKey returns the contract with the key specified, if it
    35  // exists. The contract will be resolved if possible to the most recent child
    36  // contract.
    37  func (c *Contractor) ContractByPublicKey(pk types.SiaPublicKey) (modules.RenterContract, bool) {
    38  	c.mu.RLock()
    39  	id, ok := c.pubKeysToContractID[string(pk.Key)]
    40  	c.mu.RUnlock()
    41  	if !ok {
    42  		return modules.RenterContract{}, false
    43  	}
    44  	return c.staticContracts.View(id)
    45  }
    46  
    47  // CancelContract cancels the Contractor's contract by marking it !GoodForRenew
    48  // and !GoodForUpload
    49  func (c *Contractor) CancelContract(id types.FileContractID) error {
    50  	defer c.threadedContractMaintenance()
    51  	return c.managedCancelContract(id)
    52  }
    53  
    54  // Contracts returns the contracts formed by the contractor in the current
    55  // allowance period. Only contracts formed with currently online hosts are
    56  // returned.
    57  func (c *Contractor) Contracts() []modules.RenterContract {
    58  	return c.staticContracts.ViewAll()
    59  }
    60  
    61  // OldContracts returns the contracts formed by the contractor that have
    62  // expired
    63  func (c *Contractor) OldContracts() []modules.RenterContract {
    64  	c.mu.Lock()
    65  	defer c.mu.Unlock()
    66  	contracts := make([]modules.RenterContract, 0, len(c.oldContracts))
    67  	for _, c := range c.oldContracts {
    68  		contracts = append(contracts, c)
    69  	}
    70  	return contracts
    71  }
    72  
    73  // ContractUtility returns the utility fields for the given contract.
    74  func (c *Contractor) ContractUtility(pk types.SiaPublicKey) (modules.ContractUtility, bool) {
    75  	c.mu.RLock()
    76  	id, ok := c.pubKeysToContractID[string(pk.Key)]
    77  	c.mu.RUnlock()
    78  	if !ok {
    79  		return modules.ContractUtility{}, false
    80  	}
    81  	return c.managedContractUtility(id)
    82  }
    83  
    84  // ResolveIDToPubKey returns the ID of the most recent renewal of id.
    85  func (c *Contractor) ResolveIDToPubKey(id types.FileContractID) types.SiaPublicKey {
    86  	c.mu.RLock()
    87  	defer c.mu.RUnlock()
    88  	pk, exists := c.contractIDToPubKey[id]
    89  	if !exists {
    90  		panic("renewed should never miss an id")
    91  	}
    92  	return pk
    93  }