lightchain.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package light implements on-demand retrieval capable state and chain objects
  17. // for the Ethereum Light Client.
  18. package light
  19. import (
  20. "context"
  21. "errors"
  22. "math/big"
  23. "sync"
  24. "sync/atomic"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/consensus"
  28. "github.com/ethereum/go-ethereum/core"
  29. "github.com/ethereum/go-ethereum/core/rawdb"
  30. "github.com/ethereum/go-ethereum/core/state"
  31. "github.com/ethereum/go-ethereum/core/types"
  32. "github.com/ethereum/go-ethereum/ethdb"
  33. "github.com/ethereum/go-ethereum/event"
  34. "github.com/ethereum/go-ethereum/log"
  35. "github.com/ethereum/go-ethereum/params"
  36. "github.com/ethereum/go-ethereum/rlp"
  37. lru "github.com/hashicorp/golang-lru"
  38. )
  39. var (
  40. bodyCacheLimit = 256
  41. blockCacheLimit = 256
  42. )
  43. // LightChain represents a canonical chain that by default only handles block
  44. // headers, downloading block bodies and receipts on demand through an ODR
  45. // interface. It only does header validation during chain insertion.
  46. type LightChain struct {
  47. hc *core.HeaderChain
  48. indexerConfig *IndexerConfig
  49. chainDb ethdb.Database
  50. engine consensus.Engine
  51. odr OdrBackend
  52. chainFeed event.Feed
  53. chainSideFeed event.Feed
  54. chainHeadFeed event.Feed
  55. scope event.SubscriptionScope
  56. genesisBlock *types.Block
  57. bodyCache *lru.Cache // Cache for the most recent block bodies
  58. bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
  59. blockCache *lru.Cache // Cache for the most recent entire blocks
  60. chainmu sync.RWMutex // protects header inserts
  61. quit chan struct{}
  62. wg sync.WaitGroup
  63. // Atomic boolean switches:
  64. running int32 // whether LightChain is running or stopped
  65. procInterrupt int32 // interrupts chain insert
  66. disableCheckFreq int32 // disables header verification
  67. }
  68. // NewLightChain returns a fully initialised light chain using information
  69. // available in the database. It initialises the default Ethereum header
  70. // validator.
  71. func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.Engine, checkpoint *params.TrustedCheckpoint) (*LightChain, error) {
  72. bodyCache, _ := lru.New(bodyCacheLimit)
  73. bodyRLPCache, _ := lru.New(bodyCacheLimit)
  74. blockCache, _ := lru.New(blockCacheLimit)
  75. bc := &LightChain{
  76. chainDb: odr.Database(),
  77. indexerConfig: odr.IndexerConfig(),
  78. odr: odr,
  79. quit: make(chan struct{}),
  80. bodyCache: bodyCache,
  81. bodyRLPCache: bodyRLPCache,
  82. blockCache: blockCache,
  83. engine: engine,
  84. }
  85. var err error
  86. bc.hc, err = core.NewHeaderChain(odr.Database(), config, bc.engine, bc.getProcInterrupt)
  87. if err != nil {
  88. return nil, err
  89. }
  90. bc.genesisBlock, _ = bc.GetBlockByNumber(NoOdr, 0)
  91. if bc.genesisBlock == nil {
  92. return nil, core.ErrNoGenesis
  93. }
  94. if checkpoint != nil {
  95. bc.AddTrustedCheckpoint(checkpoint)
  96. }
  97. if err := bc.loadLastState(); err != nil {
  98. return nil, err
  99. }
  100. // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
  101. for hash := range core.BadHashes {
  102. if header := bc.GetHeaderByHash(hash); header != nil {
  103. log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash)
  104. bc.SetHead(header.Number.Uint64() - 1)
  105. log.Error("Chain rewind was successful, resuming normal operation")
  106. }
  107. }
  108. return bc, nil
  109. }
  110. // AddTrustedCheckpoint adds a trusted checkpoint to the blockchain
  111. func (lc *LightChain) AddTrustedCheckpoint(cp *params.TrustedCheckpoint) {
  112. if lc.odr.ChtIndexer() != nil {
  113. StoreChtRoot(lc.chainDb, cp.SectionIndex, cp.SectionHead, cp.CHTRoot)
  114. lc.odr.ChtIndexer().AddCheckpoint(cp.SectionIndex, cp.SectionHead)
  115. }
  116. if lc.odr.BloomTrieIndexer() != nil {
  117. StoreBloomTrieRoot(lc.chainDb, cp.SectionIndex, cp.SectionHead, cp.BloomRoot)
  118. lc.odr.BloomTrieIndexer().AddCheckpoint(cp.SectionIndex, cp.SectionHead)
  119. }
  120. if lc.odr.BloomIndexer() != nil {
  121. lc.odr.BloomIndexer().AddCheckpoint(cp.SectionIndex, cp.SectionHead)
  122. }
  123. log.Info("Added trusted checkpoint", "block", (cp.SectionIndex+1)*lc.indexerConfig.ChtSize-1, "hash", cp.SectionHead)
  124. }
  125. func (lc *LightChain) getProcInterrupt() bool {
  126. return atomic.LoadInt32(&lc.procInterrupt) == 1
  127. }
  128. // Odr returns the ODR backend of the chain
  129. func (lc *LightChain) Odr() OdrBackend {
  130. return lc.odr
  131. }
  132. // HeaderChain returns the underlying header chain.
  133. func (lc *LightChain) HeaderChain() *core.HeaderChain {
  134. return lc.hc
  135. }
  136. // loadLastState loads the last known chain state from the database. This method
  137. // assumes that the chain manager mutex is held.
  138. func (lc *LightChain) loadLastState() error {
  139. if head := rawdb.ReadHeadHeaderHash(lc.chainDb); head == (common.Hash{}) {
  140. // Corrupt or empty database, init from scratch
  141. lc.Reset()
  142. } else {
  143. if header := lc.GetHeaderByHash(head); header != nil {
  144. lc.hc.SetCurrentHeader(header)
  145. }
  146. }
  147. // Issue a status log and return
  148. header := lc.hc.CurrentHeader()
  149. headerTd := lc.GetTd(header.Hash(), header.Number.Uint64())
  150. log.Info("Loaded most recent local header", "number", header.Number, "hash", header.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(header.Time), 0)))
  151. return nil
  152. }
  153. // SetHead rewinds the local chain to a new head. Everything above the new
  154. // head will be deleted and the new one set.
  155. func (lc *LightChain) SetHead(head uint64) error {
  156. lc.chainmu.Lock()
  157. defer lc.chainmu.Unlock()
  158. lc.hc.SetHead(head, nil, nil)
  159. return lc.loadLastState()
  160. }
  161. // GasLimit returns the gas limit of the current HEAD block.
  162. func (lc *LightChain) GasLimit() uint64 {
  163. return lc.hc.CurrentHeader().GasLimit
  164. }
  165. // Reset purges the entire blockchain, restoring it to its genesis state.
  166. func (lc *LightChain) Reset() {
  167. lc.ResetWithGenesisBlock(lc.genesisBlock)
  168. }
  169. // ResetWithGenesisBlock purges the entire blockchain, restoring it to the
  170. // specified genesis state.
  171. func (lc *LightChain) ResetWithGenesisBlock(genesis *types.Block) {
  172. // Dump the entire block chain and purge the caches
  173. lc.SetHead(0)
  174. lc.chainmu.Lock()
  175. defer lc.chainmu.Unlock()
  176. // Prepare the genesis block and reinitialise the chain
  177. batch := lc.chainDb.NewBatch()
  178. rawdb.WriteTd(batch, genesis.Hash(), genesis.NumberU64(), genesis.Difficulty())
  179. rawdb.WriteBlock(batch, genesis)
  180. rawdb.WriteHeadHeaderHash(batch, genesis.Hash())
  181. if err := batch.Write(); err != nil {
  182. log.Crit("Failed to reset genesis block", "err", err)
  183. }
  184. lc.genesisBlock = genesis
  185. lc.hc.SetGenesis(lc.genesisBlock.Header())
  186. lc.hc.SetCurrentHeader(lc.genesisBlock.Header())
  187. }
  188. // Accessors
  189. // Engine retrieves the light chain's consensus engine.
  190. func (lc *LightChain) Engine() consensus.Engine { return lc.engine }
  191. // Genesis returns the genesis block
  192. func (lc *LightChain) Genesis() *types.Block {
  193. return lc.genesisBlock
  194. }
  195. func (lc *LightChain) StateCache() state.Database {
  196. panic("not implemented")
  197. }
  198. // GetBody retrieves a block body (transactions and uncles) from the database
  199. // or ODR service by hash, caching it if found.
  200. func (lc *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.Body, error) {
  201. // Short circuit if the body's already in the cache, retrieve otherwise
  202. if cached, ok := lc.bodyCache.Get(hash); ok {
  203. body := cached.(*types.Body)
  204. return body, nil
  205. }
  206. number := lc.hc.GetBlockNumber(hash)
  207. if number == nil {
  208. return nil, errors.New("unknown block")
  209. }
  210. body, err := GetBody(ctx, lc.odr, hash, *number)
  211. if err != nil {
  212. return nil, err
  213. }
  214. // Cache the found body for next time and return
  215. lc.bodyCache.Add(hash, body)
  216. return body, nil
  217. }
  218. // GetBodyRLP retrieves a block body in RLP encoding from the database or
  219. // ODR service by hash, caching it if found.
  220. func (lc *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.RawValue, error) {
  221. // Short circuit if the body's already in the cache, retrieve otherwise
  222. if cached, ok := lc.bodyRLPCache.Get(hash); ok {
  223. return cached.(rlp.RawValue), nil
  224. }
  225. number := lc.hc.GetBlockNumber(hash)
  226. if number == nil {
  227. return nil, errors.New("unknown block")
  228. }
  229. body, err := GetBodyRLP(ctx, lc.odr, hash, *number)
  230. if err != nil {
  231. return nil, err
  232. }
  233. // Cache the found body for next time and return
  234. lc.bodyRLPCache.Add(hash, body)
  235. return body, nil
  236. }
  237. // HasBlock checks if a block is fully present in the database or not, caching
  238. // it if present.
  239. func (lc *LightChain) HasBlock(hash common.Hash, number uint64) bool {
  240. blk, _ := lc.GetBlock(NoOdr, hash, number)
  241. return blk != nil
  242. }
  243. // GetBlock retrieves a block from the database or ODR service by hash and number,
  244. // caching it if found.
  245. func (lc *LightChain) GetBlock(ctx context.Context, hash common.Hash, number uint64) (*types.Block, error) {
  246. // Short circuit if the block's already in the cache, retrieve otherwise
  247. if block, ok := lc.blockCache.Get(hash); ok {
  248. return block.(*types.Block), nil
  249. }
  250. block, err := GetBlock(ctx, lc.odr, hash, number)
  251. if err != nil {
  252. return nil, err
  253. }
  254. // Cache the found block for next time and return
  255. lc.blockCache.Add(block.Hash(), block)
  256. return block, nil
  257. }
  258. // GetBlockByHash retrieves a block from the database or ODR service by hash,
  259. // caching it if found.
  260. func (lc *LightChain) GetBlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  261. number := lc.hc.GetBlockNumber(hash)
  262. if number == nil {
  263. return nil, errors.New("unknown block")
  264. }
  265. return lc.GetBlock(ctx, hash, *number)
  266. }
  267. // GetBlockByNumber retrieves a block from the database or ODR service by
  268. // number, caching it (associated with its hash) if found.
  269. func (lc *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error) {
  270. hash, err := GetCanonicalHash(ctx, lc.odr, number)
  271. if hash == (common.Hash{}) || err != nil {
  272. return nil, err
  273. }
  274. return lc.GetBlock(ctx, hash, number)
  275. }
  276. // Stop stops the blockchain service. If any imports are currently in progress
  277. // it will abort them using the procInterrupt.
  278. func (lc *LightChain) Stop() {
  279. if !atomic.CompareAndSwapInt32(&lc.running, 0, 1) {
  280. return
  281. }
  282. close(lc.quit)
  283. lc.StopInsert()
  284. lc.wg.Wait()
  285. log.Info("Blockchain stopped")
  286. }
  287. // StopInsert interrupts all insertion methods, causing them to return
  288. // errInsertionInterrupted as soon as possible. Insertion is permanently disabled after
  289. // calling this method.
  290. func (lc *LightChain) StopInsert() {
  291. atomic.StoreInt32(&lc.procInterrupt, 1)
  292. }
  293. // Rollback is designed to remove a chain of links from the database that aren't
  294. // certain enough to be valid.
  295. func (lc *LightChain) Rollback(chain []common.Hash) {
  296. lc.chainmu.Lock()
  297. defer lc.chainmu.Unlock()
  298. batch := lc.chainDb.NewBatch()
  299. for i := len(chain) - 1; i >= 0; i-- {
  300. hash := chain[i]
  301. // Degrade the chain markers if they are explicitly reverted.
  302. // In theory we should update all in-memory markers in the
  303. // last step, however the direction of rollback is from high
  304. // to low, so it's safe the update in-memory markers directly.
  305. if head := lc.hc.CurrentHeader(); head.Hash() == hash {
  306. rawdb.WriteHeadHeaderHash(batch, head.ParentHash)
  307. lc.hc.SetCurrentHeader(lc.GetHeader(head.ParentHash, head.Number.Uint64()-1))
  308. }
  309. }
  310. if err := batch.Write(); err != nil {
  311. log.Crit("Failed to rollback light chain", "error", err)
  312. }
  313. }
  314. // postChainEvents iterates over the events generated by a chain insertion and
  315. // posts them into the event feed.
  316. func (lc *LightChain) postChainEvents(events []interface{}) {
  317. for _, event := range events {
  318. switch ev := event.(type) {
  319. case core.ChainEvent:
  320. if lc.CurrentHeader().Hash() == ev.Hash {
  321. lc.chainHeadFeed.Send(core.ChainHeadEvent{Block: ev.Block})
  322. }
  323. lc.chainFeed.Send(ev)
  324. case core.ChainSideEvent:
  325. lc.chainSideFeed.Send(ev)
  326. }
  327. }
  328. }
  329. // InsertHeaderChain attempts to insert the given header chain in to the local
  330. // chain, possibly creating a reorg. If an error is returned, it will return the
  331. // index number of the failing header as well an error describing what went wrong.
  332. //
  333. // The verify parameter can be used to fine tune whether nonce verification
  334. // should be done or not. The reason behind the optional check is because some
  335. // of the header retrieval mechanisms already need to verfy nonces, as well as
  336. // because nonces can be verified sparsely, not needing to check each.
  337. //
  338. // In the case of a light chain, InsertHeaderChain also creates and posts light
  339. // chain events when necessary.
  340. func (lc *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
  341. if atomic.LoadInt32(&lc.disableCheckFreq) == 1 {
  342. checkFreq = 0
  343. }
  344. start := time.Now()
  345. if i, err := lc.hc.ValidateHeaderChain(chain, checkFreq); err != nil {
  346. return i, err
  347. }
  348. // Make sure only one thread manipulates the chain at once
  349. lc.chainmu.Lock()
  350. defer lc.chainmu.Unlock()
  351. lc.wg.Add(1)
  352. defer lc.wg.Done()
  353. var events []interface{}
  354. whFunc := func(header *types.Header) error {
  355. status, err := lc.hc.WriteHeader(header)
  356. switch status {
  357. case core.CanonStatTy:
  358. log.Debug("Inserted new header", "number", header.Number, "hash", header.Hash())
  359. events = append(events, core.ChainEvent{Block: types.NewBlockWithHeader(header), Hash: header.Hash()})
  360. case core.SideStatTy:
  361. log.Debug("Inserted forked header", "number", header.Number, "hash", header.Hash())
  362. events = append(events, core.ChainSideEvent{Block: types.NewBlockWithHeader(header)})
  363. }
  364. return err
  365. }
  366. i, err := lc.hc.InsertHeaderChain(chain, whFunc, start)
  367. lc.postChainEvents(events)
  368. return i, err
  369. }
  370. // CurrentHeader retrieves the current head header of the canonical chain. The
  371. // header is retrieved from the HeaderChain's internal cache.
  372. func (lc *LightChain) CurrentHeader() *types.Header {
  373. return lc.hc.CurrentHeader()
  374. }
  375. // GetTd retrieves a block's total difficulty in the canonical chain from the
  376. // database by hash and number, caching it if found.
  377. func (lc *LightChain) GetTd(hash common.Hash, number uint64) *big.Int {
  378. return lc.hc.GetTd(hash, number)
  379. }
  380. // GetTdByHash retrieves a block's total difficulty in the canonical chain from the
  381. // database by hash, caching it if found.
  382. func (lc *LightChain) GetTdByHash(hash common.Hash) *big.Int {
  383. return lc.hc.GetTdByHash(hash)
  384. }
  385. // GetHeader retrieves a block header from the database by hash and number,
  386. // caching it if found.
  387. func (lc *LightChain) GetHeader(hash common.Hash, number uint64) *types.Header {
  388. return lc.hc.GetHeader(hash, number)
  389. }
  390. // GetHeaderByHash retrieves a block header from the database by hash, caching it if
  391. // found.
  392. func (lc *LightChain) GetHeaderByHash(hash common.Hash) *types.Header {
  393. return lc.hc.GetHeaderByHash(hash)
  394. }
  395. // HasHeader checks if a block header is present in the database or not, caching
  396. // it if present.
  397. func (lc *LightChain) HasHeader(hash common.Hash, number uint64) bool {
  398. return lc.hc.HasHeader(hash, number)
  399. }
  400. // GetCanonicalHash returns the canonical hash for a given block number
  401. func (bc *LightChain) GetCanonicalHash(number uint64) common.Hash {
  402. return bc.hc.GetCanonicalHash(number)
  403. }
  404. // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
  405. // hash, fetching towards the genesis block.
  406. func (lc *LightChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
  407. return lc.hc.GetBlockHashesFromHash(hash, max)
  408. }
  409. // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
  410. // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
  411. // number of blocks to be individually checked before we reach the canonical chain.
  412. //
  413. // Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
  414. func (lc *LightChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
  415. return lc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical)
  416. }
  417. // GetHeaderByNumber retrieves a block header from the database by number,
  418. // caching it (associated with its hash) if found.
  419. func (lc *LightChain) GetHeaderByNumber(number uint64) *types.Header {
  420. return lc.hc.GetHeaderByNumber(number)
  421. }
  422. // GetHeaderByNumberOdr retrieves a block header from the database or network
  423. // by number, caching it (associated with its hash) if found.
  424. func (lc *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64) (*types.Header, error) {
  425. if header := lc.hc.GetHeaderByNumber(number); header != nil {
  426. return header, nil
  427. }
  428. return GetHeaderByNumber(ctx, lc.odr, number)
  429. }
  430. // Config retrieves the header chain's chain configuration.
  431. func (lc *LightChain) Config() *params.ChainConfig { return lc.hc.Config() }
  432. // SyncCheckpoint fetches the checkpoint point block header according to
  433. // the checkpoint provided by the remote peer.
  434. //
  435. // Note if we are running the clique, fetches the last epoch snapshot header
  436. // which covered by checkpoint.
  437. func (lc *LightChain) SyncCheckpoint(ctx context.Context, checkpoint *params.TrustedCheckpoint) bool {
  438. // Ensure the remote checkpoint head is ahead of us
  439. head := lc.CurrentHeader().Number.Uint64()
  440. latest := (checkpoint.SectionIndex+1)*lc.indexerConfig.ChtSize - 1
  441. if clique := lc.hc.Config().Clique; clique != nil {
  442. latest -= latest % clique.Epoch // epoch snapshot for clique
  443. }
  444. if head >= latest {
  445. return true
  446. }
  447. // Retrieve the latest useful header and update to it
  448. if header, err := GetHeaderByNumber(ctx, lc.odr, latest); header != nil && err == nil {
  449. lc.chainmu.Lock()
  450. defer lc.chainmu.Unlock()
  451. // Ensure the chain didn't move past the latest block while retrieving it
  452. if lc.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
  453. log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash(), "age", common.PrettyAge(time.Unix(int64(header.Time), 0)))
  454. rawdb.WriteHeadHeaderHash(lc.chainDb, header.Hash())
  455. lc.hc.SetCurrentHeader(header)
  456. }
  457. return true
  458. }
  459. return false
  460. }
  461. // LockChain locks the chain mutex for reading so that multiple canonical hashes can be
  462. // retrieved while it is guaranteed that they belong to the same version of the chain
  463. func (lc *LightChain) LockChain() {
  464. lc.chainmu.RLock()
  465. }
  466. // UnlockChain unlocks the chain mutex
  467. func (lc *LightChain) UnlockChain() {
  468. lc.chainmu.RUnlock()
  469. }
  470. // SubscribeChainEvent registers a subscription of ChainEvent.
  471. func (lc *LightChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  472. return lc.scope.Track(lc.chainFeed.Subscribe(ch))
  473. }
  474. // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
  475. func (lc *LightChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  476. return lc.scope.Track(lc.chainHeadFeed.Subscribe(ch))
  477. }
  478. // SubscribeChainSideEvent registers a subscription of ChainSideEvent.
  479. func (lc *LightChain) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  480. return lc.scope.Track(lc.chainSideFeed.Subscribe(ch))
  481. }
  482. // SubscribeLogsEvent implements the interface of filters.Backend
  483. // LightChain does not send logs events, so return an empty subscription.
  484. func (lc *LightChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  485. return lc.scope.Track(new(event.Feed).Subscribe(ch))
  486. }
  487. // SubscribeRemovedLogsEvent implements the interface of filters.Backend
  488. // LightChain does not send core.RemovedLogsEvent, so return an empty subscription.
  489. func (lc *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  490. return lc.scope.Track(new(event.Feed).Subscribe(ch))
  491. }
  492. // DisableCheckFreq disables header validation. This is used for ultralight mode.
  493. func (lc *LightChain) DisableCheckFreq() {
  494. atomic.StoreInt32(&lc.disableCheckFreq, 1)
  495. }
  496. // EnableCheckFreq enables header validation.
  497. func (lc *LightChain) EnableCheckFreq() {
  498. atomic.StoreInt32(&lc.disableCheckFreq, 0)
  499. }