Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from mtl1979/masternode
Browse files Browse the repository at this point in the history
Masternode + daemon/wallet fixes
  • Loading branch information
BITTORIUM authored Dec 22, 2018
2 parents ac8c121 + 49e9425 commit 472b685
Show file tree
Hide file tree
Showing 39 changed files with 4,719 additions and 3,998 deletions.
9 changes: 8 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.git* export-ignore
/CMakeLists.txt export-subst
*.sh text eol=lf
*.sh text eol=lf
*.h text
*.c text
*.cc text
*.cpp text
*.java text
*.md text
*.rc text eol=crlf
5 changes: 5 additions & 0 deletions include/INode.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -99,6 +100,8 @@ class INode {
virtual uint32_t getLocalBlockCount() const = 0;
virtual uint32_t getKnownBlockCount() const = 0;
virtual uint64_t getLastLocalBlockTimestamp() const = 0;
virtual std::string getLastFeeAddress() const = 0;
virtual std::string getLastCollateralHash() const = 0;

virtual void getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) = 0;
virtual void getTransactionHashesByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionHashes, const Callback& callback) = 0;
Expand All @@ -116,6 +119,8 @@ class INode {
virtual void getBlocks(const std::vector<Crypto::Hash>& blockHashes, std::vector<BlockDetails>& blocks, const Callback& callback) = 0;
virtual void getBlock(const uint32_t blockHeight, BlockDetails &block, const Callback& callback) = 0;
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<TransactionDetails>& transactions, const Callback& callback) = 0;
virtual void getFeeAddress(std::string &feeAddress, const Callback& callback) = 0;
virtual void getCollateralHash(std::string &collateralHash, const Callback & callback) = 0;
virtual void isSynchronized(bool& syncStatus, const Callback& callback) = 0;
};

Expand Down
11 changes: 11 additions & 0 deletions src/CryptoNoteCore/BlockchainCache.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -590,6 +591,16 @@ BinaryArray BlockchainCache::getRawTransaction(uint32_t index, uint32_t transact
}
}

BinaryArray BlockchainCache::getRawTransaction(const Crypto::Hash &transaction) const {
auto& index = transactions.get<TransactionHashTag>();
auto it = index.find(transaction);
if (it == index.end()) {
return parent->getRawTransaction(transaction);
}

return getRawTransaction(it->blockIndex, it->transactionIndex);
}

std::vector<BinaryArray>
BlockchainCache::getRawTransactions(const std::vector<Crypto::Hash>& requestedTransactions) const {
std::vector<Crypto::Hash> misses;
Expand Down
3 changes: 3 additions & 0 deletions src/CryptoNoteCore/BlockchainCache.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -177,6 +178,8 @@ class BlockchainCache : public IBlockchainCache {
std::vector<Crypto::Hash> &missedTransactions) const override;
virtual RawBlock getBlockByIndex(uint32_t index) const override;
virtual BinaryArray getRawTransaction(uint32_t blockIndex, uint32_t transactionIndex) const override;
virtual BinaryArray getRawTransaction(const Crypto::Hash &transaction) const override;

virtual std::vector<Crypto::Hash> getTransactionHashes() const override;
virtual std::vector<uint32_t> getRandomOutsByAmount(uint64_t amount, size_t count, uint32_t blockIndex) const override;
virtual ExtractOutputKeysResult extractKeyOutputs(uint64_t amount, uint32_t blockIndex, Common::ArrayView<uint32_t> globalIndexes,
Expand Down
34 changes: 34 additions & 0 deletions src/CryptoNoteCore/Core.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -458,6 +459,39 @@ bool Core::queryBlocksLite(const std::vector<Crypto::Hash>& knownBlockHashes, ui
}
}

bool Core::getTransaction(const Crypto::Hash& transactionHash, BinaryArray& transaction) const {
assert(!chainsLeaves.empty());
assert(!chainsStorage.empty());
throwIfNotInitialized();

IBlockchainCache* segment = chainsLeaves[0];
assert(segment != nullptr);

// find in main chain
do {
if (segment->hasTransaction(transactionHash)) {
transaction = segment->getRawTransaction(transactionHash);
return true;
}
segment = segment->getParent();
} while (segment != nullptr);

// find in alternative chains
for (size_t chain = 1; chain < chainsLeaves.size(); ++chain) {
segment = chainsLeaves[chain];

while (mainChainSet.count(segment) == 0) {
if (segment->hasTransaction(transactionHash)) {
transaction = segment->getRawTransaction(transactionHash);
return true;
}
segment = segment->getParent();
}
}

return false;
}

void Core::getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<BinaryArray>& transactions,
std::vector<Crypto::Hash>& missedHashes) const {
assert(!chainsLeaves.empty());
Expand Down
1 change: 1 addition & 0 deletions src/CryptoNoteCore/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Core : public ICore, public ICoreInformation {
uint32_t& startIndex, uint32_t& currentIndex, uint32_t& fullOffset, std::vector<BlockShortInfo>& entries) const override;

virtual bool hasTransaction(const Crypto::Hash& transactionHash) const override;
virtual bool getTransaction(const Crypto::Hash& transactionHash, BinaryArray& transaction) const;
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<BinaryArray>& transactions, std::vector<Crypto::Hash>& missedHashes) const override;

virtual Difficulty getBlockDifficulty(uint32_t blockIndex) const override;
Expand Down
4 changes: 2 additions & 2 deletions src/CryptoNoteCore/CryptoNoteFormatUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ bool constructTransaction(
//fill inputs
for (const TransactionSourceEntry& src_entr : sources) {
if (src_entr.realOutput >= src_entr.outputs.size()) {
logger(ERROR) << "real_output index (" << src_entr.realOutput << ")bigger than output_keys.size()=" << src_entr.outputs.size();
logger(ERROR) << "real_output index (" << src_entr.realOutput << ") bigger than output_keys.size()=" << src_entr.outputs.size();
return false;
}
summary_inputs_money += src_entr.amount;
Expand Down Expand Up @@ -307,7 +307,7 @@ bool checkOutsValid(const TransactionPrefix& tx, std::string* error) {
if (out.target.type() == typeid(KeyOutput)) {
if (out.amount == 0) {
if (error) {
*error = "Zero amount ouput";
*error = "Zero amount output";
}
return false;
}
Expand Down
33 changes: 33 additions & 0 deletions src/CryptoNoteCore/DatabaseBlockchainCache.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -1489,6 +1490,38 @@ BinaryArray DatabaseBlockchainCache::getRawTransaction(uint32_t blockIndex, uint
return getBlockByIndex(blockIndex).transactions.at(transactionIndex);
}

BinaryArray DatabaseBlockchainCache::getRawTransaction(const Crypto::Hash &transaction) const {
auto batch = BlockchainReadBatch().requestCachedTransaction(transaction);
auto res = readDatabase(batch);
for (auto& tx : res.getCachedTransactions()) {
batch.requestRawBlock(tx.second.blockIndex);
}

auto blocks = readDatabase(batch);

auto& hashesMap = res.getCachedTransactions();
auto& blocksMap = blocks.getRawBlocks();
auto transactionIt = hashesMap.find(transaction);
if (transactionIt == hashesMap.end()) {
logger(Logging::ERROR) << "detected missing transaction for hash " << transaction << " in getRawTransaction";
return BinaryArray();
}

auto blockIt = blocksMap.find(transactionIt->second.blockIndex);
if (blockIt == blocksMap.end()) {
logger(Logging::ERROR) << "detected missing transaction for hash " << transaction << " in getRawTransaction";
return BinaryArray();
}

if (transactionIt->second.transactionIndex == 0) {
auto block = fromBinaryArray<BlockTemplate>(blockIt->second.block);
return toBinaryArray(block.baseTransaction);
} else {
assert(blockIt->second.transactions.size() >= transactionIt->second.transactionIndex - 1);
return blockIt->second.transactions[transactionIt->second.transactionIndex - 1];
}
}

std::vector<Crypto::Hash> DatabaseBlockchainCache::getTransactionHashes() const {
assert(false);
return {};
Expand Down
2 changes: 2 additions & 0 deletions src/CryptoNoteCore/DatabaseBlockchainCache.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -148,6 +149,7 @@ class DatabaseBlockchainCache : public IBlockchainCache {
std::vector<Crypto::Hash>& missedTransactions) const override;
virtual RawBlock getBlockByIndex(uint32_t index) const override;
virtual BinaryArray getRawTransaction(uint32_t blockIndex, uint32_t transactionIndex) const override;
virtual BinaryArray getRawTransaction(const Crypto::Hash &transaction) const override;
virtual std::vector<Crypto::Hash> getTransactionHashes() const override;
virtual std::vector<uint32_t> getRandomOutsByAmount(uint64_t amount, size_t count,
uint32_t blockIndex) const override;
Expand Down
2 changes: 2 additions & 0 deletions src/CryptoNoteCore/IBlockchainCache.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium Project
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -85,6 +86,7 @@ class IBlockchainCache {

virtual RawBlock getBlockByIndex(uint32_t index) const = 0;
virtual BinaryArray getRawTransaction(uint32_t blockIndex, uint32_t transactionIndex) const = 0;
virtual BinaryArray getRawTransaction(const Crypto::Hash &transaction) const = 0;
virtual std::unique_ptr<IBlockchainCache> split(uint32_t splitBlockIndex) = 0;
virtual void pushBlock(
const CachedBlock& cachedBlock,
Expand Down
8 changes: 8 additions & 0 deletions src/CryptoNoteCore/TransactionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ const TransactionInput& getInputChecked(const CryptoNote::TransactionPrefix& tra

// TransactionOutput helper functions

uint64_t getTransactionOutputAmount(const TransactionOutput& out) {
if (out.target.type() == typeid(KeyOutput)) {
return out.amount;
}

return 0;
}

TransactionTypes::OutputType getTransactionOutputType(const TransactionOutputTarget& out) {
if (out.type() == typeid(KeyOutput)) {
return TransactionTypes::OutputType::Key;
Expand Down
1 change: 1 addition & 0 deletions src/CryptoNoteCore/TransactionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const TransactionInput& getInputChecked(const CryptoNote::TransactionPrefix& tra
bool isOutToKey(const Crypto::PublicKey& spendPublicKey, const Crypto::PublicKey& outKey, const Crypto::KeyDerivation& derivation, size_t keyIndex);

// TransactionOutput helper functions
uint64_t getTransactionOutputAmount(const TransactionOutput& out);
TransactionTypes::OutputType getTransactionOutputType(const TransactionOutputTarget& out);
const TransactionOutput& getOutputChecked(const CryptoNote::TransactionPrefix& transaction, size_t index);
const TransactionOutput& getOutputChecked(const CryptoNote::TransactionPrefix& transaction, size_t index, TransactionTypes::OutputType type);
Expand Down
32 changes: 31 additions & 1 deletion src/Daemon/Daemon.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2016-2018, The Karbo developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -73,6 +74,9 @@ namespace
const command_line::arg_descriptor<std::vector<std::string>> arg_genesis_block_reward_address = { "genesis-block-reward-address", "" };
const command_line::arg_descriptor<bool> arg_blockexplorer_on = {"enable_blockexplorer", "Enable blockchain explorer RPC", false};
const command_line::arg_descriptor<std::vector<std::string>> arg_enable_cors = { "enable-cors", "Adds header 'Access-Control-Allow-Origin' to the daemon's RPC responses. Uses the value as domain. Use * for all" };
const command_line::arg_descriptor<std::string> arg_set_fee_address = { "fee-address", "Sets fee address for light wallets to the daemon's RPC responses.", "" };
const command_line::arg_descriptor<std::string> arg_set_view_key = { "view-key", "Sets private view key to check for masternode's fee.", "" };
const command_line::arg_descriptor<std::string> arg_set_collateral_hash = { "collateral-hash", "Sets collateral transaction hash for masternode.", "" };
const command_line::arg_descriptor<bool> arg_testnet_on = {"testnet", "Used to deploy test nets. Checkpoints and hardcoded seeds are ignored, "
"network id is changed. Use it with --data-dir flag. The wallet must be launched with --testnet flag.", false};
const command_line::arg_descriptor<std::string> arg_load_checkpoints = {"load-checkpoints", "<default|filename> Use builtin default checkpoints or checkpoint csv file for faster initial blockchain sync", ""};
Expand Down Expand Up @@ -174,6 +178,9 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, arg_console);
command_line::add_arg(desc_cmd_sett, arg_testnet_on);
command_line::add_arg(desc_cmd_sett, arg_enable_cors);
command_line::add_arg(desc_cmd_sett, arg_set_fee_address);
command_line::add_arg(desc_cmd_sett, arg_set_view_key);
command_line::add_arg(desc_cmd_sett, arg_set_collateral_hash);
command_line::add_arg(desc_cmd_sett, arg_blockexplorer_on);
command_line::add_arg(desc_cmd_sett, arg_print_genesis_tx);
command_line::add_arg(desc_cmd_sett, arg_genesis_block_reward_address);
Expand Down Expand Up @@ -350,7 +357,30 @@ int main(int argc, char* argv[])

logger(INFO) << "Starting core rpc server on address " << rpcConfig.getBindAddress();
rpcServer.start(rpcConfig.bindIp, rpcConfig.bindPort);
rpcServer.enableCors(command_line::get_arg(vm, arg_enable_cors));
rpcServer.enableCors(command_line::get_arg(vm, arg_enable_cors));
if (command_line::has_arg(vm, arg_set_fee_address)) {
std::string addr_str = command_line::get_arg(vm, arg_set_fee_address);
if (!addr_str.empty()) {
AccountPublicAddress acc = boost::value_initialized<AccountPublicAddress>();
if (!currency.parseAccountAddressString(addr_str, acc)) {
logger(ERROR, BRIGHT_RED) << "Bad fee address: " << addr_str;
return 1;
}
rpcServer.setFeeAddress(addr_str, acc);
}
}
if (command_line::has_arg(vm, arg_set_view_key)) {
std::string vk_str = command_line::get_arg(vm, arg_set_view_key);
if (!vk_str.empty()) {
rpcServer.setViewKey(vk_str);
}
}
if (command_line::has_arg(vm, arg_set_collateral_hash)) {
std::string ch_str = command_line::get_arg(vm, arg_set_collateral_hash);
if (!ch_str.empty()) {
rpcServer.setCollateralHash(ch_str);
}
}
logger(INFO) << "Core rpc server started ok";

Tools::SignalHandler::install([&dch, &p2psrv] {
Expand Down
8 changes: 4 additions & 4 deletions src/Daemon/DaemonCommandsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ bool DaemonCommandsHandler::print_alternate_chains(const std::vector<std::string
//--------------------------------------------------------------------------------
bool DaemonCommandsHandler::print_block_by_height(uint32_t height)
{
if (height - 1 > m_core.getTopBlockIndex()) {
std::cout << "block wasn't found. Current block chain height: " << m_core.getTopBlockIndex() + 1 << ", requested: " << height << std::endl;
if (height > m_core.getTopBlockIndex()) {
std::cout << "block wasn't found. Current block chain height: " << m_core.getTopBlockIndex() << ", requested: " << height << std::endl;
return false;
}

auto hash = m_core.getBlockHashByIndex(height - 1);
auto hash = m_core.getBlockHashByIndex(height);
std::cout << "block_id: " << hash << ENDL;
print_as_json(m_core.getBlockByIndex(height - 1));
print_as_json(m_core.getBlockByIndex(height));

return true;
}
Expand Down
19 changes: 19 additions & 0 deletions src/InProcessNode/InProcessNode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -551,6 +552,14 @@ BlockHeaderInfo InProcessNode::getLastLocalBlockHeaderInfo() const {
return lastLocalBlockHeaderInfo;
}

std::string InProcessNode::getLastFeeAddress() const {
return "";
}

std::string InProcessNode::getLastCollateralHash() const {
return "";
}

void InProcessNode::getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) {
std::unique_lock<std::mutex> lock(mutex);
if (state != INITIALIZED) {
Expand Down Expand Up @@ -887,6 +896,16 @@ std::error_code InProcessNode::doGetTransactions(const std::vector<Crypto::Hash>
return std::error_code();
}

void InProcessNode::getFeeAddress(std::string& feeAddress, const Callback& callback) {
feeAddress = "";
callback({});
}

void InProcessNode::getCollateralHash(std::string& collateralHash, const Callback &callback) {
collateralHash = "";
callback({});
}

void InProcessNode::isSynchronized(bool& syncStatus, const Callback& callback) {
std::unique_lock<std::mutex> lock(mutex);
if (state != INITIALIZED) {
Expand Down
5 changes: 5 additions & 0 deletions src/InProcessNode/InProcessNode.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -66,6 +67,8 @@ class InProcessNode : public INode, public CryptoNote::ICryptoNoteProtocolObserv
virtual uint32_t getLocalBlockCount() const override;
virtual uint32_t getKnownBlockCount() const override;
virtual uint64_t getLastLocalBlockTimestamp() const override;
virtual std::string getLastFeeAddress() const override;
virtual std::string getLastCollateralHash() const override;

virtual void getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) override;
virtual void getTransactionHashesByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionHashes, const Callback& callback) override;
Expand All @@ -86,6 +89,8 @@ class InProcessNode : public INode, public CryptoNote::ICryptoNoteProtocolObserv
virtual void getBlocks(const std::vector<Crypto::Hash>& blockHashes, std::vector<BlockDetails>& blocks, const Callback& callback) override;
virtual void getBlock(const uint32_t blockHeight, BlockDetails &block, const Callback& callback) override;
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<TransactionDetails>& transactions, const Callback& callback) override;
virtual void getFeeAddress(std::string& feeAddress, const Callback& callback) override;
virtual void getCollateralHash(std::string &collateralHash, const Callback& callback) override;
virtual void isSynchronized(bool& syncStatus, const Callback& callback) override;

private:
Expand Down
Loading

0 comments on commit 472b685

Please sign in to comment.