Skip to content

Commit

Permalink
Merge pull request #123 from BitHaru/master
Browse files Browse the repository at this point in the history
new features for V1.4.0
  • Loading branch information
BitHaru authored Nov 22, 2020
2 parents 2b8c3e8 + 72b1bfd commit c1a78d1
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.4)
project (rai)

set (CPACK_PACKAGE_VERSION_MAJOR "1")
set (CPACK_PACKAGE_VERSION_MINOR "3")
set (CPACK_PACKAGE_VERSION_MINOR "4")
set (CPACK_PACKAGE_VERSION_PATCH "0")

set (TAG_VERSION_STRING "V${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}")
Expand Down
4 changes: 4 additions & 0 deletions rai/common/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ std::string rai::ErrorString(rai::ErrorCode error_code)
{
return "Encounter unexpected error";
}
case rai::ErrorCode::INVALID_PRIVATE_KEY:
{
return "Invalid private key";
}
case rai::ErrorCode::SUBSCRIBE_TIMESTAMP:
{
return "Invalid subscription timestamp";
Expand Down
1 change: 1 addition & 0 deletions rai/common/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ enum class ErrorCode : int
WALLET_TIME_SYNC = 110,
ELECTION_CONFLICT = 111,
UNEXPECTED = 112,
INVALID_PRIVATE_KEY = 113,

// json parsing errors: 200 ~ 299
JSON_GENERIC = 200,
Expand Down
9 changes: 5 additions & 4 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,11 @@ void rai::NodeRpcHandler::DelegatorList()
for (const auto& i : list)
{
rai::Ptree entry;
total_weight += i.second;
entry.put("account", i.first.StringAccount());
entry.put("weight", i.second.StringDec());
entry.put("weight_in_rai", i.second.StringBalance(rai::RAI) + " RAI");
total_weight += i.weight_;
entry.put("account", i.account_.StringAccount());
entry.put("type", rai::BlockTypeToString(i.type_));
entry.put("weight", i.weight_.StringDec());
entry.put("weight_in_rai", i.weight_.StringBalance(rai::RAI) + " RAI");
list_ptree.push_back(std::make_pair("", entry));
}
response_.put_child("list", list_ptree);
Expand Down
21 changes: 17 additions & 4 deletions rai/rai_node/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,24 @@ rai::ErrorCode GetKeyPath(const boost::program_options::variables_map& vm,
rai::ErrorCode ProcessDaemon(const boost::program_options::variables_map& vm,
const boost::filesystem::path& data_path)
{
boost::filesystem::path key_path;
rai::ErrorCode error_code = GetKeyPath(vm, data_path, key_path);
IF_NOT_SUCCESS_RETURN(error_code);
rai::ErrorCode error_code = rai::ErrorCode::SUCCESS;
rai::Fan key(rai::uint256_union(0), rai::Fan::FAN_OUT);
if (vm.count("raw_key"))
{
error_code = InputRawKey(key);
IF_NOT_SUCCESS_RETURN(error_code);
}
else
{
boost::filesystem::path key_path;
error_code = GetKeyPath(vm, data_path, key_path);
IF_NOT_SUCCESS_RETURN(error_code);
error_code = rai::DecryptKey(key, key_path);
IF_NOT_SUCCESS_RETURN(error_code);
}

rai::Daemon daemon;
return daemon.Run(data_path, key_path);
return daemon.Run(data_path, key);
}

rai::ErrorCode ProcessKeyCreate(const boost::program_options::variables_map& vm,
Expand Down Expand Up @@ -221,6 +233,7 @@ void rai::CliAddOptions(boost::program_options::options_description& desc){
("version", "Prints out version")
("config_create", "Generate the config.json file")
("forward_reward_to", boost::program_options::value<std::string>(), "Specify a wallet account to receive node reward")
("raw_key", "Specify daemon to start with raw private key")
;

// clang-format on
Expand Down
6 changes: 1 addition & 5 deletions rai/rai_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <rai/node/node.hpp>

rai::ErrorCode rai::Daemon::Run(const boost::filesystem::path& data_path,
const boost::filesystem::path& key_path)
rai::Fan& key)
{
try
{
Expand All @@ -23,10 +23,6 @@ rai::ErrorCode rai::Daemon::Run(const boost::filesystem::path& data_path,
IF_NOT_SUCCESS_RETURN(error_code);
rai::Log::Init(data_path, config.node_.log_);

rai::Fan key(rai::uint256_union(0), rai::Fan::FAN_OUT);
error_code = rai::DecryptKey(key, key_path);
IF_NOT_SUCCESS_RETURN(error_code);

boost::asio::io_service service;
rai::Alarm alarm(service);
auto node = std::make_shared<rai::Node>(error_code, service, data_path,
Expand Down
4 changes: 2 additions & 2 deletions rai/rai_node/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include <rai/common/util.hpp>
#include <rai/node/node.hpp>
#include <rai/node/rpc.hpp>
#include <rai/secure/common.hpp>

namespace rai
{
class Daemon
{
public:
rai::ErrorCode Run(const boost::filesystem::path&,
const boost::filesystem::path&);
rai::ErrorCode Run(const boost::filesystem::path&, rai::Fan&);
};

class DaemonConfig
Expand Down
21 changes: 12 additions & 9 deletions rai/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,22 +2015,23 @@ void rai::Ledger::UpdateDelegatorList(const rai::Block& block)

std::lock_guard<std::mutex> lock(delegator_list_mutex_);
UpdateDelegatorList_(block.Account(), block.Representative(),
block.Balance());
block.Balance(), block.Type());
}

std::vector<std::pair<rai::Account, rai::Amount>> rai::Ledger::GetDelegatorList(
std::vector<rai::DelegatorListEntry> rai::Ledger::GetDelegatorList(
const rai::Account& rep, uint64_t max)
{
std::multimap<rai::Amount, rai::Account, std::greater<rai::Amount>>
std::multimap<rai::Amount, rai::DelegatorListEntry,
std::greater<rai::Amount>>
delegators;
std::vector<std::pair<rai::Account, rai::Amount>> result;
std::vector<rai::DelegatorListEntry> result;
std::lock_guard<std::mutex> lock(delegator_list_mutex_);

auto i = delegator_list_.get<1>().lower_bound(rep);
auto n = delegator_list_.get<1>().upper_bound(rep);
for (; i != n; ++i)
{
delegators.insert(std::make_pair(i->weight_, i->account_));
delegators.insert(std::make_pair(i->weight_, *i));
if (delegators.size() > max)
{
delegators.erase(std::prev(delegators.end()));
Expand All @@ -2041,7 +2042,7 @@ std::vector<std::pair<rai::Account, rai::Amount>> rai::Ledger::GetDelegatorList(

for (const auto& i : delegators)
{
result.emplace_back(i.second, i.first);
result.push_back(i.second);
}
return result;
}
Expand Down Expand Up @@ -2269,7 +2270,7 @@ rai::ErrorCode rai::Ledger::InitMemoryTables_(rai::Transaction& transaction)
if (enable_delegator_list_ && block->HasRepresentative())
{
UpdateDelegatorList_(account, block->Representative(),
block->Balance());
block->Balance(), block->Type());
}

if (enable_rich_list_ && block->Type() == rai::BlockType::TX_BLOCK)
Expand Down Expand Up @@ -2307,18 +2308,20 @@ void rai::Ledger::UpdateRichList_(const rai::Account& account,

void rai::Ledger::UpdateDelegatorList_(const rai::Account& account,
const rai::Account& rep,
const rai::Amount& weight)
const rai::Amount& weight,
rai::BlockType type)
{
auto it = delegator_list_.find(account);
if (it == delegator_list_.end())
{
delegator_list_.insert({account, rep, weight});
delegator_list_.insert({account, rep, weight, type});
}
else
{
delegator_list_.modify(it, [&](rai::DelegatorListEntry& data) {
data.rep_ = rep;
data.weight_ = weight;
data.type_ = type;
});
}
}
7 changes: 4 additions & 3 deletions rai/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class DelegatorListEntry
rai::Account account_;
rai::Account rep_;
rai::Amount weight_;
rai::BlockType type_;
};

typedef boost::multi_index_container<
Expand Down Expand Up @@ -321,8 +322,8 @@ class Ledger
void UpdateRichList(const rai::Block&);
std::vector<rai::RichListEntry> GetRichList(uint64_t);
void UpdateDelegatorList(const rai::Block&);
std::vector<std::pair<rai::Account, rai::Amount>> GetDelegatorList(
const rai::Account&, uint64_t);
std::vector<rai::DelegatorListEntry> GetDelegatorList(const rai::Account&,
uint64_t);

rai::ErrorCode UpgradeWallet(rai::Transaction&);
rai::ErrorCode UpgradeWalletV1V2(rai::Transaction&);
Expand All @@ -339,7 +340,7 @@ class Ledger
rai::ErrorCode InitMemoryTables_(rai::Transaction&);
void UpdateRichList_(const rai::Account&, const rai::Amount&);
void UpdateDelegatorList_(const rai::Account&, const rai::Account&,
const rai::Amount&);
const rai::Amount&, rai::BlockType);

static uint32_t constexpr BLOCKS_PER_INDEX = 8;
const rai::Amount RICH_LIST_MINIMUM = rai::Amount(10 * rai::RAI);
Expand Down
13 changes: 13 additions & 0 deletions rai/secure/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,16 @@ rai::ErrorCode rai::DecryptKey(rai::KeyPair& key_pair, const std::string& path,

return rai::ErrorCode::SUCCESS;
}

rai::ErrorCode rai::InputRawKey(rai::Fan& key)
{
rai::Password password;
password.Input("Input raw secret key:");

rai::RawKey raw_key;
bool error = raw_key.data_.DecodeHex(password.Get());
IF_ERROR_RETURN(error, rai::ErrorCode::INVALID_PRIVATE_KEY)

key.Set(raw_key);
return rai::ErrorCode::SUCCESS;
}
1 change: 1 addition & 0 deletions rai/secure/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void OpenOrCreate(std::fstream&, const std::string&);
rai::ErrorCode CreateKey(const boost::filesystem::path&, bool);
rai::ErrorCode DecryptKey(rai::Fan&, const boost::filesystem::path&);
rai::ErrorCode DecryptKey(rai::KeyPair&, const std::string&, const std::string&);
rai::ErrorCode InputRawKey(rai::Fan&);

// Reads a json object from the stream and if was changed, write the object back
// to the stream
Expand Down

0 comments on commit c1a78d1

Please sign in to comment.