Bitcoin ABC  0.22.13
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 
43 public:
44  explicit constexpr BlockStatus() : status(0) {}
45 
47  return BlockValidity(status & VALIDITY_MASK);
48  }
49 
51  return BlockStatus((status & ~VALIDITY_MASK) | uint32_t(validity));
52  }
53 
54  bool hasData() const { return status & HAS_DATA_FLAG; }
55  BlockStatus withData(bool hasData = true) const {
56  return BlockStatus((status & ~HAS_DATA_FLAG) |
57  (hasData ? HAS_DATA_FLAG : 0));
58  }
59 
60  bool hasUndo() const { return status & HAS_UNDO_FLAG; }
61  BlockStatus withUndo(bool hasUndo = true) const {
62  return BlockStatus((status & ~HAS_UNDO_FLAG) |
63  (hasUndo ? HAS_UNDO_FLAG : 0));
64  }
65 
66  bool hasFailed() const { return status & FAILED_FLAG; }
67  BlockStatus withFailed(bool hasFailed = true) const {
68  return BlockStatus((status & ~FAILED_FLAG) |
69  (hasFailed ? FAILED_FLAG : 0));
70  }
71 
72  bool hasFailedParent() const { return status & FAILED_PARENT_FLAG; }
74  return BlockStatus((status & ~FAILED_PARENT_FLAG) |
75  (hasFailedParent ? FAILED_PARENT_FLAG : 0));
76  }
77 
78  bool isParked() const { return status & PARKED_FLAG; }
79  BlockStatus withParked(bool parked = true) const {
80  return BlockStatus((status & ~PARKED_FLAG) |
81  (parked ? PARKED_FLAG : 0));
82  }
83 
84  bool hasParkedParent() const { return status & PARKED_PARENT_FLAG; }
85  BlockStatus withParkedParent(bool parkedParent = true) const {
86  return BlockStatus((status & ~PARKED_PARENT_FLAG) |
87  (parkedParent ? PARKED_PARENT_FLAG : 0));
88  }
89 
95  if (isInvalid()) {
96  return false;
97  }
98 
99  return getValidity() >= nUpTo;
100  }
101 
102  bool isInvalid() const { return status & INVALID_MASK; }
104  return BlockStatus(status & ~INVALID_MASK);
105  }
106 
107  bool isOnParkedChain() const { return status & PARKED_MASK; }
109  return BlockStatus(status & ~PARKED_MASK);
110  }
111 
113 
114  template <typename Stream, typename Operation>
115  inline void SerializationOp(Stream &s, Operation ser_action) {
116  READWRITE(VARINT(status));
117  }
118 
119  friend constexpr bool operator==(const BlockStatus a, const BlockStatus b) {
120  return a.status == b.status;
121  }
122 
123  friend constexpr bool operator!=(const BlockStatus a, const BlockStatus b) {
124  return !(a == b);
125  }
126 };
127 
128 #endif // BITCOIN_BLOCKSTATUS_H
#define VARINT(obj)
Definition: serialize.h:608
uint32_t status
Definition: blockstatus.h:15
BlockStatus withFailed(bool hasFailed=true) const
Definition: blockstatus.h:67
void SerializationOp(Stream &s, Operation ser_action)
Definition: blockstatus.h:115
BlockStatus withFailedParent(bool hasFailedParent=true) const
Definition: blockstatus.h:73
static const uint32_t INVALID_MASK
Definition: blockstatus.h:32
BlockStatus withValidity(BlockValidity validity) const
Definition: blockstatus.h:50
BlockStatus withUndo(bool hasUndo=true) const
Definition: blockstatus.h:61
bool hasData() const
Definition: blockstatus.h:54
bool hasParkedParent() const
Definition: blockstatus.h:84
BlockStatus withParkedParent(bool parkedParent=true) const
Definition: blockstatus.h:85
static const uint32_t PARKED_PARENT_FLAG
Definition: blockstatus.h:38
BlockValidity
Definition: blockvalidity.h:10
bool isParked() const
Definition: blockstatus.h:78
bool hasUndo() const
Definition: blockstatus.h:60
static const uint32_t HAS_DATA_FLAG
Definition: blockstatus.h:22
friend constexpr bool operator==(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:119
bool isInvalid() const
Definition: blockstatus.h:102
static const uint32_t FAILED_PARENT_FLAG
Definition: blockstatus.h:29
BlockStatus withClearedFailureFlags() const
Definition: blockstatus.h:103
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
bool hasFailedParent() const
Definition: blockstatus.h:72
constexpr BlockStatus()
Definition: blockstatus.h:44
bool isOnParkedChain() const
Definition: blockstatus.h:107
static const uint32_t FAILED_FLAG
Definition: blockstatus.h:27
friend constexpr bool operator!=(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:123
bool hasFailed() const
Definition: blockstatus.h:66
static const uint32_t HAS_UNDO_FLAG
Definition: blockstatus.h:24
BlockStatus withParked(bool parked=true) const
Definition: blockstatus.h:79
static const uint32_t PARKED_FLAG
Definition: blockstatus.h:36
BlockValidity getValidity() const
Definition: blockstatus.h:46
static const uint32_t PARKED_MASK
Definition: blockstatus.h:41
BlockStatus withClearedParkedFlags() const
Definition: blockstatus.h:108
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:94
BlockStatus withData(bool hasData=true) const
Definition: blockstatus.h:55
#define READWRITE(...)
Definition: serialize.h:191
static const uint32_t VALIDITY_MASK
Definition: blockstatus.h:19
constexpr BlockStatus(uint32_t nStatusIn)
Definition: blockstatus.h:17