Skip to content

Commit

Permalink
Merge branch 'clear_cathode_3.0RC'
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolse committed Jul 19, 2019
2 parents 056d65d + 1b48a23 commit 04b244a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
1 change: 1 addition & 0 deletions core/unittest/mini_blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ namespace beam {
State& s0 = m_vStates[i - 1];
s.m_Hdr = s0.m_Hdr;
s.m_Hdr.NextPrefix();
s.m_Hdr.m_TimeStamp = getTimestamp();

uint32_t nSize = m_Mmr.get_NodeSize(i - 1);
s0.m_pMmrData.reset(new uint8_t[nSize]);
Expand Down
61 changes: 28 additions & 33 deletions wallet/base_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace beam::wallet
namespace
{
const char* LOCAL_NONCE_SEEDS = "NonceSeeds";
const size_t kMaxNonces = 1000000;
}

LocalPrivateKeyKeeper::LocalPrivateKeyKeeper(IWalletDB::Ptr walletDB)
Expand Down Expand Up @@ -127,39 +128,21 @@ namespace beam::wallet

size_t LocalPrivateKeyKeeper::AllocateNonceSlot()
{
if (m_Nonces.size() == numeric_limits<uint8_t>::max())
{
throw runtime_error("has no place for nonces");
}
++m_NonceSlotLast %= kMaxNonces;

size_t i = 0;
auto it = m_Nonces.begin();
while (it != m_Nonces.end() && i < numeric_limits<size_t>::max())
{
if (i > it->first)
{
++it;
}
else if (i == it->first)
{
++it;
++i;
}
else
{
break;
}
}
if (m_NonceSlotLast >= m_Nonces.size())
{
m_NonceSlotLast = m_Nonces.size();
m_Nonces.emplace_back();
}

// Don't store the generated nonce for the kernel multisig. Instead - store the raw random, from which the nonce is derived using kdf.
NoLeak<Hash::Value> hvRandom;
ECC::GenRandom(hvRandom.V);

m_Nonces.insert({ i, hvRandom.V });
ECC::GenRandom(m_Nonces[m_NonceSlotLast].V);

SaveNonceSeeds();

return i;
return m_NonceSlotLast;
}

////
Expand Down Expand Up @@ -241,19 +224,31 @@ namespace beam::wallet

void LocalPrivateKeyKeeper::LoadNonceSeeds()
{
ByteBuffer buffer;
if (m_WalletDB->getBlob(LOCAL_NONCE_SEEDS, buffer) && !buffer.empty())
try
{
ByteBuffer buffer;
if (m_WalletDB->getBlob(LOCAL_NONCE_SEEDS, buffer) && !buffer.empty())
{
Deserializer d;
d.reset(buffer);
d & m_Nonces;
d & m_NonceSlotLast;
}
}
catch (...)
{
Deserializer d;
d.reset(buffer);
d & m_Nonces;
m_Nonces.clear();
}

if (m_NonceSlotLast >= m_Nonces.size())
m_NonceSlotLast = m_Nonces.size() - 1;
}

void LocalPrivateKeyKeeper::SaveNonceSeeds()
{
Serializer s;
s& m_Nonces;
s & m_Nonces;
s & m_NonceSlotLast;
ByteBuffer buffer;
s.swap_buf(buffer);
m_WalletDB->setVarRaw(LOCAL_NONCE_SEEDS, buffer.data(), buffer.size());
Expand All @@ -268,7 +263,7 @@ namespace beam::wallet

Scalar::Native LocalPrivateKeyKeeper::GetNonce(size_t slot)
{
auto randomValue = m_Nonces[slot];
const auto& randomValue = m_Nonces[slot].V;

NoLeak<Scalar::Native> nonce;
m_MasterKdf->DeriveKey(nonce.V, randomValue);
Expand Down
9 changes: 8 additions & 1 deletion wallet/base_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ namespace beam::wallet
IWalletDB::Ptr m_WalletDB;
Key::IKdf::Ptr m_MasterKdf;

std::map<size_t, ECC::Hash::Value> m_Nonces;
struct MyNonce :public ECC::NoLeak<ECC::Hash::Value> {
template <typename Archive> void serialize(Archive& ar) {
ar & V;
}
};

std::vector<MyNonce> m_Nonces;
size_t m_NonceSlotLast = 0;
};

std::string GetFailureMessage(TxFailureReason reason);
Expand Down
11 changes: 11 additions & 0 deletions wallet/unittests/wallet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,16 @@ namespace
}
}

void TestTxNonces()
{
cout << "\nTesting tx nonce...\n";

PerformanceRig t2(200);
t2.Run();
t2.Run();

}

void TestColdWalletSending()
{
cout << "\nTesting cold wallet sending...\n";
Expand Down Expand Up @@ -1461,6 +1471,7 @@ int main()

TestTransactionUpdate();
//TestTxPerformance();
//TestTxNonces();

TestColdWalletSending();
TestColdWalletReceiving();
Expand Down
9 changes: 9 additions & 0 deletions wallet/unittests/wallet_test_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,15 @@ class PerformanceRig
mainReactor->run();
sw.stop();
m_TotalTime = sw.milliseconds();

auto txHistory = sender.m_WalletDB->getTxHistory();

size_t totalTxCount = m_TxCount * m_TxPerCall;
WALLET_CHECK(txHistory.size() == totalTxCount);
for (const auto& tx : txHistory)
{
WALLET_CHECK(tx.m_status == TxStatus::Completed);
}
}

uint64_t GetTotalTime() const
Expand Down

0 comments on commit 04b244a

Please sign in to comment.