Skip to content

backport: Merge bitcoin/bitcoin#27712, 27695, 27665, 25594 #6713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class WalletLoader : public ChainClient
virtual std::string getWalletDir() = 0;

//! Restore backup wallet
virtual std::unique_ptr<Wallet> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
virtual BResult<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;

//! Return available wallets in wallet directory.
virtual std::vector<std::string> listWalletDir() = 0;
Expand Down
5 changes: 3 additions & 2 deletions src/qt/walletcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,10 @@ void RestoreWalletActivity::restore(const fs::path& backup_file, const std::stri
tr("Restoring Wallet <b>%1</b>…").arg(name.toHtmlEscaped()));

QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] {
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().restoreWallet(backup_file, wallet_name, m_error_message, m_warning_message);
auto wallet{node().walletLoader().restoreWallet(backup_file, wallet_name, m_warning_message)};

if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
m_error_message = wallet ? bilingual_str{} : wallet.GetError();
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(wallet.ReleaseObj());

QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
});
Expand Down
7 changes: 5 additions & 2 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,13 @@ class WalletLoaderImpl : public WalletLoader
options.require_existing = true;
return MakeWallet(m_context, LoadWallet(m_context, name, true /* load_on_start */, options, status, error, warnings));
}
std::unique_ptr<Wallet> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
BResult<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override
{
DatabaseStatus status;
return MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings));
bilingual_str error;
BResult<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))};
if (!wallet) return error;
return wallet;
}
std::string getWalletDir() override
{
Expand Down
10 changes: 0 additions & 10 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,13 +1188,3 @@ std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
{
return std::make_unique<DummyDatabase>();
}

/** Return object for accessing temporary in-memory database. */
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase()
{
#ifdef USE_SQLITE
return std::make_unique<SQLiteDatabase>("", "", true);
#elif defined(USE_BDB)
return std::make_unique<BerkeleyDatabase>(std::make_shared<BerkeleyEnvironment>(), "");
#endif
}
3 changes: 0 additions & 3 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,4 @@ bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, st
/** Return object for accessing dummy database with no read/write capabilities. */
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();

/** Return object for accessing temporary in-memory database. */
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();

#endif // BITCOIN_WALLET_WALLETDB_H
23 changes: 22 additions & 1 deletion test/functional/p2p_invalid_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test node responses to invalid network messages."""

import random


from test_framework.messages import (
CBlockHeader,
Expand Down Expand Up @@ -61,7 +63,7 @@ def run_test(self):
self.test_oversized_inv_msg()
self.test_oversized_getdata_msg()
self.test_oversized_headers_msg()
self.test_resource_exhaustion()
self.test_noncontinuous_headers_msg()

def test_buffer(self):
self.log.info("Test message with header split across two buffers is received")
Expand Down Expand Up @@ -186,6 +188,25 @@ def test_oversized_headers2_msg(self):
size = MAX_HEADERS_COMPRESSED_RESULT + 1
self.test_oversized_msg(msg_headers2([CBlockHeader()] * size), size)

def test_noncontinuous_headers_msg(self):
self.log.info("Test headers message with non-continuous headers sequence is logged as misbehaving")
block_hashes = self.generate(self.nodes[0], 10)
block_headers = []
for block_hash in block_hashes:
block_headers.append(from_hex(CBlockHeader(), self.nodes[0].getblockheader(block_hash, False)))

# continuous headers sequence should be fine
MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS = ['Misbehaving', 'non-continuous headers sequence']
peer = self.nodes[0].add_p2p_connection(P2PInterface())
with self.nodes[0].assert_debug_log([], unexpected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS):
peer.send_and_ping(msg_headers(block_headers))

# delete arbitrary block header somewhere in the middle to break link
del block_headers[random.randrange(1, len(block_headers)-1)]
with self.nodes[0].assert_debug_log(expected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS):
peer.send_and_ping(msg_headers(block_headers))
self.nodes[0].disconnect_p2ps()

def test_resource_exhaustion(self):
self.log.info("Test node stays up despite many large junk messages")
# Don't use v2 here - the non-optimised encryption would take too long to encrypt
Expand Down
35 changes: 28 additions & 7 deletions test/functional/p2p_leak_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) 2017-2020 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test that we don't leak txs to inbound peers that we haven't yet announced to"""
"""Test transaction upload"""

from test_framework.messages import msg_getdata, CInv, MSG_TX
from test_framework.p2p import p2p_lock, P2PDataStore
Expand All @@ -23,19 +23,40 @@ def set_test_params(self):
self.num_nodes = 1

def run_test(self):
gen_node = self.nodes[0] # The block and tx generating node
miniwallet = MiniWallet(gen_node)
self.gen_node = self.nodes[0] # The block and tx generating node
self.miniwallet = MiniWallet(self.gen_node)
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
self.generate(miniwallet, 1)
self.generate(gen_node, 100)
self.generate(self.miniwallet, 1)
self.generate(self.gen_node, 100)

inbound_peer = self.nodes[0].add_p2p_connection(P2PNode()) # An "attacking" inbound peer
self.test_tx_in_block()
self.test_notfound_on_unannounced_tx()

def test_tx_in_block(self):
self.log.info("Check that a transaction in the last block is uploaded (beneficial for compact block relay)")
inbound_peer = self.gen_node.add_p2p_connection(P2PNode())

self.log.debug("Generate transaction and block")
inbound_peer.last_message.pop("inv", None)
wtxid = self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"]
inbound_peer.wait_until(lambda: "inv" in inbound_peer.last_message and inbound_peer.last_message.get("inv").inv[0].hash == int(wtxid, 16))
want_tx = msg_getdata(inv=inbound_peer.last_message.get("inv").inv)
self.generate(self.gen_node, 1)

self.log.debug("Request transaction")
inbound_peer.last_message.pop("tx", None)
inbound_peer.send_and_ping(want_tx)
assert_equal(inbound_peer.last_message.get("tx").tx.getwtxid(), wtxid)

def test_notfound_on_unannounced_tx(self):
self.log.info("Check that we don't leak txs to inbound peers that we haven't yet announced to")
inbound_peer = self.gen_node.add_p2p_connection(P2PNode()) # An "attacking" inbound peer

MAX_REPEATS = 100
self.log.info("Running test up to {} times.".format(MAX_REPEATS))
for i in range(MAX_REPEATS):
self.log.info('Run repeat {}'.format(i + 1))
txid = miniwallet.send_self_transfer(from_node=gen_node)['txid']
txid = self.miniwallet.send_self_transfer(from_node=self.gen_node)['txid']

want_tx = msg_getdata()
want_tx.inv.append(CInv(t=MSG_TX, h=int(txid, 16)))
Expand Down
Loading