Skip to content

Commit

Permalink
Merge branch 'vladik'
Browse files Browse the repository at this point in the history
  • Loading branch information
valdok committed Dec 29, 2018
2 parents 2fa56a6 + 16d35ee commit 8bf6696
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 26 deletions.
81 changes: 58 additions & 23 deletions core/treasury.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ namespace beam
r.m_WalletID = pid;

HeightRange hr;
hr.m_Max = Rules::HeightGenesis - 1;
hr.m_Max = pars.m_Maturity0 + Rules::HeightGenesis - 1;

for (uint32_t iBurst = 0; iBurst < pars.m_Bursts; iBurst++)
{
Expand Down Expand Up @@ -431,37 +431,66 @@ namespace beam
return ctx.m_bValid;
}

std::vector<Treasury::Data::Burst> Treasury::Data::get_Bursts() const
{
std::vector<Treasury::Data::Burst> ret;
ret.reserve(m_vGroups.size());

for (size_t iG = 0; iG < m_vGroups.size(); iG++)
{
const Group& g = m_vGroups[iG];
if (g.m_Value == Zero)
continue;

ret.emplace_back();
Burst& b = ret.back();

b.m_Value = AmountBig::get_Hi(g.m_Value) ?
Amount(-1) :
AmountBig::get_Lo(g.m_Value);

b.m_Height = MaxHeight;

for (size_t i = 0; i < g.m_Data.m_vOutputs.size(); i++)
b.m_Height = std::min(b.m_Height, g.m_Data.m_vOutputs[i]->m_Incubation);
}

return ret;
}

void Treasury::Build(Data& d) const
{
// Assuming all the plans are generated with the same group/incubation parameters.
for (size_t iG = 0; ; iG++)
// Current design: group only responses of the same height.
typedef std::map<Height, Data::Group> GroupMap;
GroupMap map;

for (EntryMap::const_iterator itSrc = m_Entries.begin(); m_Entries.end() != itSrc; )
{
bool bNoPeers = true, bNoBlock = true;
const Entry& e = (itSrc++)->second;
if (!e.m_pResponse)
continue;
const Response& resp = *e.m_pResponse;

for (EntryMap::const_iterator it = m_Entries.begin(); m_Entries.end() != it; )
for (size_t iG = 0; iG < resp.m_vGroups.size(); iG++)
{
const Entry& e = (it++)->second;
if (!e.m_pResponse)
continue;
const Response& resp = *e.m_pResponse;

if (iG >= resp.m_vGroups.size())
continue;
bNoPeers = false;
const Response::Group& g = resp.m_vGroups[iG];

if (bNoBlock)
d.m_vGroups.emplace_back();
Height h = MaxHeight;
for (size_t i = 0; i < g.m_vCoins.size(); i++)
h = std::min(h, g.m_vCoins[i].m_pOutput->m_Incubation);

Data::Group& gOut = d.m_vGroups.back();
GroupMap::iterator it = map.find(h);
bool bNew = (map.end() == it);
if (bNew)
it = map.emplace(h, Data::Group()).first;

if (bNoBlock)
Data::Group& gOut = it->second;
if (bNew)
{
ZeroObject(gOut.m_Value);
gOut.m_Data.m_Offset = Zero;
bNoBlock = false;
}

const Response::Group& g = resp.m_vGroups[iG];
Response::Group::Reader r(g);

// merge
Expand All @@ -472,12 +501,18 @@ namespace beam
off += g.m_Base.m_Offset;
gOut.m_Data.m_Offset = off;
}
}

if (bNoPeers)
break;
d.m_vGroups.reserve(map.size());

if (!bNoBlock)
d.m_vGroups.back().m_Data.Normalize();
for (GroupMap::iterator it = map.begin(); map.end() != it; it++)
{
Data::Group& gSrc = it->second;
if (!(gSrc.m_Value == Zero))
{
gSrc.m_Data.Normalize();
d.m_vGroups.push_back(std::move(gSrc));
}
}

// finalize
Expand Down
9 changes: 9 additions & 0 deletions core/treasury.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ namespace beam

struct Parameters
{
Height m_Maturity0 = 0;
Height m_MaturityStep = 1440 * 365 / 12; // 1 month roughly
uint32_t m_Bursts = 12 * 5; // 5 years plan

Expand Down Expand Up @@ -178,6 +179,14 @@ namespace beam

bool IsValid() const;

struct Burst
{
Height m_Height;
Amount m_Value; // rounded to max value in case of overflow
};

std::vector<Burst> get_Bursts() const;

struct Coin
{
Height m_Incubation;
Expand Down
2 changes: 2 additions & 0 deletions core/unittest/ecc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,8 @@ void TestTreasury()
tres.Build(data);
verify_test(!data.m_vGroups.empty());

std::vector<beam::Treasury::Data::Burst> vBursts = data.get_Bursts();

// test serialization
beam::ByteBuffer bb;
ser1.swap_buf(bb);
Expand Down
8 changes: 6 additions & 2 deletions utility/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ namespace beam
const char* TR_WID = "tr_wid";
const char* TR_PERC = "tr_pecents";
const char* TR_COMMENT = "tr_comment";
// ui
const char* TR_M = "tr_M";
const char* TR_N = "tr_N";
// ui
const char* WALLET_ADDR = "addr";
const char* APPDATA_PATH = "appdata";
}
Expand Down Expand Up @@ -166,7 +168,9 @@ namespace beam
(cli::TR_OPCODE, po::value<uint32_t>()->default_value(0), "treasury operation: 0=print ID, 1=plan, 2=response, 3=import, 4=generate, 5=print")
(cli::TR_WID, po::value<std::string>(), "treasury WalletID")
(cli::TR_PERC, po::value<double>(), "treasury percent of the total emission, designated to this WalletID")
(cli::TR_COMMENT, po::value<std::string>(), "treasury custom message");
(cli::TR_M, po::value<uint32_t>()->default_value(0), "naggle index")
(cli::TR_N, po::value<uint32_t>()->default_value(1), "naggle count")
(cli::TR_COMMENT, po::value<std::string>(), "treasury custom message");

po::options_description uioptions("UI options");
uioptions.add_options()
Expand Down
4 changes: 3 additions & 1 deletion utility/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ namespace beam
extern const char* TR_WID;
extern const char* TR_PERC;
extern const char* TR_COMMENT;
// ui
extern const char* TR_M;
extern const char* TR_N;
// ui
extern const char* WALLET_ADDR;
extern const char* APPDATA_PATH;
}
Expand Down
31 changes: 31 additions & 0 deletions wallet/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ int HandleTreasury(const po::variables_map& vm, Key::IKdf& kdf)
Amount val = static_cast<Amount>(Rules::get().Emission.Value0 * perc); // rounded down

Treasury::Parameters pars; // default

uint32_t m = vm[cli::TR_M].as<uint32_t>();
uint32_t n = vm[cli::TR_N].as<uint32_t>();

if (m >= n)
throw std::runtime_error("bad m/n");

assert(n);
if (pars.m_Bursts % n)
throw std::runtime_error("bad n (roundoff)");

pars.m_Bursts /= n;
pars.m_Maturity0 = pars.m_MaturityStep * pars.m_Bursts * m;

Treasury::Entry* pE = tres.CreatePlan(wid, val, pars);

FSave(pE->m_Request, sID + szRequest);
Expand Down Expand Up @@ -332,6 +346,23 @@ int HandleTreasury(const po::variables_map& vm, Key::IKdf& kdf)
}
break;

case 6:
{
// bursts
Treasury::Data data;
FLoad(data, szData);

auto vBursts = data.get_Bursts();

cout << "Total bursts: " << vBursts.size() << std::endl;

for (size_t i = 0; i < vBursts.size(); i++)
{
const Treasury::Data::Burst& b = vBursts[i];
cout << "\t" << "Height=" << b.m_Height << ", Value=" << b.m_Value << std::endl;
}
}
break;
}

return 0;
Expand Down

0 comments on commit 8bf6696

Please sign in to comment.