Skip to content

Commit

Permalink
Merge branch 'bitcoin' into particl_dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tecnovert committed Mar 30, 2024
2 parents 4258513 + 61de64d commit 901ec08
Show file tree
Hide file tree
Showing 24 changed files with 967 additions and 171 deletions.
23 changes: 13 additions & 10 deletions src/kernel/mempool_persist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active

AutoFile file{opts.mockable_fopen_function(load_path, "rb")};
if (file.IsNull()) {
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
LogInfo("Failed to open mempool file. Continuing anyway.\n");
return false;
}

Expand All @@ -70,12 +70,12 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
uint64_t total_txns_to_load;
file >> total_txns_to_load;
uint64_t txns_tried = 0;
LogInfo("Loading %u mempool transactions from disk...\n", total_txns_to_load);
LogInfo("Loading %u mempool transactions from file...\n", total_txns_to_load);
int next_tenth_to_report = 0;
while (txns_tried < total_txns_to_load) {
const int percentage_done(100.0 * txns_tried / total_txns_to_load);
if (next_tenth_to_report < percentage_done / 10) {
LogInfo("Progress loading mempool transactions from disk: %d%% (tried %u, %u remaining)\n",
LogInfo("Progress loading mempool transactions from file: %d%% (tried %u, %u remaining)\n",
percentage_done, txns_tried, total_txns_to_load - txns_tried);
next_tenth_to_report = percentage_done / 10;
}
Expand Down Expand Up @@ -138,11 +138,11 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
}
}
} catch (const std::exception& e) {
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
LogInfo("Failed to deserialize mempool data on file: %s. Continuing anyway.\n", e.what());
return false;
}

LogPrintf("Imported mempool transactions from disk: %i succeeded, %i failed, %i expired, %i already there, %i waiting for initial broadcast\n", count, failed, expired, already_there, unbroadcast);
LogInfo("Imported mempool transactions from file: %i succeeded, %i failed, %i expired, %i already there, %i waiting for initial broadcast\n", count, failed, expired, already_there, unbroadcast);
return true;
}

Expand Down Expand Up @@ -184,7 +184,9 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
}
file.SetXor(xor_key);

file << (uint64_t)vinfo.size();
uint64_t mempool_transactions_to_write(vinfo.size());
file << mempool_transactions_to_write;
LogInfo("Writing %u mempool transactions to file...\n", mempool_transactions_to_write);
for (const auto& i : vinfo) {
file << TX_WITH_WITNESS(*(i.tx));
file << int64_t{count_seconds(i.m_time)};
Expand All @@ -194,7 +196,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock

file << mapDeltas;

LogPrintf("Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size());
LogInfo("Writing %d unbroadcast transactions to file.\n", unbroadcast_txids.size());
file << unbroadcast_txids;

if (!skip_file_commit && !FileCommit(file.Get()))
Expand All @@ -205,11 +207,12 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
}
auto last = SteadyClock::now();

LogPrintf("Dumped mempool: %.3fs to copy, %.3fs to dump\n",
LogInfo("Dumped mempool: %.3fs to copy, %.3fs to dump, %d bytes dumped to file\n",
Ticks<SecondsDouble>(mid - start),
Ticks<SecondsDouble>(last - mid));
Ticks<SecondsDouble>(last - mid),
fs::file_size(dump_path));
} catch (const std::exception& e) {
LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
LogInfo("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
return false;
}
return true;
Expand Down
6 changes: 6 additions & 0 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ struct CExtKey {
a.key == b.key;
}
CExtKey() = default;
CExtKey(const CExtPubKey& xpub, const CKey& key_in) : nDepth(xpub.nDepth), nChild(xpub.nChild), chaincode(xpub.chaincode), key(key_in)
{
std::copy(xpub.vchFingerprint, xpub.vchFingerprint + sizeof(xpub.vchFingerprint), vchFingerprint);
}
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
[[nodiscard]] bool Derive(CExtKey& out, unsigned int nChild) const;
Expand Down
7 changes: 7 additions & 0 deletions src/key/extkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ struct CExtKey {
memcmp(&a.chaincode[0], &b.chaincode[0], 32) == 0 && a.key == b.key;
}

CExtKey() = default;
CExtKey(const CExtPubKey& xpub, const CKey& key_in) : nDepth(xpub.nDepth), nChild(xpub.nChild), key(key_in)
{
std::copy(xpub.vchFingerprint, xpub.vchFingerprint + sizeof(xpub.vchFingerprint), vchFingerprint);
std::copy(xpub.chaincode, xpub.chaincode + sizeof(xpub.chaincode), chaincode);
}

bool IsValid() const { return key.IsValid(); }

void Encode(unsigned char code[74]) const;
Expand Down
4 changes: 1 addition & 3 deletions src/policy/rbf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ std::optional<std::pair<DiagramCheckError, std::string>> ImprovesFeerateDiagram(
CAmount replacement_fees,
int64_t replacement_vsize)
{
// Require that the replacement strictly improve the mempool's feerate diagram.
std::vector<FeeFrac> old_diagram, new_diagram;

// Require that the replacement strictly improves the mempool's feerate diagram.
const auto diagram_results{pool.CalculateFeerateDiagramsForRBF(replacement_fees, replacement_vsize, direct_conflicts, all_conflicts)};

if (!diagram_results.has_value()) {
Expand Down
5 changes: 5 additions & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "logging", 1, "exclude" },
{ "disconnectnode", 1, "nodeid" },
{ "upgradewallet", 0, "version" },
{ "gethdkeys", 0, "active_only" },
{ "gethdkeys", 0, "options" },
{ "gethdkeys", 0, "private" },
{ "createwalletdescriptor", 1, "options" },
{ "createwalletdescriptor", 1, "internal" },
// Echo with conversion (For testing only)
{ "echojson", 0, "arg0" },
{ "echojson", 1, "arg1" },
Expand Down
42 changes: 42 additions & 0 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ struct PubkeyProvider

/** Derive a private key, if private data is available in arg. */
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;

/** Return the non-extended public key for this PubkeyProvider, if it has one. */
virtual std::optional<CPubKey> GetRootPubKey() const = 0;
/** Return the extended public key for this PubkeyProvider, if it has one. */
virtual std::optional<CExtPubKey> GetRootExtPubKey() const = 0;
};

class OriginPubkeyProvider final : public PubkeyProvider
Expand Down Expand Up @@ -265,6 +270,14 @@ class OriginPubkeyProvider final : public PubkeyProvider
{
return m_provider->GetPrivKey(pos, arg, key);
}
std::optional<CPubKey> GetRootPubKey() const override
{
return m_provider->GetRootPubKey();
}
std::optional<CExtPubKey> GetRootExtPubKey() const override
{
return m_provider->GetRootExtPubKey();
}
};

/** An object representing a parsed constant public key in a descriptor. */
Expand Down Expand Up @@ -310,6 +323,14 @@ class ConstPubkeyProvider final : public PubkeyProvider
{
return arg.GetKey(m_pubkey.GetID(), key);
}
std::optional<CPubKey> GetRootPubKey() const override
{
return m_pubkey;
}
std::optional<CExtPubKey> GetRootExtPubKey() const override
{
return std::nullopt;
}
};

enum class DeriveType {
Expand Down Expand Up @@ -525,6 +546,14 @@ class BIP32PubkeyProvider final : public PubkeyProvider
key = extkey.key;
return true;
}
std::optional<CPubKey> GetRootPubKey() const override
{
return std::nullopt;
}
std::optional<CExtPubKey> GetRootExtPubKey() const override
{
return m_root_extkey;
}
};

/** Base class for all Descriptor implementations. */
Expand Down Expand Up @@ -720,6 +749,19 @@ class DescriptorImpl : public Descriptor
std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; }

std::optional<int64_t> MaxSatisfactionElems() const override { return {}; }

void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override
{
for (const auto& p : m_pubkey_args) {
std::optional<CPubKey> pub = p->GetRootPubKey();
if (pub) pubkeys.insert(*pub);
std::optional<CExtPubKey> ext_pub = p->GetRootExtPubKey();
if (ext_pub) ext_pubs.insert(*ext_pub);
}
for (const auto& arg : m_subdescriptor_args) {
arg->GetPubKeys(pubkeys, ext_pubs);
}
}
};

/** A parsed addr(A) descriptor. */
Expand Down
7 changes: 7 additions & 0 deletions src/script/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ struct Descriptor {

/** Get the maximum size number of stack elements for satisfying this descriptor. */
virtual std::optional<int64_t> MaxSatisfactionElems() const = 0;

/** Return all (extended) public keys for this descriptor, including any from subdescriptors.
*
* @param[out] pubkeys Any public keys
* @param[out] ext_pubs Any extended public keys
*/
virtual void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const = 0;
};

/** Parse a `descriptor` string. Included private keys are put in `out`.
Expand Down
17 changes: 16 additions & 1 deletion src/test/fuzz/rbf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)
}
mempool_txs.emplace_back(*child);
pool.addUnchecked(ConsumeTxMemPoolEntry(fuzzed_data_provider, mempool_txs.back()));

if (fuzzed_data_provider.ConsumeBool()) {
pool.PrioritiseTransaction(mempool_txs.back().GetHash().ToUint256(), fuzzed_data_provider.ConsumeIntegralInRange<int32_t>(-100000, 100000));
}
}

// Pick some transactions at random to be the direct conflicts
Expand Down Expand Up @@ -174,5 +178,16 @@ FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)

// If internals report error, wrapper should too
auto err_tuple{ImprovesFeerateDiagram(pool, direct_conflicts, all_conflicts, replacement_fees, replacement_vsize)};
if (!calc_results.has_value()) assert(err_tuple.has_value());
if (!calc_results.has_value()) {
assert(err_tuple.value().first == DiagramCheckError::UNCALCULABLE);
} else {
// Diagram check succeeded
if (!err_tuple.has_value()) {
// New diagram's final fee should always match or exceed old diagram's
assert(calc_results->first.back().fee <= calc_results->second.back().fee);
} else if (calc_results->first.back().fee > calc_results->second.back().fee) {
// Or it failed, and if old diagram had higher fees, it should be a failure
assert(err_tuple.value().first == DiagramCheckError::FAILURE);
}
}
}
Loading

0 comments on commit 901ec08

Please sign in to comment.