Skip to content

Commit c5e8a2b

Browse files
authored
Merge pull request #1337 from ElementsProject/master
elements-23.x: merge master to prep release
2 parents 8882cdc + cdcc74b commit c5e8a2b

35 files changed

+1159
-152
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ BITCOIN_CORE_H = \
202202
noui.h \
203203
outputtype.h \
204204
pegins.h \
205+
policy/discount.h \
205206
policy/feerate.h \
206207
policy/fees.h \
207208
policy/packages.h \

src/bench/rpc_blockchain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace {
1616

1717
struct TestBlockAndIndex {
18-
const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN)};
18+
std::unique_ptr<TestingSetup> testing_setup{MakeNoLogFileContext<TestingSetup>(CBaseChainParams::MAIN)};
1919
CBlock block{};
2020
uint256 blockHash{};
2121
CBlockIndex blockindex{};
@@ -28,6 +28,7 @@ struct TestBlockAndIndex {
2828

2929
stream >> block;
3030

31+
CBlockIndex::SetNodeContext(&(testing_setup->m_node));
3132
blockHash = block.GetHash();
3233
blockindex.phashBlock = &blockHash;
3334
blockindex.nBits = 403014710;

src/chain.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
#include <chain.h>
77
#include <util/time.h>
8+
#include <validation.h>
9+
#include <node/context.h>
10+
11+
12+
node::NodeContext *CBlockIndex::m_pcontext;
813

914
std::string CBlockFileInfo::ToString() const
1015
{
@@ -51,6 +56,27 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
5156
return CBlockLocator(vHave);
5257
}
5358

59+
void CBlockIndex::untrim() EXCLUSIVE_LOCKS_REQUIRED(::cs_main){
60+
AssertLockHeld(::cs_main);
61+
if (!trimmed())
62+
return;
63+
CBlockIndex tmp;
64+
const CBlockIndex *pindexfull = untrim_to(&tmp);
65+
assert(pindexfull!=this);
66+
m_trimmed = false;
67+
set_stored();
68+
proof = pindexfull->proof;
69+
m_dynafed_params = pindexfull->m_dynafed_params;
70+
m_signblock_witness = pindexfull->m_signblock_witness;
71+
m_pcontext->chainman->m_blockman.m_dirty_blockindex.insert(this);
72+
}
73+
74+
const CBlockIndex *CBlockIndex::untrim_to(CBlockIndex *pindexNew) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
75+
{
76+
AssertLockHeld(::cs_main);
77+
return m_pcontext->chainman->m_blockman.m_block_tree_db->RegenerateFullIndex(this, pindexNew);
78+
}
79+
5480
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
5581
if (pindex == nullptr) {
5682
return nullptr;

src/chain.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#include <vector>
1818

19+
namespace node {
20+
struct NodeContext;
21+
}
1922
/**
2023
* Maximum amount of time that a block timestamp is allowed to exceed the
2124
* current network-adjusted time before the block will be accepted.
@@ -215,26 +218,41 @@ class CBlockIndex
215218

216219
bool m_trimmed{false};
217220
bool m_trimmed_dynafed_block{false};
221+
bool m_stored_lvl{false};
218222

219223
friend class CBlockTreeDB;
220224

225+
static node::NodeContext *m_pcontext;
226+
221227
public:
228+
static void SetNodeContext(node::NodeContext *context) {m_pcontext = context;};
222229

223230
// Irrevocably remove blocksigning and dynafed-related stuff from this
224231
// in-memory copy of the block header.
225-
void trim() {
232+
bool trim() {
226233
assert_untrimmed();
234+
if (!m_stored_lvl) {
235+
// We can't trim in-memory data if it's not on disk yet, but we can if it's already been recovered once
236+
return false;
237+
}
227238
m_trimmed = true;
228239
m_trimmed_dynafed_block = !m_dynafed_params.value().IsNull();
229240
proof = std::nullopt;
230241
m_dynafed_params = std::nullopt;
231242
m_signblock_witness = std::nullopt;
243+
return true;
232244
}
233245

246+
void untrim();
247+
const CBlockIndex * untrim_to(CBlockIndex *pindexNew) const;
248+
234249
inline bool trimmed() const {
235250
return m_trimmed;
236251
}
237252

253+
inline void set_stored() {
254+
m_stored_lvl = true;
255+
}
238256
inline void assert_untrimmed() const {
239257
assert(!m_trimmed);
240258
}
@@ -501,6 +519,9 @@ class CDiskBlockIndex : public CBlockIndex
501519

502520
// For compatibility with elements 0.14 based chains
503521
if (g_signed_blocks) {
522+
if (!ser_action.ForRead()) {
523+
obj.assert_untrimmed();
524+
}
504525
if (is_dyna) {
505526
READWRITE(obj.m_dynafed_params.value());
506527
READWRITE(obj.m_signblock_witness.value().stack);

src/chainparams.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ class CMainParams : public CChainParams {
223223
anyonecanspend_aremine = false;
224224
enforce_pak = false;
225225
multi_data_permitted = false;
226+
accept_discount_ct = false;
227+
create_discount_ct = false;
226228
consensus.has_parent_chain = false;
227229
g_signed_blocks = false;
228230
g_con_elementsmode = false;
@@ -361,6 +363,8 @@ class CTestNetParams : public CChainParams {
361363
anyonecanspend_aremine = false;
362364
enforce_pak = false;
363365
multi_data_permitted = false;
366+
accept_discount_ct = false;
367+
create_discount_ct = false;
364368
consensus.has_parent_chain = false;
365369
g_signed_blocks = false;
366370
g_con_elementsmode = false;
@@ -517,6 +521,8 @@ class SigNetParams : public CChainParams {
517521
anyonecanspend_aremine = false;
518522
enforce_pak = false;
519523
multi_data_permitted = false;
524+
accept_discount_ct = false;
525+
create_discount_ct = false;
520526
consensus.has_parent_chain = false;
521527
g_signed_blocks = false; // lol
522528
g_con_elementsmode = false;
@@ -610,6 +616,8 @@ class CRegTestParams : public CChainParams {
610616
anyonecanspend_aremine = false;
611617
enforce_pak = false;
612618
multi_data_permitted = false;
619+
accept_discount_ct = false;
620+
create_discount_ct = false;
613621
consensus.has_parent_chain = false;
614622
g_signed_blocks = false;
615623
g_con_elementsmode = false;
@@ -887,6 +895,8 @@ class CCustomParams : public CRegTestParams {
887895
const CScript default_script(CScript() << OP_TRUE);
888896
consensus.fedpegScript = StrHexToScriptWithDefault(args.GetArg("-fedpegscript", ""), default_script);
889897
consensus.start_p2wsh_script = args.GetIntArg("-con_start_p2wsh_script", consensus.start_p2wsh_script);
898+
create_discount_ct = args.GetBoolArg("-creatediscountct", false);
899+
accept_discount_ct = args.GetBoolArg("-acceptdiscountct", create_discount_ct);
890900

891901
// Calculate pegged Bitcoin asset
892902
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID);
@@ -1023,7 +1033,7 @@ class CLiquidTestNetParams : public CCustomParams {
10231033
*/
10241034
class CLiquidV1Params : public CChainParams {
10251035
public:
1026-
CLiquidV1Params()
1036+
explicit CLiquidV1Params(const ArgsManager& args)
10271037
{
10281038

10291039
strNetworkID = "liquidv1";
@@ -1118,6 +1128,8 @@ class CLiquidV1Params : public CChainParams {
11181128
enforce_pak = true;
11191129

11201130
multi_data_permitted = true;
1131+
create_discount_ct = args.GetBoolArg("-creatediscountct", false);
1132+
accept_discount_ct = args.GetBoolArg("-acceptdiscountct", false);
11211133

11221134
parentGenesisBlockHash = uint256S("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
11231135
const bool parent_genesis_is_null = parentGenesisBlockHash == uint256();
@@ -1261,7 +1273,7 @@ class CLiquidV1Params : public CChainParams {
12611273
*/
12621274
class CLiquidV1TestParams : public CLiquidV1Params {
12631275
public:
1264-
explicit CLiquidV1TestParams(const ArgsManager& args)
1276+
explicit CLiquidV1TestParams(const ArgsManager& args) : CLiquidV1Params(args)
12651277
{
12661278
// Our goal here is to override ONLY the things from liquidv1 that make no sense for a test chain / which are pointless and burdensome to require people to override manually.
12671279

@@ -1466,6 +1478,8 @@ class CLiquidV1TestParams : public CLiquidV1Params {
14661478
enforce_pak = args.GetBoolArg("-enforce_pak", enforce_pak);
14671479

14681480
multi_data_permitted = args.GetBoolArg("-multi_data_permitted", multi_data_permitted);
1481+
create_discount_ct = args.GetBoolArg("-creatediscountct", create_discount_ct);
1482+
accept_discount_ct = args.GetBoolArg("-acceptdiscountct", accept_discount_ct || create_discount_ct);
14691483

14701484
if (args.IsArgSet("-parentgenesisblockhash")) {
14711485
parentGenesisBlockHash = uint256S(args.GetArg("-parentgenesisblockhash", ""));
@@ -1557,7 +1571,7 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
15571571
} else if (chain == CBaseChainParams::REGTEST) {
15581572
return std::unique_ptr<CChainParams>(new CRegTestParams(args));
15591573
} else if (chain == CBaseChainParams::LIQUID1) {
1560-
return std::unique_ptr<CChainParams>(new CLiquidV1Params());
1574+
return std::unique_ptr<CChainParams>(new CLiquidV1Params(args));
15611575
} else if (chain == CBaseChainParams::LIQUID1TEST) {
15621576
return std::unique_ptr<CChainParams>(new CLiquidV1TestParams(args));
15631577
} else if (chain == CBaseChainParams::LIQUIDTESTNET) {

src/chainparams.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class CChainParams
135135
const std::string& ParentBlech32HRP() const { return parent_blech32_hrp; }
136136
bool GetEnforcePak() const { return enforce_pak; }
137137
bool GetMultiDataPermitted() const { return multi_data_permitted; }
138+
bool GetAcceptDiscountCT() const { return accept_discount_ct; }
139+
bool GetCreateDiscountCT() const { return create_discount_ct; }
138140

139141
protected:
140142
CChainParams() {}
@@ -167,6 +169,8 @@ class CChainParams
167169
std::string parent_blech32_hrp;
168170
bool enforce_pak;
169171
bool multi_data_permitted;
172+
bool accept_discount_ct;
173+
bool create_discount_ct;
170174
};
171175

172176
/**

src/core_write.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <consensus/validation.h>
1010
#include <issuance.h>
1111
#include <key_io.h>
12+
#include <policy/discount.h> // ELEMENTS
1213
#include <script/descriptor.h>
1314
#include <script/script.h>
1415
#include <script/sign.h>
@@ -238,6 +239,10 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
238239
entry.pushKV("version", static_cast<int64_t>(static_cast<uint32_t>(tx.nVersion)));
239240
entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION));
240241
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
242+
// ELEMENTS: add discountvsize
243+
if (Params().GetAcceptDiscountCT()) {
244+
entry.pushKV("discountvsize", GetDiscountVirtualTransactionSize(tx));
245+
}
241246
entry.pushKV("weight", GetTransactionWeight(tx));
242247
entry.pushKV("locktime", (int64_t)tx.nLockTime);
243248

src/dynafed.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
#include <dynafed.h>
33
#include <hash.h>
4+
#include <validation.h>
5+
#include <node/context.h>
46

57
bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consensus::Params& consensus, DynaFedParamEntry& winning_entry)
68
{
@@ -15,6 +17,10 @@ bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consens
1517
for (int32_t height = next_height - 1; height >= (int32_t)(next_height - consensus.dynamic_epoch_length); --height) {
1618
const CBlockIndex* p_epoch_walk = pindexPrev->GetAncestor(height);
1719
assert(p_epoch_walk);
20+
if (node::fTrimHeaders) {
21+
LOCK(cs_main);
22+
ForceUntrimHeader(p_epoch_walk);
23+
}
1824
const DynaFedParamEntry& proposal = p_epoch_walk->dynafed_params().m_proposed;
1925
const uint256 proposal_root = proposal.CalculateRoot();
2026
vote_tally[proposal_root]++;
@@ -60,6 +66,10 @@ DynaFedParamEntry ComputeNextBlockFullCurrentParameters(const CBlockIndex* pinde
6066
// may be pre-dynafed params
6167
const CBlockIndex* p_epoch_start = pindexPrev->GetAncestor(epoch_start_height);
6268
assert(p_epoch_start);
69+
if (node::fTrimHeaders) {
70+
LOCK(cs_main);
71+
ForceUntrimHeader(p_epoch_start);
72+
}
6373
if (p_epoch_start->dynafed_params().IsNull()) {
6474
// We need to construct the "full" current parameters of pre-dynafed
6575
// consensus
@@ -93,6 +103,10 @@ DynaFedParamEntry ComputeNextBlockCurrentParameters(const CBlockIndex* pindexPre
93103
{
94104
assert(pindexPrev);
95105

106+
if (node::fTrimHeaders) {
107+
LOCK(cs_main);
108+
ForceUntrimHeader(pindexPrev);
109+
}
96110
DynaFedParamEntry entry = ComputeNextBlockFullCurrentParameters(pindexPrev, consensus);
97111

98112
uint32_t next_height = pindexPrev->nHeight+1;

src/init.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ void SetupServerArgs(ArgsManager& argsman)
640640
argsman.AddArg("-initialreissuancetokens=<n>", "The amount of reissuance tokens created in the genesis block. (default: 0)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
641641
argsman.AddArg("-ct_bits", strprintf("The default number of hiding bits in a rangeproof. Will be exceeded to cover amounts exceeding the maximum hiding value. (default: %d)", 52), ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
642642
argsman.AddArg("-ct_exponent", strprintf("The hiding exponent. (default: %s)", 0), ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
643+
argsman.AddArg("-acceptdiscountct", "Accept discounted fees for Confidential Transactions (default: true for liquidv1, false for other chains)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
644+
argsman.AddArg("-creatediscountct", "Create Confidential Transactions with discounted fees (default: false). Setting this to true will also set 'acceptdiscountct' to true.", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
643645

644646
#if defined(USE_SYSCALL_SANDBOX)
645647
argsman.AddArg("-sandbox=<mode>", "Use the experimental syscall sandbox in the specified mode (-sandbox=log-and-abort or -sandbox=abort). Allow only expected syscalls to be used by bitcoind. Note that this is an experimental new feature that may cause bitcoind to exit or crash unexpectedly: use with caution. In the \"log-and-abort\" mode the invocation of an unexpected syscall results in a debug handler being invoked which will log the incident and terminate the program (without executing the unexpected syscall). In the \"abort\" mode the invocation of an unexpected syscall results in the entire process being killed immediately by the kernel without executing the unexpected syscall.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -1009,13 +1011,13 @@ bool AppInitParameterInteraction(const ArgsManager& args)
10091011
}
10101012

10111013
if (args.GetBoolArg("-trim_headers", false)) {
1012-
LogPrintf("Configured for header-trimming mode. This will reduce memory usage substantially, but we will be unable to serve as a full P2P peer, and certain header fields may be missing from JSON RPC output.\n");
1014+
LogPrintf("Configured for header-trimming mode. This will reduce memory usage substantially, but will increase IO usage when the headers need to be temporarily untrimmed.\n");
10131015
node::fTrimHeaders = true;
10141016
// This calculation is driven by GetValidFedpegScripts in pegins.cpp, which walks the chain
10151017
// back to current epoch start, and then an additional total_valid_epochs on top of that.
10161018
// We add one epoch here for the current partial epoch, and then another one for good luck.
10171019

1018-
node::nMustKeepFullHeaders = (chainparams.GetConsensus().total_valid_epochs + 2) * epoch_length;
1020+
node::nMustKeepFullHeaders = chainparams.GetConsensus().total_valid_epochs * epoch_length;
10191021
// This is the number of headers we can have in flight downloading at a time, beyond the
10201022
// set of blocks we've already validated. Capping this is necessary to keep memory usage
10211023
// bounded during IBD.
@@ -1241,6 +1243,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12411243
const ArgsManager& args = *Assert(node.args);
12421244
const CChainParams& chainparams = Params();
12431245

1246+
CBlockIndex::SetNodeContext(&node);
12441247
auto opt_max_upload = ParseByteUnits(args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET), ByteUnit::M);
12451248
if (!opt_max_upload) {
12461249
return InitError(strprintf(_("Unable to parse -maxuploadtarget: '%s'"), args.GetArg("-maxuploadtarget", "")));
@@ -1710,7 +1713,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17101713

17111714
// if pruning, unset the service bit and perform the initial blockstore prune
17121715
// after any wallet rescanning has taken place.
1713-
if (fPruneMode || node::fTrimHeaders) {
1716+
if (fPruneMode) {
17141717
LogPrintf("Unsetting NODE_NETWORK on prune mode\n");
17151718
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
17161719
if (!fReindex) {
@@ -1722,11 +1725,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17221725
}
17231726
}
17241727

1725-
if (node::fTrimHeaders) {
1726-
LogPrintf("Unsetting NODE_NETWORK_LIMITED on header trim mode\n");
1727-
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK_LIMITED);
1728-
}
1729-
17301728
// ********************************************************* Step 11: import blocks
17311729

17321730
if (!CheckDiskSpace(gArgs.GetDataDirNet())) {

src/net_processing.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,12 +3319,13 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33193319
for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex))
33203320
{
33213321
if (pindex->trimmed()) {
3322-
// For simplicity, if any of the headers they're asking for are trimmed,
3323-
// just drop the request.
3324-
LogPrint(BCLog::NET, "%s: ignoring getheaders from peer=%i which would return at least one trimmed header\n", __func__, pfrom.GetId());
3325-
return;
3322+
// Header is trimmed, reload from disk before sending
3323+
CBlockIndex tmpBlockIndexFull;
3324+
const CBlockIndex* pindexfull = pindex->untrim_to(&tmpBlockIndexFull);
3325+
vHeaders.push_back(pindexfull->GetBlockHeader());
3326+
} else {
3327+
vHeaders.push_back(pindex->GetBlockHeader());
33263328
}
3327-
vHeaders.push_back(pindex->GetBlockHeader());
33283329
if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
33293330
break;
33303331
}
@@ -4980,7 +4981,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
49804981
auto txid = txinfo.tx->GetHash();
49814982
auto wtxid = txinfo.tx->GetWitnessHash();
49824983
// Peer told you to not send transactions at that feerate? Don't bother sending it.
4983-
if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
4984+
// ELEMENTS: use the discounted vsize here so that discounted CTs are relayed.
4985+
// discountvsize only differs from vsize if accept_discount_ct is true.
4986+
if (txinfo.fee < filterrate.GetFee(txinfo.discountvsize)) {
49844987
continue;
49854988
}
49864989
if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;

0 commit comments

Comments
 (0)