Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
x

WIP

clang-format

WIP

x
  • Loading branch information
scravy committed Oct 12, 2018
1 parent ad63de5 commit 2c18172
Show file tree
Hide file tree
Showing 15 changed files with 456 additions and 89 deletions.
7 changes: 7 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ UNITE_CORE_H = \
policy/policy.h \
policy/rbf.h \
pow.h \
proposer/blockproposer.h \
proposer/chaininterface.h \
proposer/networkinterface.h \
proposer/proposer.h \
proposer/proposer_init.h \
proposer/transactionpicker.h \
Expand Down Expand Up @@ -265,6 +268,9 @@ libunite_server_a_SOURCES = \
policy/policy.cpp \
policy/rbf.cpp \
pow.cpp \
proposer/chaininterface.cpp \
proposer/networkinterface.cpp \
proposer/transactionpicker.cpp \
rest.cpp \
rpc/blockchain.cpp \
rpc/esperanza.cpp \
Expand Down Expand Up @@ -313,6 +319,7 @@ libunite_wallet_a_SOURCES = \
esperanza/stakevalidation.cpp \
esperanza/walletextension.cpp \
key/mnemonic/mnemonic.cpp \
proposer/blockproposer.cpp \
proposer/proposer.cpp \
proposer/proposer_init.cpp \
proposer/transactionpicker.cpp \
Expand Down
63 changes: 63 additions & 0 deletions src/esperanza/proposer_logic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 <esperanza/proposer_logic.h>

#include <esperanza/stakevalidation.h>
#include <esperanza/walletextension.h>
#include <miner.h>
#include <util.h>
#include <validation.h>
#include <wallet/wallet.h>

namespace esperanza {

CWallet *wallet;
//
// std::shared_ptr<const CBlock> ProposeBlock(
// const CChainParams &chainParams, CWallet *wallet,
// const BlockProposalRequest &blockProposalRequest) {
//
// CScript coinbaseScript;
// BlockAssembler blockAssembler(chainParams);
// std::unique_ptr<CBlockTemplate> blockTemplate =
// blockAssembler.CreateNewBlock(coinbaseScript, /* fMineWitnessTx */
// true);
//
// if (!blockTemplate) {
// // did not get block template
// return nullptr;
// }
//
// WalletExtension &walletExt = wallet->GetWalletExtension();
//
// // CreateCoinStake()
// // SignBlock()
//
// const CBlock &block = blockTemplate->block;
// auto sharedBlock = std::make_shared<const CBlock>(block);
//
// if (!CheckBlock(block)) {
// // failed PoS validation
// return nullptr;
// }
//
// if (!::ProcessNewBlock(chainParams, sharedBlock,
// /* fForceProcessing */ true,
// /* fNewBlock out */ nullptr)) {
// return nullptr;
// }
// return sharedBlock;
//}
//
// class BlockAssemblerAdapter {
//
// private:
// BlockAssembler blockAssembler;
//
//
//
//};

} // namespace esperanza
28 changes: 28 additions & 0 deletions src/esperanza/proposer_logic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 <chainparams.h>

#include <stdint.h>

#ifndef UNIT_E_ESPERANZA_PROPOSER_LOGIC_H
#define UNIT_E_ESPERANZA_PROPOSER_LOGIC_H

class CWallet;

namespace esperanza {
//
// struct BlockProposalRequest {
// int64_t blockTime;
// uint64_t blockHeight;
// uint64_t targetDifficulty;
//};
//
////! \brief tries to propose a block using the given block proposal request
// std::shared_ptr<const CBlock> ProposeBlock(const CChainParams &, CWallet *,
// const BlockProposalRequest &);

} // namespace esperanza

#endif // UNIT_E_PROPOSER_LOGIC_H
10 changes: 2 additions & 8 deletions src/esperanza/walletextension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ bool WalletExtension::SelectCoinsForStaking(
bool WalletExtension::CreateCoinStake(unsigned int nBits, int64_t nTime,
int nBlockHeight, int64_t nFees,
::CMutableTransaction &txNew,
::CKey &key) {
::CKey &keyOut) {
CBlockIndex *pindexPrev = chainActive.Tip();
arith_uint256 bnTargetPerCoinDay;
bnTargetPerCoinDay.SetCompact(nBits);
Expand Down Expand Up @@ -212,7 +212,7 @@ bool WalletExtension::CreateCoinStake(unsigned int nBits, int64_t nTime,
break; // only support pay to address (pay to pubkey hash)
}

if (!m_enclosingWallet->GetKey(spendId, key)) {
if (!m_enclosingWallet->GetKey(spendId, keyOut)) {
LogPrint(BCLog::PROPOSING,
"%s: Failed to get key for kernel type=%d.\n", __func__,
whichType);
Expand Down Expand Up @@ -362,12 +362,6 @@ bool WalletExtension::CreateCoinStake(unsigned int nBits, int64_t nTime,
return true;
}

bool WalletExtension::SignBlock(::CBlockTemplate *pblocktemplate, int nHeight,
int64_t nSearchTime) {
// UNIT-E: todo
return false;
}

bool WalletExtension::SetMasterKeyFromSeed(const key::mnemonic::Seed &seed,
std::string &error) {
const std::string walletFileName = m_enclosingWallet->GetName();
Expand Down
5 changes: 1 addition & 4 deletions src/esperanza/walletextension.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ class WalletExtension {

bool CreateCoinStake(unsigned int nBits, int64_t nTime, int nBlockHeight,
int64_t nFees, ::CMutableTransaction &txNew,
::CKey &key);

bool SignBlock(::CBlockTemplate *pblocktemplate, int nHeight,
int64_t nSearchTime);
::CKey &keyOut);

bool SetMasterKeyFromSeed(const key::mnemonic::Seed &seed,
std::string &error);
Expand Down
2 changes: 2 additions & 0 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class CBlock : public CBlockHeader
*((CBlockHeader*)this) = header;
}

CBlock(const CBlock &block) = default;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
Expand Down
29 changes: 29 additions & 0 deletions src/proposer/blockproposer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 "blockproposer.h"

namespace proposer {

class BlockProposerImpl : public BlockProposer {

private:
ChainInterface &m_chain;

public:
BlockProposerImpl(ChainInterface &chain) : m_chain(chain) {}

std::shared_ptr<const CBlock> ProposeBlock(
const ProposeBlockParameters &parameters) {
//m_chain.ProcessNewBlock(nullptr);
return nullptr;
};
};

std::shared_ptr<BlockProposer> BlockProposer::MakeBlockProposer(
ChainInterface &chain) {
return std::shared_ptr<BlockProposer>(new BlockProposerImpl(chain));
}

} // namespace proposer
72 changes: 72 additions & 0 deletions src/proposer/blockproposer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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_BLOCKPROPOSER_H
#define UNIT_E_PROPOSER_BLOCKPROPOSER_H

#include <proposer/chaininterface.h>

#include <stdint.h>

#include <memory>

class CBlock;
class CWallet;

namespace proposer {

//! \brief a component for proposing new blocks.
//!
//! The block proposer will build and propose a block, given a wallet
//! that has enough stake.
//!
//! The BlockProposer is different from the Proposer in proposer.cpp.
//! That one is managing concurrency (number of staking threads),
//! availability of wallets, balance, etc. The BlockProposer is used
//! to actually propose a block once we know that we have the means to
//! do so.
//!
//! This class is an interface.
class BlockProposer {

public:
struct ProposeBlockParameters {
//! \brief the height to propose the block for.
//!
//! The height of a block is encoded inside the coinbase transaction.
//! This is defined in BIP34 (Block v2, Height in Coinbase).
//! This also helps ensuring that the coinbase transaction has a
//! unique hash, hence prevent BIP30 (Duplicate transactions) from
//! applying.
//!
//! Block height is up to 2^31 which is enough to support one block
//! every second for 68 years. This is because block height used to
//! be signed and the signbit is now overloaded in some places to
//! signal a coinstake transaction in various serializations of
//! coins / UTXOs.
uint32_t blockHeight;

//! \brief the block time to propose the block with.
int64_t blockTime;

//! \brief the wallet to draw funds for staking from.
//!
//! The stake to propose with is drawn from the given wallet. The
//! amount of stake will increase the chances of proposing since
//! a certain difficulty threshold has to be met in order to do so.
CWallet *wallet;
};

virtual std::shared_ptr<const CBlock> ProposeBlock(
const ProposeBlockParameters &) = 0;

virtual ~BlockProposer() = default;

//! \brief Factory method for creating a BlockProposer
static std::shared_ptr<BlockProposer> MakeBlockProposer(ChainInterface &);
};

} // namespace proposer

#endif // UNIT_E_PROPOSER_BLOCKPROPOSER_H
50 changes: 50 additions & 0 deletions src/proposer/chaininterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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/chaininterface.h>

#include <chainparams.h>
#include <timedata.h>
#include <util.h>
#include <validation.h>

namespace proposer {

class ChainAdapter final : public ChainInterface {

CCriticalSection& GetLock() const override {
return cs_main;
}

uint32_t GetHeight() const override {
const int height = chainActive.Height();
if (height < 0) {
throw std::runtime_error("no active chain yet");
}
return static_cast<uint32_t>(height);
};

std::unique_ptr<const CBlockHeader> GetTip() const override {
const CBlockIndex *tip = chainActive.Tip();
if (tip == nullptr) {
throw std::runtime_error("no active chain yet");
}
return MakeUnique<const CBlockHeader>(tip->GetBlockHeader());
};

bool ProcessNewBlock(std::shared_ptr<const CBlock> pblock) override {
bool newBlock;
return ::ProcessNewBlock(::Params(), pblock, true, &newBlock);
};

::SyncStatus GetInitialBlockDownloadStatus() const override {
return ::GetInitialBlockDownloadStatus();
}
};

std::shared_ptr<ChainInterface> ChainInterface::MakeChainInterface() {
return std::shared_ptr<ChainInterface>(new ChainAdapter());
}

} // namespace proposer
Loading

0 comments on commit 2c18172

Please sign in to comment.