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 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (c) 2016 The Bitcoin Core 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 BITCOIN_CONSENSUS_VERSIONBITS
  5. #define BITCOIN_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 NULL or a block with (height + 1) % Period() == 0.
  26. typedef std::map<const CBlockIndex*, ThresholdState> ThresholdConditionCache;
  27. /**
  28. * Abstract class that implements BIP9-style threshold logic, and caches results.
  29. */
  30. class AbstractThresholdConditionChecker {
  31. protected:
  32. virtual bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const =0;
  33. virtual int64_t BeginTime(const Consensus::Params& params) const =0;
  34. virtual int64_t EndTime(const Consensus::Params& params) const =0;
  35. virtual int Period(const Consensus::Params& params) const =0;
  36. virtual int Threshold(const Consensus::Params& params) const =0;
  37. public:
  38. // Note that the function below takes a pindexPrev as input: they compute information for block B based on its parent.
  39. ThresholdState GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const;
  40. };
  41. struct VersionBitsCache
  42. {
  43. ThresholdConditionCache caches[Consensus::MAX_VERSION_BITS_DEPLOYMENTS];
  44. void Clear();
  45. };
  46. ThresholdState VersionBitsState(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache);
  47. uint32_t VersionBitsMask(const Consensus::Params& params, Consensus::DeploymentPos pos);
  48. #endif