// #include "echoion/api/base/StateBlob.hpp" #include "echoion/core/state/StateMigration.hpp" #include "echoion/core/state/StateSerializer.hpp" #include #include #include #include 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 data(jsonStr.begin(), jsonStr.end()); return Api::StateBlob{.data = std::move(data), .version = CURRENT_VERSION, .timestamp = Void::Time::utc_now_iso8601()}; } Api::Result StateSerializer::deserialize(const Api::StateBlob& blob) { if (blob.data.empty()) { return Api::fail(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 Api::fail( Api::ErrorCode::IncompatibleVersion, std::format("State version {} is from a newer Core. " "Please update/restart to load this state.", dataVersion)); } else { return Api::fail( 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 Api::fail( Api::ErrorCode::ParseError, std::format("Failed to parse state ({}:{}): {}", parsed.error().line, parsed.error().col, parsed.error().message)); } auto state = Void::Json::from_json(*parsed); if (!state) { return Api::fail( 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(jsonStr[pos]))) { pos++; } // Parse number if (pos < jsonStr.size() && std::isdigit(static_cast(jsonStr[pos]))) { try { return static_cast( std::stoul(jsonStr.substr(pos))); } catch (...) { return 0; } } } } // Default to 0 (unknown/legacy) return 0; } } // namespace Echoion::Core