Skip to content

Commit 9712092

Browse files
authored
Merge pull request #871 from CortexFoundation/dev
Speed up header check
2 parents 43a4ce7 + c87b3e6 commit 9712092

File tree

18 files changed

+108
-109
lines changed

18 files changed

+108
-109
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)

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ go 1.15
55
require (
66
github.com/Azure/azure-storage-blob-go v0.8.1-0.20191213204130-762620a866ba
77
github.com/CortexFoundation/inference v1.0.2-0.20201207080139-4cbe4856d997
8-
github.com/CortexFoundation/torrentfs v1.0.23-0.20201217020639-7f4fd5ad4f26
8+
github.com/CortexFoundation/torrentfs v1.0.23-0.20201221051208-242a67758c4f
99
github.com/VictoriaMetrics/fastcache v1.5.8-0.20200305212624-8835719dc76c
10+
github.com/anacrolix/torrent v1.19.3-0.20201221081250-6e43db6a89fb // indirect
1011
github.com/aristanetworks/goarista v0.0.0-20200513152637-638451432ae4
1112
github.com/arsham/figurine v1.0.1
1213
github.com/aws/aws-sdk-go v1.31.0
@@ -52,7 +53,7 @@ require (
5253
github.com/ucwong/golang-set v1.8.1-0.20200419153428-d7b0b1ac2d43
5354
github.com/ucwong/tsdb v0.10.4-0.20200518132041-df9cb51f3a80
5455
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208
55-
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9
56+
golang.org/x/crypto v0.0.0-20201217014255-9d1352758620
5657
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
5758
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d
5859
golang.org/x/text v0.3.4-0.20200826142016-a8b467125457

go.sum

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ github.com/CortexFoundation/torrentfs v1.0.13-0.20200623060705-ce027f43f2f8/go.m
5555
github.com/CortexFoundation/torrentfs v1.0.14-0.20200703071639-3fcabcabf274/go.mod h1:qnb3YlIJmuetVBtC6Lsejr0Xru+1DNmDCdTqnwy7lhk=
5656
github.com/CortexFoundation/torrentfs v1.0.20-0.20200810031954-d36d26f82fcc/go.mod h1:N5BsicP5ynjXIi/Npl/SRzlJ630n1PJV2sRj0Z0t2HA=
5757
github.com/CortexFoundation/torrentfs v1.0.22-0.20201126093427-f455d2481b9b/go.mod h1:lcAYtGEoWAeIhWH/JpXno34eNA17xslgwg6UBBd769U=
58-
github.com/CortexFoundation/torrentfs v1.0.23-0.20201217020639-7f4fd5ad4f26 h1:HWSIcTERkn+Y3wUmzINBnVwu72I8ddeD8Ycv4HlSLr0=
59-
github.com/CortexFoundation/torrentfs v1.0.23-0.20201217020639-7f4fd5ad4f26/go.mod h1:l12uUEX64GmaG56BaSW3XgIAG6wTFRSzcXXCFklogDM=
58+
github.com/CortexFoundation/torrentfs v1.0.23-0.20201221051208-242a67758c4f h1:Ul/TBC9QSnLV4YPBrsyhUlgCG38ZsKMNfsxEO/IU+tQ=
59+
github.com/CortexFoundation/torrentfs v1.0.23-0.20201221051208-242a67758c4f/go.mod h1:yL4YfxSBQno+mYM05wuxOBG0J/iOAT79VQ4B8d+IYO4=
6060
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
6161
github.com/Julusian/godocdown v0.0.0-20170816220326-6d19f8ff2df8/go.mod h1:INZr5t32rG59/5xeltqoCJoNY7e5x/3xoY9WSWVWg74=
6262
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
@@ -174,8 +174,9 @@ github.com/anacrolix/torrent v1.15.1-0.20200504230043-cc5d2abe18e5/go.mod h1:QlO
174174
github.com/anacrolix/torrent v1.15.1-0.20200619022403-dd51e99b88cc/go.mod h1:wuopQPC5+/M+zHYvhcA2vp5UCTm9rUc+VqjyBa882Q8=
175175
github.com/anacrolix/torrent v1.15.1-0.20200715061614-dd906f8fa72e/go.mod h1:XWo/fJN1oKgcjgxM+pUZpvalHfqHDs27BY5mBZjIQWo=
176176
github.com/anacrolix/torrent v1.18.1-0.20201121024423-388e6899a15b/go.mod h1:nhOLTTTOcr5WnvNKJzHGKzyz5D8mujPIgdbRU2818v8=
177-
github.com/anacrolix/torrent v1.19.0 h1:8nUgqrrvnf7PFr2wkd6PNfKCGtbTmIF1JNX2noYW8m4=
178-
github.com/anacrolix/torrent v1.19.0/go.mod h1:WjA5XIOm/3qWM8DUEhd5ACfD771/wsddW90nCAhCUOQ=
177+
github.com/anacrolix/torrent v1.19.1-0.20201220230444-f3d3fd37244f/go.mod h1:WjA5XIOm/3qWM8DUEhd5ACfD771/wsddW90nCAhCUOQ=
178+
github.com/anacrolix/torrent v1.19.3-0.20201221081250-6e43db6a89fb h1:gcGCBwHXhYywpeIUUN6TfRvmBxgtnyOzDMh50Kce9Hs=
179+
github.com/anacrolix/torrent v1.19.3-0.20201221081250-6e43db6a89fb/go.mod h1:WjA5XIOm/3qWM8DUEhd5ACfD771/wsddW90nCAhCUOQ=
179180
github.com/anacrolix/upnp v0.1.1/go.mod h1:LXsbsp5h+WGN7YR+0A7iVXm5BL1LYryDev1zuJMWYQo=
180181
github.com/anacrolix/upnp v0.1.2-0.20200416075019-5e9378ed1425 h1:/Wi6l2ONI1FUFWN4cBwHOO90V4ylp4ud/eov6GUcVFk=
181182
github.com/anacrolix/upnp v0.1.2-0.20200416075019-5e9378ed1425/go.mod h1:Pz94W3kl8rf+wxH3IbCa9Sq+DTJr8OSbV2Q3/y51vYs=
@@ -979,6 +980,8 @@ go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
979980
go.etcd.io/bbolt v1.3.5-0.20200424005604-a8af23b57f67/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
980981
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
981982
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
983+
go.etcd.io/bbolt v1.3.6-0.20200807205753-f6be82302843 h1:g0YWcnTxZ70pMN+rjjHC2/ba4T+R6okysNm3KdSt7gA=
984+
go.etcd.io/bbolt v1.3.6-0.20200807205753-f6be82302843/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
982985
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
983986
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
984987
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
@@ -1028,6 +1031,8 @@ golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPh
10281031
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
10291032
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9 h1:sYNJzB4J8toYPQTM6pAkcmBRgw9SnQKP9oXCHfgy604=
10301033
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
1034+
golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 h1:3wPMTskHO3+O6jqTEXyFcsnuxMQOqYSaHsDxcbUXpqA=
1035+
golang.org/x/crypto v0.0.0-20201217014255-9d1352758620/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
10311036
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10321037
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10331038
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

vendor/github.com/CortexFoundation/torrentfs/go.mod

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)