Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251if (opcode == Instruction::RETURN) {252// There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)253if (prevopcode == Instruction::DEFAULT) return false;254uint32_t asn = DecodeASN(pos, asmap);255if (asn == INVALID) return false; // ASN straddles EOF256if (jumps.empty()) {257// Nothing to execute anymore258if (endpos - pos > 7) return false; // Excessive padding259while (pos != endpos) {260if (ConsumeBitLE(pos, asmap)) return false; // Nonzero padding bit261}262return true; // Sanely reached EOF263} else {264// Continue by pretending we jumped to the next instruction265if (pos != jumps.back().first) return false; // Unreachable code266bits = jumps.back().second; // Restore the number of bits we would have had left after this jump267jumps.pop_back();268prevopcode = Instruction::JUMP;269}270} else if (opcode == Instruction::JUMP) {271uint32_t jump = DecodeJump(pos, asmap);272if (jump == INVALID) return false; // Jump offset straddles EOF273if (int64_t{jump} > static_cast<int64_t>(endpos - pos)) return false; // Jump out of range274if (bits == 0) return false; // Consuming bits past the end of the input275--bits;276uint32_t jump_offset = pos + jump;277if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps278jumps.emplace_back(jump_offset, bits); // Queue jump target for validation279prevopcode = Instruction::JUMP;280} else if (opcode == Instruction::MATCH) {281uint32_t match = DecodeMatch(pos, asmap);282if (match == INVALID) return false; // Match bits straddle EOF283int matchlen = std::bit_width(match) - 1;284if (prevopcode != Instruction::MATCH) had_incomplete_match = false;285// Within a sequence of matches only at most one should be incomplete286if (matchlen < 8 && had_incomplete_match) return false;287had_incomplete_match = (matchlen < 8);288if (bits < matchlen) return false; // Consuming bits past the end of the input289bits -= matchlen;290prevopcode = Instruction::MATCH;291} else if (opcode == Instruction::DEFAULT) {292// There should not be two successive DEFAULTs (they could be combined into one)293if (prevopcode == Instruction::DEFAULT) return false;294uint32_t asn = DecodeASN(pos, asmap);295if (asn == INVALID) return false; // ASN straddles EOF296prevopcode = Instruction::DEFAULT;297} else {298return false; // Instruction straddles EOF299}300}301return false; // Reached EOF without RETURN instruction302}304/**305* Provides a safe interface for validating ASMap data before use.306* Returns true if the data is valid for 128 bits long inputs.307*/308bool CheckStandardAsmap(const std::span<const std::byte> data)309{310if (!SanityCheckAsmap(data, 128)) {311LogWarning("Sanity check of asmap data failed\n");312return false;313}314return true;315}317/**318* Loads an ASMap file from disk and validates it.319*/320std::vector<std::byte> DecodeAsmap(fs::path path)321{322FILE *filestr = fsbridge::fopen(path, "rb");323AutoFile file{filestr};324if (file.IsNull()) {325LogWarning("Failed to open asmap file from disk");326return {};327}328int64_t length{file.size()};329LogInfo("Opened asmap file %s (%d bytes) from disk", fs::quoted(fs::PathToString(path)), length);331// Read entire file into memory332std::vector<std::byte> buffer(length);333file.read(buffer);335if (!CheckStandardAsmap(buffer)) {336LogWarning("Sanity check of asmap file %s failed", fs::quoted(fs::PathToString(path)));337return {};338}340return buffer;341}343/**344* Computes SHA256 hash of ASMap data for versioning and consistency checks.345*/346uint256 AsmapVersion(const std::span<const std::byte> data)347{348if (data.empty()) return {};350HashWriter asmap_hasher;351asmap_hasher << data;352return asmap_hasher.GetSHA256();353}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.