Skip to content

Commit c87b3e6

Browse files
committed
Merge branch 'header-check' into dev
2 parents 4359bfe + 6118859 commit c87b3e6

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

consensus/cuckoo/consensus.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ import (
4646

4747
// Cuckoo proof-of-work protocol constants.
4848
var (
49-
FrontierBlockReward *big.Int = big.NewInt(7e+18) // Block reward in wei for successfully mining a block
50-
ByzantiumBlockReward *big.Int = big.NewInt(7e+18) // Block reward in wei for successfully mining a block upward from Byzantium
51-
ConstantinopleBlockReward = big.NewInt(7e+18)
52-
maxUncles = 2 // Maximum number of uncles allowed in a single block
53-
allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
54-
FixHashes = map[common.Hash]bool{
49+
FrontierBlockReward *big.Int = big.NewInt(7e+18) // Block reward in wei for successfully mining a block
50+
ByzantiumBlockReward *big.Int = big.NewInt(7e+18) // Block reward in wei for successfully mining a block upward from Byzantium
51+
ConstantinopleBlockReward = big.NewInt(7e+18)
52+
maxUncles = 2 // Maximum number of uncles allowed in a single block
53+
allowedFutureBlockTimeSeconds = int64(15)
54+
FixHashes = map[common.Hash]bool{
5555
common.HexToHash("0x367e111f0f274d54f357ed3dc2d16107b39772c3a767138b857f5c02b5c30607"): true,
5656
common.HexToHash("0xbde83a87b6d526ada5a02e394c5f21327acb080568f7cc6f8fff423620f0eec3"): true,
5757
}
@@ -107,7 +107,7 @@ func (cuckoo *Cuckoo) VerifyHeader(chain consensus.ChainHeaderReader, header *ty
107107
return consensus.ErrUnknownAncestor
108108
}
109109
// Sanity checks passed, do a proper verification
110-
return cuckoo.verifyHeader(chain, header, parent, false, seal)
110+
return cuckoo.verifyHeader(chain, header, parent, false, seal, time.Now().Unix())
111111
}
112112

113113
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
@@ -135,11 +135,12 @@ func (cuckoo *Cuckoo) VerifyHeaders(chain consensus.ChainHeaderReader, headers [
135135
done = make(chan int, workers)
136136
errors = make([]error, len(headers))
137137
abort = make(chan struct{})
138+
utcNow = time.Now().Unix()
138139
)
139140
for i := 0; i < workers; i++ {
140141
go func() {
141142
for index := range inputs {
142-
errors[index] = cuckoo.verifyHeaderWorker(chain, headers, seals, index)
143+
errors[index] = cuckoo.verifyHeaderWorker(chain, headers, seals, index, utcNow)
143144
done <- index
144145
}
145146
}()
@@ -175,7 +176,7 @@ func (cuckoo *Cuckoo) VerifyHeaders(chain consensus.ChainHeaderReader, headers [
175176
return abort, errorsOut
176177
}
177178

178-
func (cuckoo *Cuckoo) verifyHeaderWorker(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool, index int) error {
179+
func (cuckoo *Cuckoo) verifyHeaderWorker(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool, index int, utcNow int64) error {
179180
var parent *types.Header
180181
if index == 0 {
181182
parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
@@ -185,10 +186,7 @@ func (cuckoo *Cuckoo) verifyHeaderWorker(chain consensus.ChainHeaderReader, head
185186
if parent == nil {
186187
return consensus.ErrUnknownAncestor
187188
}
188-
if chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()) != nil {
189-
return nil // known block
190-
}
191-
return cuckoo.verifyHeader(chain, headers[index], parent, false, seals[index])
189+
return cuckoo.verifyHeader(chain, headers[index], parent, false, seals[index], utcNow)
192190
}
193191

194192
// VerifyUncles verifies that the given block's uncles conform to the consensus
@@ -240,7 +238,7 @@ func (cuckoo *Cuckoo) VerifyUncles(chain consensus.ChainReader, block *types.Blo
240238
if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() {
241239
return errDanglingUncle
242240
}
243-
if err := cuckoo.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
241+
if err := cuckoo.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, true, time.Now().Unix()); err != nil {
244242
return err
245243
}
246244
}
@@ -250,14 +248,14 @@ func (cuckoo *Cuckoo) VerifyUncles(chain consensus.ChainReader, block *types.Blo
250248
// verifyHeader checks whether a header conforms to the consensus rules of the
251249
// stock Cortex cuckoo engine.
252250
// See YP section 4.3.4. "Block Header Validity"
253-
func (cuckoo *Cuckoo) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header, uncle, seal bool) error {
251+
func (cuckoo *Cuckoo) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header, uncle, seal bool, utcNow int64) error {
254252
// Ensure that the header's extra-data section is of a reasonable size
255253
if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
256254
return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
257255
}
258256
// Verify the header's timestamp
259257
if !uncle {
260-
if header.Time > uint64(time.Now().Add(allowedFutureBlockTime).Unix()) {
258+
if header.Time > uint64(utcNow+allowedFutureBlockTimeSeconds) {
261259
return consensus.ErrFutureBlock
262260
}
263261
}

core/headerchain.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int)
303303
parentHash.Bytes()[:4], i, chain[i].Number, chain[i].Hash().Bytes()[:4], chain[i].ParentHash[:4])
304304
}
305305
// If the header is a banned one, straight out abort
306-
if BadHashes[parentHash] {
306+
if BadHashes[chain[i].ParentHash] {
307307
return i - 1, ErrBlacklistedHash
308308
}
309309
// If it's the last header in the cunk, we need to check it too
@@ -332,17 +332,18 @@ func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int)
332332

333333
// Iterate over the headers and ensure they all check out
334334
for i := range chain {
335-
// If the chain is terminating, stop processing blocks
336-
if hc.procInterrupt() {
337-
log.Debug("Premature abort during headers verification")
338-
return 0, errors.New("aborted")
339-
}
340335
// Otherwise wait for headers checks and ensure they pass
341336
if err := <-results; err != nil {
342337
return i, err
343338
}
344339
}
345340

341+
// If the chain is terminating, stop processing blocks
342+
if hc.procInterrupt() {
343+
log.Debug("Premature abort during headers verification")
344+
return 0, errors.New("aborted")
345+
}
346+
346347
return 0, nil
347348
}
348349

core/rawdb/freezer_table.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ type freezerTable struct {
103103
lock sync.RWMutex // Mutex protecting the data file descriptors
104104
}
105105

106+
// NewFreezerTable opens the given path as a freezer table.
107+
func NewFreezerTable(path, name string, disableSnappy bool) (*freezerTable, error) {
108+
return newTable(path, name, metrics.NilMeter{}, metrics.NilMeter{}, metrics.NilGauge{}, disableSnappy)
109+
}
110+
106111
// newTable opens a freezer table with default settings - 2G files
107112
func newTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeGauge metrics.Gauge, disableSnappy bool) (*freezerTable, error) {
108113
return newCustomTable(path, name, readMeter, writeMeter, sizeGauge, 2*1000*1000*1000, disableSnappy)

0 commit comments

Comments
 (0)