Skip to content

Commit

Permalink
inject proposer
Browse files Browse the repository at this point in the history
  • Loading branch information
scravy committed Oct 21, 2018
1 parent 30549ac commit 4c62f37
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 97 deletions.
12 changes: 2 additions & 10 deletions src/esperanza/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ Settings::Settings(::ArgsManager &args) : Settings(args, Settings::Default()) {}
//! initializes the settings by reading them from an args manager.
Settings::Settings(::ArgsManager &args, const Settings &defaultConfig)
: m_proposing(args.GetBoolArg("-proposing", defaultConfig.m_proposing)),
m_validating(args.GetBoolArg("-validating", defaultConfig.m_validating)),
m_numberOfProposerThreads(static_cast<size_t>(args.GetArg(
"-proposerthreads",
static_cast<int64_t>(defaultConfig.m_numberOfProposerThreads)))),
m_proposerSleep(std::chrono::milliseconds(args.GetArg(
"-proposersleep",
static_cast<int64_t>(defaultConfig.m_proposerSleep.count())))),
m_minProposeInterval(std::chrono::milliseconds(args.GetArg(
"-minproposeinterval",
static_cast<int64_t>(defaultConfig.m_minProposeInterval.count())))) {}
m_validating(args.GetBoolArg("-validating", defaultConfig.m_validating)) {
}

} // namespace esperanza
11 changes: 0 additions & 11 deletions src/esperanza/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ struct Settings {
//! Whether this node should be a validator.
bool m_validating = false;

//! How many threads to use for proposing. At least 1, at most number of
//! wallets.
size_t m_numberOfProposerThreads = 1;

std::chrono::milliseconds m_proposerSleep = std::chrono::seconds(30);

//! Minimum interval between proposing blocks
std::chrono::milliseconds m_minProposeInterval = std::chrono::seconds(4);

std::string m_proposerThreadName = "proposer";

Settings(::ArgsManager &args);

Settings(::ArgsManager &args, const Settings &defaultConfig);
Expand Down
11 changes: 11 additions & 0 deletions src/injector.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@

#include <proposer/blockproposer.h>
#include <proposer/chainstate.h>
#include <proposer/cliargs.h>
#include <proposer/multiwallet.h>
#include <proposer/network.h>
#include <proposer/proposer.h>
#include <proposer/proposer_settings.h>
#include <proposer/transactionpicker.h>

class UnitEInjector : public Injector<UnitEInjector> {

COMPONENT(CliArgs, proposer::CliArgs, proposer::CliArgs::MakeCliArgs)

COMPONENT(Network, proposer::Network, proposer::Network::MakeNetwork)

COMPONENT(ChainState, proposer::ChainState, proposer::ChainState::MakeChain)

COMPONENT(MultiWallet, proposer::MultiWallet,
proposer::MultiWallet::MakeMultiWallet);

COMPONENT(TransactionPicker, proposer::TransactionPicker,
proposer::TransactionPicker::MakeBlockAssemblerAdapter)

Expand All @@ -29,6 +36,10 @@ class UnitEInjector : public Injector<UnitEInjector> {

COMPONENT(ProposerSettings, proposer::Settings,
proposer::Settings::MakeSettings)

COMPONENT(Proposer, proposer::Proposer, proposer::Proposer::MakeProposer,
proposer::Settings, proposer::MultiWallet, proposer::Network,
proposer::ChainState, proposer::BlockProposer)
};

#endif // UNIT_E_INJECTOR_H
20 changes: 20 additions & 0 deletions src/proposer/cliargs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2018 The unit-e core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <proposer/cliargs.h>

#include <util.h>

namespace proposer {

class ArgsManagerAdapter : public CliArgs {
public:
ArgsManager &GetArgsManager() { return gArgs; }
};

static std::unique_ptr<CliArgs> CliArgs::MakeCliArgs() {
return std::unique_ptr(new ArgsManagerAdapter());
}

} // namespace proposer
20 changes: 20 additions & 0 deletions src/proposer/cliargs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2018 The unit-e core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef UNIT_E_PROPOSER_CLIARGS_H
#define UNIT_E_PROPOSER_CLIARGS_H

#include <memory>

namespace proposer {

class CliArgs {
public:
virtual ~CliArgs() = default;
static std::unique_ptr<CliArgs> MakeCliArgs();
};

} // namespace proposer

#endif // UNIT_E_PROPOSER_CLIARGS_H
4 changes: 3 additions & 1 deletion src/proposer/multiwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace proposer {
class MultiWalletAdapter : public MultiWallet {

public:
const std::vector<CWallet *> GetWallets() const override { return vpwallets; }
const std::vector<CWallet *> &GetWallets() const override {
return vpwallets;
}
};

std::unique_ptr<MultiWallet> MultiWallet::MakeMultiWallet() {
Expand Down
2 changes: 1 addition & 1 deletion src/proposer/multiwallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace proposer {
class MultiWallet {

public:
virtual const std::vector<CWallet *> GetWallets() const = 0;
virtual const std::vector<CWallet *> &GetWallets() const = 0;

virtual ~MultiWallet() = default;

Expand Down
29 changes: 19 additions & 10 deletions src/proposer/proposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ void Proposer::Thread::SetStatus(const Status status, CWallet *const wallet) {
}

std::vector<std::unique_ptr<Proposer::Thread>> Proposer::CreateProposerThreads(
const std::vector<CWallet *> &wallets) {
Dependency<MultiWallet> multiWallet) {
const std::vector<CWallet *> &wallets = multiWallet->GetWallets();
// total number of threads can not exceed number of wallets
const size_t numThreads =
std::min(wallets.size(),
std::max<size_t>(1, m_settings.m_numberOfProposerThreads));
std::max<size_t>(1, m_settings->m_numberOfProposerThreads));

using WalletIndex = size_t;
using ThreadIndex = size_t;
Expand All @@ -76,7 +77,7 @@ std::vector<std::unique_ptr<Proposer::Thread>> Proposer::CreateProposerThreads(
thisThreadsWallets.push_back(wallets[entry->second]);
}
std::string threadName =
m_settings.m_proposerThreadName + "-" + std::to_string(threadIx);
m_settings->m_proposerThreadName + "-" + std::to_string(threadIx);
threads.emplace_back(std::unique_ptr<Proposer::Thread>(
new Proposer::Thread(threadName, *this, thisThreadsWallets)));
}
Expand All @@ -87,8 +88,8 @@ std::vector<std::unique_ptr<Proposer::Thread>> Proposer::CreateProposerThreads(
return threads;
}

Proposer::Proposer(const esperanza::Settings &settings,
const std::vector<CWallet *> &wallets,
Proposer::Proposer(Dependency<Settings> settings,
Dependency<MultiWallet> multiWallet,
Dependency<Network> networkInterface,
Dependency<ChainState> chainInterface,
Dependency<BlockProposer> blockProposer)
Expand All @@ -99,7 +100,7 @@ Proposer::Proposer(const esperanza::Settings &settings,
m_initSemaphore(0),
m_startSemaphore(0),
m_stopSemaphore(0),
m_threads(CreateProposerThreads(wallets)) {}
m_threads(CreateProposerThreads(multiWallet)) {}

Proposer::~Proposer() { Stop(); }

Expand Down Expand Up @@ -173,7 +174,7 @@ void Proposer::Run(Proposer::Thread &thread) {
continue;
}

int bestHeight;
uint32_t bestHeight;
int64_t bestTime;
{
LOCK(thread.m_proposer.m_chain->GetLock());
Expand All @@ -187,7 +188,7 @@ void Proposer::Run(Proposer::Thread &thread) {

for (auto *wallet : thread.m_wallets) {
const int64_t gracePeriod =
seconds(thread.m_proposer.m_settings.m_minProposeInterval);
seconds(thread.m_proposer.m_settings->m_minProposeInterval);
const int64_t lastTimeProposed =
wallet->GetWalletExtension().m_proposerState.m_lastTimeProposed;
const int64_t timeSinceLastProposal = currentTime - lastTimeProposed;
Expand Down Expand Up @@ -221,7 +222,7 @@ void Proposer::Run(Proposer::Thread &thread) {
// and induce a sleep for a different duration. The thread as a whole
// only has to sleep as long as the minimum of these durations to check
// the wallet which is due next in time.
auto sleepFor = thread.m_proposer.m_settings.m_proposerSleep;
auto sleepFor = thread.m_proposer.m_settings->m_proposerSleep;
const auto setSleepDuration =
[&sleepFor](const decltype(sleepFor) amount) {
sleepFor = std::min(sleepFor, amount);
Expand All @@ -231,7 +232,7 @@ void Proposer::Run(Proposer::Thread &thread) {

const int64_t waitTill =
walletExt.m_proposerState.m_lastTimeProposed +
seconds(thread.m_proposer.m_settings.m_minProposeInterval);
seconds(thread.m_proposer.m_settings->m_minProposeInterval);
if (bestTime < waitTill) {
const decltype(sleepFor) amount =
std::chrono::seconds(waitTill - bestTime);
Expand Down Expand Up @@ -298,4 +299,12 @@ void Proposer::Run(Proposer::Thread &thread) {
thread.m_proposer.m_stopSemaphore.release();
}

std::unique_ptr<Proposer> Proposer::MakeProposer(
Dependency<Settings> settings, Dependency<MultiWallet> multiWallet,
Dependency<Network> network, Dependency<ChainState> chainState,
Dependency<BlockProposer> blockProposer) {
return MakeUnique<Proposer>(settings, multiWallet, network, chainState,
blockProposer);
}

} // namespace proposer
29 changes: 12 additions & 17 deletions src/proposer/proposer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include <primitives/block.h>
#include <proposer/blockproposer.h>
#include <proposer/chainstate.h>
#include <proposer/multiwallet.h>
#include <proposer/network.h>
#include <proposer/proposer_settings.h>
#include <proposer/proposer_status.h>
#include <proposer/sync.h>
#include <proposer/waiter.h>
Expand All @@ -38,17 +40,14 @@ class Proposer {
friend struct ProposerAccess;

public:
Proposer(
//! [in] esperanza settings
const esperanza::Settings &,
//! [in] a reference to all wallets to propose from
const std::vector<CWallet *> &,
//! dependency on network interface
Dependency<Network>,
//! dependency on chain interface
Dependency<ChainState>,
//! dependency on block proposer
Dependency<BlockProposer>);
static std::unique_ptr<Proposer> MakeProposer(Dependency<Settings>,
Dependency<MultiWallet>,
Dependency<Network>,
Dependency<ChainState>,
Dependency<BlockProposer>);

Proposer(Dependency<Settings>, Dependency<MultiWallet>, Dependency<Network>,
Dependency<ChainState>, Dependency<BlockProposer>);

~Proposer();

Expand Down Expand Up @@ -105,13 +104,9 @@ class Proposer {
}
};

//! reference to esperanza settings
const esperanza::Settings &m_settings;

Dependency<Settings> m_settings;
Dependency<Network> m_network;

Dependency<ChainState> m_chain;

Dependency<BlockProposer> m_blockProposer;

//! a semaphore for synchronizing initialization
Expand All @@ -126,7 +121,7 @@ class Proposer {
const std::vector<std::unique_ptr<Thread>> m_threads;

std::vector<std::unique_ptr<Proposer::Thread>> CreateProposerThreads(
const std::vector<CWallet *> &wallets);
Dependency<MultiWallet>);

static void Run(Thread &);
};
Expand Down
11 changes: 11 additions & 0 deletions src/proposer/proposer_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ namespace proposer {

struct Settings {

//! How many threads to use for proposing. At least 1, at most number of
//! wallets.
size_t m_numberOfProposerThreads = 1;

std::chrono::milliseconds m_proposerSleep = std::chrono::seconds(30);

//! Minimum interval between proposing blocks
std::chrono::milliseconds m_minProposeInterval = std::chrono::seconds(4);

std::string m_proposerThreadName = "proposer";

//! for regtest, don't stake above nStakeLimitHeight
int m_stakeLimitHeight = 0;

Expand Down
Loading

0 comments on commit 4c62f37

Please sign in to comment.