<commit_before>package contractor

import (
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/types"
)

// ProcessConsensusChange will be called by the consensus set every time there
// is a change in the blockchain. Updates will always be called in order.
func (c *Contractor) ProcessConsensusChange(cc modules.ConsensusChange) {
	c.mu.Lock()
	for _, block := range cc.RevertedBlocks {
		if block.ID() != types.GenesisID {
			c.blockHeight--
		}
	}
	for _, block := range cc.AppliedBlocks {
		if block.ID() != types.GenesisID {
			c.blockHeight++
		}
	}

	// delete expired contracts
	var expired []types.FileContractID
	for id, contract := range c.contracts {
		if c.blockHeight > contract.EndHeight() {
			// No need to wait for extra confirmations - any processes which
			// depend on this contract should have taken care of any issues
			// already.
			expired = append(expired, id)
		}
	}
	for _, id := range expired {
		delete(c.contracts, id)
		c.log.Debugln("INFO: deleted expired contract", id)
	}

	// if we have entered the next period, update currentPeriod
	if c.blockHeight > c.currentPeriod+c.allowance.Period {
		c.currentPeriod += c.allowance.Period
	}

	c.lastChange = cc.ID
	err := c.save()
	if err != nil {
		c.log.Println("Unable to save while processing a consensus change:", err)
	}
	c.mu.Unlock()

	// only attempt contract formation/renewal if we are synced
	// (harmless if not synced, since hosts will reject our renewal attempts,
	// but very slow)
	if cc.Synced {
		go func() {
			// only one goroutine should be editing contracts at a time
			if !c.editLock.TryLock() {
				return
			}
			defer c.editLock.Unlock()

			// renew any (online) contracts that have entered the renew window
			err := c.managedRenewContracts()
			if err != nil {
				c.log.Debugln("WARN: failed to renew contracts after processing a consensus chage:", err)
			}

			// if we don't have enough (online) contracts, form new ones
			c.mu.RLock()
			a := c.allowance
			remaining := int(a.Hosts) - len(c.onlineContracts())<commit_msg>fix currentPeriod updating<commit_after>package contractor

import (
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/types"
)

// ProcessConsensusChange will be called by the consensus set every time there
// is a change in the blockchain. Updates will always be called in order.
func (c *Contractor) ProcessConsensusChange(cc modules.ConsensusChange) {
	c.mu.Lock()
	for _, block := range cc.RevertedBlocks {
		if block.ID() != types.GenesisID {
			c.blockHeight--
		}
	}
	for _, block := range cc.AppliedBlocks {
		if block.ID() != types.GenesisID {
			c.blockHeight++
		}
	}

	// delete expired contracts
	var expired []types.FileContractID
	for id, contract := range c.contracts {
		if c.blockHeight > contract.EndHeight() {
			// No need to wait for extra confirmations - any processes which
			// depend on this contract should have taken care of any issues
			// already.
			expired = append(expired, id)
		}
	}
	for _, id := range expired {
		delete(c.contracts, id)
		c.log.Debugln("INFO: deleted expired contract", id)
	}

	// if we have entered the next period, update currentPeriod
	// NOTE: "period" refers to the duration of contracts, whereas "cycle"
	// refers to how frequently the period metrics are reset. Should think
	// about how to make this more explicit.
	cycleLen := c.allowance.Period - c.allowance.RenewWindow
	if c.blockHeight > c.currentPeriod+cycleLen {
		c.currentPeriod += cycleLen
	}

	c.lastChange = cc.ID
	err := c.save()
	if err != nil {
		c.log.Println("Unable to save while processing a consensus change:", err)
	}
	c.mu.Unlock()

	// only attempt contract formation/renewal if we are synced
	// (harmless if not synced, since hosts will reject our renewal attempts,
	// but very slow)
	if cc.Synced {
		go func() {
			// only one goroutine should be editing contracts at a time
			if !c.editLock.TryLock() {
				return
			}
			defer c.editLock.Unlock()

			// renew any (online) contracts that have entered the renew window
			err := c.managedRenewContracts()
			if err != nil {
				c.log.Debugln("WARN: failed to renew contracts after processing a consensus chage:", err)
			}

			// if we don't have enough (online) contracts, form new ones
			c.mu.RLock()
			a := c.allowance
			remaining := int(a.Hosts) - len(c.onlineContracts())
{'fraction_non_alphanumeric': 0.10957960027567196, 'fraction_numerical': 0.0, 'mean_word_length': 3.302371541501976, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4651133', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1364, 'n_words': 462}