|
| 1 | + |
| 2 | +#include <dynafed.h> |
| 3 | +#include <script/standard.h> |
| 4 | + |
| 5 | +bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consensus::Params& consensus, ConsensusParamEntry& winning_entry) |
| 6 | +{ |
| 7 | + uint32_t next_height = pindexPrev->nHeight + 1; |
| 8 | + assert(consensus.dynamic_epoch_length != 0); |
| 9 | + if (next_height % consensus.dynamic_epoch_length != 0) { |
| 10 | + winning_entry.SetNull(); |
| 11 | + return false; |
| 12 | + } |
| 13 | + std::map<uint256, uint32_t> vote_tally; |
| 14 | + assert(next_height >= consensus.dynamic_epoch_length); |
| 15 | + for (int32_t height = next_height - 1; height >= (int32_t)(next_height - consensus.dynamic_epoch_length); --height) { |
| 16 | + const CBlockIndex* p_epoch_walk = pindexPrev->GetAncestor(height); |
| 17 | + assert(p_epoch_walk); |
| 18 | + const ConsensusParamEntry& proposal = p_epoch_walk->d_params.m_proposed; |
| 19 | + const uint256 proposal_root = proposal.CalculateRoot(); |
| 20 | + vote_tally[proposal_root]++; |
| 21 | + // Short-circuit once 4/5 threshhold is reached |
| 22 | + if (vote_tally[proposal_root] >= |
| 23 | + (consensus.dynamic_epoch_length*4)/5) { |
| 24 | + winning_entry = proposal; |
| 25 | + return true; |
| 26 | + } |
| 27 | + // Also stop early if "no-vote" crosses 1/4 |
| 28 | + if (proposal_root.IsNull() && |
| 29 | + vote_tally[proposal_root] > consensus.dynamic_epoch_length/5) { |
| 30 | + winning_entry.SetNull(); |
| 31 | + return false; |
| 32 | + } |
| 33 | + } |
| 34 | + winning_entry.SetNull(); |
| 35 | + return false; |
| 36 | +} |
| 37 | + |
| 38 | +ConsensusParamEntry ComputeNextBlockFullCurrentParameters(const CBlockIndex* pindexPrev, const Consensus::Params& consensus) |
| 39 | +{ |
| 40 | + assert(pindexPrev); |
| 41 | + |
| 42 | + uint32_t next_height = pindexPrev->nHeight+1; |
| 43 | + const uint32_t epoch_length = consensus.dynamic_epoch_length; |
| 44 | + uint32_t epoch_age = next_height % epoch_length; |
| 45 | + |
| 46 | + ConsensusParamEntry winning_proposal; |
| 47 | + // Early return when there is a winning proposal |
| 48 | + if (NextBlockIsParameterTransition(pindexPrev, consensus, winning_proposal)) { |
| 49 | + assert(epoch_age == 0); |
| 50 | + return winning_proposal; |
| 51 | + } |
| 52 | + |
| 53 | + // Since no transition took place, find most recent epoch start |
| 54 | + |
| 55 | + // If next block is start of new epoch, walk backwards one epoch |
| 56 | + uint32_t epoch_start_height = next_height - epoch_age; |
| 57 | + if (epoch_age == 0) { |
| 58 | + epoch_start_height -= epoch_length; |
| 59 | + } |
| 60 | + |
| 61 | + // We need to put in place the previous epoch's current which |
| 62 | + // may be pre-dynafed params |
| 63 | + const CBlockIndex* p_epoch_start = pindexPrev->GetAncestor(epoch_start_height); |
| 64 | + assert(p_epoch_start); |
| 65 | + if (p_epoch_start->d_params.IsNull()) { |
| 66 | + // We need to construct the "full" current parameters of pre-dynafed |
| 67 | + // consensus |
| 68 | + |
| 69 | + // Convert signblockscript to P2WSH |
| 70 | + CScript p2wsh_signblock_script = GetScriptForDestination(WitnessV0ScriptHash(p_epoch_start->proof.challenge)); |
| 71 | + winning_proposal = ConsensusParamEntry(p2wsh_signblock_script, consensus.max_block_signature_size+consensus.signblockscript.size(), consensus.fedpegScript, consensus.first_extension_space); |
| 72 | + } else { |
| 73 | + winning_proposal = p_epoch_start->d_params.m_current; |
| 74 | + } |
| 75 | + return winning_proposal; |
| 76 | +} |
| 77 | + |
| 78 | +// TODO cache this in CBlockIndex itself? |
| 79 | +ConsensusParamEntry ComputeNextBlockCurrentParameters(const CBlockIndex* pindexPrev, const Consensus::Params& consensus) |
| 80 | +{ |
| 81 | + assert(pindexPrev); |
| 82 | + |
| 83 | + ConsensusParamEntry entry = ComputeNextBlockFullCurrentParameters(pindexPrev, consensus); |
| 84 | + |
| 85 | + uint32_t next_height = pindexPrev->nHeight+1; |
| 86 | + const uint32_t epoch_length = consensus.dynamic_epoch_length; |
| 87 | + uint32_t epoch_age = next_height % epoch_length; |
| 88 | + |
| 89 | + // Return appropriate format based on epoch age |
| 90 | + if (epoch_age > 0) { |
| 91 | + // TODO implement "prune" function to remove fields in place and change serialize type |
| 92 | + return ConsensusParamEntry(entry.m_signblockscript, entry.m_sbs_wit_limit); |
| 93 | + } else { |
| 94 | + return entry; |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments