| 1146 | } |
| 1147 | |
| 1148 | FlatFilePos 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 | |
| 1182 | static auto InitBlocksdirXorKey(const BlockManager::Options& opts) |
| 1183 | { |