Skip to content

Commit 53fd1d8

Browse files
committed
Switch to bitstream writer
1 parent ecfbe31 commit 53fd1d8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/swiftsync.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717

1818
using namespace swiftsync;
1919

20+
void BitsWriter::WriteBitInternal(uint8_t is_not_spent) noexcept
21+
{
22+
m_curr_byte <<= 1;
23+
m_curr_byte |= (is_not_spent & 0x01);
24+
++m_bits_written;
25+
if (m_bits_written % 8 == 0) {
26+
m_hints.push_back(m_curr_byte);
27+
m_curr_byte = 0;
28+
}
29+
}
30+
2031
HintsfileWriter::HintsfileWriter(AutoFile& file, const uint32_t& preallocate) : m_file(file.release())
2132
{
2233
uint64_t dummy_file_pos{};
@@ -29,7 +40,7 @@ HintsfileWriter::HintsfileWriter(AutoFile& file, const uint32_t& preallocate) :
2940
}
3041
}
3142

32-
bool HintsfileWriter::WriteNextUnspents(const std::vector<uint64_t>& unspent_offsets, const uint32_t& height)
43+
bool HintsfileWriter::WriteNextUnspents(BitsWriter& writer, const uint32_t& height)
3344
{
3445
// First write the current file position for the current height in the header section.
3546
uint64_t curr_pos = m_file.size();
@@ -39,10 +50,7 @@ bool HintsfileWriter::WriteNextUnspents(const std::vector<uint64_t>& unspent_off
3950
m_file << curr_pos;
4051
// Next append the positions of the unspent offsets in the block at this height.
4152
m_file.seek(curr_pos, SEEK_SET);
42-
WriteCompactSize(m_file, unspent_offsets.size());
43-
for (const auto& offset : unspent_offsets) {
44-
WriteCompactSize(m_file, offset);
45-
}
53+
writer.EncodeToStream(m_file);
4654
++m_index;
4755
return m_file.Commit();
4856
}

src/swiftsync.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#include <primitives/transaction.h>
88
#include <streams.h>
99
#include <uint256.h>
10+
#include <util/fs.h>
1011
#include <util/hasher.h>
1112
#include <array>
1213
#include <cstdint>
1314
#include <unordered_map>
14-
#include <util/fs.h>
1515
#include <vector>
1616

1717
namespace swiftsync {
@@ -39,6 +39,24 @@ class Aggregate
3939
void Spend(const COutPoint& outpoint) { m_spent += m_hasher(outpoint); }
4040
};
4141

42+
class BitsWriter
43+
{
44+
private:
45+
std::vector<uint8_t> m_hints{};
46+
uint8_t m_curr_byte{};
47+
uint32_t m_bits_written{};
48+
void WriteBitInternal(uint8_t bit) noexcept;
49+
50+
public:
51+
void PushHighBit() noexcept { return WriteBitInternal(0x01); }
52+
void PushLowBit() noexcept { return WriteBitInternal(0x00); }
53+
void EncodeToStream(AutoFile& file)
54+
{
55+
file << m_bits_written;
56+
file << m_hints;
57+
};
58+
};
59+
4260
/**
4361
* Create a new hint file for writing.
4462
*/
@@ -56,7 +74,7 @@ class HintsfileWriter
5674
(void)m_file.fclose();
5775
}
5876
// Write the next hints to file.
59-
bool WriteNextUnspents(const std::vector<uint64_t>& unspent_offsets, const uint32_t& height);
77+
bool WriteNextUnspents(BitsWriter& writer, const uint32_t& height);
6078
// Close the underlying file.
6179
int Close() { return m_file.fclose(); };
6280
};

0 commit comments

Comments
 (0)