chia-pos2 0.5.0

A library for creating plots, validating proofs of space, farming partial proofs and solving proofs. Using Chia proof of space v2
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#pragma once

#include "common/Timer.hpp"
#include "common/Utils.hpp"
#include "pos/Chainer.hpp"
#include "pos/ProofCore.hpp"
#include "pos/ProofParams.hpp"
#include "prove/Prover.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

// Simple helpers for pretty printing
namespace pretty {

constexpr int LABEL_WIDTH = 38;
constexpr int VALUE_WIDTH = 22;

inline void printSeparator(char ch = '-', int extra = 0)
{
    int totalWidth = LABEL_WIDTH + VALUE_WIDTH + 7 + extra; // borders + spaces
    std::cout << std::string(totalWidth, ch) << "\n";
}

inline void printSectionHeader(std::string const& title)
{
    printSeparator('=');
    std::cout << "| " << std::left << std::setw(LABEL_WIDTH + VALUE_WIDTH + 3) << title << " |\n";
    printSeparator('=');
}

template <typename T>
void printRow(std::string const& label, T const& value)
{
    std::cout << "| " << std::left << std::setw(LABEL_WIDTH) << label << " | " << std::right
              << std::setw(VALUE_WIDTH) << value << " |\n";
}

inline std::string pct(double v, int precision = 2)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(precision) << v << " %";
    return oss.str();
}

inline std::string ms(double v, int precision = 2)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(precision) << v << " ms";
    return oss.str();
}

inline std::string bytes_sensible(double bytes, int precision = 2)
{
    char const* sizes[] = { "B", "KB", "MB", "GB", "TB", "PB" };
    int order = 0;
    while (bytes >= 1000.0 && order < 5) {
        order++;
        bytes = bytes / 1000.0;
    }
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(precision) << bytes << " " << sizes[order];
    return oss.str();
}

inline std::string mb(double v, int precision = 2)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(precision) << v << " MB";
    return oss.str();
}

inline std::string gb(double v, int precision = 2)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(precision) << v << " GB";
    return oss.str();
}

inline std::string tb(double v, int precision = 1)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(precision) << v << " TB";
    return oss.str();
}

template <typename T>
std::string num(T v, int precision = 2, bool fixed = true)
{
    std::ostringstream oss;
    if constexpr (std::is_floating_point<T>::value) {
        if (fixed)
            oss << std::fixed;
        oss << std::setprecision(precision);
    }
    oss << v;
    return oss.str();
}

}

using namespace pretty;

class DiskBench {
public:
    DiskBench(ProofParams const& proof_params) : proof_params_(proof_params) {}

    // Run a simulation of disk reads for a given number of plots.
    // Outputs: total time in ms and total data read in MB.
    void simulateChallengeDiskReads(size_t plot_id_filter_bits,
        size_t num_plots_in_group,
        size_t diskTB,
        double diskSeekMs,
        double diskReadMBs) const
    {
        // std::cout << "Simulating disk reads with plot ID filter bits: " << plot_id_filter_bits
        //           << ", Disk size: " << diskTB << " TB, Seek time: " << diskSeekMs << " ms, Read
        //           speed: " << diskReadMBs << " MB/s\n";

        double bits_per_entry = 1.45 + double(proof_params_.get_k());
        size_t plot_bytes = (size_t)(bits_per_entry * (1ULL << proof_params_.get_k()) / 8);
        size_t grouped_plot_bytes = plot_bytes * num_plots_in_group;

        uint32_t chaining_set_size = proof_params_.get_chaining_set_size();
        size_t chaining_set_bytes
            = (size_t)(static_cast<double>(chaining_set_size) * bits_per_entry / 8);

        size_t num_plots = (diskTB * 1000 * 1000 * 1000 * 1000) / plot_bytes;
        size_t num_grouped_plots = num_plots / num_plots_in_group;
        std::cout << std::endl;
        std::cout << "------------------------------------\n";
        std::cout << "Harvester Disk Simulation Parameters:\n";
        std::cout << "------------------------------------\n";
        std::cout << "   Plot ID filter                   : " << (1 << plot_id_filter_bits)
                  << " (bits: " << plot_id_filter_bits << ")\n";
        std::cout << "   ----------------------------------\n";
        std::cout << "   Disk capacity                    : " << diskTB << " TB\n";
        std::cout << "   Disk seek time (ms)              : " << diskSeekMs << " ms\n";
        std::cout << "   Disk read speed                  : " << diskReadMBs << " MB/s\n";
        std::cout << "   ----------------------------------\n";
        std::cout << "   Plot size bytes                  : "
                  << bytes_sensible(static_cast<double>(plot_bytes)) << std::endl;
        std::cout << "   Total plots per Disk             : " << num_plots << std::endl;
        std::cout << "   ----------------------------------\n";
        std::cout << "   Plots in group                   : " << num_plots_in_group << std::endl;
        std::cout << "   Grouped plot size bytes          : "
                  << bytes_sensible(static_cast<double>(grouped_plot_bytes)) << std::endl;
        std::cout << "   Num grouped plots on disk        : " << num_grouped_plots << std::endl;
        std::cout << "   ----------------------------------\n";

        std::mt19937 rng(1245); // fixed seed for reproducibility
        std::uniform_int_distribution<uint32_t> plot_filter_dist(
            0, (1U << plot_id_filter_bits) - 1);
        // Allocate one synthetic fragment list per challenge set, each placed in a
        // different chaining set so the validate logic in Chainer would accept them.
        std::array<Range, NUM_CHALLENGE_SETS> set_ranges;
        for (int s = 0; s < NUM_CHALLENGE_SETS; ++s) {
            set_ranges[s] = proof_params_.get_chaining_set_range(s);
        }
        ProofFragment range_span
            = static_cast<ProofFragment>(set_ranges[0].end - set_ranges[0].start);
        std::uniform_int_distribution<ProofFragment> fragment_dist(0, range_span);
        std::array<std::vector<ProofFragment>, NUM_CHALLENGE_SETS> fragments_per_set;
        for (int s = 0; s < NUM_CHALLENGE_SETS; ++s) {
            fragments_per_set[s].resize(chaining_set_size);
            for (int i = 0; i < static_cast<int>(chaining_set_size); ++i) {
                fragments_per_set[s][i] = set_ranges[s].start + fragment_dist(rng);
            }
        }

        size_t num_challenges = 1000; // 9216; // simulate 9216 challenges (about one every 9.375
                                      // seconds for 24 hours)

        // Simulate disk reads for chaining sets
        size_t total_plots_passed_filter = 0;

        constexpr uint8_t k = 28;
        std::string plot_id_hex
            = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
        std::string challenge_hex
            = "5c00000000000000000000000000000000000000000000000000000000000000";
        std::array<uint8_t, 32> challenge = Utils::hexToBytes(challenge_hex);
        uint32_t sim_challenge_id = 0;
        ProofParams proof_params(Utils::hexToBytes(plot_id_hex).data(), k, 2, 0);
        ProofCore proof_core(proof_params);
        Timer timer;
        double total_harvesting_compute_time_ms = 0.0;
        size_t proofs_found = 0;

        double const CAP_COMPUTE_TOTAL_SIMULATION_TIME_MS
            = 20000.0; // cap at 20 seconds total compute time
        size_t total_challenges_before_compute_cap = 0;
        double max_compute_ms_per_challenge = 0;
        size_t max_plots_passing_filter_per_challenge = 0;

        // start progress bar output
        std::cout << std::endl;
        std::cout << "Running simulation (cap at "
                  << std::ceil(CAP_COMPUTE_TOTAL_SIMULATION_TIME_MS / 1000.0) << "s):\n";
        int progress_bar_steps = 40;
        std::cout << "[";
        for (int i = 0; i < progress_bar_steps; ++i)
            std::cout << " ";
        std::cout << "]\n";
        // move cursor back to start of line
        std::cout << "\r";
        std::cout.flush();
        // move cursor up one line
        std::cout << "\033[A" << "[" << std::flush;

        size_t progress_bar_step_size = num_challenges / progress_bar_steps;

        bool cap_harvesting_compute_reached = false;
        for (size_t challenge_id = 0; challenge_id < num_challenges; ++challenge_id) {
            size_t challenge_plots_passed_filter = 0;
            double challenge_compute_time_ms = 0.0;
            if (total_harvesting_compute_time_ms > CAP_COMPUTE_TOTAL_SIMULATION_TIME_MS) {
                cap_harvesting_compute_reached = true;
            }
            else {
                total_challenges_before_compute_cap++;
            }
            for (size_t plot_id = 0; plot_id < num_grouped_plots; ++plot_id) {
                // Simulate whether this plot passes the plot ID filter

                uint32_t plot_id_filter_value = plot_filter_dist(rng);
                if (plot_id_filter_value != 0) {
                    continue; // does not pass filter
                }

                // if it passes filter, requires NUM_CHALLENGE_SETS disk seeks and
                // NUM_CHALLENGE_SETS*chaining_set_bytes read
                challenge_plots_passed_filter++;

                if (challenge_plots_passed_filter > max_plots_passing_filter_per_challenge) {
                    max_plots_passing_filter_per_challenge = challenge_plots_passed_filter;
                }

                // simulate harvester challenge compute time for chaining.
                // create new random challenge each plot passing filter
                // remember have to compute for all plots in group
                if (cap_harvesting_compute_reached) {
                    continue; // cap reached, skip further compute
                }

                for (size_t i = 0; i < num_plots_in_group; ++i) {
                    challenge[0] = static_cast<uint8_t>(sim_challenge_id & 0xFF);
                    challenge[1] = static_cast<uint8_t>((sim_challenge_id >> 8) & 0xFF);
                    challenge[2] = static_cast<uint8_t>((sim_challenge_id >> 16) & 0xFF);
                    challenge[3] = static_cast<uint8_t>((sim_challenge_id >> 24) & 0xFF);
                    sim_challenge_id++;
                    timer.start();
                    Chainer chainer(proof_params, challenge);
                    std::array<std::span<ProofFragment const>, NUM_CHALLENGE_SETS>
                        fragments_per_set_spans;
                    for (int s = 0; s < NUM_CHALLENGE_SETS; ++s) {
                        fragments_per_set_spans[s] = fragments_per_set[s];
                    }
                    auto chains = chainer.find_links(fragments_per_set_spans);
                    double elapsed_ms = timer.stop();
                    total_harvesting_compute_time_ms += elapsed_ms;
                    proofs_found += chains.size();
                    challenge_compute_time_ms += elapsed_ms;
                }
                if (challenge_compute_time_ms > max_compute_ms_per_challenge) {
                    max_compute_ms_per_challenge = challenge_compute_time_ms;
                }
            }

            total_plots_passed_filter += challenge_plots_passed_filter;

            // update progress bar
            if ((challenge_id + 1) % progress_bar_step_size == 0) {
                std::cout << "=" << std::flush;
            }
        }
        // finish progress bar
        std::cout << "]" << std::endl << std::endl;

        // now we can compute our disk stats
        // each plot passing filter requires NUM_CHALLENGE_SETS seeks + reading the
        // corresponding NUM_CHALLENGE_SETS chaining sets
        size_t total_seeks = total_plots_passed_filter * NUM_CHALLENGE_SETS;
        size_t total_data_read_bytes = total_plots_passed_filter * num_plots_in_group
            * chaining_set_bytes * NUM_CHALLENGE_SETS;
        double diskSeekTimeMs = total_seeks * diskSeekMs;
        double diskReadTimeMs
            = (static_cast<double>(total_data_read_bytes) / (diskReadMBs * 1000.0));
        double total_time_ms = diskSeekTimeMs + diskReadTimeMs;
        double disk_load_percentage
            = 100.0 * (total_time_ms / (numeric_cast<double>(num_challenges) * 9375.0));

        // std::cout << "---- Disk Read Simulation Results ----" << std::endl;
        double plots_passed_perc = (static_cast<double>(total_plots_passed_filter))
            / static_cast<double>(num_grouped_plots * num_challenges);
        // std::cout << "Total plots passed filter: " << total_plots_passed_filter << " (1 out of "
        // << (1/plots_passed_perc) << ")" << std::endl; std::cout << "Total disk seeks: " <<
        // total_seeks << ", Total data read: " << (total_data_read_bytes / (1024.0 * 1024.0)) << "
        // MB" << std::endl; std::cout << "Total disk seek time: " << diskSeekTimeMs << " ms, Total
        // disk read time: " << diskReadTimeMs << " ms" << std::endl; std::cout << "Total time for "
        // << num_challenges << " challenges: " << total_time_ms << " ms" << std::endl; std::cout <<
        // "Estimated HDD load for 1 challenge every 9.375 seconds: " << disk_load_percentage << "%"
        // << std::endl;
        //  get max load experienced for a challenge
        // std::cout << "Max compute time for a single challenge: " << max_compute_ms_per_challenge
        // << " ms" << std::endl;
        // std::cout << "Max plots passing filter for a single challenge: " <<
        // max_plots_passing_filter_per_challenge << std::endl;
        // and load calc for max
        double max_disk_load_percentage = 100.0
            * ((static_cast<double>(max_plots_passing_filter_per_challenge) * NUM_CHALLENGE_SETS
                       * diskSeekMs
                   + (static_cast<double>(max_plots_passing_filter_per_challenge
                          * num_plots_in_group * chaining_set_bytes * NUM_CHALLENGE_SETS)
                       / (diskReadMBs * 1000.0)))
                / 9375.0);
        // std::cout << "Estimated max HDD load for a single challenge: " <<
        // max_disk_load_percentage << "%" << std::endl;
        double max_compute_load_percentage = 100.0 * (max_compute_ms_per_challenge / 9375.0);
        // std::cout << "Estimated max CPU harvesting load for a single challenge: " <<
        // max_compute_load_percentage << "%" << std::endl;

        // std::cout << "---- Harvesting Compute Time ----" << std::endl;
        // std::cout << "Total harvesting compute time for " << num_challenges << " challenges: " <<
        // total_harvesting_compute_time_ms << " ms" << std::endl; std::cout << "Total proofs found:
        // " << proofs_found << std::endl;
        double avg_compute_time_per_challenge_ms = total_harvesting_compute_time_ms
            / static_cast<double>(total_challenges_before_compute_cap);
        // std::cout << "Average harvesting compute time per challenge: " <<
        // avg_compute_time_per_challenge_ms << " ms" << std::endl;
        double cpu_harvesting_load_percentage = 100.0
            * (total_harvesting_compute_time_ms / (total_challenges_before_compute_cap * 9375.0));
        // std::cout << "Estimated CPU harvesting load for 1 challenge every 9.375 seconds: " <<
        // cpu_harvesting_load_percentage << "%" << std::endl;

        // --------------------------------------------------
        // OVERVIEW SECTION
        // --------------------------------------------------
        printSectionHeader("Overall Harvesting Overview");

        printRow("Challenges simulated", num(num_challenges, 0));
        printRow("Total proofs found", num(proofs_found, 0));
        printSeparator();
        printRow("HDD Capacity", tb(static_cast<double>(diskTB)));
        printRow("Avg HDD load (all challenges)", pct(disk_load_percentage));
        printRow("Max HDD load (single challenge)", pct(max_disk_load_percentage));
        size_t read_bytes_per_day = (total_data_read_bytes * 9216) / num_challenges;
        printRow(
            "Estimated data read per day", bytes_sensible(static_cast<double>(read_bytes_per_day)));
        printSeparator();

        printRow("Avg CPU harvesting load", pct(cpu_harvesting_load_percentage));
        printRow("Max CPU harvesting load", pct(max_compute_load_percentage));
        printSeparator();

        printRow("Max plots passing filter (1 challenge)",
            num(max_plots_passing_filter_per_challenge, 0));
        printRow("Overall filter pass rate", num(plots_passed_perc * 100.0, 4) + std::string(" %"));
        // printRow("Approx. 1 out of", num(one_out_of, 2));
        printSeparator('=');
        std::cout << std::endl;

        // --------------------------------------------------
        // DISK I/O SECTION
        // --------------------------------------------------
        printSectionHeader("Disk I/O Details");

        printRow("Total plots passed filter", num(total_plots_passed_filter, 0));
        printRow("Total disk seeks", num(total_seeks, 0));
        printRow("Total data read", bytes_sensible(static_cast<double>(total_data_read_bytes)));
        printSeparator();

        printRow("Total disk seek time", ms(diskSeekTimeMs));
        printRow("Total disk read time", ms(diskReadTimeMs));
        printRow("Total disk time (all challenges)", ms(total_time_ms));
        printRow("HDD load @ 1 challenge / 9.375s", pct(disk_load_percentage));
        printSeparator('=');
        std::cout << std::endl;

        // --------------------------------------------------
        // HARVESTING COMPUTE SECTION
        // --------------------------------------------------
        printSectionHeader("Harvesting Compute Details");
        printRow("Total simulation runs before cap", num(total_challenges_before_compute_cap, 0));
        printSeparator();
        printRow("Farm size (plots)", num(num_plots, 0));
        printRow("Farm netspace", tb(static_cast<double>(diskTB)));
        printSeparator();
        printRow("Total harvesting compute time", ms(total_harvesting_compute_time_ms));
        printRow("Average compute time / challenge", ms(avg_compute_time_per_challenge_ms));
        printRow("Max compute time (single challenge)", ms(max_compute_ms_per_challenge));
        printSeparator();

        printRow("Avg CPU harvesting load @ 9.375s", pct(cpu_harvesting_load_percentage));
        printRow("Max CPU harvesting load @ 9.375s", pct(max_compute_load_percentage));
        printSeparator('=');
        std::cout << std::endl;
    }

private:
    ProofParams proof_params_;
};