Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

State Trie Download #5052

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions eth/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ int main(int argc, char** argv)
}


MinerCLI m(MinerCLI::OperationMode::None);
MinerCLI minerCLI(MinerCLI::OperationMode::None);

bool listenSet = false;
bool chainConfigIsSet = false;
Expand All @@ -288,6 +288,7 @@ int main(int argc, char** argv)
"Configure specialised blockchain using given JSON information\n");
addClientOption("mode,o", po::value<string>()->value_name("<full/peer>"),
"Start a full node or a peer node (default: full)\n");
addClientOption("download-state", "(experimental) Download state trie, part of fast-sync\n");
addClientOption("ipc", "Enable IPC server (default: on)");
addClientOption("ipcpath", po::value<string>()->value_name("<path>"),
"Set .ipc socket path (default: data directory)");
Expand Down Expand Up @@ -428,7 +429,7 @@ int main(int argc, char** argv)
return -1;
}
for (size_t i = 0; i < unrecognisedOptions.size(); ++i)
if (!m.interpretOption(i, unrecognisedOptions))
if (!minerCLI.interpretOption(i, unrecognisedOptions))
{
cerr << "Invalid argument: " << unrecognisedOptions[i] << "\n";
return -1;
Expand Down Expand Up @@ -805,7 +806,7 @@ int main(int argc, char** argv)
if (loggingOptions.verbosity > 0)
cout << EthGrayBold "cpp-ethereum, a C++ Ethereum client" EthReset << "\n";

m.execute();
minerCLI.execute();

fs::path secretsPath;
if (testingMode)
Expand Down Expand Up @@ -863,8 +864,11 @@ int main(int argc, char** argv)
chainParams.allowFutureBlocks = true;
}

SyncMode const syncMode =
vm.count("download-state") ? SyncMode::StateTrieDownload : SyncMode::FullSync;

dev::WebThreeDirect web3(WebThreeDirect::composeClientVersion("eth"), getDataDir(),
snapshotPath, chainParams, withExisting, nodeMode == NodeMode::Full ? caps : set<string>(),
snapshotPath, syncMode, chainParams, withExisting, nodeMode == NodeMode::Full ? caps : set<string>(),
netPrefs, &nodesState, testingMode);

if (!extraData.empty())
Expand Down Expand Up @@ -1029,8 +1033,8 @@ int main(int argc, char** argv)
if (c)
{
c->setGasPricer(gasPricer);
DEV_IGNORE_EXCEPTIONS(asEthashClient(c)->setShouldPrecomputeDAG(m.shouldPrecompute()));
c->setSealer(m.minerType());
DEV_IGNORE_EXCEPTIONS(asEthashClient(c)->setShouldPrecomputeDAG(minerCLI.shouldPrecompute()));
c->setSealer(minerCLI.minerType());
c->setAuthor(author);
if (networkID != NoNetworkID)
c->setNetworkId(networkID);
Expand Down
5 changes: 4 additions & 1 deletion libdevcore/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <boost/log/attributes/function.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/async_frontend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
Expand Down Expand Up @@ -80,8 +81,10 @@ BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)

void setupLogging(LoggingOptions const& _options)
{
/* auto sink = boost::make_shared<
boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>>();*/
auto sink = boost::make_shared<
boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>>();
boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>>();

boost::shared_ptr<std::ostream> stream{&std::cout, boost::null_deleter{}};
sink->locked_backend()->add_stream(stream);
Expand Down
6 changes: 3 additions & 3 deletions libethashseal/EthashClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ DEV_SIMPLE_EXCEPTION(ChainParamsNotEthash);

EthashClient::EthashClient(ChainParams const& _params, int _networkID, p2p::Host* _host,
std::shared_ptr<GasPricer> _gpForAdoption, fs::path const& _dbPath,
fs::path const& _snapshotPath, WithExisting _forceAction,
fs::path const& _snapshotPath, SyncMode _syncMode, WithExisting _forceAction,
TransactionQueue::Limits const& _limits)
: Client(
_params, _networkID, _host, _gpForAdoption, _dbPath, _snapshotPath, _forceAction, _limits)
: Client(_params, _networkID, _host, _gpForAdoption, _dbPath, _snapshotPath, _syncMode,
_forceAction, _limits)
{
// will throw if we're not an Ethash seal engine.
asEthashClient(*this);
Expand Down
2 changes: 1 addition & 1 deletion libethashseal/EthashClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class EthashClient: public Client
/// Trivial forwarding constructor.
EthashClient(ChainParams const& _params, int _networkID, p2p::Host* _host,
std::shared_ptr<GasPricer> _gpForAdoption, boost::filesystem::path const& _dbPath = {},
boost::filesystem::path const& _snapshotPath = {},
boost::filesystem::path const& _snapshotPath = {}, SyncMode _syncMode = SyncMode::FullSync,
WithExisting _forceAction = WithExisting::Trust,
TransactionQueue::Limits const& _l = TransactionQueue::Limits{1024, 1024});
~EthashClient();
Expand Down
9 changes: 7 additions & 2 deletions libethereum/BlockChainSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ template<typename T> void mergeInto(std::map<unsigned, std::vector<T>>& _contain

} // Anonymous namespace -- helper functions.

BlockChainSync::BlockChainSync(EthereumHost& _host):
m_host(_host),
BlockChainSync::BlockChainSync(EthereumHost& _host, SyncState _initialState /*= SyncState::Idle*/)
: m_host(_host),
m_state(_initialState),
m_chainStartBlock(_host.chain().chainStartBlockNumber()),
m_startingBlock(_host.chain().number()),
m_lastImportedBlock(m_startingBlock),
Expand Down Expand Up @@ -206,6 +207,10 @@ void BlockChainSync::onPeerStatus(std::shared_ptr<EthereumPeer> _peer)
{
RecursiveGuard l(x_sync);
DEV_INVARIANT_CHECK;

if (m_state == SyncState::Waiting)
return;

std::shared_ptr<SessionFace> session = _peer->session();
if (!session)
return; // Expired
Expand Down
2 changes: 1 addition & 1 deletion libethereum/BlockChainSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class EthereumPeer;
class BlockChainSync final: public HasInvariants
{
public:
BlockChainSync(EthereumHost& _host);
BlockChainSync(EthereumHost& _host, SyncState _initialState = SyncState::Idle);
~BlockChainSync();
void abortSync(); ///< Abort all sync activity

Expand Down
11 changes: 7 additions & 4 deletions libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ std::ostream& dev::eth::operator<<(std::ostream& _out, ActivityReport const& _r)

Client::Client(ChainParams const& _params, int _networkID, p2p::Host* _host,
std::shared_ptr<GasPricer> _gpForAdoption, fs::path const& _dbPath,
fs::path const& _snapshotPath, WithExisting _forceAction, TransactionQueue::Limits const& _l)
fs::path const& _snapshotPath, SyncMode _syncMode, WithExisting _forceAction,
TransactionQueue::Limits const& _l)
: ClientBase(),
Worker("eth", 0),
m_bc(_params, _dbPath, _forceAction,
Expand All @@ -85,7 +86,7 @@ Client::Client(ChainParams const& _params, int _networkID, p2p::Host* _host,
m_postSeal(chainParams().accountStartNonce),
m_working(chainParams().accountStartNonce)
{
init(_host, _dbPath, _snapshotPath, _forceAction, _networkID);
init(_host, _dbPath, _snapshotPath, _syncMode, _forceAction, _networkID);
}

Client::~Client()
Expand All @@ -95,7 +96,9 @@ Client::~Client()
terminate();
}

void Client::init(p2p::Host* _extNet, fs::path const& _dbPath, fs::path const& _snapshotDownloadPath, WithExisting _forceAction, u256 _networkId)
void Client::init(p2p::Host* _extNet, fs::path const& _dbPath,
fs::path const& _snapshotDownloadPath, SyncMode _syncMode, WithExisting _forceAction,
u256 _networkId)
{
DEV_TIMED_FUNCTION_ABOVE(500);

Expand Down Expand Up @@ -133,7 +136,7 @@ void Client::init(p2p::Host* _extNet, fs::path const& _dbPath, fs::path const& _
if (_snapshotDownloadPath.empty())
{
auto host = _extNet->registerCapability(
make_shared<EthereumHost>(bc(), m_stateDB, m_tq, m_bq, _networkId));
make_shared<EthereumHost>(bc(), m_stateDB, m_tq, m_bq, _networkId, _syncMode));
m_host = host;

_extNet->addCapability(host, EthereumHost::staticName(),
Expand Down
13 changes: 5 additions & 8 deletions libethereum/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,12 @@ std::ostream& operator<<(std::ostream& _out, ActivityReport const& _r);
class Client: public ClientBase, protected Worker
{
public:
Client(
ChainParams const& _params,
int _networkID,
p2p::Host* _host,
Client(ChainParams const& _params, int _networkID, p2p::Host* _host,
std::shared_ptr<GasPricer> _gpForAdoption,
boost::filesystem::path const& _dbPath = boost::filesystem::path(),
boost::filesystem::path const& _snapshotPath = boost::filesystem::path(),
WithExisting _forceAction = WithExisting::Trust,
TransactionQueue::Limits const& _l = TransactionQueue::Limits{1024, 1024}
);
SyncMode _syncMode = SyncMode::FullSync, WithExisting _forceAction = WithExisting::Trust,
TransactionQueue::Limits const& _l = TransactionQueue::Limits{1024, 1024});
/// Destructor.
virtual ~Client();

Expand Down Expand Up @@ -215,7 +211,8 @@ class Client: public ClientBase, protected Worker
/// Perform critical setup functions.
/// Must be called in the constructor of the finally derived class.
void init(p2p::Host* _extNet, boost::filesystem::path const& _dbPath,
boost::filesystem::path const& _snapshotPath, WithExisting _forceAction, u256 _networkId);
boost::filesystem::path const& _snapshotPath, SyncMode _syncMode, WithExisting _forceAction,
u256 _networkId);

/// InterfaceStub methods
BlockChain& bc() override { return m_bc; }
Expand Down
4 changes: 2 additions & 2 deletions libethereum/ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ ClientTest* dev::eth::asClientTest(Interface* _c)
ClientTest::ClientTest(ChainParams const& _params, int _networkID, p2p::Host* _host,
std::shared_ptr<GasPricer> _gpForAdoption, fs::path const& _dbPath, WithExisting _forceAction,
TransactionQueue::Limits const& _limits)
: Client(
_params, _networkID, _host, _gpForAdoption, _dbPath, std::string(), _forceAction, _limits)
: Client(_params, _networkID, _host, _gpForAdoption, _dbPath, std::string(), SyncMode::FullSync,
_forceAction, _limits)
{}

ClientTest::~ClientTest()
Expand Down
5 changes: 5 additions & 0 deletions libethereum/CommonNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,10 @@ struct SyncStatus
bool majorSyncing = false;
};

enum class SyncMode
{
FullSync,
StateTrieDownload
};
}
}
65 changes: 44 additions & 21 deletions libethereum/EthereumHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@

#include "EthereumHost.h"

#include <chrono>
#include <thread>
#include <libdevcore/Common.h>
#include <libp2p/Host.h>
#include <libp2p/Session.h>
#include <libethcore/Exceptions.h>
#include "BlockChain.h"
#include "TransactionQueue.h"
#include "BlockChainSync.h"
#include "BlockQueue.h"
#include "EthereumPeer.h"
#include "BlockChainSync.h"
#include "FastSync.h"
#include "TransactionQueue.h"
#include <libdevcore/Common.h>
#include <libethcore/Exceptions.h>
#include <libp2p/Host.h>
#include <libp2p/Session.h>
#include <chrono>
#include <thread>

using namespace std;
using namespace dev;
Expand All @@ -48,7 +49,10 @@ namespace
class EthereumPeerObserver: public EthereumPeerObserverFace
{
public:
EthereumPeerObserver(shared_ptr<BlockChainSync> _sync, TransactionQueue& _tq): m_sync(_sync), m_tq(_tq) {}
EthereumPeerObserver(
shared_ptr<BlockChainSync> _sync, shared_ptr<FastSync> _fastSync, TransactionQueue& _tq)
: m_sync(move(_sync)), m_fastSync(move(_fastSync)), m_tq(_tq)
{}

void onPeerStatus(std::shared_ptr<EthereumPeer> _peer) override
{
Expand All @@ -62,6 +66,9 @@ class EthereumPeerObserver: public EthereumPeerObserverFace
cwarn << "Failed invariant during sync, restarting sync";
m_sync->restartSync();
}

if (m_fastSync)
m_fastSync->onPeerStatus(_peer);
}

void onPeerTransactions(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) override
Expand Down Expand Up @@ -95,6 +102,9 @@ class EthereumPeerObserver: public EthereumPeerObserverFace
cwarn << "Failed invariant during sync, restarting sync";
m_sync->restartSync();
}

if (m_fastSync)
m_fastSync->onPeerStatus(_peer);
}

void onPeerBlockBodies(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) override
Expand Down Expand Up @@ -151,8 +161,15 @@ class EthereumPeerObserver: public EthereumPeerObserverFace
LOG(m_logger) << "Receipts (" << dec << itemCount << " entries)";
}

void onPeerDisconnect(std::shared_ptr<EthereumPeer> _peer, Asking _asking) override
{
if (m_fastSync)
m_fastSync->onPeerDisconnect(_peer, _asking);
}

private:
shared_ptr<BlockChainSync> m_sync;
shared_ptr<FastSync> m_fastSync;
TransactionQueue& m_tq;

Logger m_logger{createLogger(VerbosityDebug, "host")};
Expand Down Expand Up @@ -367,21 +384,27 @@ class EthereumHostData: public EthereumHostDataFace

}

EthereumHost::EthereumHost(BlockChain const& _ch, OverlayDB const& _db, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId):
HostCapability<EthereumPeer>(),
Worker ("ethsync"),
m_chain (_ch),
EthereumHost::EthereumHost(BlockChain const& _chain, OverlayDB const& _db, TransactionQueue& _tq,
BlockQueue& _bq, u256 _networkId, SyncMode _syncMode)
: HostCapability<EthereumPeer>(),
Worker("ethsync"),
m_chain(_chain),
m_db(_db),
m_tq (_tq),
m_bq (_bq),
m_networkId (_networkId),
m_tq(_tq),
m_bq(_bq),
m_networkId(_networkId),
m_hostData(make_shared<EthereumHostData>(m_chain, m_db))
{
// TODO: Composition would be better. Left like that to avoid initialization
// issues as BlockChainSync accesses other EthereumHost members.
m_sync.reset(new BlockChainSync(*this));
m_peerObserver = make_shared<EthereumPeerObserver>(m_sync, m_tq);
m_latestBlockSent = _ch.currentHash();
// create BlockChainSync in Waiting state if it's not full sync
SyncState const initialFullSyncState =
_syncMode == SyncMode::FullSync ? SyncState::Idle : SyncState::Waiting;
m_sync.reset(new BlockChainSync(*this, initialFullSyncState));

if (_syncMode == SyncMode::StateTrieDownload)
m_fastSync = std::make_shared<FastSync>(*this, _chain);

m_peerObserver = make_shared<EthereumPeerObserver>(m_sync, m_fastSync, m_tq);
m_latestBlockSent = _chain.currentHash();
m_tq.onImport([this](ImportResult _ir, h256 const& _h, h512 const& _nodeId) { onTransactionImported(_ir, _h, _nodeId); });
}

Expand Down
5 changes: 4 additions & 1 deletion libethereum/EthereumHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace eth
class TransactionQueue;
class BlockQueue;
class BlockChainSync;
class FastSync;

/**
* @brief The EthereumHost class
Expand All @@ -60,7 +61,8 @@ class EthereumHost: public p2p::HostCapability<EthereumPeer>, Worker
{
public:
/// Start server, but don't listen.
EthereumHost(BlockChain const& _ch, OverlayDB const& _db, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId);
EthereumHost(BlockChain const& _ch, OverlayDB const& _db, TransactionQueue& _tq,
BlockQueue& _bq, u256 _networkId, SyncMode _syncMode);

/// Will block on network process events.
virtual ~EthereumHost();
Expand Down Expand Up @@ -132,6 +134,7 @@ class EthereumHost: public p2p::HostCapability<EthereumPeer>, Worker

mutable Mutex x_transactions;
std::shared_ptr<BlockChainSync> m_sync;
std::shared_ptr<FastSync> m_fastSync;
std::atomic<time_t> m_lastTick = { 0 };

std::shared_ptr<EthereumHostDataFace> m_hostData;
Expand Down