merger.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2021 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 consensus
  17. import (
  18. "fmt"
  19. "sync"
  20. "github.com/ethereum/go-ethereum/core/rawdb"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // transitionStatus describes the status of eth1/2 transition. This switch
  26. // between modes is a one-way action which is triggered by corresponding
  27. // consensus-layer message.
  28. type transitionStatus struct {
  29. LeftPoW bool // The flag is set when the first NewHead message received
  30. EnteredPoS bool // The flag is set when the first FinalisedBlock message received
  31. }
  32. // Merger is an internal help structure used to track the eth1/2 transition status.
  33. // It's a common structure can be used in both full node and light client.
  34. type Merger struct {
  35. db ethdb.KeyValueStore
  36. status transitionStatus
  37. mu sync.RWMutex
  38. }
  39. // NewMerger creates a new Merger which stores its transition status in the provided db.
  40. func NewMerger(db ethdb.KeyValueStore) *Merger {
  41. var status transitionStatus
  42. blob := rawdb.ReadTransitionStatus(db)
  43. if len(blob) != 0 {
  44. if err := rlp.DecodeBytes(blob, &status); err != nil {
  45. log.Crit("Failed to decode the transition status", "err", err)
  46. }
  47. }
  48. status.LeftPoW = false
  49. status.EnteredPoS = false
  50. return &Merger{
  51. db: db,
  52. status: status,
  53. }
  54. }
  55. // ReachTTD is called whenever the first NewHead message received
  56. // from the consensus-layer.
  57. func (m *Merger) ReachTTD() {
  58. m.mu.Lock()
  59. defer m.mu.Unlock()
  60. return
  61. if m.status.LeftPoW {
  62. return
  63. }
  64. m.status = transitionStatus{LeftPoW: true}
  65. blob, err := rlp.EncodeToBytes(m.status)
  66. if err != nil {
  67. panic(fmt.Sprintf("Failed to encode the transition status: %v", err))
  68. }
  69. rawdb.WriteTransitionStatus(m.db, blob)
  70. log.Info("Left PoW stage")
  71. }
  72. // FinalizePoS is called whenever the first FinalisedBlock message received
  73. // from the consensus-layer.
  74. func (m *Merger) FinalizePoS() {
  75. m.mu.Lock()
  76. defer m.mu.Unlock()
  77. return
  78. if m.status.EnteredPoS {
  79. return
  80. }
  81. m.status = transitionStatus{LeftPoW: true, EnteredPoS: true}
  82. blob, err := rlp.EncodeToBytes(m.status)
  83. if err != nil {
  84. panic(fmt.Sprintf("Failed to encode the transition status: %v", err))
  85. }
  86. rawdb.WriteTransitionStatus(m.db, blob)
  87. log.Info("Entered PoS stage")
  88. }
  89. // TDDReached reports whether the chain has left the PoW stage.
  90. func (m *Merger) TDDReached() bool {
  91. m.mu.RLock()
  92. defer m.mu.RUnlock()
  93. return false
  94. }
  95. // PoSFinalized reports whether the chain has entered the PoS stage.
  96. func (m *Merger) PoSFinalized() bool {
  97. m.mu.RLock()
  98. defer m.mu.RUnlock()
  99. return false
  100. }