Changeset View
Changeset View
Standalone View
Standalone View
src/chain.h
Show First 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | enum class BlockValidity : uint32_t { | ||||
CHAIN = 4, | CHAIN = 4, | ||||
/** | /** | ||||
* Scripts & signatures ok. Implies all parents are also at least SCRIPTS. | * Scripts & signatures ok. Implies all parents are also at least SCRIPTS. | ||||
*/ | */ | ||||
SCRIPTS = 5, | SCRIPTS = 5, | ||||
}; | }; | ||||
enum BlockStatusEnum : uint32_t { | struct BlockStatus { | ||||
/** | private: | ||||
* Validity levels. | uint32_t status; | ||||
*/ | |||||
BLOCK_VALID_UNKNOWN = uint32_t(BlockValidity::UNKNOWN), | |||||
BLOCK_VALID_HEADER = uint32_t(BlockValidity::HEADER), | |||||
BLOCK_VALID_TREE = uint32_t(BlockValidity::TREE), | |||||
BLOCK_VALID_TRANSACTIONS = uint32_t(BlockValidity::TRANSACTIONS), | |||||
BLOCK_VALID_CHAIN = uint32_t(BlockValidity::CHAIN), | |||||
BLOCK_VALID_SCRIPTS = uint32_t(BlockValidity::SCRIPTS), | |||||
/** | explicit BlockStatus(uint32_t nStatusIn) : status(nStatusIn) {} | ||||
* All validity bits. | |||||
*/ | |||||
BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | | |||||
BLOCK_VALID_TRANSACTIONS | BLOCK_VALID_CHAIN | | |||||
BLOCK_VALID_SCRIPTS, | |||||
// Just a helper | static const uint32_t VALIDITY_MASK = 0x07; | ||||
BLOCK_HAVE_NOTHING = 0, | |||||
// Full block available in blk*.dat | // Full block available in blk*.dat | ||||
BLOCK_HAVE_DATA = 8, | static const uint32_t HAS_DATA_FLAG = 0x08; | ||||
// Undo data available in rev*.dat | // Undo data available in rev*.dat | ||||
BLOCK_HAVE_UNDO = 16, | static const uint32_t HAS_UNDO_FLAG = 0x10; | ||||
BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO, | |||||
// The block is invalid. | // The block is invalid. | ||||
BLOCK_FAILED_VALID = 32, | static const uint32_t FAILED_FLAG = 0x20; | ||||
// The block has an invalid parent. | // The block has an invalid parent. | ||||
BLOCK_FAILED_CHILD = 64, | static const uint32_t FAILED_PARENT_FLAG = 0x40; | ||||
BLOCK_INVALID_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD, | |||||
}; | |||||
struct BlockStatus { | // Mask used to check if the block failed. | ||||
private: | static const uint32_t INVALID_MASK = FAILED_FLAG | FAILED_PARENT_FLAG; | ||||
uint32_t status; | |||||
explicit BlockStatus(uint32_t nStatusIn) : status(nStatusIn) {} | |||||
public: | public: | ||||
explicit BlockStatus() : status(BLOCK_VALID_UNKNOWN) {} | explicit BlockStatus() : status(0) {} | ||||
BlockValidity getValidity() const { | BlockValidity getValidity() const { | ||||
return BlockValidity(status & BLOCK_VALID_MASK); | return BlockValidity(status & VALIDITY_MASK); | ||||
} | } | ||||
BlockStatus withValidity(BlockValidity validity) const { | BlockStatus withValidity(BlockValidity validity) const { | ||||
return BlockStatus((status & ~BLOCK_VALID_MASK) | uint32_t(validity)); | return BlockStatus((status & ~VALIDITY_MASK) | uint32_t(validity)); | ||||
} | } | ||||
bool hasData() const { return status & BLOCK_HAVE_DATA; } | bool hasData() const { return status & HAS_DATA_FLAG; } | ||||
BlockStatus withData(bool hasData = true) const { | BlockStatus withData(bool hasData = true) const { | ||||
return BlockStatus((status & ~BLOCK_HAVE_DATA) | | return BlockStatus((status & ~HAS_DATA_FLAG) | | ||||
(hasData ? BLOCK_HAVE_DATA : BLOCK_HAVE_NOTHING)); | (hasData ? HAS_DATA_FLAG : 0)); | ||||
} | } | ||||
bool hasUndo() const { return status & BLOCK_HAVE_UNDO; } | bool hasUndo() const { return status & HAS_UNDO_FLAG; } | ||||
BlockStatus withUndo(bool hasUndo = true) const { | BlockStatus withUndo(bool hasUndo = true) const { | ||||
return BlockStatus((status & ~BLOCK_HAVE_UNDO) | | return BlockStatus((status & ~HAS_UNDO_FLAG) | | ||||
(hasUndo ? BLOCK_HAVE_UNDO : BLOCK_HAVE_NOTHING)); | (hasUndo ? HAS_UNDO_FLAG : 0)); | ||||
} | } | ||||
bool hasFailed() const { return status & BLOCK_FAILED_VALID; } | bool hasFailed() const { return status & FAILED_FLAG; } | ||||
BlockStatus withFailed(bool hasFailed = true) const { | BlockStatus withFailed(bool hasFailed = true) const { | ||||
return BlockStatus( | return BlockStatus((status & ~FAILED_FLAG) | | ||||
(status & ~BLOCK_FAILED_VALID) | | (hasFailed ? FAILED_FLAG : 0)); | ||||
(hasFailed ? BLOCK_FAILED_VALID : BLOCK_HAVE_NOTHING)); | |||||
} | } | ||||
bool hasFailedParent() const { return status & BLOCK_FAILED_CHILD; } | bool hasFailedParent() const { return status & FAILED_PARENT_FLAG; } | ||||
BlockStatus withFailedParent(bool hasFailedParent = true) const { | BlockStatus withFailedParent(bool hasFailedParent = true) const { | ||||
return BlockStatus( | return BlockStatus((status & ~FAILED_PARENT_FLAG) | | ||||
(status & ~BLOCK_FAILED_CHILD) | | (hasFailedParent ? FAILED_PARENT_FLAG : 0)); | ||||
(hasFailedParent ? BLOCK_FAILED_CHILD : BLOCK_HAVE_NOTHING)); | |||||
} | } | ||||
/** | /** | ||||
* Check whether this block index entry is valid up to the passed validity | * Check whether this block index entry is valid up to the passed validity | ||||
* level. | * level. | ||||
*/ | */ | ||||
bool isValid(enum BlockValidity nUpTo = BlockValidity::TRANSACTIONS) const { | bool isValid(enum BlockValidity nUpTo = BlockValidity::TRANSACTIONS) const { | ||||
if (isInvalid()) { | if (isInvalid()) { | ||||
return false; | return false; | ||||
} | } | ||||
return getValidity() >= nUpTo; | return getValidity() >= nUpTo; | ||||
} | } | ||||
bool isInvalid() const { return status & BLOCK_INVALID_MASK; } | bool isInvalid() const { return status & INVALID_MASK; } | ||||
BlockStatus withClearedFailureFlags() const { | BlockStatus withClearedFailureFlags() const { | ||||
return withFailed(false).withFailedParent(false); | return BlockStatus(status & ~INVALID_MASK); | ||||
} | } | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(VARINT(status)); | READWRITE(VARINT(status)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 373 Lines • Show Last 20 Lines |