MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / WriteBlock

Method WriteBlock

src/node/blockstorage.cpp:1148–1180  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1146}
1147
1148FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
1149{
1150 AssertLockHeld(::cs_main);
1151 const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
1152 FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
1153 if (pos.IsNull()) {
1154 LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
1155 return FlatFilePos();
1156 }
1157 AutoFile file{OpenBlockFile(pos, /*fReadOnly=*/false)};
1158 if (file.IsNull()) {
1159 LogError("OpenBlockFile failed for %s while writing block", pos.ToString());
1160 m_opts.notifications.fatalError(_("Failed to write block."));
1161 return FlatFilePos();
1162 }
1163 {
1164 BufferedWriter fileout{file};
1165
1166 // Write index header
1167 fileout << GetParams().MessageStart() << block_size;
1168 pos.nPos += STORAGE_HEADER_BYTES;
1169 // Write block
1170 fileout << TX_WITH_WITNESS(block);
1171 }
1172
1173 if (file.fclose() != 0) {
1174 LogError("Failed to close block file %s: %s", pos.ToString(), SysErrorString(errno));
1175 m_opts.notifications.fatalError(_("Failed to close file when writing block."));
1176 return FlatFilePos();
1177 }
1178
1179 return pos;
1180}
1181
1182static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
1183{

Callers 4

AcceptBlockMethod · 0.45
LoadGenesisBlockMethod · 0.45
BOOST_AUTO_TEST_CASEFunction · 0.45
WriteBlockBenchFunction · 0.45

Calls 9

GetSerializeSizeFunction · 0.85
FlatFilePosClass · 0.85
_Function · 0.85
SysErrorStringFunction · 0.85
fcloseMethod · 0.80
GetBlockTimeMethod · 0.45
IsNullMethod · 0.45
ToStringMethod · 0.45
fatalErrorMethod · 0.45

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.36