Skip to content

Commit 1dcad8b

Browse files
authored
Merge pull request #17466 from karalabe/rinkeby-light-snapshots
consensus/clique, light: light client snapshots on Rinkeby
2 parents 86acdf1 + 9f03664 commit 1dcad8b

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

consensus/clique/clique.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -387,22 +387,23 @@ func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash commo
387387
break
388388
}
389389
}
390-
// If we're at block zero, make a snapshot
391-
if number == 0 {
392-
genesis := chain.GetHeaderByNumber(0)
393-
if err := c.VerifyHeader(chain, genesis, false); err != nil {
394-
return nil, err
395-
}
396-
signers := make([]common.Address, (len(genesis.Extra)-extraVanity-extraSeal)/common.AddressLength)
397-
for i := 0; i < len(signers); i++ {
398-
copy(signers[i][:], genesis.Extra[extraVanity+i*common.AddressLength:])
399-
}
400-
snap = newSnapshot(c.config, c.signatures, 0, genesis.Hash(), signers)
401-
if err := snap.store(c.db); err != nil {
402-
return nil, err
390+
// If we're at an checkpoint block, make a snapshot if it's known
391+
if number%c.config.Epoch == 0 {
392+
checkpoint := chain.GetHeaderByNumber(number)
393+
if checkpoint != nil {
394+
hash := checkpoint.Hash()
395+
396+
signers := make([]common.Address, (len(checkpoint.Extra)-extraVanity-extraSeal)/common.AddressLength)
397+
for i := 0; i < len(signers); i++ {
398+
copy(signers[i][:], checkpoint.Extra[extraVanity+i*common.AddressLength:])
399+
}
400+
snap = newSnapshot(c.config, c.signatures, number, hash, signers)
401+
if err := snap.store(c.db); err != nil {
402+
return nil, err
403+
}
404+
log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash)
405+
break
403406
}
404-
log.Trace("Stored genesis voting snapshot to disk")
405-
break
406407
}
407408
// No snapshot for this header, gather the header and move backward
408409
var header *types.Header

consensus/clique/snapshot_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (r *testerChainReader) GetHeaderByNumber(number uint64) *types.Header {
8484
if number == 0 {
8585
return rawdb.ReadHeader(r.db, rawdb.ReadCanonicalHash(r.db, 0), 0)
8686
}
87-
panic("not supported")
87+
return nil
8888
}
8989

9090
// Tests that voting is evaluated correctly for various simple and complex scenarios.

light/lightchain.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -464,22 +464,32 @@ func (self *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64)
464464
func (self *LightChain) Config() *params.ChainConfig { return self.hc.Config() }
465465

466466
func (self *LightChain) SyncCht(ctx context.Context) bool {
467+
// If we don't have a CHT indexer, abort
467468
if self.odr.ChtIndexer() == nil {
468469
return false
469470
}
470-
headNum := self.CurrentHeader().Number.Uint64()
471-
chtCount, _, _ := self.odr.ChtIndexer().Sections()
472-
if headNum+1 < chtCount*CHTFrequencyClient {
473-
num := chtCount*CHTFrequencyClient - 1
474-
header, err := GetHeaderByNumber(ctx, self.odr, num)
475-
if header != nil && err == nil {
476-
self.mu.Lock()
477-
if self.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
478-
self.hc.SetCurrentHeader(header)
479-
}
480-
self.mu.Unlock()
481-
return true
471+
// Ensure the remote CHT head is ahead of us
472+
head := self.CurrentHeader().Number.Uint64()
473+
sections, _, _ := self.odr.ChtIndexer().Sections()
474+
475+
latest := sections*CHTFrequencyClient - 1
476+
if clique := self.hc.Config().Clique; clique != nil {
477+
latest -= latest % clique.Epoch // epoch snapshot for clique
478+
}
479+
if head >= latest {
480+
return false
481+
}
482+
// Retrieve the latest useful header and update to it
483+
if header, err := GetHeaderByNumber(ctx, self.odr, latest); header != nil && err == nil {
484+
self.mu.Lock()
485+
defer self.mu.Unlock()
486+
487+
// Ensure the chain didn't move past the latest block while retrieving it
488+
if self.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
489+
log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash())
490+
self.hc.SetCurrentHeader(header)
482491
}
492+
return true
483493
}
484494
return false
485495
}

light/postprocess.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,29 @@ type TrustedCheckpoint struct {
5858
SectionHead, CHTRoot, BloomRoot common.Hash
5959
}
6060

61-
var (
62-
mainnetCheckpoint = TrustedCheckpoint{
61+
// trustedCheckpoints associates each known checkpoint with the genesis hash of the chain it belongs to
62+
var trustedCheckpoints = map[common.Hash]TrustedCheckpoint{
63+
params.MainnetGenesisHash: {
6364
name: "mainnet",
6465
SectionIdx: 187,
6566
SectionHead: common.HexToHash("e6baa034efa31562d71ff23676512dec6562c1ad0301e08843b907e81958c696"),
6667
CHTRoot: common.HexToHash("28001955219719cf06de1b08648969139d123a9835fc760547a1e4dabdabc15a"),
6768
BloomRoot: common.HexToHash("395ca2373fc662720ac6b58b3bbe71f68aa0f38b63b2d3553dd32ff3c51eebc4"),
68-
}
69-
70-
ropstenCheckpoint = TrustedCheckpoint{
69+
},
70+
params.TestnetGenesisHash: {
7171
name: "ropsten",
7272
SectionIdx: 117,
7373
SectionHead: common.HexToHash("9529b38631ae30783f56cbe4c3b9f07575b770ecba4f6e20a274b1e2f40fede1"),
7474
CHTRoot: common.HexToHash("6f48e9f101f1fac98e7d74fbbcc4fda138358271ffd974d40d2506f0308bb363"),
7575
BloomRoot: common.HexToHash("8242342e66e942c0cd893484e6736b9862ceb88b43ca344bb06a8285ac1b6d64"),
76-
}
77-
)
78-
79-
// trustedCheckpoints associates each known checkpoint with the genesis hash of the chain it belongs to
80-
var trustedCheckpoints = map[common.Hash]TrustedCheckpoint{
81-
params.MainnetGenesisHash: mainnetCheckpoint,
82-
params.TestnetGenesisHash: ropstenCheckpoint,
76+
},
77+
params.RinkebyGenesisHash: {
78+
name: "rinkeby",
79+
SectionIdx: 85,
80+
SectionHead: common.HexToHash("92cfa67afc4ad8ab0dcbc6fa49efd14b5b19402442e7317e6bc879d85f89d64d"),
81+
CHTRoot: common.HexToHash("2802ec92cd7a54a75bca96afdc666ae7b99e5d96cf8192dcfb09588812f51564"),
82+
BloomRoot: common.HexToHash("ebefeb31a9a42866d8cf2d2477704b4c3d7c20d0e4e9b5aaa77f396e016a1263"),
83+
},
8384
}
8485

8586
var (

params/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
var (
2828
MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
2929
TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
30+
RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
3031
)
3132

3233
var (

0 commit comments

Comments
 (0)