forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpop_service.cpp
More file actions
430 lines (361 loc) · 14 KB
/
Copy pathpop_service.cpp
File metadata and controls
430 lines (361 loc) · 14 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright (c) 2019-2021 Xenios SEZC
// https://www.veriblock.org
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "arith_uint256.h"
#include <chain.h>
#include <chainparams.h>
#include <consensus/validation.h>
#include <dbwrapper.h>
#include <limits>
#include <shutdown.h>
#include <validation.h>
#include <vbk/adaptors/ethash_cache_provider.hpp>
#include <vbk/adaptors/payloads_provider.hpp>
#include <vbk/adaptors/progpow_header_cache_provider.hpp>
#include <veriblock/pop.hpp>
#ifdef WIN32
#include <boost/thread/interruption.hpp>
#endif // WIN32
#include "pop_service.hpp"
#include <utility>
#include <vbk/adaptors/block_provider.hpp>
#include <vbk/p2p_sync.hpp>
#include <vbk/pop_common.hpp>
#include <veriblock/pop.hpp>
namespace VeriBlock {
static uint64_t popScoreComparisons = 0ULL;
void InitPopContext(CDBWrapper& db)
{
auto payloads_provider = std::make_shared<PayloadsProvider>(db);
auto block_provider = std::make_shared<BlockReader>(db);
// disable ethash leveldb realisation due to performance decreasing
// auto ethash_cache = altintegration::make_unique<EthashCache>(db);
auto progpow_header_cache = altintegration::make_unique<ProgpowHeaderCache>(db);
SetPop(payloads_provider, block_provider, nullptr, std::move(progpow_header_cache));
}
CBlockIndex* compareTipToBlock(CBlockIndex* candidate)
{
AssertLockHeld(cs_main);
assert(candidate != nullptr && "block has no according header in block tree");
auto blockHash = candidate->GetBlockHash();
auto* tip = ChainActive().Tip();
if (!tip) {
// if tip is not set, candidate wins
return nullptr;
}
auto tipHash = tip->GetBlockHash();
if (tipHash == blockHash) {
// we compare tip with itself
return tip;
}
int result = 0;
if (Params().isPopActive(tip->nHeight)) {
result = compareForks(*tip, *candidate);
} else {
result = CBlockIndexWorkComparator()(tip, candidate) ? -1 : 1;
}
if (result < 0) {
// candidate has higher POP score
return candidate;
}
if (result == 0 && tip->nChainWork < candidate->nChainWork) {
// candidate is POP equal to current tip;
// candidate has higher chainwork
return candidate;
}
// otherwise, current chain wins
return tip;
}
bool acceptBlock(const CBlockIndex& indexNew, BlockValidationState& state)
{
AssertLockHeld(cs_main);
auto containing = VeriBlock::blockToAltBlock(indexNew);
altintegration::ValidationState instate;
if (!GetPop().getAltBlockTree().acceptBlockHeader(containing, instate)) {
LogPrintf("ERROR: alt tree cannot accept block %s\n", instate.toString());
return state.Invalid(BlockValidationResult::BLOCK_CACHED_INVALID, instate.GetPath());
}
return true;
}
bool addAllBlockPayloads(const CBlock& block, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
auto bootstrapBlockHeight = GetPop().getConfig().getAltParams().getBootstrapBlock().height;
auto hash = block.GetHash();
auto* index = LookupBlockIndex(hash);
if (index->nHeight == bootstrapBlockHeight) {
// skip bootstrap block block
return true;
}
altintegration::ValidationState instate;
if (!GetPop().check(block.popData, instate)) {
return error("[%s] block %s is not accepted because popData is invalid: %s", __func__, block.GetHash().ToString(),
instate.toString());
}
GetPop().getAltBlockTree().acceptBlock(block.GetHash().asVector(), block.popData);
return true;
}
bool setState(const uint256& block, altintegration::ValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
return GetPop().getAltBlockTree().setState(block.asVector(), state);
}
altintegration::PopData generatePopData() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
return GetPop().generatePopData();
}
// PoP rewards are calculated for the current tip but are paid in the next block
PoPRewards getPopRewards(const CBlockIndex& tip, const CChainParams& params) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
auto& pop = GetPop();
if (!params.isPopActive(tip.nHeight)) {
return {};
}
auto& cfg = pop.getConfig();
if (tip.nHeight < (int)cfg.alt->getEndorsementSettlementInterval()) {
return {};
}
if (tip.nHeight < (int)cfg.alt->getPayoutParams().getPopPayoutDelay()) {
return {};
}
altintegration::ValidationState state;
auto prevHash = tip.GetBlockHash().asVector();
bool ret = pop.getAltBlockTree().setState(prevHash, state);
VBK_ASSERT_MSG(ret, "error: %s", state.toString());
altintegration::PopPayouts rewards;
ret = pop.getPopPayout(prevHash, rewards, state);
VBK_ASSERT_MSG(ret, "error: %s", state.toString());
// erase rewards, that pay 0 satoshis, then halve rewards
PoPRewards result{};
for (const auto& r : rewards) {
// we use airth_uint256 to prevent any overflows
arith_uint256 coeff(r.second);
// 50% of multiplier towards POP.
// we divide by COIN here because `coeff` is X * COIN, Multiplier is Y*COIN.
// so payout becomes = X*Y*COIN*COIN/2.
arith_uint256 payout = (coeff * VeriBlock::GetSubsidyMultiplier(tip.nHeight + 1, params) / 2) / COIN;
if (payout > 0) {
CScript key = CScript(r.first.begin(), r.first.end());
assert(payout <= std::numeric_limits<int64_t>::max() && "overflow!");
result[key] = payout.GetLow64();
}
}
return result;
}
void addPopPayoutsIntoCoinbaseTx(CMutableTransaction& coinbaseTx, const CBlockIndex& pindexPrev, const CChainParams& params) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
PoPRewards rewards = getPopRewards(pindexPrev, params);
assert(coinbaseTx.vout.size() == 1 && "at this place we should have only PoW payout here");
for (const auto& itr : rewards) {
CTxOut out;
out.scriptPubKey = itr.first;
out.nValue = itr.second;
coinbaseTx.vout.push_back(out);
}
}
bool checkCoinbaseTxWithPopRewards(const CTransaction& tx, const CAmount& nFees, const CBlockIndex& pindex, const CChainParams& params, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
PoPRewards expectedRewards = getPopRewards(*pindex.pprev, params);
CAmount nTotalPopReward = 0;
if (tx.vout.size() < expectedRewards.size()) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-pop-vouts-size",
strprintf("checkCoinbaseTxWithPopRewards(): coinbase has incorrect size of pop vouts (actual vouts size=%d vs expected vouts=%d)", tx.vout.size(), expectedRewards.size()));
}
std::map<CScript, CAmount> cbpayouts;
// skip first reward, as it is always PoW payout
for (auto out = tx.vout.begin() + 1, end = tx.vout.end(); out != end; ++out) {
// pop payouts can not be null
if (out->IsNull()) {
continue;
}
cbpayouts[out->scriptPubKey] += out->nValue;
}
// skip first (regular pow) payout, and last 2 0-value payouts
for (const auto& payout : expectedRewards) {
auto& script = payout.first;
auto& expectedAmount = payout.second;
auto p = cbpayouts.find(script);
// coinbase pays correct reward?
if (p == cbpayouts.end()) {
// we expected payout for that address
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-pop-missing-payout",
strprintf("[tx: %s] missing payout for scriptPubKey: '%s' with amount: '%d'",
tx.GetHash().ToString(),
HexStr(script),
expectedAmount));
}
// payout found
auto& actualAmount = p->second;
// does it have correct amount?
if (actualAmount != expectedAmount) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-pop-wrong-payout",
strprintf("[tx: %s] wrong payout for scriptPubKey: '%s'. Expected %d, got %d.",
tx.GetHash().ToString(),
HexStr(script),
expectedAmount, actualAmount));
}
nTotalPopReward += expectedAmount;
}
CAmount PoWBlockReward = GetBlockSubsidy(pindex.nHeight, params);
if (tx.GetValueOut() > nTotalPopReward + PoWBlockReward + nFees) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
"bad-cb-pop-amount",
strprintf("ConnectBlock(): coinbase pays too much (actual=%d vs POW=%d + POP=%d)", tx.GetValueOut(), PoWBlockReward, nTotalPopReward));
}
return true;
}
std::vector<BlockBytes> getLastKnownVBKBlocks(size_t blocks)
{
LOCK(cs_main);
return altintegration::getLastKnownBlocks(GetPop().getVbkBlockTree(), blocks);
}
std::vector<BlockBytes> getLastKnownBTCBlocks(size_t blocks)
{
LOCK(cs_main);
return altintegration::getLastKnownBlocks(GetPop().getBtcBlockTree(), blocks);
}
bool hasPopData(CBlockTreeDB& db)
{
return db.Exists(tip_key<altintegration::BtcBlock>()) &&
db.Exists(tip_key<altintegration::VbkBlock>()) &&
db.Exists(tip_key<altintegration::AltBlock>());
}
void saveTrees(CDBBatch* batch)
{
AssertLockHeld(cs_main);
VeriBlock::BlockBatch b(*batch);
GetPop().saveAllTrees(b);
}
bool loadTrees(bool fast_load)
{
altintegration::ValidationState state;
if (!GetPop().loadAllTrees(fast_load, state)) {
return error("%s: failed to load trees %s", __func__, state.toString());
}
return true;
}
void removePayloadsFromMempool(const altintegration::PopData& popData) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
GetPop().getMemPool().removeAll(popData);
}
int compareForks(const CBlockIndex& leftForkTip, const CBlockIndex& rightForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
auto& pop = GetPop();
AssertLockHeld(cs_main);
if (&leftForkTip == &rightForkTip) {
return 0;
}
if (leftForkTip.GetAncestor(rightForkTip.nHeight) == &rightForkTip) {
// do not run POP FR on blocks which are already in active chain
return 1;
}
auto left = blockToAltBlock(leftForkTip);
auto right = blockToAltBlock(rightForkTip);
auto state = altintegration::ValidationState();
if (!pop.getAltBlockTree().setState(left.hash, state)) {
assert(false && "current tip is invalid");
}
popScoreComparisons++;
return pop.getAltBlockTree().comparePopScore(left.hash, right.hash);
}
void addDisconnectedPopdata(const altintegration::PopData& popData) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
altintegration::ValidationState state;
auto& popmp = VeriBlock::GetPop().getMemPool();
for (const auto& i : popData.context) {
popmp.submit(i, false, state);
}
for (const auto& i : popData.vtbs) {
popmp.submit(i, false, state);
}
for (const auto& i : popData.atvs) {
popmp.submit(i, false, state);
}
}
bool isCrossedBootstrapBlock()
{
auto* tip = ChainActive().Tip();
if (tip != nullptr) {
return isCrossedBootstrapBlock(tip->nHeight);
}
return false;
}
bool isCrossedBootstrapBlock(int32_t height)
{
auto block = VeriBlock::GetPop().getConfig().getAltParams().getBootstrapBlock();
return height >= block.getHeight();
}
bool isPopActive()
{
auto* tip = ChainActive().Tip();
if (tip != nullptr) {
return isPopActive(tip->nHeight);
}
return false;
}
bool isPopActive(int32_t height)
{
if (!isCrossedBootstrapBlock(height)) {
// if we didn't cross bootstrap block, then POP can't be active
return false;
}
return Params().isPopActive(height);
}
// get stats on POP score comparisons
uint64_t getPopScoreComparisons() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
return popScoreComparisons;
}
CAmount GetSubsidyMultiplier(int nHeight, const CChainParams& params)
{
// Offset halvings by the initial "non-halving" emissions which last 4183200 blocks
int halvings = (nHeight - 4183200) / params.GetConsensus().nSubsidyHalvingInterval;
// Force block reward to zero when right shift is undefined.
if (halvings >= 64)
return 0;
// If none of the switches below are hit, this value will be used (which is the value
// for the first halving period (so halvings=0)
CAmount nSubsidy = 1504471080 * COIN / 1000000000;
if (nHeight < 64800) {
nSubsidy = 7 * COIN; // First period, reward = 7.00
} else if (nHeight < 136800) {
nSubsidy = 65 * COIN / 10; // Second period, reward = 6.50
} else if (nHeight < 223200) {
nSubsidy = 6 * COIN; // Third period, reward = 6.00
} else if (nHeight < 331200) {
nSubsidy = 575 * COIN / 100; // Fourth period, reward = 5.75
} else if (nHeight < 468000) {
nSubsidy = 55 * COIN / 10; // Fifth period, reward = 5.50
} else if (nHeight < 640800) {
nSubsidy = 525 * COIN / 100; // Sixth period, reward = 5.25
} else if (nHeight < 856800) {
nSubsidy = 5 * COIN; // Seventh period, reward = 5.00
} else if (nHeight < 1123200) {
nSubsidy = 475 * COIN / 100; // Eigth period, reward = 4.75
} else if (nHeight < 1447200) {
nSubsidy = 45 * COIN / 10; // Ninth period, reward = 4.50
} else if (nHeight < 1836000) {
nSubsidy = 425 * COIN / 100; // Tenth period, reward = 4.25
} else if (nHeight < 2296800) {
nSubsidy = 4 * COIN; // Eleventh period, reward = 4.00
} else if (nHeight < 2836800) {
nSubsidy = 375 * COIN / 100; // Twelfth period, reward = 3.75
} else if (nHeight < 3463200) {
nSubsidy = 35 * COIN / 10; // Thirteenth period, reward = 3.50
} else if (nHeight < 4183200) {
nSubsidy = 3 * COIN; // Fourteenth period, reward = 3.00
}
// Subsidy is cut in half every 1,051,200 blocks which will occur approximately every 4 years.
if (halvings > 0) {
nSubsidy >>= halvings;
}
return nSubsidy;
}
} // namespace VeriBlock