|
5 | 5 |
|
6 | 6 | #include "vm/runtime/impl/tipset_randomness.hpp"
|
7 | 7 |
|
| 8 | +#include "drand/beaconizer.hpp" |
8 | 9 | #include "primitives/tipset/chain.hpp"
|
9 | 10 |
|
10 | 11 | namespace fc::vm::runtime {
|
11 |
| - TipsetRandomness::TipsetRandomness(TsLoadPtr ts_load, |
12 |
| - SharedMutexPtr ts_branches_mutex) |
| 12 | + using drand::BeaconEntry; |
| 13 | + |
| 14 | + TipsetRandomness::TipsetRandomness( |
| 15 | + TsLoadPtr ts_load, |
| 16 | + SharedMutexPtr ts_branches_mutex, |
| 17 | + std::shared_ptr<DrandSchedule> drand_schedule) |
13 | 18 | : ts_load{std::move(ts_load)},
|
14 |
| - ts_branches_mutex{std::move(ts_branches_mutex)} {} |
| 19 | + ts_branches_mutex{std::move(ts_branches_mutex)}, |
| 20 | + drand_schedule{std::move(drand_schedule)} {} |
15 | 21 |
|
16 | 22 | outcome::result<Randomness> TipsetRandomness::getRandomnessFromTickets(
|
17 | 23 | const TsBranchPtr &ts_branch,
|
18 | 24 | DomainSeparationTag tag,
|
19 | 25 | ChainEpoch epoch,
|
20 | 26 | gsl::span<const uint8_t> seed) const {
|
21 | 27 | std::shared_lock ts_lock{*ts_branches_mutex};
|
| 28 | + const auto network{version::getNetworkVersion(epoch)}; |
22 | 29 | OUTCOME_TRY(it,
|
23 | 30 | find(ts_branch,
|
24 | 31 | std::max<ChainEpoch>(0, epoch),
|
25 |
| - epoch <= kUpgradeHyperdriveHeight)); |
| 32 | + network < NetworkVersion::kVersion13)); |
26 | 33 | OUTCOME_TRY(ts, ts_load->lazyLoad(it.second->second));
|
27 | 34 | ts_lock.unlock();
|
28 | 35 |
|
29 | 36 | return crypto::randomness::drawRandomness(
|
30 | 37 | ts->getMinTicketBlock().ticket->bytes, tag, epoch, seed);
|
31 | 38 | }
|
32 | 39 |
|
| 40 | + inline outcome::result<BeaconEntry> extractBeaconEntryForEpoch( |
| 41 | + TsLoadPtr ts_load, |
| 42 | + primitives::tipset::chain::TsBranchIter it, |
| 43 | + drand::Round round) { |
| 44 | + // magic number from lotus |
| 45 | + for (auto i{0}; i < 20; ++i) { |
| 46 | + OUTCOME_TRY(ts, ts_load->lazyLoad(it.second->second)); |
| 47 | + for (const auto &beacon : ts->blks[0].beacon_entries) { |
| 48 | + if (beacon.round == round) { |
| 49 | + return beacon; |
| 50 | + } |
| 51 | + } |
| 52 | + if (it.second->first == 0) { |
| 53 | + break; |
| 54 | + } |
| 55 | + OUTCOME_TRYA(it, stepParent(it)); |
| 56 | + } |
| 57 | + return primitives::tipset::TipsetError::kNoBeacons; |
| 58 | + } |
| 59 | + |
33 | 60 | outcome::result<Randomness> TipsetRandomness::getRandomnessFromBeacon(
|
34 | 61 | const TsBranchPtr &ts_branch,
|
35 | 62 | DomainSeparationTag tag,
|
36 | 63 | ChainEpoch epoch,
|
37 | 64 | gsl::span<const uint8_t> seed) const {
|
38 | 65 | std::shared_lock ts_lock{*ts_branches_mutex};
|
| 66 | + const auto network{version::getNetworkVersion(epoch)}; |
39 | 67 | OUTCOME_TRY(it,
|
40 | 68 | find(ts_branch,
|
41 | 69 | std::max<ChainEpoch>(0, epoch),
|
42 |
| - epoch <= kUpgradeHyperdriveHeight)); |
43 |
| - OUTCOME_TRY(beacon, latestBeacon(ts_load, it)); |
| 70 | + network < NetworkVersion::kVersion13)); |
| 71 | + BeaconEntry beacon; |
| 72 | + if (network <= NetworkVersion::kVersion13 || epoch < 0) { |
| 73 | + OUTCOME_TRYA(beacon, latestBeacon(ts_load, it)); |
| 74 | + } else { |
| 75 | + OUTCOME_TRYA(beacon, |
| 76 | + extractBeaconEntryForEpoch( |
| 77 | + ts_load, it, drand_schedule->maxRound(epoch))); |
| 78 | + } |
44 | 79 | ts_lock.unlock();
|
45 | 80 |
|
46 | 81 | return crypto::randomness::drawRandomness(beacon.data, tag, epoch, seed);
|
|
0 commit comments