Bitcoin ABC 0.31.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
43 // Unused flag that was previously set on assumeutxo snapshot blocks and
44 // their ancestors before they were validated, and unset when they were
45 // validated.
46 static const uint32_t RESERVED_FLAG = 0x200;
47
48public:
49 explicit constexpr BlockStatus() : status(0) {}
50
53 }
54
56 return BlockStatus((status & ~VALIDITY_MASK) | uint32_t(validity));
57 }
58
59 bool hasData() const { return status & HAS_DATA_FLAG; }
60 BlockStatus withData(bool hasData = true) const {
61 return BlockStatus((status & ~HAS_DATA_FLAG) |
62 (hasData ? HAS_DATA_FLAG : 0));
63 }
64
65 bool hasUndo() const { return status & HAS_UNDO_FLAG; }
66 BlockStatus withUndo(bool hasUndo = true) const {
67 return BlockStatus((status & ~HAS_UNDO_FLAG) |
68 (hasUndo ? HAS_UNDO_FLAG : 0));
69 }
70
71 bool hasFailed() const { return status & FAILED_FLAG; }
72 BlockStatus withFailed(bool hasFailed = true) const {
73 return BlockStatus((status & ~FAILED_FLAG) |
74 (hasFailed ? FAILED_FLAG : 0));
75 }
76
77 bool hasFailedParent() const { return status & FAILED_PARENT_FLAG; }
81 }
82
83 bool isParked() const { return status & PARKED_FLAG; }
84 BlockStatus withParked(bool parked = true) const {
85 return BlockStatus((status & ~PARKED_FLAG) |
86 (parked ? PARKED_FLAG : 0));
87 }
88
89 bool hasParkedParent() const { return status & PARKED_PARENT_FLAG; }
90 BlockStatus withParkedParent(bool parkedParent = true) const {
92 (parkedParent ? PARKED_PARENT_FLAG : 0));
93 }
94
100 if (isInvalid()) {
101 return false;
102 }
103
104 return getValidity() >= nUpTo;
105 }
106
107 bool isInvalid() const { return status & INVALID_MASK; }
110 }
111
112 bool isOnParkedChain() const { return status & PARKED_MASK; }
114 return BlockStatus(status & ~PARKED_MASK);
115 }
116
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
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:112
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:99
static const uint32_t PARKED_FLAG
Definition: blockstatus.h:36
bool hasParkedParent() const
Definition: blockstatus.h:89
bool hasFailedParent() const
Definition: blockstatus.h:77
BlockStatus withFailedParent(bool hasFailedParent=true) const
Definition: blockstatus.h:78
BlockStatus withClearedFailureFlags() const
Definition: blockstatus.h:108
friend constexpr bool operator==(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:119
static const uint32_t PARKED_MASK
Definition: blockstatus.h:41
static const uint32_t RESERVED_FLAG
Definition: blockstatus.h:46
constexpr BlockStatus()
Definition: blockstatus.h:49
BlockStatus withUndo(bool hasUndo=true) const
Definition: blockstatus.h:66
bool isInvalid() const
Definition: blockstatus.h:107
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:60
bool hasUndo() const
Definition: blockstatus.h:65
uint32_t status
Definition: blockstatus.h:15
BlockStatus withParked(bool parked=true) const
Definition: blockstatus.h:84
bool isParked() const
Definition: blockstatus.h:83
BlockStatus withParkedParent(bool parkedParent=true) const
Definition: blockstatus.h:90
SERIALIZE_METHODS(BlockStatus, obj)
Definition: blockstatus.h:117
static const uint32_t VALIDITY_MASK
Definition: blockstatus.h:19
BlockStatus withClearedParkedFlags() const
Definition: blockstatus.h:113
BlockStatus withFailed(bool hasFailed=true) const
Definition: blockstatus.h:72
BlockStatus withValidity(BlockValidity validity) const
Definition: blockstatus.h:55
friend constexpr bool operator!=(const BlockStatus a, const BlockStatus b)
Definition: blockstatus.h:123
constexpr BlockStatus(uint32_t nStatusIn)
Definition: blockstatus.h:17
BlockValidity getValidity() const
Definition: blockstatus.h:51
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:59
bool hasFailed() const
Definition: blockstatus.h:71