gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/renter/contractor/contracts.go (about) 1 package contractor 2 3 import ( 4 "gitlab.com/SiaPrime/SiaPrime/modules" 5 "gitlab.com/SiaPrime/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 // managedContractByPublicKey returns the contract with the key specified, if 26 // it exists. The contract will be resolved if possible to the most recent 27 // child contract. 28 func (c *Contractor) managedContractByPublicKey(pk types.SiaPublicKey) (modules.RenterContract, bool) { 29 c.mu.RLock() 30 id, ok := c.pubKeysToContractID[pk.String()] 31 c.mu.RUnlock() 32 if !ok { 33 return modules.RenterContract{}, false 34 } 35 return c.staticContracts.View(id) 36 } 37 38 // managedContractUtility returns the ContractUtility for a contract with a given id. 39 func (c *Contractor) managedContractUtility(id types.FileContractID) (modules.ContractUtility, bool) { 40 rc, exists := c.staticContracts.View(id) 41 if !exists { 42 return modules.ContractUtility{}, false 43 } 44 return rc.Utility, true 45 } 46 47 // ContractByPublicKey returns the contract with the key specified, if it 48 // exists. The contract will be resolved if possible to the most recent child 49 // contract. 50 func (c *Contractor) ContractByPublicKey(pk types.SiaPublicKey) (modules.RenterContract, bool) { 51 return c.managedContractByPublicKey(pk) 52 } 53 54 // CancelContract cancels the Contractor's contract by marking it !GoodForRenew 55 // and !GoodForUpload 56 func (c *Contractor) CancelContract(id types.FileContractID) error { 57 if err := c.tg.Add(); err != nil { 58 return err 59 } 60 defer c.tg.Done() 61 defer c.threadedContractMaintenance() 62 return c.managedCancelContract(id) 63 } 64 65 // Contracts returns the contracts formed by the contractor in the current 66 // allowance period. Only contracts formed with currently online hosts are 67 // returned. 68 func (c *Contractor) Contracts() []modules.RenterContract { 69 return c.staticContracts.ViewAll() 70 } 71 72 // ContractUtility returns the utility fields for the given contract. 73 func (c *Contractor) ContractUtility(pk types.SiaPublicKey) (modules.ContractUtility, bool) { 74 c.mu.RLock() 75 id, ok := c.pubKeysToContractID[pk.String()] 76 c.mu.RUnlock() 77 if !ok { 78 return modules.ContractUtility{}, false 79 } 80 return c.managedContractUtility(id) 81 } 82 83 // OldContracts returns the contracts formed by the contractor that have 84 // expired 85 func (c *Contractor) OldContracts() []modules.RenterContract { 86 c.mu.Lock() 87 contracts := make([]modules.RenterContract, 0, len(c.oldContracts)) 88 for _, c := range c.oldContracts { 89 contracts = append(contracts, c) 90 } 91 c.mu.Unlock() 92 return contracts 93 } 94 95 // RecoverableContracts returns the contracts that the contractor deems 96 // recoverable. That means they are not expired yet and also not part of the 97 // active contracts. Usually this should return an empty slice unless the host 98 // isn't available for recovery or something went wrong. 99 func (c *Contractor) RecoverableContracts() []modules.RecoverableContract { 100 c.mu.Lock() 101 contracts := make([]modules.RecoverableContract, 0, len(c.recoverableContracts)) 102 for _, c := range c.recoverableContracts { 103 contracts = append(contracts, c) 104 } 105 c.mu.Unlock() 106 return contracts 107 }