release.go 5.9 KB

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