Bitcoin ABC 0.30.12
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
14private:
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
58 static const uint32_t ASSUMED_VALID_FLAG = 0x200;
59
60public:
61 explicit constexpr BlockStatus() : status(0) {}
62
65 }
66
68 return BlockStatus((status & ~VALIDITY_MASK) | uint32_t(validity));
69 }
70
71 bool hasData() const { return status & HAS_DATA_FLAG; }
72 BlockStatus withData(bool hasData = true) const {
73 return BlockStatus((status & ~HAS_DATA_FLAG) |
74 (hasData ? HAS_DATA_FLAG : 0));
75 }
76
77 bool hasUndo() const { return status & HAS_UNDO_FLAG; }
78 BlockStatus withUndo(bool hasUndo = true) const {
79 return BlockStatus((status & ~HAS_UNDO_FLAG) |
80 (hasUndo ? HAS_UNDO_FLAG : 0));
81 }
82
83 bool hasFailed() const { return status & FAILED_FLAG; }
84 BlockStatus withFailed(bool hasFailed = true) const {
85 return BlockStatus((status & ~FAILED_FLAG) |
86 (hasFailed ? FAILED_FLAG : 0));
87 }
88
89 bool hasFailedParent() const { return status & FAILED_PARENT_FLAG; }
93 }
94
95 bool isParked() const { return status & PARKED_FLAG; }
96 BlockStatus withParked(bool parked = true) const {
97 return BlockStatus((status & ~PARKED_FLAG) |
98 (parked ? PARKED_FLAG : 0));
99 }
100
101 bool hasParkedParent() const { return status & PARKED_PARENT_FLAG; }
102 BlockStatus withParkedParent(bool parkedParent = true) const {
104 (parkedParent ? PARKED_PARENT_FLAG : 0));
105 }
106
112 if (isInvalid()) {
113 return false;
114 }
115
116 return getValidity() >= nUpTo;
117 }
118
119 bool isAssumedValid() const { return status & ASSUMED_VALID_FLAG; }
120 BlockStatus withAssumedValid(bool assumed_valid = true) const {
122 (assumed_valid ? ASSUMED_VALID_FLAG : 0));
123 }
126 }
127
128 bool isInvalid() const { return status & INVALID_MASK; }
131 }
132
133 bool isOnParkedChain() const { return status & PARKED_MASK; }
135 return BlockStatus(status & ~PARKED_MASK);
136 }
137
139
140 friend constexpr bool operator==(const BlockStatus a, const BlockStatus b) {
141 return a.status == b.status;
142 }
143
144 friend constexpr bool operator!=(const BlockStatus a, const BlockStatus b) {
145 return !(a == b);
146 }
147};
148
149#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:133
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:111
static const uint32_t PARKED_FLAG
Definition: blockstatus.h:36
bool hasParkedParent() const
Definition: blockstatus.h:101
bool hasFailedParent() const
Definition: blockstatus.h:89
BlockStatus withFailedParent(bool hasFailedParent=true) const
Definition: blockstatus.h:90
BlockStatus withClearedFailureFlags() const
Definition: blockstatus.h:129
bool isAssumedValid() const
Definition: blockstatus.h:119
friend constexpr bool operator==(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:140
static const uint32_t PARKED_MASK
Definition: blockstatus.h:41
constexpr BlockStatus()
Definition: blockstatus.h:61
BlockStatus withUndo(bool hasUndo=true) const
Definition: blockstatus.h:78
static const uint32_t ASSUMED_VALID_FLAG
If ASSUMED_VALID_FLAG is set, it means that this block has not been validated and has validity status...
Definition: blockstatus.h:58
bool isInvalid() const
Definition: blockstatus.h:128
BlockStatus withClearedAssumedValidFlags() const
Definition: blockstatus.h:124
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:72
bool hasUndo() const
Definition: blockstatus.h:77
uint32_t status
Definition: blockstatus.h:15
BlockStatus withParked(bool parked=true) const
Definition: blockstatus.h:96
bool isParked() const
Definition: blockstatus.h:95
BlockStatus withParkedParent(bool parkedParent=true) const
Definition: blockstatus.h:102
SERIALIZE_METHODS(BlockStatus, obj)
Definition: blockstatus.h:138
static const uint32_t VALIDITY_MASK
Definition: blockstatus.h:19
BlockStatus withClearedParkedFlags() const
Definition: blockstatus.h:134
BlockStatus withAssumedValid(bool assumed_valid=true) const
Definition: blockstatus.h:120
BlockStatus withFailed(bool hasFailed=true) const
Definition: blockstatus.h:84
BlockStatus withValidity(BlockValidity validity) const
Definition: blockstatus.h:67
friend constexpr bool operator!=(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:144
constexpr BlockStatus(uint32_t nStatusIn)
Definition: blockstatus.h:17
BlockValidity getValidity() const
Definition: blockstatus.h:63
static const uint32_t HAS_DATA_FLAG
Definition: blockstatus.h:22
static const uint32_t PARKED_PARENT_FLAG
Definition: blockstatus.h:38
bool hasData() const
Definition: blockstatus.h:71
bool hasFailed() const
Definition: blockstatus.h:83