Migrate core infrastructure to VoidLibrary

This commit is contained in:
faumaray committed 2026-06-22 15:53:23 +03:00
1 parent 53801f2b53
commit 2bdb63766d
65 files changed
+16109 -12011

No files matched your search

+144
View File
@@ -0,0 +1,144 @@
// #include "echoion/api/base/StateBlob.hpp"
#include "echoion/core/state/StateMigration.hpp"
#include "echoion/core/state/StateSerializer.hpp"
#include <Void/Data/Json.hpp>
#include <Void/Data/Reflect.hpp>
#include <format>
#include <iomanip>
#include <sstream>
namespace Echoion::Core
{
Api::StateBlob StateSerializer::serialize(const StateV1& state)
{
// Serialize to JSON using Void::Json reflection
std::string jsonStr =
Void::Json::serialize(Void::Json::to_json(state));
// Convert to binary blob
std::vector<uint8_t> data(jsonStr.begin(), jsonStr.end());
return Api::StateBlob{.data = std::move(data),
.version = CURRENT_VERSION,
.timestamp = getCurrentTimestampISO8601()};
}
Api::Result<StateV1>
StateSerializer::deserialize(const Api::StateBlob& blob)
{
if (blob.data.empty()) {
return std::unexpected(Api::Error{Api::ErrorCode::InvalidArgument,
"Empty state blob"});
}
// Convert blob to string
std::string jsonStr(blob.data.begin(), blob.data.end());
// Extract version
uint32_t dataVersion = extractVersion(jsonStr);
// Check if version is supported
if (!isVersionSupported(dataVersion)) {
if (dataVersion > CURRENT_VERSION) {
return std::unexpected(Api::Error{
Api::ErrorCode::IncompatibleVersion,
std::format("State version {} is from a newer Core. "
"Please update/restart to load this state.",
dataVersion)});
} else {
return std::unexpected(Api::Error{
Api::ErrorCode::IncompatibleVersion,
std::format("State version {} is too old. "
"Maximum supported age is {} versions.",
dataVersion,
Api::MAX_SUPPORTED_STATE_VERSION_AGE)});
}
}
// If current version, parse directly
if (dataVersion == CURRENT_VERSION) {
auto parsed = Void::Json::parse(jsonStr);
if (!parsed) {
return std::unexpected(Api::Error{
Api::ErrorCode::ParseError,
std::format("Failed to parse state ({}:{}): {}",
parsed.error().line, parsed.error().col,
parsed.error().message)});
}
auto state = Void::Json::from_json<StateV1>(*parsed);
if (!state) {
return std::unexpected(Api::Error{
Api::ErrorCode::ParseError,
std::format("Failed to decode state: field '{}': {}",
state.error().field, state.error().message)});
}
return *state;
}
// Otherwise, migrate
return StateMigration::migrate(dataVersion, jsonStr);
}
bool StateSerializer::isVersionSupported(uint32_t version)
{
if (version > CURRENT_VERSION) {
return false; // Future version
}
uint32_t minVersion = getMinimumSupportedVersion();
return version >= minVersion && version <= CURRENT_VERSION;
}
uint32_t StateSerializer::getMinimumSupportedVersion()
{
// Support 10 versions back, but not below 1
return (CURRENT_VERSION > Api::MAX_SUPPORTED_STATE_VERSION_AGE)
? CURRENT_VERSION - Api::MAX_SUPPORTED_STATE_VERSION_AGE
: 1;
}
uint32_t StateSerializer::getCurrentVersion() { return CURRENT_VERSION; }
uint32_t StateSerializer::extractVersion(const std::string& jsonStr)
{
// Quick extraction without full parse
// Look for "version": N pattern
size_t pos = jsonStr.find("\"version\"");
if (pos != std::string::npos) {
pos = jsonStr.find(':', pos);
if (pos != std::string::npos) {
// Skip whitespace
pos++;
while (pos < jsonStr.size() &&
std::isspace(static_cast<unsigned char>(jsonStr[pos]))) {
pos++;
}
// Parse number
if (pos < jsonStr.size() &&
std::isdigit(static_cast<unsigned char>(jsonStr[pos]))) {
try {
return static_cast<uint32_t>(
std::stoul(jsonStr.substr(pos)));
} catch (...) {
return 0;
}
}
}
}
// Default to 0 (unknown/legacy)
return 0;
}
std::string StateSerializer::getCurrentTimestampISO8601()
{
auto now = std::chrono::system_clock::now();
auto time_t_now = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::gmtime(&time_t_now), "%Y-%m-%dT%H:%M:%SZ");
return ss.str();
}
} // namespace Echoion::Core