aboutsummaryrefslogtreecommitdiffstats
path: root/light/lightchain.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2018-08-21 19:39:28 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-08-21 20:21:59 +0800
commit9f036647e4b3e7c3aa8941dc239f85326a5e5ecd (patch)
tree4a9062b5300e326ad773c0dc010f7d7606480eaa /light/lightchain.go
parent355fc47d396298bccf93c37bdbba9b9e88864790 (diff)
downloaddexon-9f036647e4b3e7c3aa8941dc239f85326a5e5ecd.tar.gz
dexon-9f036647e4b3e7c3aa8941dc239f85326a5e5ecd.tar.zst
dexon-9f036647e4b3e7c3aa8941dc239f85326a5e5ecd.zip
consensus/clique, light: light client snapshots on Rinkeby
Diffstat (limited to 'light/lightchain.go')
-rw-r--r--light/lightchain.go34
1 files changed, 22 insertions, 12 deletions
diff --git a/light/lightchain.go b/light/lightchain.go
index b7e629e88..b5afe1f0e 100644
--- a/light/lightchain.go
+++ b/light/lightchain.go
@@ -464,22 +464,32 @@ func (self *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64)
func (self *LightChain) Config() *params.ChainConfig { return self.hc.Config() }
func (self *LightChain) SyncCht(ctx context.Context) bool {
+ // If we don't have a CHT indexer, abort
if self.odr.ChtIndexer() == nil {
return false
}
- headNum := self.CurrentHeader().Number.Uint64()
- chtCount, _, _ := self.odr.ChtIndexer().Sections()
- if headNum+1 < chtCount*CHTFrequencyClient {
- num := chtCount*CHTFrequencyClient - 1
- header, err := GetHeaderByNumber(ctx, self.odr, num)
- if header != nil && err == nil {
- self.mu.Lock()
- if self.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
- self.hc.SetCurrentHeader(header)
- }
- self.mu.Unlock()
- return true
+ // Ensure the remote CHT head is ahead of us
+ head := self.CurrentHeader().Number.Uint64()
+ sections, _, _ := self.odr.ChtIndexer().Sections()
+
+ latest := sections*CHTFrequencyClient - 1
+ if clique := self.hc.Config().Clique; clique != nil {
+ latest -= latest % clique.Epoch // epoch snapshot for clique
+ }
+ if head >= latest {
+ return false
+ }
+ // Retrieve the latest useful header and update to it
+ if header, err := GetHeaderByNumber(ctx, self.odr, latest); header != nil && err == nil {
+ self.mu.Lock()
+ defer self.mu.Unlock()
+
+ // Ensure the chain didn't move past the latest block while retrieving it
+ if self.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
+ log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash())
+ self.hc.SetCurrentHeader(header)
}
+ return true
}
return false
}