You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

versionbits.h 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_CONSENSUS_VERSIONBITS
  5. #define STARWELS_CONSENSUS_VERSIONBITS
  6. #include "chain.h"
  7. #include <map>
  8. /** What block version to use for new blocks (pre versionbits) */
  9. static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4;
  10. /** What bits to set in version for versionbits blocks */
  11. static const int32_t VERSIONBITS_TOP_BITS = 0x20000000UL;
  12. /** What bitmask determines whether versionbits is in use */
  13. static const int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL;
  14. /** Total bits available for versionbits */
  15. static const int32_t VERSIONBITS_NUM_BITS = 29;
  16. enum ThresholdState {
  17. THRESHOLD_DEFINED,
  18. THRESHOLD_STARTED,
  19. THRESHOLD_LOCKED_IN,
  20. THRESHOLD_ACTIVE,
  21. THRESHOLD_FAILED,
  22. };
  23. // A map that gives the state for blocks whose height is a multiple of Period().
  24. // The map is indexed by the block's parent, however, so all keys in the map
  25. // will either be nullptr or a block with (height + 1) % Period() == 0.
  26. typedef std::map<const CBlockIndex*, ThresholdState> ThresholdConditionCache;
  27. struct VBDeploymentInfo {
  28. /** Deployment name */
  29. const char *name;
  30. /** Whether GBT clients can safely ignore this rule in simplified usage */
  31. bool gbt_force;
  32. };
  33. struct BIP9Stats {
  34. int period;
  35. int threshold;
  36. int elapsed;
  37. int count;
  38. bool possible;
  39. };
  40. extern const struct VBDeploymentInfo VersionBitsDeploymentInfo[];
  41. /**
  42. * Abstract class that implements BIP9-style threshold logic, and caches results.
  43. */
  44. class AbstractThresholdConditionChecker {
  45. protected:
  46. virtual bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const =0;
  47. virtual int64_t BeginTime(const Consensus::Params& params) const =0;
  48. virtual int64_t EndTime(const Consensus::Params& params) const =0;
  49. virtual int Period(const Consensus::Params& params) const =0;
  50. virtual int Threshold(const Consensus::Params& params) const =0;
  51. public:
  52. BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, const Consensus::Params& params) const;
  53. // Note that the functions below take a pindexPrev as input: they compute information for block B based on its parent.
  54. ThresholdState GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const;
  55. int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const;
  56. };
  57. struct VersionBitsCache
  58. {
  59. ThresholdConditionCache caches[Consensus::MAX_VERSION_BITS_DEPLOYMENTS];
  60. void Clear();
  61. };
  62. ThresholdState VersionBitsState(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache);
  63. BIP9Stats VersionBitsStatistics(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
  64. int VersionBitsStateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache);
  65. uint32_t VersionBitsMask(const Consensus::Params& params, Consensus::DeploymentPos pos);
  66. #endif