16 std::vector<std::byte> bytes;
17 bytes.reserve(hex.length() / 2);
19 for (
size_t i{0}; i < hex.length(); i += 2) {
21 auto [ptr, ec] = std::from_chars(hex.data() + i, hex.data() + i + 2, byte_value, 16);
23 if (ec != std::errc{} || ptr != hex.data() + i + 2) {
24 throw std::invalid_argument(
"Invalid hex character");
26 bytes.push_back(
static_cast<std::byte
>(byte_value));
36 std::cout <<
"kernel: " << message;
51 case ValidationMode::VALID: {
52 std::cout <<
"Valid block" << std::endl;
55 case ValidationMode::INVALID: {
56 std::cout <<
"Invalid block: ";
59 case BlockValidationResult::UNSET:
60 std::cout <<
"initial value. Block has not yet been rejected" << std::endl;
62 case BlockValidationResult::HEADER_LOW_WORK:
63 std::cout <<
"the block header may be on a too-little-work chain" << std::endl;
65 case BlockValidationResult::CONSENSUS:
66 std::cout <<
"invalid by consensus rules" << std::endl;
68 case BlockValidationResult::CACHED_INVALID:
69 std::cout <<
"this block was cached as being invalid and we didn't store the reason why" << std::endl;
71 case BlockValidationResult::INVALID_HEADER:
72 std::cout <<
"invalid proof of work or time too old" << std::endl;
74 case BlockValidationResult::MUTATED:
75 std::cout <<
"the block's data didn't match the data committed to by the PoW" << std::endl;
77 case BlockValidationResult::MISSING_PREV:
78 std::cout <<
"We don't have the previous block the checked one is built on" << std::endl;
80 case BlockValidationResult::INVALID_PREV:
81 std::cout <<
"A block this one builds on is invalid" << std::endl;
83 case BlockValidationResult::TIME_FUTURE:
84 std::cout <<
"block timestamp was > 2 hours in the future (or our clock is bad)" << std::endl;
89 case ValidationMode::INTERNAL_ERROR: {
90 std::cout <<
"Internal error" << std::endl;
102 std::cout <<
"Block tip changed" << std::endl;
105 void ProgressHandler(std::string_view title,
int progress_percent,
bool resume_possible)
override
107 std::cout <<
"Made progress: " << title <<
" " << progress_percent <<
"%" << std::endl;
112 std::cout << message << std::endl;
117 std::cout <<
"Warning unset: " <<
static_cast<std::underlying_type_t<Warning>
>(warning) << std::endl;
122 std::cout << error << std::endl;
127 std::cout << error << std::endl;
131int main(
int argc,
char* argv[])
136 <<
"Usage: " << argv[0] <<
" DATADIR" << std::endl
137 <<
"Display DATADIR information, and process hex-encoded blocks on standard input." << std::endl
139 <<
"IMPORTANT: THIS EXECUTABLE IS EXPERIMENTAL, FOR TESTING ONLY, AND EXPECTED TO" << std::endl
140 <<
" BREAK IN FUTURE VERSIONS. DO NOT USE ON YOUR ACTUAL DATADIR." << std::endl;
143 std::filesystem::path abs_datadir{std::filesystem::absolute(argv[1])};
144 std::filesystem::create_directories(abs_datadir);
148 .log_time_micros =
false,
149 .log_threadnames =
false,
150 .log_sourcelocations =
false,
151 .always_print_category_levels =
true,
156 Logger logger{std::make_unique<KernelLog>()};
160 options.SetChainParams(params);
162 options.SetNotifications(std::make_shared<TestKernelNotifications>());
163 options.SetValidationInterface(std::make_shared<TestValidationInterface>());
170 std::unique_ptr<ChainMan> chainman;
172 chainman = std::make_unique<ChainMan>(context, chainman_opts);
173 }
catch (std::exception&) {
174 std::cerr <<
"Failed to instantiate ChainMan, exiting" << std::endl;
178 std::cout <<
"Enter the block you want to validate on the next line:" << std::endl;
180 for (std::string line; std::getline(std::cin, line);) {
182 std::cerr <<
"Empty line found, try again:" << std::endl;
187 std::unique_ptr<Block> block;
189 block = std::make_unique<Block>(raw_block);
190 }
catch (std::exception&) {
191 std::cerr <<
"Block decode failed, try again:" << std::endl;
195 bool new_block =
false;
196 bool accepted = chainman->ProcessBlock(*block, &new_block);
198 std::cerr <<
"Block has not yet been rejected" << std::endl;
200 std::cerr <<
"Block was not accepted" << std::endl;
203 std::cerr <<
"Block is a duplicate" << std::endl;
int main(int argc, char *argv[])
std::vector< std::byte > hex_string_to_byte_vec(std::string_view hex)
void LogMessage(std::string_view message)
void FlushErrorHandler(std::string_view error) override
void FatalErrorHandler(std::string_view error) override
void WarningUnsetHandler(Warning warning) override
void WarningSetHandler(Warning warning, std::string_view message) override
void ProgressHandler(std::string_view title, int progress_percent, bool resume_possible) override
void BlockTipHandler(SynchronizationState, const BlockTreeEntry, double) override
TestValidationInterface()=default
void BlockChecked(const Block block, const BlockValidationState state) override
std::optional< std::string > m_expected_valid_block
BlockValidationResult GetBlockValidationResult() const
ValidationMode GetValidationMode() const
void SetWorkerThreads(int worker_threads)
void logging_set_options(const btck_LoggingOptions &logging_options)
Options controlling the format of log messages.
int log_timestamps
Prepend a timestamp to log messages.