forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
95 lines (85 loc) · 2.65 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package contractor
import (
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/types"
)
// managedArchiveContracts will figure out which contracts are no longer needed
// and move them to the historic set of contracts.
//
// TODO: This function should be performed by threadedContractMaintenance.
// threadedContractMaintenance will currently quit if there are no hosts, but it
// should at least run this code before quitting.
func (c *Contractor) managedArchiveContracts() {
err := c.tg.Add()
if err != nil {
return
}
defer c.tg.Done()
// Determine the current block height.
c.mu.RLock()
currentHeight := c.blockHeight
c.mu.RUnlock()
// Loop through the current set of contracts and migrate any expired ones to
// the set of old contracts.
var expired []types.FileContractID
for _, contract := range c.contracts.ViewAll() {
if currentHeight > contract.EndHeight {
id := contract.ID
c.mu.Lock()
c.oldContracts[id] = contract
c.mu.Unlock()
expired = append(expired, id)
c.log.Println("INFO: archived expired contract", id)
}
}
// Save.
c.mu.Lock()
c.save()
c.mu.Unlock()
// Delete all the expired contracts from the contract set.
for _, id := range expired {
if sc, ok := c.contracts.Acquire(id); ok {
c.contracts.Delete(sc)
}
}
}
// 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++
}
}
// 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.
// TODO: How to make this more explicit.
cycleLen := c.allowance.Period - c.allowance.RenewWindow
if c.blockHeight >= c.currentPeriod+cycleLen {
c.currentPeriod += cycleLen
// COMPATv1.0.4-lts
// if we were storing a special metrics contract, it will be invalid
// after we enter the next period.
delete(c.oldContracts, metricsContractID)
}
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()
// Perform contract maintenance if our blockchain is synced. Use a separate
// goroutine so that the rest of the contractor is not blocked during
// maintenance.
if cc.Synced {
go c.threadedContractMaintenance()
go c.managedArchiveContracts()
}
}