mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 08:12:12 +11:00
84 lines
2.3 KiB
C++
84 lines
2.3 KiB
C++
#include "log.h"
|
|
|
|
void Log::AddTrans(Transaction &&t) noexcept
|
|
{
|
|
#if MAX_LOG_SIZE == 1
|
|
data = std::move(t);
|
|
#else
|
|
if (data.size() == MAX_LOG_SIZE) // If we hit the max size
|
|
{
|
|
for (uint32_t i = 1; i < data.size(); i++) // Make room at the back
|
|
{
|
|
data[i - 1] = std::move(data[i]); // Shifts everything left
|
|
}
|
|
data[data.size() - 1] = std::move(t); // Place new in opened spot
|
|
return;
|
|
}
|
|
else if (data.size() == data.capacity()) // If we haven't hit the max but hit capacity
|
|
{
|
|
data.reserve(data.capacity() + PRE_LOG_SIZE); // Reserve more memory
|
|
}
|
|
data.push_back(std::move(t)); // In either case we have space under max length, move to new spot
|
|
#endif
|
|
log_flag.SetChangesOn();
|
|
}
|
|
|
|
const Json::Value &Log::GetLog() noexcept
|
|
{
|
|
if (log_flag.GetChangeState()) //if there are changes
|
|
{
|
|
//re-generate snapshot
|
|
Json::Value res;
|
|
#if MAX_LOG_SIZE == 1
|
|
res[0]["to"] = data.to;
|
|
res[0]["from"] = data.from;
|
|
#ifdef _USE_32BIT_TIME_T
|
|
res[0]["time"] = (Json::UInt)data.time;
|
|
#else
|
|
res[0]["time"] = (Json::UInt64)data.time;
|
|
#endif
|
|
#else
|
|
for (uint32_t i = data.size(); i > 0; --i)
|
|
{
|
|
res[i - 1]["to"] = data[data.size() - i].to;
|
|
res[i - 1]["from"] = data[data.size() - i].from;
|
|
res[i - 1]["amount"] = (Json::UInt)data[data.size() - i].amount;
|
|
#ifdef _USE_32BIT_TIME_T
|
|
res[i - 1]["time"] = (Json::UInt)data[data.size() - i].time;
|
|
#else
|
|
res[i - 1]["time"] = (Json::UInt64)data[data.size() - i].time;
|
|
#endif
|
|
}
|
|
#endif
|
|
log_flag.SetChangesOff();
|
|
log_snapshot = res;
|
|
}
|
|
return log_snapshot;
|
|
}
|
|
|
|
Json::Value Log::Serialize() const
|
|
{
|
|
Json::Value res;
|
|
#if MAX_LOG_SIZE == 1
|
|
res[0]["to"] = data.to;
|
|
res[0]["from"] = data.from;
|
|
#ifdef _USE_32BIT_TIME_T
|
|
res[0]["time"] = (Json::UInt)data.time;
|
|
#else
|
|
res[0]["time"] = (Json::UInt64)data.time;
|
|
#endif
|
|
#else
|
|
for (uint32_t i = 0; i < data.size(); ++i)
|
|
{
|
|
res[i]["to"] = data[i].to;
|
|
res[i]["from"] = data[i].from;
|
|
res[i]["amount"] = (Json::UInt)data[i].amount;
|
|
#ifdef _USE_32BIT_TIME_T
|
|
res[i]["time"] = (Json::UInt)data[i].time;
|
|
#else
|
|
res[i]["time"] = (Json::UInt64)data[i].time;
|
|
#endif
|
|
}
|
|
#endif
|
|
return res;
|
|
}
|