Bitcoin ABC  0.29.2
P2P Digital Currency
blockstatus.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2019 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_BLOCKSTATUS_H
6 #define BITCOIN_BLOCKSTATUS_H
7 
8 #include <blockvalidity.h>
9 #include <serialize.h>
10 
11 #include <cstdint>
12 
13 struct BlockStatus {
14 private:
15  uint32_t status;
16 
17  explicit constexpr BlockStatus(uint32_t nStatusIn) : status(nStatusIn) {}
18 
19  static const uint32_t VALIDITY_MASK = 0x07;
20 
21  // Full block available in blk*.dat
22  static const uint32_t HAS_DATA_FLAG = 0x08;
23  // Undo data available in rev*.dat
24  static const uint32_t HAS_UNDO_FLAG = 0x10;
25 
26  // The block is invalid.
27  static const uint32_t FAILED_FLAG = 0x20;
28  // The block has an invalid parent.
29  static const uint32_t FAILED_PARENT_FLAG = 0x40;
30 
31  // Mask used to check if the block failed.
32  static const uint32_t INVALID_MASK = FAILED_FLAG | FAILED_PARENT_FLAG;
33 
34  // The block is being parked for some reason. It will be reconsidered if its
35  // chains grows.
36  static const uint32_t PARKED_FLAG = 0x80;
37  // One of the block's parent is parked.
38  static const uint32_t PARKED_PARENT_FLAG = 0x100;
39 
40  // Mask used to check for parked blocks.
41  static const uint32_t PARKED_MASK = PARKED_FLAG | PARKED_PARENT_FLAG;
42 
49  static const uint32_t ASSUMED_VALID_FLAG = 0x200;
50 
51 public:
52  explicit constexpr BlockStatus() : status(0) {}
53 
56  }
57 
59  return BlockStatus((status & ~VALIDITY_MASK) | uint32_t(validity));
60  }
61 
62  bool hasData() const { return status & HAS_DATA_FLAG; }
63  BlockStatus withData(bool hasData = true) const {
64  return BlockStatus((status & ~HAS_DATA_FLAG) |
65  (hasData ? HAS_DATA_FLAG : 0));
66  }
67 
68  bool hasUndo() const { return status & HAS_UNDO_FLAG; }
69  BlockStatus withUndo(bool hasUndo = true) const {
70  return BlockStatus((status & ~HAS_UNDO_FLAG) |
71  (hasUndo ? HAS_UNDO_FLAG : 0));
72  }
73 
74  bool hasFailed() const { return status & FAILED_FLAG; }
75  BlockStatus withFailed(bool hasFailed = true) const {
76  return BlockStatus((status & ~FAILED_FLAG) |
77  (hasFailed ? FAILED_FLAG : 0));
78  }
79 
80  bool hasFailedParent() const { return status & FAILED_PARENT_FLAG; }
84  }
85 
86  bool isParked() const { return status & PARKED_FLAG; }
87  BlockStatus withParked(bool parked = true) const {
88  return BlockStatus((status & ~PARKED_FLAG) |
89  (parked ? PARKED_FLAG : 0));
90  }
91 
92  bool hasParkedParent() const { return status & PARKED_PARENT_FLAG; }
93  BlockStatus withParkedParent(bool parkedParent = true) const {
95  (parkedParent ? PARKED_PARENT_FLAG : 0));
96  }
97 
103  if (isInvalid()) {
104  return false;
105  }
106 
107  return getValidity() >= nUpTo;
108  }
109 
110  bool isAssumedValid() const { return status & ASSUMED_VALID_FLAG; }
111  BlockStatus withAssumedValid(bool assumed_valid = true) const {
113  (assumed_valid ? ASSUMED_VALID_FLAG : 0));
114  }
117  }
118 
119  bool isInvalid() const { return status & INVALID_MASK; }
121  return BlockStatus(status & ~INVALID_MASK);
122  }
123 
124  bool isOnParkedChain() const { return status & PARKED_MASK; }
126  return BlockStatus(status & ~PARKED_MASK);
127  }
128 
130 
131  friend constexpr bool operator==(const BlockStatus a, const BlockStatus b) {
132  return a.status == b.status;
133  }
134 
135  friend constexpr bool operator!=(const BlockStatus a, const BlockStatus b) {
136  return !(a == b);
137  }
138 };
139 
140 #endif // BITCOIN_BLOCKSTATUS_H
BlockValidity
Definition: blockvalidity.h:10
@ TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
#define VARINT(obj)
Definition: serialize.h:579
#define READWRITE(...)
Definition: serialize.h:166
bool isOnParkedChain() const
Definition: blockstatus.h:124
bool isValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Definition: blockstatus.h:102
static const uint32_t PARKED_FLAG
Definition: blockstatus.h:36
bool hasParkedParent() const
Definition: blockstatus.h:92
bool hasFailedParent() const
Definition: blockstatus.h:80
BlockStatus withFailedParent(bool hasFailedParent=true) const
Definition: blockstatus.h:81
BlockStatus withClearedFailureFlags() const
Definition: blockstatus.h:120
bool isAssumedValid() const
Definition: blockstatus.h:110
static const uint32_t PARKED_MASK
Definition: blockstatus.h:41
constexpr BlockStatus()
Definition: blockstatus.h:52
BlockStatus withUndo(bool hasUndo=true) const
Definition: blockstatus.h:69
static const uint32_t ASSUMED_VALID_FLAG
If set, this indicates that the block index entry is assumed-valid.
Definition: blockstatus.h:49
bool isInvalid() const
Definition: blockstatus.h:119
BlockStatus withClearedAssumedValidFlags() const
Definition: blockstatus.h:115
static const uint32_t HAS_UNDO_FLAG
Definition: blockstatus.h:24
static const uint32_t INVALID_MASK
Definition: blockstatus.h:32
static const uint32_t FAILED_PARENT_FLAG
Definition: blockstatus.h:29
static const uint32_t FAILED_FLAG
Definition: blockstatus.h:27
BlockStatus withData(bool hasData=true) const
Definition: blockstatus.h:63
bool hasUndo() const
Definition: blockstatus.h:68
uint32_t status
Definition: blockstatus.h:15
constexpr friend bool operator!=(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:135
BlockStatus withParked(bool parked=true) const
Definition: blockstatus.h:87
bool isParked() const
Definition: blockstatus.h:86
BlockStatus withParkedParent(bool parkedParent=true) const
Definition: blockstatus.h:93
SERIALIZE_METHODS(BlockStatus, obj)
Definition: blockstatus.h:129
static const uint32_t VALIDITY_MASK
Definition: blockstatus.h:19
BlockStatus withClearedParkedFlags() const
Definition: blockstatus.h:125
BlockStatus withAssumedValid(bool assumed_valid=true) const
Definition: blockstatus.h:111
BlockStatus withFailed(bool hasFailed=true) const
Definition: blockstatus.h:75
BlockStatus withValidity(BlockValidity validity) const
Definition: blockstatus.h:58
constexpr BlockStatus(uint32_t nStatusIn)
Definition: blockstatus.h:17
BlockValidity getValidity() const
Definition: blockstatus.h:54
static const uint32_t HAS_DATA_FLAG
Definition: blockstatus.h:22
static const uint32_t PARKED_PARENT_FLAG
Definition: blockstatus.h:38
constexpr friend bool operator==(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:131
bool hasData() const
Definition: blockstatus.h:62
bool hasFailed() const
Definition: blockstatus.h:74