-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintenance.go
193 lines (176 loc) · 6.26 KB
/
maintenance.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package consensus
import (
"errors"
"SiaPrime/build"
"SiaPrime/encoding"
"SiaPrime/modules"
"SiaPrime/types"
"gitlab.com/NebulousLabs/bolt"
)
var (
errMissingFileContract = errors.New("storage proof submitted for non existing file contract")
errOutputAlreadyMature = errors.New("delayed siacoin output is already in the matured outputs set")
errPayoutsAlreadyPaid = errors.New("payouts are already in the consensus set")
errStorageProofTiming = errors.New("missed proof triggered for file contract that is not expiring")
)
// applyMinerPayouts adds a block's miner payouts to the consensus set as
// delayed siacoin outputs.
func applyMinerPayouts(tx *bolt.Tx, pb *processedBlock) {
for i := range pb.Block.MinerPayouts {
mpid := pb.Block.MinerPayoutID(uint64(i))
dscod := modules.DelayedSiacoinOutputDiff{
Direction: modules.DiffApply,
ID: mpid,
SiacoinOutput: pb.Block.MinerPayouts[i],
MaturityHeight: pb.Height + types.MaturityDelay,
}
pb.DelayedSiacoinOutputDiffs = append(pb.DelayedSiacoinOutputDiffs, dscod)
commitDelayedSiacoinOutputDiff(tx, dscod, modules.DiffApply)
}
}
// applyMaturedSiacoinOutputs goes through the list of siacoin outputs that
// have matured and adds them to the consensus set. This also updates the block
// node diff set.
func applyMaturedSiacoinOutputs(tx *bolt.Tx, pb *processedBlock) {
// Skip this step if the blockchain is not old enough to have maturing
// outputs.
if pb.Height < types.MaturityDelay {
return
}
// Iterate through the list of delayed siacoin outputs. Sometimes boltdb
// has trouble if you delete elements in a bucket while iterating through
// the bucket (and sometimes not - nondeterministic), so all of the
// elements are collected into an array and then deleted after the bucket
// scan is complete.
bucketID := append(prefixDSCO, encoding.Marshal(pb.Height)...)
var scods []modules.SiacoinOutputDiff
var dscods []modules.DelayedSiacoinOutputDiff
dbErr := tx.Bucket(bucketID).ForEach(func(idBytes, scoBytes []byte) error {
// Decode the key-value pair into an id and a siacoin output.
var id types.SiacoinOutputID
var sco types.SiacoinOutput
copy(id[:], idBytes)
encErr := encoding.Unmarshal(scoBytes, &sco)
if build.DEBUG && encErr != nil {
panic(encErr)
}
// Sanity check - the output should not already be in siacoinOuptuts.
if build.DEBUG && isSiacoinOutput(tx, id) {
panic(errOutputAlreadyMature)
}
// Add the output to the ConsensusSet and record the diff in the
// blockNode.
scod := modules.SiacoinOutputDiff{
Direction: modules.DiffApply,
ID: id,
SiacoinOutput: sco,
}
scods = append(scods, scod)
// Create the dscod and add it to the list of dscods that should be
// deleted.
dscod := modules.DelayedSiacoinOutputDiff{
Direction: modules.DiffRevert,
ID: id,
SiacoinOutput: sco,
MaturityHeight: pb.Height,
}
dscods = append(dscods, dscod)
return nil
})
if build.DEBUG && dbErr != nil {
panic(dbErr)
}
for _, scod := range scods {
pb.SiacoinOutputDiffs = append(pb.SiacoinOutputDiffs, scod)
commitSiacoinOutputDiff(tx, scod, modules.DiffApply)
}
for _, dscod := range dscods {
pb.DelayedSiacoinOutputDiffs = append(pb.DelayedSiacoinOutputDiffs, dscod)
commitDelayedSiacoinOutputDiff(tx, dscod, modules.DiffApply)
}
deleteDSCOBucket(tx, pb.Height)
}
// applyMissedStorageProof adds the outputs and diffs that result from a file
// contract expiring.
func applyMissedStorageProof(tx *bolt.Tx, pb *processedBlock, fcid types.FileContractID) (dscods []modules.DelayedSiacoinOutputDiff, fcd modules.FileContractDiff) {
// Sanity checks.
fc, err := getFileContract(tx, fcid)
if build.DEBUG && err != nil {
panic(err)
}
if build.DEBUG {
// Check that the file contract in question expires at pb.Height.
if fc.WindowEnd != pb.Height {
panic(errStorageProofTiming)
}
}
// Add all of the outputs in the missed proof outputs to the consensus set.
for i, mpo := range fc.MissedProofOutputs {
// Sanity check - output should not already exist.
spoid := fcid.StorageProofOutputID(types.ProofMissed, uint64(i))
if build.DEBUG && isSiacoinOutput(tx, spoid) {
panic(errPayoutsAlreadyPaid)
}
// Don't add the output if the value is zero.
dscod := modules.DelayedSiacoinOutputDiff{
Direction: modules.DiffApply,
ID: spoid,
SiacoinOutput: mpo,
MaturityHeight: pb.Height + types.MaturityDelay,
}
dscods = append(dscods, dscod)
}
// Remove the file contract from the consensus set and record the diff in
// the blockNode.
fcd = modules.FileContractDiff{
Direction: modules.DiffRevert,
ID: fcid,
FileContract: fc,
}
return dscods, fcd
}
// applyFileContractMaintenance looks for all of the file contracts that have
// expired without an appropriate storage proof, and calls 'applyMissedProof'
// for the file contract.
func applyFileContractMaintenance(tx *bolt.Tx, pb *processedBlock) {
// Get the bucket pointing to all of the expiring file contracts.
fceBucketID := append(prefixFCEX, encoding.Marshal(pb.Height)...)
fceBucket := tx.Bucket(fceBucketID)
// Finish if there are no expiring file contracts.
if fceBucket == nil {
return
}
var dscods []modules.DelayedSiacoinOutputDiff
var fcds []modules.FileContractDiff
err := fceBucket.ForEach(func(keyBytes, valBytes []byte) error {
var id types.FileContractID
copy(id[:], keyBytes)
amspDSCODS, fcd := applyMissedStorageProof(tx, pb, id)
fcds = append(fcds, fcd)
dscods = append(dscods, amspDSCODS...)
return nil
})
if build.DEBUG && err != nil {
panic(err)
}
for _, dscod := range dscods {
pb.DelayedSiacoinOutputDiffs = append(pb.DelayedSiacoinOutputDiffs, dscod)
commitDelayedSiacoinOutputDiff(tx, dscod, modules.DiffApply)
}
for _, fcd := range fcds {
pb.FileContractDiffs = append(pb.FileContractDiffs, fcd)
commitFileContractDiff(tx, fcd, modules.DiffApply)
}
err = tx.DeleteBucket(fceBucketID)
if build.DEBUG && err != nil {
panic(err)
}
}
// applyMaintenance applies block-level alterations to the consensus set.
// Maintenance is applied after all of the transactions for the block have been
// applied.
func applyMaintenance(tx *bolt.Tx, pb *processedBlock) {
applyMinerPayouts(tx, pb)
applyMaturedSiacoinOutputs(tx, pb)
applyFileContractMaintenance(tx, pb)
}