forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 7
/
block_body_in_context.go
201 lines (164 loc) · 5.75 KB
/
block_body_in_context.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
194
195
196
197
198
199
200
201
package blockvalidator
import (
"github.com/Hoosat-Oy/HTND/domain/consensus/model"
"github.com/Hoosat-Oy/HTND/domain/consensus/model/externalapi"
"github.com/Hoosat-Oy/HTND/domain/consensus/ruleerrors"
"github.com/Hoosat-Oy/HTND/domain/consensus/utils/transactionhelper"
"github.com/Hoosat-Oy/HTND/domain/consensus/utils/virtual"
"github.com/Hoosat-Oy/HTND/infrastructure/logger"
"github.com/pkg/errors"
)
// ValidateBodyInContext validates block bodies in the context of the current
// consensus state
func (v *blockValidator) ValidateBodyInContext(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash, isBlockWithTrustedData bool) error {
onEnd := logger.LogAndMeasureExecutionTime(log, "ValidateBodyInContext")
defer onEnd()
if !isBlockWithTrustedData {
err := v.checkBlockIsNotPruned(stagingArea, blockHash)
if err != nil {
return err
}
}
err := v.checkBlockTransactions(stagingArea, blockHash)
if err != nil {
return err
}
if !isBlockWithTrustedData {
err := v.checkParentBlockBodiesExist(stagingArea, blockHash)
if err != nil {
return err
}
err = v.checkCoinbaseSubsidy(stagingArea, blockHash)
if err != nil {
return err
}
}
return nil
}
// checkBlockIsNotPruned Checks we don't add block bodies to pruned blocks
func (v *blockValidator) checkBlockIsNotPruned(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) error {
hasValidatedHeader, err := v.hasValidatedHeader(stagingArea, blockHash)
if err != nil {
return err
}
// If we don't add block body to a header only block it can't be in the past
// of the tips, because it'll be a new tip.
if !hasValidatedHeader {
return nil
}
tips, err := v.consensusStateStore.Tips(stagingArea, v.databaseContext)
if err != nil {
return err
}
isAncestorOfSomeTips, err := v.dagTopologyManagers[0].IsAncestorOfAny(stagingArea, blockHash, tips)
if err != nil {
return err
}
// A header only block in the past of one of the tips has to be pruned
if isAncestorOfSomeTips {
return errors.Wrapf(ruleerrors.ErrPrunedBlock, "cannot add block body to a pruned block %s", blockHash)
}
return nil
}
func (v *blockValidator) checkParentBlockBodiesExist(
stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) error {
missingParentHashes := []*externalapi.DomainHash{}
parents, err := v.dagTopologyManagers[0].Parents(stagingArea, blockHash)
if err != nil {
return err
}
if virtual.ContainsOnlyVirtualGenesis(parents) {
return nil
}
for _, parent := range parents {
hasBlock, err := v.blockStore.HasBlock(v.databaseContext, stagingArea, parent)
if err != nil {
return err
}
if !hasBlock {
pruningPoint, err := v.pruningStore.PruningPoint(v.databaseContext, stagingArea)
if err != nil {
return err
}
isInPastOfPruningPoint, err := v.dagTopologyManagers[0].IsAncestorOf(stagingArea, parent, pruningPoint)
if err != nil {
return err
}
// If a block parent is in the past of the pruning point
// it means its body will never be used, so it's ok if
// it's missing.
// This will usually happen during IBD when getting the blocks
// in the pruning point anticone.
if isInPastOfPruningPoint {
log.Debugf("Block %s parent %s is missing a body, but is in the past of the pruning point",
blockHash, parent)
continue
}
log.Debugf("Block %s parent %s is missing a body", blockHash, parent)
missingParentHashes = append(missingParentHashes, parent)
}
}
if len(missingParentHashes) > 0 {
return ruleerrors.NewErrMissingParents(missingParentHashes)
}
return nil
}
func (v *blockValidator) checkBlockTransactions(
stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) error {
block, err := v.blockStore.Block(v.databaseContext, stagingArea, blockHash)
if err != nil {
return err
}
// Ensure all transactions in the block are finalized.
pastMedianTime, err := v.pastMedianTimeManager.PastMedianTime(stagingArea, blockHash)
if err != nil {
return err
}
for _, tx := range block.Transactions {
if err = v.transactionValidator.ValidateTransactionInContextIgnoringUTXO(stagingArea, tx, blockHash, pastMedianTime); err != nil {
return err
}
}
return nil
}
func (v *blockValidator) checkCoinbaseSubsidy(
stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) error {
pruningPoint, err := v.pruningStore.PruningPoint(v.databaseContext, stagingArea)
if err != nil {
return err
}
parents, err := v.dagTopologyManagers[0].Parents(stagingArea, blockHash)
if err != nil {
return err
}
for _, parent := range parents {
isInFutureOfPruningPoint, err := v.dagTopologyManagers[0].IsAncestorOf(stagingArea, pruningPoint, parent)
if err != nil {
return err
}
// The pruning proof ( https://github.com/hoosatnet/docs/blob/main/Reference/prunality/Prunality.pdf ) concludes
// that it's impossible for a block to be merged if it was created in the anticone of the pruning point that was
// present at the time of the block creation. So if such situation happens we can be sure that it happens during
// IBD and that this block has at least pruningDepth-finalityInterval confirmations.
if !isInFutureOfPruningPoint {
return nil
}
}
block, err := v.blockStore.Block(v.databaseContext, stagingArea, blockHash)
if err != nil {
return err
}
expectedSubsidy, err := v.coinbaseManager.CalcBlockSubsidy(stagingArea, blockHash)
if err != nil {
return err
}
_, _, subsidy, err := v.coinbaseManager.ExtractCoinbaseDataBlueScoreAndSubsidy(block.Transactions[transactionhelper.CoinbaseTransactionIndex])
if err != nil {
return err
}
if expectedSubsidy != subsidy {
return errors.Wrapf(ruleerrors.ErrWrongCoinbaseSubsidy, "the subsidy specified on the coinbase of %s is "+
"wrong: expected %d but got %d", blockHash, expectedSubsidy, subsidy)
}
return nil
}