github.com/nebulouslabs/sia@v1.3.7/modules/renter/contractor/update.go (about) 1 package contractor 2 3 import ( 4 "github.com/NebulousLabs/Sia/modules" 5 "github.com/NebulousLabs/Sia/types" 6 ) 7 8 // managedArchiveContracts will figure out which contracts are no longer needed 9 // and move them to the historic set of contracts. 10 func (c *Contractor) managedArchiveContracts() { 11 // Determine the current block height. 12 c.mu.RLock() 13 currentHeight := c.blockHeight 14 c.mu.RUnlock() 15 16 // Loop through the current set of contracts and migrate any expired ones to 17 // the set of old contracts. 18 var expired []types.FileContractID 19 for _, contract := range c.staticContracts.ViewAll() { 20 if currentHeight > contract.EndHeight { 21 id := contract.ID 22 c.mu.Lock() 23 c.oldContracts[id] = contract 24 c.mu.Unlock() 25 expired = append(expired, id) 26 c.log.Println("INFO: archived expired contract", id) 27 } 28 } 29 30 // Save. 31 c.mu.Lock() 32 c.save() 33 c.mu.Unlock() 34 35 // Delete all the expired contracts from the contract set. 36 for _, id := range expired { 37 if sc, ok := c.staticContracts.Acquire(id); ok { 38 c.staticContracts.Delete(sc) 39 } 40 } 41 } 42 43 // ProcessConsensusChange will be called by the consensus set every time there 44 // is a change in the blockchain. Updates will always be called in order. 45 func (c *Contractor) ProcessConsensusChange(cc modules.ConsensusChange) { 46 c.mu.Lock() 47 for _, block := range cc.RevertedBlocks { 48 if block.ID() != types.GenesisID { 49 c.blockHeight-- 50 } 51 } 52 for _, block := range cc.AppliedBlocks { 53 if block.ID() != types.GenesisID { 54 c.blockHeight++ 55 } 56 } 57 58 // If we have entered the next period, update currentPeriod 59 // NOTE: "period" refers to the duration of contracts, whereas "cycle" 60 // refers to how frequently the period metrics are reset. 61 // TODO: How to make this more explicit. 62 cycleLen := c.allowance.Period - c.allowance.RenewWindow 63 if c.blockHeight >= c.currentPeriod+cycleLen { 64 c.currentPeriod += cycleLen 65 // COMPATv1.0.4-lts 66 // if we were storing a special metrics contract, it will be invalid 67 // after we enter the next period. 68 delete(c.oldContracts, metricsContractID) 69 } 70 71 c.lastChange = cc.ID 72 err := c.save() 73 if err != nil { 74 c.log.Println("Unable to save while processing a consensus change:", err) 75 } 76 c.mu.Unlock() 77 78 // Perform contract maintenance if our blockchain is synced. Use a separate 79 // goroutine so that the rest of the contractor is not blocked during 80 // maintenance. 81 if cc.Synced { 82 go c.threadedContractMaintenance() 83 } 84 }