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

     1  package contractor
     2  
     3  import (
     4  	"SiaPrime/modules"
     5  	"SiaPrime/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  		// Check map of renewedTo in case renew code was interrupted before
    21  		// archiving old contract
    22  		c.mu.RLock()
    23  		_, renewed := c.renewedTo[contract.ID]
    24  		c.mu.RUnlock()
    25  		if currentHeight > contract.EndHeight || renewed {
    26  			id := contract.ID
    27  			c.mu.Lock()
    28  			c.oldContracts[id] = contract
    29  			c.mu.Unlock()
    30  			expired = append(expired, id)
    31  			c.log.Println("INFO: archived expired contract", id)
    32  		}
    33  	}
    34  
    35  	// Save.
    36  	c.mu.Lock()
    37  	c.save()
    38  	c.mu.Unlock()
    39  
    40  	// Delete all the expired contracts from the contract set.
    41  	for _, id := range expired {
    42  		if sc, ok := c.staticContracts.Acquire(id); ok {
    43  			c.staticContracts.Delete(sc)
    44  		}
    45  	}
    46  }
    47  
    48  // ProcessConsensusChange will be called by the consensus set every time there
    49  // is a change in the blockchain. Updates will always be called in order.
    50  func (c *Contractor) ProcessConsensusChange(cc modules.ConsensusChange) {
    51  	c.mu.Lock()
    52  	for _, block := range cc.RevertedBlocks {
    53  		if block.ID() != types.GenesisID {
    54  			c.blockHeight--
    55  		}
    56  	}
    57  	for _, block := range cc.AppliedBlocks {
    58  		if block.ID() != types.GenesisID {
    59  			c.blockHeight++
    60  		}
    61  	}
    62  
    63  	// If we have entered the next period, update currentPeriod
    64  	if c.blockHeight >= c.currentPeriod+c.allowance.Period {
    65  		c.currentPeriod += c.allowance.Period
    66  		// COMPATv1.0.4-lts
    67  		// if we were storing a special metrics contract, it will be invalid
    68  		// after we enter the next period.
    69  		delete(c.oldContracts, metricsContractID)
    70  	}
    71  
    72  	c.lastChange = cc.ID
    73  	err := c.save()
    74  	if err != nil {
    75  		c.log.Println("Unable to save while processing a consensus change:", err)
    76  	}
    77  	c.mu.Unlock()
    78  
    79  	// Perform contract maintenance if our blockchain is synced. Use a separate
    80  	// goroutine so that the rest of the contractor is not blocked during
    81  	// maintenance.
    82  	if cc.Synced {
    83  		go c.threadedContractMaintenance()
    84  	}
    85  }