release.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2015 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 release contains the node service that tracks client releases.
  17. package release
  18. //go:generate abigen --sol ./contract.sol --pkg release --out ./contract.go
  19. import (
  20. "fmt"
  21. "strings"
  22. "time"
  23. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/eth"
  26. "github.com/ethereum/go-ethereum/internal/ethapi"
  27. "github.com/ethereum/go-ethereum/les"
  28. "github.com/ethereum/go-ethereum/log"
  29. "github.com/ethereum/go-ethereum/node"
  30. "github.com/ethereum/go-ethereum/p2p"
  31. "github.com/ethereum/go-ethereum/rpc"
  32. "golang.org/x/net/context"
  33. )
  34. // Interval to check for new releases
  35. const releaseRecheckInterval = time.Hour
  36. // Config contains the configurations of the release service.
  37. type Config struct {
  38. Oracle common.Address // Ethereum address of the release oracle
  39. Major uint32 // Major version component of the release
  40. Minor uint32 // Minor version component of the release
  41. Patch uint32 // Patch version component of the release
  42. Commit [20]byte // Git SHA1 commit hash of the release
  43. }
  44. // ReleaseService is a node service that periodically checks the blockchain for
  45. // newly released versions of the client being run and issues a warning to the
  46. // user about it.
  47. type ReleaseService struct {
  48. config Config // Current version to check releases against
  49. oracle *ReleaseOracle // Native binding to the release oracle contract
  50. quit chan chan error // Quit channel to terminate the version checker
  51. }
  52. // NewReleaseService creates a new service to periodically check for new client
  53. // releases and notify the user of such.
  54. func NewReleaseService(ctx *node.ServiceContext, config Config) (node.Service, error) {
  55. // Retrieve the Ethereum service dependency to access the blockchain
  56. var apiBackend ethapi.Backend
  57. var ethereum *eth.Ethereum
  58. if err := ctx.Service(&ethereum); err == nil {
  59. apiBackend = ethereum.ApiBackend
  60. } else {
  61. var ethereum *les.LightEthereum
  62. if err := ctx.Service(&ethereum); err == nil {
  63. apiBackend = ethereum.ApiBackend
  64. } else {
  65. return nil, err
  66. }
  67. }
  68. // Construct the release service
  69. contract, err := NewReleaseOracle(config.Oracle, eth.NewContractBackend(apiBackend))
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &ReleaseService{
  74. config: config,
  75. oracle: contract,
  76. quit: make(chan chan error),
  77. }, nil
  78. }
  79. // Protocols returns an empty list of P2P protocols as the release service does
  80. // not have a networking component.
  81. func (r *ReleaseService) Protocols() []p2p.Protocol { return nil }
  82. // APIs returns an empty list of RPC descriptors as the release service does not
  83. // expose any functioanlity to the outside world.
  84. func (r *ReleaseService) APIs() []rpc.API { return nil }
  85. // Start spawns the periodic version checker goroutine
  86. func (r *ReleaseService) Start(server *p2p.Server) error {
  87. go r.checker()
  88. return nil
  89. }
  90. // Stop terminates all goroutines belonging to the service, blocking until they
  91. // are all terminated.
  92. func (r *ReleaseService) Stop() error {
  93. errc := make(chan error)
  94. r.quit <- errc
  95. return <-errc
  96. }
  97. // checker runs indefinitely in the background, periodically checking for new
  98. // client releases.
  99. func (r *ReleaseService) checker() {
  100. // Set up the timers to periodically check for releases
  101. timer := time.NewTimer(0) // Immediately fire a version check
  102. defer timer.Stop()
  103. for {
  104. select {
  105. // If the time arrived, check for a new release
  106. case <-timer.C:
  107. // Rechedule the timer before continuing
  108. timer.Reset(releaseRecheckInterval)
  109. // Retrieve the current version, and handle missing contracts gracefully
  110. ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
  111. opts := &bind.CallOpts{Context: ctx}
  112. version, err := r.oracle.CurrentVersion(opts)
  113. if err != nil {
  114. if err == bind.ErrNoCode {
  115. log.Debug(fmt.Sprintf("Release oracle not found at %x", r.config.Oracle))
  116. continue
  117. }
  118. log.Error(fmt.Sprintf("Failed to retrieve current release: %v", err))
  119. continue
  120. }
  121. // Version was successfully retrieved, notify if newer than ours
  122. if version.Major > r.config.Major ||
  123. (version.Major == r.config.Major && version.Minor > r.config.Minor) ||
  124. (version.Major == r.config.Major && version.Minor == r.config.Minor && version.Patch > r.config.Patch) {
  125. warning := fmt.Sprintf("Client v%d.%d.%d-%x seems older than the latest upstream release v%d.%d.%d-%x",
  126. r.config.Major, r.config.Minor, r.config.Patch, r.config.Commit[:4], version.Major, version.Minor, version.Patch, version.Commit[:4])
  127. howtofix := fmt.Sprintf("Please check https://github.com/ethereum/go-ethereum/releases for new releases")
  128. separator := strings.Repeat("-", len(warning))
  129. log.Warn(fmt.Sprint(separator))
  130. log.Warn(fmt.Sprint(warning))
  131. log.Warn(fmt.Sprint(howtofix))
  132. log.Warn(fmt.Sprint(separator))
  133. } else {
  134. log.Debug(fmt.Sprintf("Client v%d.%d.%d-%x seems up to date with upstream v%d.%d.%d-%x",
  135. r.config.Major, r.config.Minor, r.config.Patch, r.config.Commit[:4], version.Major, version.Minor, version.Patch, version.Commit[:4]))
  136. }
  137. // If termination was requested, return
  138. case errc := <-r.quit:
  139. errc <- nil
  140. return
  141. }
  142. }
  143. }