Skip to content

Commit e249994

Browse files
committed
swiftsync: Add hintfile reading
1 parent d565b85 commit e249994

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/swiftsync.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#include <array>
2+
#include <cstddef>
3+
#include <cstdint>
24
#include <primitives/transaction.h>
35
#include <random.h>
6+
#include <serialize.h>
7+
#include <streams.h>
48
#include <swiftsync.h>
59
#include <uint256.h>
10+
#include <vector>
611

712
Aggregate::Aggregate() : m_limb0(0), m_limb1(0), m_limb2(0), m_limb3(0)
813
{
@@ -23,3 +28,59 @@ void Aggregate::Spend(const COutPoint& outpoint)
2328
auto a0 = (HashWriter(m_salted_hasher) << outpoint).GetSHA256().GetUint64(0);
2429
m_limb0 -= a0;
2530
}
31+
32+
Hintfile::Hintfile(AutoFile file) : m_file(file.release())
33+
{
34+
uint256 m_stop_hash;
35+
uint32_t m_stop_height;
36+
m_file >> m_stop_height;
37+
m_file >> m_stop_hash;
38+
m_curr_height = 0;
39+
}
40+
41+
Hintfile::Hintfile(AutoFile file, const uint256& stop_hash, const uint32_t& stop_height) : m_file(file.release())
42+
{
43+
m_file << stop_height;
44+
m_file << stop_hash;
45+
m_curr_height = 0;
46+
}
47+
48+
BlockUnspentHints::BlockUnspentHints(const std::vector<uint64_t> offsets)
49+
{
50+
// Assuming a vector of ordered offsets is given, unpack into the literal indexes.
51+
std::vector<uint64_t> m_unspent_indexes;
52+
uint64_t prev = 0;
53+
for (const auto& offset : offsets) {
54+
auto next = prev + offset;
55+
m_unspent_indexes.push_back(next);
56+
prev = next;
57+
}
58+
}
59+
60+
bool BlockUnspentHints::IsUnspent(const uint64_t index)
61+
{
62+
auto found = std::find(m_unspent_indexes.begin(), m_unspent_indexes.end(), index);
63+
return found != m_unspent_indexes.end();
64+
}
65+
66+
BlockUnspentHints Hintfile::ReadNextBlock()
67+
{
68+
m_curr_height++;
69+
uint64_t num_unspent = ReadCompactSize(m_file);
70+
std::vector<uint64_t> offsets;
71+
offsets.reserve(num_unspent);
72+
for (uint64_t i = 0; i < num_unspent; i++) {
73+
offsets.push_back(ReadCompactSize(m_file));
74+
}
75+
return BlockUnspentHints(offsets);
76+
}
77+
78+
bool Hintfile::WriteNextBlock(const std::vector<uint64_t>& unspent_offsets)
79+
{
80+
m_curr_height++;
81+
WriteCompactSize(m_file, unspent_offsets.size());
82+
for (const auto& offset : unspent_offsets) {
83+
WriteCompactSize(m_file, offset);
84+
}
85+
return m_file.Commit();
86+
}

src/swiftsync.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <cstdint>
22
#include <hash.h>
33
#include <primitives/transaction.h>
4+
#include <streams.h>
5+
#include <uint256.h>
6+
#include <vector>
47

58
/** An aggregate for the SwiftSync protocol.
69
* This class is intentionally left opaque, as internal changes may occur,
@@ -23,3 +26,40 @@ class Aggregate
2326
void Add(const COutPoint& outpoint);
2427
void Spend(const COutPoint& outpoint);
2528
};
29+
30+
/**
31+
* The unspent indexes of a block, assuming the list of block
32+
* outputs has been flattened.
33+
* */
34+
class BlockUnspentHints
35+
{
36+
private:
37+
std::vector<uint64_t> m_unspent_indexes;
38+
39+
public:
40+
BlockUnspentHints(const std::vector<uint64_t> offsets);
41+
bool IsUnspent(const uint64_t index);
42+
};
43+
44+
/**
45+
* A file that encodes the UTXO set state at a particular block hash.
46+
*/
47+
class Hintfile
48+
{
49+
private:
50+
AutoFile m_file;
51+
uint256 m_stop_hash;
52+
uint32_t m_stop_height;
53+
uint32_t m_curr_height;
54+
55+
public:
56+
// Construct a hint file and immediately read the stop hash and height.
57+
Hintfile(AutoFile file);
58+
// Construct a hint file and write the stop hash and height.
59+
Hintfile(AutoFile file, const uint256& stop_hash, const uint32_t& stop_height);
60+
BlockUnspentHints ReadNextBlock();
61+
bool WriteNextBlock(const std::vector<uint64_t>& unspent_offsets);
62+
uint256 StopHash() { return m_stop_hash; };
63+
uint32_t StopHeight() { return m_stop_height; };
64+
uint32_t CurrentHeight() { return m_curr_height; };
65+
};

0 commit comments

Comments
 (0)