Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 5m14s
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Successful in 46s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2022 lines
77 KiB
Rust
2022 lines
77 KiB
Rust
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use anyhow::anyhow;
|
|
use anyhow::bail;
|
|
use base64::Engine;
|
|
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
|
|
use crate::{Edge, RawDocumentHeader, Selection, SelectionDocumentRecord};
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum CultNetWireContract {
|
|
#[serde(rename = "cultnet.schema.v0")]
|
|
CultNetSchemaV0,
|
|
#[serde(rename = "gamecult.networking.v0")]
|
|
GameCultNetworkingV0,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum CultNetSchemaKind {
|
|
WireMessage,
|
|
DocumentPayload,
|
|
SharedContract,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum CultNetRawPayloadEncoding {
|
|
Messagepack,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub enum CultNetDocumentOperation {
|
|
Snapshot,
|
|
DocumentPut,
|
|
DocumentDelete,
|
|
IntentSubmit,
|
|
ReceiptWatch,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub enum CultNetMutationAuthority {
|
|
ReadOnly,
|
|
LocalUser,
|
|
Coordinator,
|
|
Runtime,
|
|
Denied,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetDocumentMutationContract {
|
|
pub document_type: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub payload_schema_version: Option<String>,
|
|
pub operations: Vec<CultNetDocumentOperation>,
|
|
pub authority: CultNetMutationAuthority,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub intent_document_types: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub receipt_document_types: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub notes: Option<Vec<String>>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetTransportProfile {
|
|
pub schema_version: String,
|
|
pub runtime_id: String,
|
|
pub transports: Vec<CultNetTransportDescriptor>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetTransportDescriptor {
|
|
pub transport_id: String,
|
|
pub protocol: CultNetTransportProtocol,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub host: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub port: Option<u16>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub path: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub discovery_group: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub wire_contracts: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub reconnect_policy: Option<CultNetReconnectPolicy>,
|
|
pub channels: Vec<CultNetTransportChannel>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetReconnectPolicy {
|
|
pub schema_version: String,
|
|
pub policy_id: String,
|
|
pub base_delay_ms: u64,
|
|
pub max_delay_ms: u64,
|
|
pub max_jitter_ms: u64,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub max_attempts: Option<u32>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum CultNetTransportProtocol {
|
|
TcpFramed,
|
|
Litenetlib,
|
|
Websocket,
|
|
Rudp,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetTransportChannel {
|
|
pub channel_id: String,
|
|
pub delivery: CultNetTransportDelivery,
|
|
pub ordering: CultNetTransportOrdering,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub max_payload_bytes: Option<u32>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub max_fragment_bytes: Option<u32>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub max_pending_reliable_packets: Option<u32>,
|
|
/// Drop a reliable packet on this channel instead of retransmitting it once
|
|
/// this many milliseconds have passed since the send. Omitted when absent,
|
|
/// so a profile that does not set it is byte-identical to one from a
|
|
/// runtime that does not know the field.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub reliable_expire_after_ms: Option<u64>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum CultNetTransportDelivery {
|
|
Reliable,
|
|
Unreliable,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum CultNetTransportOrdering {
|
|
Ordered,
|
|
Unordered,
|
|
Sequenced,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetRawDocumentRecord {
|
|
pub schema_id: String,
|
|
pub record_key: String,
|
|
pub stored_at: String,
|
|
pub payload_encoding: CultNetRawPayloadEncoding,
|
|
#[serde(with = "serde_bytes")]
|
|
pub payload: Vec<u8>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub source_runtime_id: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub source_agent_id: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub source_role: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub tags: Option<Vec<String>>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetSchemaDescriptor {
|
|
pub schema_id: String,
|
|
pub kind: CultNetSchemaKind,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub schema_version: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub document_type: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub title: Option<String>,
|
|
pub wire_contracts: Vec<CultNetWireContract>,
|
|
pub content_hash: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub schema_json: Option<String>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetShardDescriptor {
|
|
pub shard_id: String,
|
|
pub owner_runtime_id: String,
|
|
pub epoch: u64,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub is_primary: Option<bool>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub schema_ids: Vec<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub key_prefix: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub primary_endpoints: Vec<String>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub replica_endpoints: Vec<String>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub read_replica_endpoints: Vec<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub region: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub authority_lease_id: Option<String>,
|
|
}
|
|
|
|
impl CultNetShardDescriptor {
|
|
pub fn serves(&self, schema_id: Option<&str>, record_key: Option<&str>) -> bool {
|
|
if let Some(schema_id) = schema_id
|
|
&& !self.schema_ids.is_empty()
|
|
&& !self
|
|
.schema_ids
|
|
.iter()
|
|
.any(|candidate| candidate == schema_id)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if let Some(record_key) = record_key
|
|
&& let Some(prefix) = self.key_prefix.as_deref()
|
|
&& !record_key.starts_with(prefix)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
true
|
|
}
|
|
}
|
|
|
|
/// Machine-readable `cultnet.error.v0` refusal code (R-N). Mirrors the strings
|
|
/// `contracts/cultnet/cultnet.error.schema.json` enumerates for `code`; a peer that receives a
|
|
/// string it does not know refuses to decode rather than guess a meaning for it.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum CultNetErrorCode {
|
|
SelectionInvalid,
|
|
CursorStale,
|
|
CursorInvalid,
|
|
ReferenceOutsideTarget,
|
|
VariantUnsupported,
|
|
UnownedSchema,
|
|
WriteNotAuthorized,
|
|
UnsupportedSchemaVersion,
|
|
MessageInvalid,
|
|
/// `cultnet.error.v1` only: the session's remembered-write limit is full. `cultnet.error.v0` neither
|
|
/// encodes nor decodes it, so its enum stays the schema's.
|
|
LimitReached,
|
|
}
|
|
|
|
impl CultNetErrorCode {
|
|
fn as_wire_str(self) -> &'static str {
|
|
match self {
|
|
Self::SelectionInvalid => "selection_invalid",
|
|
Self::CursorStale => "cursor_stale",
|
|
Self::CursorInvalid => "cursor_invalid",
|
|
Self::ReferenceOutsideTarget => "reference_outside_target",
|
|
Self::VariantUnsupported => "variant_unsupported",
|
|
Self::UnownedSchema => "unowned_schema",
|
|
Self::WriteNotAuthorized => "write_not_authorized",
|
|
Self::UnsupportedSchemaVersion => "unsupported_schema_version",
|
|
Self::MessageInvalid => "message_invalid",
|
|
Self::LimitReached => "limit_reached",
|
|
}
|
|
}
|
|
|
|
fn from_wire_str(value: &str) -> Result<Self> {
|
|
match value {
|
|
"selection_invalid" => Ok(Self::SelectionInvalid),
|
|
"cursor_stale" => Ok(Self::CursorStale),
|
|
"cursor_invalid" => Ok(Self::CursorInvalid),
|
|
"reference_outside_target" => Ok(Self::ReferenceOutsideTarget),
|
|
"variant_unsupported" => Ok(Self::VariantUnsupported),
|
|
"unowned_schema" => Ok(Self::UnownedSchema),
|
|
"write_not_authorized" => Ok(Self::WriteNotAuthorized),
|
|
"unsupported_schema_version" => Ok(Self::UnsupportedSchemaVersion),
|
|
"message_invalid" => Ok(Self::MessageInvalid),
|
|
"limit_reached" => Ok(Self::LimitReached),
|
|
_ => Err(anyhow!("ErrorMessage.Code is not a recognized cultnet.error.v0 code")),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Code-specific structured detail carried on `cultnet.error.v0`'s `code`/`details` (R-N). Mirrors
|
|
/// `CultNetErrorDetails`: every field is populated only for the code that defines it and is nil
|
|
/// (present, not omitted) on the wire otherwise, matching the C# reference's always-five-key map.
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct CultNetErrorDetails {
|
|
/// The selection field that failed (`selection_invalid`).
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub field: Option<String>,
|
|
/// The offending value, when there is one string worth naming (`selection_invalid`).
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub value: Option<String>,
|
|
/// The cursor's own minted asOf (`cursor_stale`).
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub as_of: Option<u64>,
|
|
/// The answering server's current asOf (`cursor_stale`).
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub current: Option<u64>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
#[serde(tag = "schemaVersion")]
|
|
pub enum CultNetMessage {
|
|
#[serde(rename = "cultnet.hello.v0", rename_all = "camelCase")]
|
|
Hello {
|
|
runtime_id: String,
|
|
runtime_kind: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
agent_id: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
role: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
display_name: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
supported_document_types: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
supported_mutation_contracts: Option<Vec<CultNetDocumentMutationContract>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
supported_message_versions: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
transport_profiles: Option<Vec<CultNetTransportProfile>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
supports_schema_catalog: Option<bool>,
|
|
},
|
|
#[serde(rename = "cultnet.login.v0", rename_all = "camelCase")]
|
|
Login {
|
|
nonce: String,
|
|
auth: String,
|
|
password: String,
|
|
},
|
|
#[serde(rename = "cultnet.register.v0", rename_all = "camelCase")]
|
|
Register {
|
|
nonce: String,
|
|
email: String,
|
|
password: String,
|
|
name: String,
|
|
},
|
|
#[serde(rename = "cultnet.verify.v0", rename_all = "camelCase")]
|
|
Verify { nonce: String, session: String },
|
|
#[serde(rename = "cultnet.login_success.v0", rename_all = "camelCase")]
|
|
LoginSuccess { nonce: String, session: String },
|
|
// R-N (docs/cultnet-selection-cut.md, fix batch 3): `code`/`details` mirror the landed C#
|
|
// reference (CultNetSchemaMessages.cs) exactly, including its always-five-key map shape
|
|
// (`schemaVersion`, `error`, `routingHint`, `code`, `details`, every optional key nil rather
|
|
// than omitted). `Error` is read and written only by the raw `cultnet.schema.v0` path
|
|
// (parse_raw_cultnet_schema_message/encode_raw_cultnet_schema_message): `skip_deserializing`
|
|
// makes the derive unable to decode it, so the raw reader is its one decoder and the derive
|
|
// attributes describe `Serialize` only.
|
|
#[serde(skip_deserializing)]
|
|
#[serde(rename = "cultnet.error.v0", rename_all = "camelCase")]
|
|
Error {
|
|
error: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
code: Option<CultNetErrorCode>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
details: Option<CultNetErrorDetails>,
|
|
},
|
|
/// `cultnet.error.v1` (contracts/cultnet/interop/write-receipt.md): `cultnet.error.v0` plus the id of the
|
|
/// request it answers. Sent only to a connection that declared the write-receipt capability.
|
|
#[serde(skip_deserializing)]
|
|
#[serde(rename = "cultnet.error.v1", rename_all = "camelCase")]
|
|
ErrorV1 {
|
|
error: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
code: Option<CultNetErrorCode>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
details: Option<CultNetErrorDetails>,
|
|
message_id: Option<String>,
|
|
},
|
|
/// `cultnet.write_admitted.v0`: the receipt for one admitted put or delete, sent after its commit point.
|
|
#[serde(rename = "cultnet.write_admitted.v0", rename_all = "camelCase")]
|
|
WriteAdmitted {
|
|
message_id: String,
|
|
operation: String,
|
|
schema_id: String,
|
|
record_key: String,
|
|
},
|
|
#[serde(rename = "cultnet.sample.change_name.v0", rename_all = "camelCase")]
|
|
SampleChangeName { name: String },
|
|
#[serde(rename = "cultnet.sample.chat.v0", rename_all = "camelCase")]
|
|
SampleChat { text: String },
|
|
#[serde(rename = "cultnet.document_delete.v0", rename_all = "camelCase")]
|
|
DocumentDelete {
|
|
message_id: String,
|
|
schema_id: String,
|
|
record_key: String,
|
|
},
|
|
#[serde(skip_deserializing)]
|
|
#[serde(rename = "cultnet.document_put_raw.v0", rename_all = "camelCase")]
|
|
DocumentPutRaw {
|
|
message_id: String,
|
|
document: CultNetRawDocumentRecord,
|
|
},
|
|
#[serde(rename = "cultnet.snapshot_request.v0", rename_all = "camelCase")]
|
|
SnapshotRequest {
|
|
message_id: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
schema_ids: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
record_keys: Option<Vec<String>>,
|
|
},
|
|
#[serde(skip_deserializing)]
|
|
#[serde(rename = "cultnet.snapshot_response_raw.v0", rename_all = "camelCase")]
|
|
SnapshotResponseRaw {
|
|
message_id: String,
|
|
documents: Vec<CultNetRawDocumentRecord>,
|
|
},
|
|
/// CultNet typed selection, Cut 1 (docs/cultnet-selection-cut.md section 2/7). Mirrors
|
|
/// `CultNetSnapshotRequestV1Message`.
|
|
#[serde(rename = "cultnet.snapshot_request.v1", rename_all = "camelCase")]
|
|
SnapshotRequestV1 {
|
|
message_id: String,
|
|
selection: Selection,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
shard_id: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
shard_epoch: Option<i64>,
|
|
},
|
|
/// Mirrors `CultNetDatabaseSubscribeV1Message`.
|
|
#[serde(rename = "cultnet.database_subscribe.v1", rename_all = "camelCase")]
|
|
DatabaseSubscribeV1 {
|
|
message_id: String,
|
|
subscription_id: String,
|
|
selection: Selection,
|
|
#[serde(default = "true_default")]
|
|
include_snapshot: bool,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
consumer_runtime_id: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
body_ids: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
supported_body_transports: Option<Vec<String>>,
|
|
},
|
|
/// `cultnet.snapshot_response_raw.v1` (section 2: `SelectionPage`). Decoded by the derive from
|
|
/// the message bytes in one pass (rows and scalars, a duplicated key refused); encoded by
|
|
/// `encode_raw_cultnet_schema_message` for the reference's fixed nil-keyed shape. Mirrors
|
|
/// `CultNetSnapshotResponseRawV1Message`.
|
|
#[serde(rename = "cultnet.snapshot_response_raw.v1", rename_all = "camelCase")]
|
|
SnapshotResponseRawV1 {
|
|
message_id: String,
|
|
matched: u32,
|
|
as_of: u64,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
next: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
headers: Option<Vec<RawDocumentHeader>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
documents: Option<Vec<SelectionDocumentRecord>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
edges: Option<Vec<Edge>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
shard_id: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
shard_epoch: Option<i64>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
shard_log_sequence: Option<i64>,
|
|
},
|
|
#[serde(rename = "cultnet.schema_catalog_request.v0", rename_all = "camelCase")]
|
|
SchemaCatalogRequest {
|
|
message_id: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
include_schema_json: Option<bool>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
schema_ids: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
kinds: Option<Vec<CultNetSchemaKind>>,
|
|
},
|
|
#[serde(
|
|
rename = "cultnet.schema_catalog_response.v0",
|
|
rename_all = "camelCase"
|
|
)]
|
|
SchemaCatalogResponse {
|
|
message_id: String,
|
|
schemas: Vec<CultNetSchemaDescriptor>,
|
|
},
|
|
#[serde(rename = "cultnet.shard_catalog_request.v0", rename_all = "camelCase")]
|
|
ShardCatalogRequest {
|
|
message_id: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
schema_ids: Option<Vec<String>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
record_keys: Option<Vec<String>>,
|
|
},
|
|
#[serde(rename = "cultnet.shard_catalog_response.v0", rename_all = "camelCase")]
|
|
ShardCatalogResponse {
|
|
message_id: String,
|
|
shards: Vec<CultNetShardDescriptor>,
|
|
},
|
|
#[serde(rename = "cultnet.operation_request.v0", rename_all = "camelCase")]
|
|
OperationRequest {
|
|
message_id: String,
|
|
service_id: String,
|
|
operation: String,
|
|
payload_schema: String,
|
|
#[serde(default = "messagepack_base64_encoding")]
|
|
payload_encoding: String,
|
|
payload: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
source_runtime_id: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
target_runtime_id: Option<String>,
|
|
},
|
|
#[serde(rename = "cultnet.operation_response.v0", rename_all = "camelCase")]
|
|
OperationResponse {
|
|
message_id: String,
|
|
service_id: String,
|
|
operation: String,
|
|
status: String,
|
|
payload_schema: String,
|
|
#[serde(default = "messagepack_base64_encoding")]
|
|
payload_encoding: String,
|
|
payload: String,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
diagnostics: Vec<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
source_runtime_id: Option<String>,
|
|
},
|
|
/// `cultmesh.content_chunk_request.v1`. Mirrors `CultMeshContentChunkRequestMessage`; raw path.
|
|
#[serde(skip_deserializing)]
|
|
#[serde(rename = "cultmesh.content_chunk_request.v1", rename_all = "camelCase")]
|
|
ContentChunkRequest {
|
|
message_id: String,
|
|
chunk_hash: String,
|
|
record_key: String,
|
|
expected_size_bytes: i32,
|
|
},
|
|
/// `cultmesh.content_chunk_response.v1`. Mirrors `CultMeshContentChunkResponseMessage`. `payload`
|
|
/// is `bin`, so it travels the raw path; the serde attribute never runs on the wire.
|
|
#[serde(skip_deserializing)]
|
|
#[serde(rename = "cultmesh.content_chunk_response.v1", rename_all = "camelCase")]
|
|
ContentChunkResponse {
|
|
message_id: String,
|
|
found: bool,
|
|
chunk_hash: String,
|
|
size_bytes: i32,
|
|
#[serde(with = "serde_bytes")]
|
|
payload: Vec<u8>,
|
|
error: String,
|
|
},
|
|
}
|
|
|
|
fn messagepack_base64_encoding() -> String {
|
|
"messagepack-base64".to_string()
|
|
}
|
|
|
|
fn true_default() -> bool {
|
|
true
|
|
}
|
|
|
|
pub fn encode_cultnet_message_for_wire(
|
|
message: &CultNetMessage,
|
|
contract: CultNetWireContract,
|
|
) -> Result<rmpv::Value> {
|
|
validate_message(message)?;
|
|
match contract {
|
|
CultNetWireContract::CultNetSchemaV0 => match message {
|
|
CultNetMessage::DocumentPutRaw { .. }
|
|
| CultNetMessage::SnapshotResponseRaw { .. }
|
|
| CultNetMessage::SnapshotResponseRawV1 { .. }
|
|
| CultNetMessage::ContentChunkRequest { .. }
|
|
| CultNetMessage::ContentChunkResponse { .. }
|
|
| CultNetMessage::Error { .. }
|
|
| CultNetMessage::ErrorV1 { .. }
|
|
| CultNetMessage::WriteAdmitted { .. } => encode_raw_cultnet_schema_message(message),
|
|
_ => Ok(cultcache_rs::decode_payload(&rmp_serde::to_vec(
|
|
&serde_json::to_value(message)?,
|
|
)?)?),
|
|
},
|
|
CultNetWireContract::GameCultNetworkingV0 => encode_gamecult_networking_message(message),
|
|
}
|
|
}
|
|
|
|
pub fn encode_cultnet_message_to_vec(
|
|
message: &CultNetMessage,
|
|
contract: CultNetWireContract,
|
|
) -> Result<Vec<u8>> {
|
|
let wire_value = encode_cultnet_message_for_wire(message, contract)?;
|
|
Ok(rmp_serde::to_vec(&wire_value)?)
|
|
}
|
|
|
|
pub fn decode_cultnet_message_from_slice(
|
|
bytes: &[u8],
|
|
contract: CultNetWireContract,
|
|
) -> Result<CultNetMessage> {
|
|
let wire_value: rmpv::Value = cultcache_rs::decode_payload(bytes)?;
|
|
match contract {
|
|
CultNetWireContract::CultNetSchemaV0 => {
|
|
let message = match schema_version_from_wire_value(&wire_value)? {
|
|
Some(
|
|
"cultnet.document_put_raw.v0"
|
|
| "cultnet.snapshot_response_raw.v0"
|
|
| "cultmesh.content_chunk_request.v1"
|
|
| "cultmesh.content_chunk_response.v1"
|
|
| "cultnet.error.v0"
|
|
| "cultnet.error.v1",
|
|
) => parse_raw_cultnet_schema_message(&wire_value)
|
|
.with_context(|| received_version_label(&wire_value))?,
|
|
_ => cultcache_rs::decode_payload::<CultNetMessage>(bytes)
|
|
.with_context(|| received_version_label(&wire_value))?,
|
|
};
|
|
validate_message(&message)?;
|
|
Ok(message)
|
|
}
|
|
CultNetWireContract::GameCultNetworkingV0 => parse_gamecult_networking_message(&wire_value),
|
|
}
|
|
}
|
|
|
|
fn validate_message(message: &CultNetMessage) -> Result<()> {
|
|
match message {
|
|
CultNetMessage::Hello {
|
|
runtime_id,
|
|
runtime_kind,
|
|
agent_id,
|
|
role,
|
|
display_name,
|
|
supported_document_types,
|
|
supported_mutation_contracts,
|
|
supported_message_versions,
|
|
transport_profiles: _,
|
|
supports_schema_catalog: _,
|
|
} => {
|
|
require_non_empty(runtime_id, "runtimeId")?;
|
|
require_non_empty(runtime_kind, "runtimeKind")?;
|
|
require_optional_non_empty(agent_id.as_deref(), "agentId")?;
|
|
require_optional_non_empty(role.as_deref(), "role")?;
|
|
require_optional_non_empty(display_name.as_deref(), "displayName")?;
|
|
require_optional_string_vec(
|
|
supported_document_types.as_deref(),
|
|
"supportedDocumentTypes",
|
|
)?;
|
|
if let Some(contracts) = supported_mutation_contracts {
|
|
for contract in contracts {
|
|
validate_mutation_contract(contract)?;
|
|
}
|
|
}
|
|
require_optional_string_vec(
|
|
supported_message_versions.as_deref(),
|
|
"supportedMessageVersions",
|
|
)?;
|
|
}
|
|
CultNetMessage::Login {
|
|
nonce,
|
|
auth,
|
|
password,
|
|
} => {
|
|
require_non_empty(nonce, "nonce")?;
|
|
require_non_empty(auth, "auth")?;
|
|
require_non_empty(password, "password")?;
|
|
}
|
|
CultNetMessage::Register {
|
|
nonce,
|
|
email,
|
|
password,
|
|
name,
|
|
} => {
|
|
require_non_empty(nonce, "nonce")?;
|
|
require_non_empty(email, "email")?;
|
|
require_non_empty(password, "password")?;
|
|
require_non_empty(name, "name")?;
|
|
}
|
|
CultNetMessage::Verify { nonce, session }
|
|
| CultNetMessage::LoginSuccess { nonce, session } => {
|
|
require_non_empty(nonce, "nonce")?;
|
|
require_non_empty(session, "session")?;
|
|
}
|
|
CultNetMessage::Error { error, code: _, details: _ } => require_non_empty(error, "error")?,
|
|
CultNetMessage::ErrorV1 { error, message_id, .. } => {
|
|
require_non_empty(error, "error")?;
|
|
if let Some(message_id) = message_id {
|
|
require_non_empty(message_id, "messageId")?;
|
|
}
|
|
}
|
|
CultNetMessage::WriteAdmitted { message_id, operation, schema_id, record_key } => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
if operation != "put" && operation != "delete" {
|
|
return Err(anyhow!("operation must be put or delete"));
|
|
}
|
|
require_non_empty(schema_id, "schemaId")?;
|
|
require_non_empty(record_key, "recordKey")?;
|
|
}
|
|
CultNetMessage::SampleChangeName { name } => require_non_empty(name, "name")?,
|
|
CultNetMessage::SampleChat { text } => require_non_empty(text, "text")?,
|
|
CultNetMessage::DocumentDelete {
|
|
message_id,
|
|
schema_id,
|
|
record_key,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_non_empty(schema_id, "schemaId")?;
|
|
require_non_empty(record_key, "recordKey")?;
|
|
}
|
|
CultNetMessage::DocumentPutRaw {
|
|
message_id,
|
|
document,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
validate_raw_document_record(document)?;
|
|
}
|
|
CultNetMessage::SnapshotRequest {
|
|
message_id,
|
|
schema_ids,
|
|
record_keys,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_optional_string_vec(schema_ids.as_deref(), "schemaIds")?;
|
|
require_optional_string_vec(record_keys.as_deref(), "recordKeys")?;
|
|
}
|
|
CultNetMessage::SnapshotResponseRaw {
|
|
message_id,
|
|
documents,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
for document in documents {
|
|
validate_raw_document_record(document)?;
|
|
}
|
|
}
|
|
CultNetMessage::SnapshotRequestV1 {
|
|
message_id,
|
|
// The selection's own declared-alias semantics need a schema registry this wire-level
|
|
// deserialization does not have; the C# reference does not validate CultNetSelection at
|
|
// this layer either (CultNetSchemaMessageSerialization.cs just deserializes it). A
|
|
// server-side caller runs `selection::validate` against its RowSet before evaluating.
|
|
selection: _,
|
|
shard_id,
|
|
shard_epoch: _,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_optional_non_empty(shard_id.as_deref(), "shardId")?;
|
|
}
|
|
CultNetMessage::DatabaseSubscribeV1 {
|
|
message_id,
|
|
subscription_id,
|
|
selection: _,
|
|
include_snapshot: _,
|
|
consumer_runtime_id,
|
|
body_ids,
|
|
supported_body_transports,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_non_empty(subscription_id, "subscriptionId")?;
|
|
require_optional_non_empty(consumer_runtime_id.as_deref(), "consumerRuntimeId")?;
|
|
require_optional_string_vec(body_ids.as_deref(), "bodyIds")?;
|
|
require_optional_string_vec(
|
|
supported_body_transports.as_deref(),
|
|
"supportedBodyTransports",
|
|
)?;
|
|
}
|
|
CultNetMessage::SnapshotResponseRawV1 {
|
|
message_id,
|
|
matched: _,
|
|
as_of: _,
|
|
next: _,
|
|
headers,
|
|
documents,
|
|
edges: _,
|
|
shard_id,
|
|
shard_epoch: _,
|
|
shard_log_sequence: _,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
if headers.is_some() == documents.is_some() {
|
|
return Err(anyhow!(
|
|
"CultNet field exactly one of headers/documents must be present"
|
|
));
|
|
}
|
|
require_optional_non_empty(shard_id.as_deref(), "shardId")?;
|
|
}
|
|
CultNetMessage::SchemaCatalogRequest {
|
|
message_id,
|
|
include_schema_json: _,
|
|
schema_ids,
|
|
kinds: _,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_optional_string_vec(schema_ids.as_deref(), "schemaIds")?;
|
|
}
|
|
CultNetMessage::SchemaCatalogResponse {
|
|
message_id,
|
|
schemas,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
for schema in schemas {
|
|
validate_schema_descriptor(schema)?;
|
|
}
|
|
}
|
|
CultNetMessage::ShardCatalogRequest {
|
|
message_id,
|
|
schema_ids,
|
|
record_keys,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_optional_string_vec(schema_ids.as_deref(), "schemaIds")?;
|
|
require_optional_string_vec(record_keys.as_deref(), "recordKeys")?;
|
|
}
|
|
CultNetMessage::ShardCatalogResponse { message_id, shards } => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
for shard in shards {
|
|
validate_shard_descriptor(shard)?;
|
|
}
|
|
}
|
|
// The reference's serializer accepts any content chunk message. It refuses a bad request in
|
|
// its handler and answers `found: false` (`answer_content_chunk_request`); a bad response is
|
|
// refused by `fetch_content`'s verification. Refusing either here would leave a peer in silence.
|
|
CultNetMessage::ContentChunkRequest { .. } | CultNetMessage::ContentChunkResponse { .. } => {}
|
|
CultNetMessage::OperationRequest {
|
|
message_id,
|
|
service_id,
|
|
operation,
|
|
payload_schema,
|
|
payload_encoding,
|
|
payload,
|
|
source_runtime_id,
|
|
target_runtime_id,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_non_empty(service_id, "serviceId")?;
|
|
require_non_empty(operation, "operation")?;
|
|
require_non_empty(payload_schema, "payloadSchema")?;
|
|
require_exact(payload_encoding, "messagepack-base64", "payloadEncoding")?;
|
|
require_non_empty(payload, "payload")?;
|
|
require_optional_non_empty(source_runtime_id.as_deref(), "sourceRuntimeId")?;
|
|
require_optional_non_empty(target_runtime_id.as_deref(), "targetRuntimeId")?;
|
|
}
|
|
CultNetMessage::OperationResponse {
|
|
message_id,
|
|
service_id,
|
|
operation,
|
|
status,
|
|
payload_schema,
|
|
payload_encoding,
|
|
payload,
|
|
diagnostics: _,
|
|
source_runtime_id,
|
|
} => {
|
|
require_non_empty(message_id, "messageId")?;
|
|
require_non_empty(service_id, "serviceId")?;
|
|
require_non_empty(operation, "operation")?;
|
|
require_non_empty(status, "status")?;
|
|
require_non_empty(payload_schema, "payloadSchema")?;
|
|
require_exact(payload_encoding, "messagepack-base64", "payloadEncoding")?;
|
|
require_non_empty(payload, "payload")?;
|
|
require_optional_non_empty(source_runtime_id.as_deref(), "sourceRuntimeId")?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_mutation_contract(contract: &CultNetDocumentMutationContract) -> Result<()> {
|
|
require_non_empty(&contract.document_type, "documentType")?;
|
|
require_optional_non_empty(
|
|
contract.payload_schema_version.as_deref(),
|
|
"payloadSchemaVersion",
|
|
)?;
|
|
if contract.operations.is_empty() {
|
|
return Err(anyhow!(
|
|
"CultNet mutation contract for {:?} must advertise at least one operation",
|
|
contract.document_type
|
|
));
|
|
}
|
|
require_optional_string_vec(
|
|
contract.intent_document_types.as_deref(),
|
|
"intentDocumentTypes",
|
|
)?;
|
|
require_optional_string_vec(
|
|
contract.receipt_document_types.as_deref(),
|
|
"receiptDocumentTypes",
|
|
)?;
|
|
require_optional_string_vec(contract.notes.as_deref(), "notes")?;
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_raw_document_record(document: &CultNetRawDocumentRecord) -> Result<()> {
|
|
require_non_empty(&document.schema_id, "schemaId")?;
|
|
require_non_empty(&document.record_key, "recordKey")?;
|
|
require_non_empty(&document.stored_at, "storedAt")?;
|
|
if document.payload.is_empty() {
|
|
return Err(anyhow!(
|
|
"CultNet field payload must contain non-empty MessagePack bytes"
|
|
));
|
|
}
|
|
require_optional_non_empty(document.source_runtime_id.as_deref(), "sourceRuntimeId")?;
|
|
require_optional_non_empty(document.source_agent_id.as_deref(), "sourceAgentId")?;
|
|
require_optional_non_empty(document.source_role.as_deref(), "sourceRole")?;
|
|
require_optional_string_vec(document.tags.as_deref(), "tags")?;
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_schema_descriptor(schema: &CultNetSchemaDescriptor) -> Result<()> {
|
|
require_non_empty(&schema.schema_id, "schemaId")?;
|
|
require_optional_non_empty(schema.schema_version.as_deref(), "schemaVersion")?;
|
|
require_optional_non_empty(schema.document_type.as_deref(), "documentType")?;
|
|
require_optional_non_empty(schema.title.as_deref(), "title")?;
|
|
require_non_empty(&schema.content_hash, "contentHash")?;
|
|
require_optional_non_empty(schema.schema_json.as_deref(), "schemaJson")?;
|
|
|
|
if schema.wire_contracts.is_empty() {
|
|
return Err(anyhow!(
|
|
"CultNet field wireContracts must contain at least one supported contract"
|
|
));
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_shard_descriptor(shard: &CultNetShardDescriptor) -> Result<()> {
|
|
require_non_empty(&shard.shard_id, "shardId")?;
|
|
require_non_empty(&shard.owner_runtime_id, "ownerRuntimeId")?;
|
|
require_optional_string_vec(Some(shard.schema_ids.as_slice()), "schemaIds")?;
|
|
require_optional_non_empty(shard.key_prefix.as_deref(), "keyPrefix")?;
|
|
require_optional_string_vec(Some(shard.primary_endpoints.as_slice()), "primaryEndpoints")?;
|
|
require_optional_string_vec(Some(shard.replica_endpoints.as_slice()), "replicaEndpoints")?;
|
|
require_optional_string_vec(
|
|
Some(shard.read_replica_endpoints.as_slice()),
|
|
"readReplicaEndpoints",
|
|
)?;
|
|
require_optional_non_empty(shard.region.as_deref(), "region")?;
|
|
require_optional_non_empty(shard.authority_lease_id.as_deref(), "authorityLeaseId")?;
|
|
Ok(())
|
|
}
|
|
|
|
fn require_non_empty(value: &str, field: &str) -> Result<()> {
|
|
if value.trim().is_empty() {
|
|
return Err(anyhow!("CultNet field {field} must be non-empty"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn require_exact(value: &str, expected: &str, field: &str) -> Result<()> {
|
|
if value != expected {
|
|
return Err(anyhow!("{field} must be {expected}"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn require_optional_non_empty(value: Option<&str>, field: &str) -> Result<()> {
|
|
if let Some(value) = value {
|
|
require_non_empty(value, field)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn require_optional_string_vec(value: Option<&[String]>, field: &str) -> Result<()> {
|
|
if let Some(values) = value {
|
|
for item in values {
|
|
require_non_empty(item, field)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn schema_version_from_wire_value(input: &rmpv::Value) -> Result<Option<&str>> {
|
|
let Some(object) = input.as_map() else {
|
|
return Ok(None);
|
|
};
|
|
|
|
Ok(object.iter().find_map(|(key, value)| {
|
|
key.as_str()
|
|
.filter(|candidate| *candidate == "schemaVersion")
|
|
.and_then(|_| value.as_str())
|
|
}))
|
|
}
|
|
|
|
/// True for a name shaped like a schema version (`cultnet.hello.v0`): a received value is named
|
|
/// in a refusal only when it cannot be anything else.
|
|
fn looks_like_schema_name(candidate: &str) -> bool {
|
|
let mut parts = candidate.split('.');
|
|
let segment = |part: &str| !part.is_empty() && part.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_');
|
|
let first_ok = parts.next().is_some_and(|first| {
|
|
first.bytes().next().is_some_and(|b| b.is_ascii_lowercase()) && segment(first)
|
|
});
|
|
let rest: Vec<&str> = parts.collect();
|
|
first_ok && !rest.is_empty() && rest.iter().all(|part| segment(*part)) && candidate.len() <= 96
|
|
}
|
|
|
|
/// What a refusal says of the message it refused: its `schemaVersion` when that is shaped like a
|
|
/// schema name, else only the shape of what arrived. Never a received value that could be text.
|
|
fn received_version_label(wire_value: &rmpv::Value) -> String {
|
|
match schema_version_from_wire_value(wire_value) {
|
|
Ok(Some(version)) if looks_like_schema_name(version) => {
|
|
format!("cultnet.schema.v0 message {version} refused")
|
|
}
|
|
Ok(Some(version)) => format!(
|
|
"cultnet.schema.v0 message whose schemaVersion is not a schema name ({} bytes)",
|
|
version.len()
|
|
),
|
|
_ => "cultnet.schema.v0 message with no schemaVersion".to_string(),
|
|
}
|
|
}
|
|
|
|
/// Refuses a map that spells a string key twice, so no reader resolves a duplicate by position.
|
|
/// Names the context, never the key (it is received text).
|
|
fn refuse_duplicate_keys(object: &[(rmpv::Value, rmpv::Value)], context: &str) -> Result<()> {
|
|
let mut seen = std::collections::HashSet::new();
|
|
for (key, _) in object {
|
|
if let Some(name) = key.as_str() {
|
|
if !seen.insert(name) {
|
|
bail!("{context} spells a key twice");
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn parse_raw_cultnet_schema_message(input: &rmpv::Value) -> Result<CultNetMessage> {
|
|
let object = input
|
|
.as_map()
|
|
.ok_or_else(|| anyhow!("cultnet.schema.v0 raw messages must be objects"))?;
|
|
refuse_duplicate_keys(object, "cultnet.schema.v0 raw message")?;
|
|
|
|
let get = |name: &str| -> Option<&rmpv::Value> {
|
|
object.iter().find_map(|(key, value)| {
|
|
key.as_str()
|
|
.filter(|candidate| *candidate == name)
|
|
.map(|_| value)
|
|
})
|
|
};
|
|
|
|
let schema_version = require_legacy_string(get("schemaVersion"), "schemaVersion")?;
|
|
match schema_version.as_str() {
|
|
"cultnet.document_put_raw.v0" => Ok(CultNetMessage::DocumentPutRaw {
|
|
message_id: require_legacy_string(get("messageId"), "messageId")?,
|
|
document: require_raw_document_record(get("document"), "document")?,
|
|
}),
|
|
"cultnet.snapshot_response_raw.v0" => {
|
|
let documents = get("documents")
|
|
.and_then(rmpv::Value::as_array)
|
|
.ok_or_else(|| anyhow!("documents must be an array"))?
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(index, value)| {
|
|
require_raw_document_record(value.into(), &format!("documents[{index}]"))
|
|
})
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
Ok(CultNetMessage::SnapshotResponseRaw {
|
|
message_id: require_legacy_string(get("messageId"), "messageId")?,
|
|
documents,
|
|
})
|
|
}
|
|
"cultmesh.content_chunk_request.v1" => Ok(CultNetMessage::ContentChunkRequest {
|
|
message_id: legacy_string_or_nil(get("messageId"), "messageId")?,
|
|
chunk_hash: legacy_string_or_nil(get("chunkHash"), "chunkHash")?,
|
|
record_key: legacy_string_or_nil(get("recordKey"), "recordKey")?,
|
|
expected_size_bytes: require_legacy_i32(get("expectedSizeBytes"), "expectedSizeBytes")?,
|
|
}),
|
|
"cultmesh.content_chunk_response.v1" => Ok(CultNetMessage::ContentChunkResponse {
|
|
message_id: require_legacy_string(get("messageId"), "messageId")?,
|
|
found: require_legacy_bool(get("found"), "found")?,
|
|
chunk_hash: require_legacy_string(get("chunkHash"), "chunkHash")?,
|
|
size_bytes: require_legacy_i32(get("sizeBytes"), "sizeBytes")?,
|
|
payload: get("payload")
|
|
.and_then(rmpv::Value::as_slice)
|
|
.ok_or_else(|| anyhow!("payload must be binary data"))?
|
|
.to_vec(),
|
|
error: require_legacy_string(get("error"), "error")?,
|
|
}),
|
|
"cultnet.error.v0" | "cultnet.error.v1" => {
|
|
let code = get("code")
|
|
.filter(|value| !value.is_nil())
|
|
.map(|value| {
|
|
value
|
|
.as_str()
|
|
.ok_or_else(|| anyhow!("ErrorMessage.Code must be a string"))
|
|
.and_then(CultNetErrorCode::from_wire_str)
|
|
})
|
|
.transpose()?;
|
|
if schema_version == "cultnet.error.v0" && code == Some(CultNetErrorCode::LimitReached) {
|
|
return Err(anyhow!("ErrorMessage.Code is not a recognized cultnet.error.v0 code"));
|
|
}
|
|
let details = require_error_details(get("details"), "ErrorMessage.Details")?;
|
|
let error = require_legacy_string(get("error"), "ErrorMessage.Error")?;
|
|
if schema_version == "cultnet.error.v1" {
|
|
return Ok(CultNetMessage::ErrorV1 {
|
|
error,
|
|
code,
|
|
details,
|
|
message_id: match get("messageId") {
|
|
None | Some(rmpv::Value::Nil) => None,
|
|
other => Some(require_legacy_string(other, "messageId")?),
|
|
},
|
|
});
|
|
}
|
|
Ok(CultNetMessage::Error { error, code, details })
|
|
}
|
|
_ => Err(anyhow!(
|
|
"Unsupported raw cultnet.schema.v0 message"
|
|
)),
|
|
}
|
|
}
|
|
|
|
fn require_error_details(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<Option<CultNetErrorDetails>> {
|
|
let Some(value) = value else {
|
|
return Ok(None);
|
|
};
|
|
if value.is_nil() {
|
|
return Ok(None);
|
|
}
|
|
let object = value
|
|
.as_map()
|
|
.ok_or_else(|| anyhow!("{field_name} must be an object"))?;
|
|
refuse_duplicate_keys(object, field_name)?;
|
|
let get = |name: &str| -> Option<&rmpv::Value> {
|
|
object.iter().find_map(|(key, value)| {
|
|
key.as_str()
|
|
.filter(|candidate| *candidate == name)
|
|
.map(|_| value)
|
|
})
|
|
};
|
|
Ok(Some(CultNetErrorDetails {
|
|
field: require_legacy_optional_string(get("field"), "ErrorMessage.Details.Field")?,
|
|
value: require_legacy_optional_string(get("value"), "ErrorMessage.Details.Value")?,
|
|
as_of: require_legacy_optional_u64(get("asOf"), "ErrorMessage.Details.AsOf")?,
|
|
current: require_legacy_optional_u64(get("current"), "ErrorMessage.Details.Current")?,
|
|
}))
|
|
}
|
|
|
|
fn require_legacy_optional_u64(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<Option<u64>> {
|
|
let Some(value) = value else {
|
|
return Ok(None);
|
|
};
|
|
if value.is_nil() {
|
|
return Ok(None);
|
|
}
|
|
value
|
|
.as_u64()
|
|
.map(Some)
|
|
.ok_or_else(|| anyhow!("{field_name} must be a non-negative integer"))
|
|
}
|
|
|
|
fn encode_raw_cultnet_schema_message(message: &CultNetMessage) -> Result<rmpv::Value> {
|
|
if let CultNetMessage::Error { code: Some(CultNetErrorCode::LimitReached), .. } = message {
|
|
return Err(anyhow!("limit_reached is a cultnet.error.v1 code; cultnet.error.v0 cannot carry it"));
|
|
}
|
|
Ok(rmpv::Value::Map(match message {
|
|
// Spec field order (write-receipt.md), not serde_json's sorted map.
|
|
CultNetMessage::WriteAdmitted { message_id, operation, schema_id, record_key } => vec![
|
|
(rmpv::Value::from("schemaVersion"), rmpv::Value::from("cultnet.write_admitted.v0")),
|
|
(rmpv::Value::from("messageId"), rmpv::Value::from(message_id.as_str())),
|
|
(rmpv::Value::from("operation"), rmpv::Value::from(operation.as_str())),
|
|
(rmpv::Value::from("schemaId"), rmpv::Value::from(schema_id.as_str())),
|
|
(rmpv::Value::from("recordKey"), rmpv::Value::from(record_key.as_str())),
|
|
],
|
|
CultNetMessage::DocumentPutRaw {
|
|
message_id,
|
|
document,
|
|
} => vec![
|
|
(
|
|
rmpv::Value::from("schemaVersion"),
|
|
rmpv::Value::from("cultnet.document_put_raw.v0"),
|
|
),
|
|
(
|
|
rmpv::Value::from("messageId"),
|
|
rmpv::Value::from(message_id.as_str()),
|
|
),
|
|
(
|
|
rmpv::Value::from("document"),
|
|
encode_raw_document_record(document),
|
|
),
|
|
],
|
|
CultNetMessage::SnapshotResponseRaw {
|
|
message_id,
|
|
documents,
|
|
} => vec![
|
|
(
|
|
rmpv::Value::from("schemaVersion"),
|
|
rmpv::Value::from("cultnet.snapshot_response_raw.v0"),
|
|
),
|
|
(
|
|
rmpv::Value::from("messageId"),
|
|
rmpv::Value::from(message_id.as_str()),
|
|
),
|
|
(
|
|
rmpv::Value::from("documents"),
|
|
rmpv::Value::Array(documents.iter().map(encode_raw_document_record).collect()),
|
|
),
|
|
],
|
|
CultNetMessage::SnapshotResponseRawV1 {
|
|
message_id,
|
|
matched,
|
|
as_of,
|
|
next,
|
|
headers,
|
|
documents,
|
|
edges,
|
|
shard_id,
|
|
shard_epoch,
|
|
shard_log_sequence,
|
|
} => vec![
|
|
(
|
|
rmpv::Value::from("schemaVersion"),
|
|
rmpv::Value::from("cultnet.snapshot_response_raw.v1"),
|
|
),
|
|
(
|
|
rmpv::Value::from("messageId"),
|
|
rmpv::Value::from(message_id.as_str()),
|
|
),
|
|
(rmpv::Value::from("matched"), rmpv::Value::from(*matched)),
|
|
(rmpv::Value::from("asOf"), rmpv::Value::from(*as_of)),
|
|
(
|
|
rmpv::Value::from("next"),
|
|
next.as_deref().map(rmpv::Value::from).unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("headers"),
|
|
headers
|
|
.as_deref()
|
|
.map(|headers| rmpv::Value::Array(headers.iter().map(encode_typed).collect()))
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("documents"),
|
|
documents
|
|
.as_deref()
|
|
.map(|documents| rmpv::Value::Array(documents.iter().map(encode_typed).collect()))
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("edges"),
|
|
edges
|
|
.as_deref()
|
|
.map(|edges| rmpv::Value::Array(edges.iter().map(encode_typed).collect()))
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("shardId"),
|
|
shard_id.as_deref().map(rmpv::Value::from).unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("shardEpoch"),
|
|
shard_epoch.map(rmpv::Value::from).unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("shardLogSequence"),
|
|
shard_log_sequence
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
],
|
|
CultNetMessage::ContentChunkRequest {
|
|
message_id,
|
|
chunk_hash,
|
|
record_key,
|
|
expected_size_bytes,
|
|
} => vec![
|
|
(
|
|
rmpv::Value::from("schemaVersion"),
|
|
rmpv::Value::from("cultmesh.content_chunk_request.v1"),
|
|
),
|
|
(rmpv::Value::from("messageId"), rmpv::Value::from(message_id.as_str())),
|
|
(rmpv::Value::from("chunkHash"), rmpv::Value::from(chunk_hash.as_str())),
|
|
(rmpv::Value::from("recordKey"), rmpv::Value::from(record_key.as_str())),
|
|
(
|
|
rmpv::Value::from("expectedSizeBytes"),
|
|
rmpv::Value::from(*expected_size_bytes),
|
|
),
|
|
],
|
|
CultNetMessage::ContentChunkResponse {
|
|
message_id,
|
|
found,
|
|
chunk_hash,
|
|
size_bytes,
|
|
payload,
|
|
error,
|
|
} => vec![
|
|
(
|
|
rmpv::Value::from("schemaVersion"),
|
|
rmpv::Value::from("cultmesh.content_chunk_response.v1"),
|
|
),
|
|
(rmpv::Value::from("messageId"), rmpv::Value::from(message_id.as_str())),
|
|
(rmpv::Value::from("found"), rmpv::Value::from(*found)),
|
|
(rmpv::Value::from("chunkHash"), rmpv::Value::from(chunk_hash.as_str())),
|
|
(rmpv::Value::from("sizeBytes"), rmpv::Value::from(*size_bytes)),
|
|
(rmpv::Value::from("payload"), rmpv::Value::Binary(payload.clone())),
|
|
(rmpv::Value::from("error"), rmpv::Value::from(error.as_str())),
|
|
],
|
|
// R-N: matches the landed C# `CultNetErrorMessage`
|
|
// (src/GameCult.Networking/CultNetSchemaMessages.cs) key for key and in its declared
|
|
// order - schemaVersion, error, routingHint, code, details - with every optional key
|
|
// present as nil rather than omitted. Rust never mints a routingHint today, so that key is
|
|
// always nil on encode; a peer that sent one is ignored on decode, same as any other v0
|
|
// peer that does not know a field it did not ask for.
|
|
CultNetMessage::Error {
|
|
error,
|
|
code,
|
|
details,
|
|
} => vec![
|
|
(
|
|
rmpv::Value::from("schemaVersion"),
|
|
rmpv::Value::from("cultnet.error.v0"),
|
|
),
|
|
(rmpv::Value::from("error"), rmpv::Value::from(error.as_str())),
|
|
(rmpv::Value::from("routingHint"), rmpv::Value::Nil),
|
|
(
|
|
rmpv::Value::from("code"),
|
|
code.as_ref()
|
|
.map(|code| rmpv::Value::from(code.as_wire_str()))
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("details"),
|
|
details
|
|
.as_ref()
|
|
.map(encode_error_details)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
],
|
|
CultNetMessage::ErrorV1 {
|
|
error,
|
|
code,
|
|
details,
|
|
message_id,
|
|
} => vec![
|
|
(rmpv::Value::from("schemaVersion"), rmpv::Value::from("cultnet.error.v1")),
|
|
(rmpv::Value::from("error"), rmpv::Value::from(error.as_str())),
|
|
(rmpv::Value::from("routingHint"), rmpv::Value::Nil),
|
|
(
|
|
rmpv::Value::from("code"),
|
|
code.as_ref()
|
|
.map(|code| rmpv::Value::from(code.as_wire_str()))
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("details"),
|
|
details
|
|
.as_ref()
|
|
.map(encode_error_details)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("messageId"),
|
|
message_id
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
],
|
|
_ => {
|
|
return Err(anyhow!(
|
|
"Message is not a raw cultnet.schema.v0 binary replication message"
|
|
));
|
|
}
|
|
}))
|
|
}
|
|
|
|
fn encode_error_details(details: &CultNetErrorDetails) -> rmpv::Value {
|
|
rmpv::Value::Map(vec![
|
|
(
|
|
rmpv::Value::from("field"),
|
|
details
|
|
.field
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("value"),
|
|
details
|
|
.value
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("asOf"),
|
|
details.as_of.map(rmpv::Value::from).unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("current"),
|
|
details.current.map(rmpv::Value::from).unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
])
|
|
}
|
|
|
|
fn require_raw_document_record(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<CultNetRawDocumentRecord> {
|
|
let object = value
|
|
.and_then(rmpv::Value::as_map)
|
|
.ok_or_else(|| anyhow!("{field_name} must be an object"))?;
|
|
refuse_duplicate_keys(object, field_name)?;
|
|
|
|
let get = |name: &str| -> Option<&rmpv::Value> {
|
|
object.iter().find_map(|(key, value)| {
|
|
key.as_str()
|
|
.filter(|candidate| *candidate == name)
|
|
.map(|_| value)
|
|
})
|
|
};
|
|
|
|
let payload_encoding = require_legacy_string(
|
|
get("payloadEncoding"),
|
|
&format!("{field_name}.payloadEncoding"),
|
|
)?;
|
|
|
|
Ok(CultNetRawDocumentRecord {
|
|
schema_id: require_legacy_string(get("schemaId"), &format!("{field_name}.schemaId"))?,
|
|
record_key: require_legacy_string(get("recordKey"), &format!("{field_name}.recordKey"))?,
|
|
stored_at: require_legacy_string(get("storedAt"), &format!("{field_name}.storedAt"))?,
|
|
payload_encoding: match payload_encoding.as_str() {
|
|
"messagepack" => CultNetRawPayloadEncoding::Messagepack,
|
|
_ => {
|
|
return Err(anyhow!(
|
|
"{field_name}.payloadEncoding names an encoding other than messagepack"
|
|
));
|
|
}
|
|
},
|
|
payload: get("payload")
|
|
.and_then(rmpv::Value::as_slice)
|
|
.map(|bytes| bytes.to_vec())
|
|
.ok_or_else(|| anyhow!("{field_name}.payload must be binary MessagePack bytes"))?,
|
|
source_runtime_id: require_legacy_optional_string(
|
|
get("sourceRuntimeId"),
|
|
&format!("{field_name}.sourceRuntimeId"),
|
|
)?,
|
|
source_agent_id: require_legacy_optional_string(
|
|
get("sourceAgentId"),
|
|
&format!("{field_name}.sourceAgentId"),
|
|
)?,
|
|
source_role: require_legacy_optional_string(
|
|
get("sourceRole"),
|
|
&format!("{field_name}.sourceRole"),
|
|
)?,
|
|
tags: require_legacy_optional_string_array(get("tags"), &format!("{field_name}.tags"))?,
|
|
})
|
|
}
|
|
|
|
fn encode_raw_document_record(document: &CultNetRawDocumentRecord) -> rmpv::Value {
|
|
rmpv::Value::Map(vec![
|
|
(
|
|
rmpv::Value::from("schemaId"),
|
|
rmpv::Value::from(document.schema_id.as_str()),
|
|
),
|
|
(
|
|
rmpv::Value::from("recordKey"),
|
|
rmpv::Value::from(document.record_key.as_str()),
|
|
),
|
|
(
|
|
rmpv::Value::from("storedAt"),
|
|
rmpv::Value::from(document.stored_at.as_str()),
|
|
),
|
|
(
|
|
rmpv::Value::from("payloadEncoding"),
|
|
rmpv::Value::from(match document.payload_encoding {
|
|
CultNetRawPayloadEncoding::Messagepack => "messagepack",
|
|
}),
|
|
),
|
|
(
|
|
rmpv::Value::from("payload"),
|
|
rmpv::Value::Binary(document.payload.clone()),
|
|
),
|
|
(
|
|
rmpv::Value::from("sourceRuntimeId"),
|
|
document
|
|
.source_runtime_id
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("sourceAgentId"),
|
|
document
|
|
.source_agent_id
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("sourceRole"),
|
|
document
|
|
.source_role
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("tags"),
|
|
legacy_optional_string_array(document.tags.as_deref()),
|
|
),
|
|
])
|
|
}
|
|
|
|
// R-M: `SelectionDocumentRecord`, `RawDocumentHeader`, `Edge` and `RecordRef` derive
|
|
// `Serialize`/`Deserialize` (selection.rs) with the camelCase field names this wire uses.
|
|
// `encode_typed` goes through `rmp_serde::to_vec_named` (the named-map encoding this wire always
|
|
// uses; `rmpv::ext::to_value` would write a struct positionally) and reads the bytes back into the
|
|
// `rmpv::Value` tree the rest of this codec builds by hand, through the decode door. Received rows
|
|
// are never re-encoded to become typed: the enum's derive reads them from the original bytes.
|
|
|
|
fn encode_typed<T: Serialize>(value: &T) -> rmpv::Value {
|
|
let bytes = rmp_serde::to_vec_named(value).expect("selection wire types always encode to MessagePack");
|
|
cultcache_rs::decode_payload(&bytes).expect("named-map MessagePack bytes always decode into rmpv::Value")
|
|
}
|
|
|
|
fn require_legacy_i32(value: Option<&rmpv::Value>, field_name: &str) -> Result<i32> {
|
|
let number = value
|
|
.and_then(rmpv::Value::as_i64)
|
|
.ok_or_else(|| anyhow!("{field_name} must be an integer"))?;
|
|
i32::try_from(number).map_err(|_| anyhow!("{field_name} does not fit in i32"))
|
|
}
|
|
|
|
fn parse_gamecult_networking_message(input: &rmpv::Value) -> Result<CultNetMessage> {
|
|
let items = input.as_array().ok_or_else(|| {
|
|
anyhow!("gamecult.networking.v0 messages must be a 2-element union array")
|
|
})?;
|
|
if items.len() != 2 {
|
|
return Err(anyhow!(
|
|
"gamecult.networking.v0 messages must be a 2-element union array"
|
|
));
|
|
}
|
|
let tag = items[0]
|
|
.as_i64()
|
|
.ok_or_else(|| anyhow!("gamecult.networking.v0 message tag must be an integer"))?;
|
|
let payload = items[1]
|
|
.as_array()
|
|
.ok_or_else(|| anyhow!("gamecult.networking.v0 message payload must be an array"))?;
|
|
match tag {
|
|
0 => Ok(CultNetMessage::Login {
|
|
nonce: require_legacy_bytes(payload.first(), "LoginMessage.Nonce")?,
|
|
auth: require_legacy_bytes(payload.get(1), "LoginMessage.Auth")?,
|
|
password: require_legacy_bytes(payload.get(2), "LoginMessage.Password")?,
|
|
}),
|
|
1 => Ok(CultNetMessage::Register {
|
|
nonce: require_legacy_bytes(payload.first(), "RegisterMessage.Nonce")?,
|
|
email: require_legacy_bytes(payload.get(1), "RegisterMessage.Email")?,
|
|
password: require_legacy_bytes(payload.get(2), "RegisterMessage.Password")?,
|
|
name: require_legacy_bytes(payload.get(3), "RegisterMessage.Name")?,
|
|
}),
|
|
2 => Ok(CultNetMessage::Verify {
|
|
nonce: require_legacy_bytes(payload.first(), "VerifyMessage.Nonce")?,
|
|
session: require_legacy_bytes(payload.get(1), "VerifyMessage.Session")?,
|
|
}),
|
|
3 => Ok(CultNetMessage::LoginSuccess {
|
|
nonce: require_legacy_bytes(payload.first(), "LoginSuccessMessage.Nonce")?,
|
|
session: require_legacy_bytes(payload.get(1), "LoginSuccessMessage.Session")?,
|
|
}),
|
|
4 => Ok(CultNetMessage::Error {
|
|
error: require_legacy_string(payload.first(), "ErrorMessage.Error")?,
|
|
// gamecult.networking.v0 is the legacy union tag/array contract, unrelated to R-N's
|
|
// cultnet.error.v0 map shape; it never carried code/details before and does not gain
|
|
// them now.
|
|
code: None,
|
|
details: None,
|
|
}),
|
|
5 => Ok(CultNetMessage::SampleChangeName {
|
|
name: require_legacy_string(payload.first(), "ChangeNameMessage.Name")?,
|
|
}),
|
|
6 => Ok(CultNetMessage::SampleChat {
|
|
text: require_legacy_string(payload.first(), "ChatMessage.Text")?,
|
|
}),
|
|
7 => Ok(CultNetMessage::SchemaCatalogRequest {
|
|
message_id: require_legacy_string(
|
|
payload.first(),
|
|
"SchemaCatalogRequestMessage.MessageId",
|
|
)?,
|
|
include_schema_json: Some(require_legacy_bool(
|
|
payload.get(1),
|
|
"SchemaCatalogRequestMessage.IncludeSchemaJson",
|
|
)?),
|
|
schema_ids: require_legacy_optional_string_array(
|
|
payload.get(2),
|
|
"SchemaCatalogRequestMessage.SchemaIds",
|
|
)?,
|
|
kinds: require_legacy_optional_schema_kind_array(
|
|
payload.get(3),
|
|
"SchemaCatalogRequestMessage.Kinds",
|
|
)?,
|
|
}),
|
|
8 => Ok(CultNetMessage::SchemaCatalogResponse {
|
|
message_id: require_legacy_string(
|
|
payload.first(),
|
|
"SchemaCatalogResponseMessage.MessageId",
|
|
)?,
|
|
schemas: require_legacy_schema_descriptor_array(
|
|
payload.get(1),
|
|
"SchemaCatalogResponseMessage.Schemas",
|
|
)?,
|
|
}),
|
|
_ => Err(anyhow!(
|
|
"Unsupported gamecult.networking.v0 union tag"
|
|
)),
|
|
}
|
|
}
|
|
|
|
fn encode_gamecult_networking_message(message: &CultNetMessage) -> Result<rmpv::Value> {
|
|
let pair = match message {
|
|
CultNetMessage::Login {
|
|
nonce,
|
|
auth,
|
|
password,
|
|
} => vec![
|
|
rmpv::Value::from(0),
|
|
rmpv::Value::Array(vec![
|
|
legacy_bytes(nonce, "LoginMessage.Nonce")?,
|
|
legacy_bytes(auth, "LoginMessage.Auth")?,
|
|
legacy_bytes(password, "LoginMessage.Password")?,
|
|
]),
|
|
],
|
|
CultNetMessage::Register {
|
|
nonce,
|
|
email,
|
|
password,
|
|
name,
|
|
} => vec![
|
|
rmpv::Value::from(1),
|
|
rmpv::Value::Array(vec![
|
|
legacy_bytes(nonce, "RegisterMessage.Nonce")?,
|
|
legacy_bytes(email, "RegisterMessage.Email")?,
|
|
legacy_bytes(password, "RegisterMessage.Password")?,
|
|
legacy_bytes(name, "RegisterMessage.Name")?,
|
|
]),
|
|
],
|
|
CultNetMessage::Verify { nonce, session } => vec![
|
|
rmpv::Value::from(2),
|
|
rmpv::Value::Array(vec![
|
|
legacy_bytes(nonce, "VerifyMessage.Nonce")?,
|
|
legacy_bytes(session, "VerifyMessage.Session")?,
|
|
]),
|
|
],
|
|
CultNetMessage::LoginSuccess { nonce, session } => vec![
|
|
rmpv::Value::from(3),
|
|
rmpv::Value::Array(vec![
|
|
legacy_bytes(nonce, "LoginSuccessMessage.Nonce")?,
|
|
legacy_bytes(session, "LoginSuccessMessage.Session")?,
|
|
]),
|
|
],
|
|
CultNetMessage::Error { error, .. } => {
|
|
vec![
|
|
rmpv::Value::from(4),
|
|
rmpv::Value::Array(vec![error.as_str().into()]),
|
|
]
|
|
}
|
|
CultNetMessage::SampleChangeName { name } => {
|
|
vec![
|
|
rmpv::Value::from(5),
|
|
rmpv::Value::Array(vec![name.as_str().into()]),
|
|
]
|
|
}
|
|
CultNetMessage::SampleChat { text } => {
|
|
vec![
|
|
rmpv::Value::from(6),
|
|
rmpv::Value::Array(vec![text.as_str().into()]),
|
|
]
|
|
}
|
|
CultNetMessage::SchemaCatalogRequest {
|
|
message_id,
|
|
include_schema_json,
|
|
schema_ids,
|
|
kinds,
|
|
} => vec![
|
|
rmpv::Value::from(7),
|
|
rmpv::Value::Array(vec![
|
|
message_id.as_str().into(),
|
|
rmpv::Value::from(include_schema_json.unwrap_or(false)),
|
|
legacy_optional_string_array(schema_ids.as_deref()),
|
|
legacy_optional_schema_kind_array(kinds.as_deref()),
|
|
]),
|
|
],
|
|
CultNetMessage::SchemaCatalogResponse {
|
|
message_id,
|
|
schemas,
|
|
} => vec![
|
|
rmpv::Value::from(8),
|
|
rmpv::Value::Array(vec![
|
|
message_id.as_str().into(),
|
|
rmpv::Value::Array(
|
|
schemas
|
|
.iter()
|
|
.map(legacy_schema_descriptor)
|
|
.collect::<Result<Vec<_>>>()?,
|
|
),
|
|
]),
|
|
],
|
|
_ => {
|
|
return Err(anyhow!(
|
|
"Message is not defined in the gamecult.networking.v0 contract"
|
|
));
|
|
}
|
|
};
|
|
Ok(rmpv::Value::Array(pair))
|
|
}
|
|
|
|
fn require_legacy_bytes(value: Option<&rmpv::Value>, field_name: &str) -> Result<String> {
|
|
let bytes = value
|
|
.and_then(rmpv::Value::as_slice)
|
|
.ok_or_else(|| anyhow!("{field_name} must be binary data in gamecult.networking.v0"))?;
|
|
Ok(URL_SAFE_NO_PAD.encode(bytes))
|
|
}
|
|
|
|
/// A string the reference reads back as null when its writer set it to null (msgpack nil), which its
|
|
/// handlers treat as blank. Only content chunk requests use it: they are answered, never dropped.
|
|
fn legacy_string_or_nil(value: Option<&rmpv::Value>, field_name: &str) -> Result<String> {
|
|
match value {
|
|
Some(rmpv::Value::Nil) => Ok(String::new()),
|
|
other => require_legacy_string(other, field_name),
|
|
}
|
|
}
|
|
|
|
fn require_legacy_string(value: Option<&rmpv::Value>, field_name: &str) -> Result<String> {
|
|
let value = value
|
|
.and_then(rmpv::Value::as_str)
|
|
.ok_or_else(|| anyhow!("{field_name} must be a string in gamecult.networking.v0"))?;
|
|
Ok(value.to_string())
|
|
}
|
|
|
|
fn require_legacy_bool(value: Option<&rmpv::Value>, field_name: &str) -> Result<bool> {
|
|
value
|
|
.and_then(rmpv::Value::as_bool)
|
|
.ok_or_else(|| anyhow!("{field_name} must be a boolean in gamecult.networking.v0"))
|
|
}
|
|
|
|
fn require_legacy_optional_string_array(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<Option<Vec<String>>> {
|
|
let Some(value) = value else {
|
|
return Ok(None);
|
|
};
|
|
if value.is_nil() {
|
|
return Ok(None);
|
|
}
|
|
let items = value
|
|
.as_array()
|
|
.ok_or_else(|| anyhow!("{field_name} must be an array in gamecult.networking.v0"))?;
|
|
let values = items
|
|
.iter()
|
|
.map(|item| {
|
|
item.as_str()
|
|
.map(ToString::to_string)
|
|
.ok_or_else(|| anyhow!("{field_name} must contain only strings"))
|
|
})
|
|
.collect::<Result<Vec<_>>>()?;
|
|
Ok(Some(values))
|
|
}
|
|
|
|
fn require_legacy_optional_schema_kind_array(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<Option<Vec<CultNetSchemaKind>>> {
|
|
let Some(values) = require_legacy_optional_string_array(value, field_name)? else {
|
|
return Ok(None);
|
|
};
|
|
values
|
|
.into_iter()
|
|
.map(|value| match value.as_str() {
|
|
"wire_message" => Ok(CultNetSchemaKind::WireMessage),
|
|
"document_payload" => Ok(CultNetSchemaKind::DocumentPayload),
|
|
"shared_contract" => Ok(CultNetSchemaKind::SharedContract),
|
|
_ => Err(anyhow!("{field_name} has an unsupported schema kind")),
|
|
})
|
|
.collect::<Result<Vec<_>>>()
|
|
.map(Some)
|
|
}
|
|
|
|
fn require_legacy_schema_descriptor_array(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<Vec<CultNetSchemaDescriptor>> {
|
|
let items = value
|
|
.and_then(rmpv::Value::as_array)
|
|
.ok_or_else(|| anyhow!("{field_name} must be an array in gamecult.networking.v0"))?;
|
|
items
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(index, item)| {
|
|
require_legacy_schema_descriptor(item, &format!("{field_name}[{index}]"))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn require_legacy_schema_descriptor(
|
|
value: &rmpv::Value,
|
|
field_name: &str,
|
|
) -> Result<CultNetSchemaDescriptor> {
|
|
let object = value
|
|
.as_map()
|
|
.ok_or_else(|| anyhow!("{field_name} must be an object in gamecult.networking.v0"))?;
|
|
refuse_duplicate_keys(object, field_name)?;
|
|
|
|
let get = |name: &str| -> Option<&rmpv::Value> {
|
|
object.iter().find_map(|(key, value)| {
|
|
key.as_str()
|
|
.filter(|candidate| *candidate == name)
|
|
.map(|_| value)
|
|
})
|
|
};
|
|
|
|
let schema_id = require_legacy_string(get("schemaId"), &format!("{field_name}.SchemaId"))?;
|
|
let kind = require_legacy_string(get("kind"), &format!("{field_name}.Kind"))?;
|
|
let schema_version = require_legacy_optional_string(
|
|
get("schemaVersion"),
|
|
&format!("{field_name}.SchemaVersion"),
|
|
)?;
|
|
let document_type =
|
|
require_legacy_optional_string(get("documentType"), &format!("{field_name}.DocumentType"))?;
|
|
let title = require_legacy_optional_string(get("title"), &format!("{field_name}.Title"))?;
|
|
let wire_contracts = require_legacy_optional_wire_contracts(
|
|
get("wireContracts"),
|
|
&format!("{field_name}.WireContracts"),
|
|
)?;
|
|
let content_hash =
|
|
require_legacy_string(get("contentHash"), &format!("{field_name}.ContentHash"))?;
|
|
let schema_json =
|
|
require_legacy_optional_string(get("schemaJson"), &format!("{field_name}.SchemaJson"))?;
|
|
|
|
let kind = match kind.as_str() {
|
|
"wire_message" => CultNetSchemaKind::WireMessage,
|
|
"document_payload" => CultNetSchemaKind::DocumentPayload,
|
|
"shared_contract" => CultNetSchemaKind::SharedContract,
|
|
_ => {
|
|
return Err(anyhow!(
|
|
"{field_name}.Kind is not a supported schema kind"
|
|
));
|
|
}
|
|
};
|
|
|
|
Ok(CultNetSchemaDescriptor {
|
|
schema_id,
|
|
kind,
|
|
schema_version,
|
|
document_type,
|
|
title,
|
|
wire_contracts,
|
|
content_hash,
|
|
schema_json,
|
|
})
|
|
}
|
|
|
|
fn require_legacy_optional_string(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<Option<String>> {
|
|
let Some(value) = value else {
|
|
return Ok(None);
|
|
};
|
|
if value.is_nil() {
|
|
return Ok(None);
|
|
}
|
|
Ok(Some(require_legacy_string(Some(value), field_name)?))
|
|
}
|
|
|
|
fn require_legacy_optional_wire_contracts(
|
|
value: Option<&rmpv::Value>,
|
|
field_name: &str,
|
|
) -> Result<Vec<CultNetWireContract>> {
|
|
let Some(values) = require_legacy_optional_string_array(value, field_name)? else {
|
|
return Ok(Vec::new());
|
|
};
|
|
|
|
values
|
|
.into_iter()
|
|
.map(|value| match value.as_str() {
|
|
"cultnet.schema.v0" => Ok(CultNetWireContract::CultNetSchemaV0),
|
|
"gamecult.networking.v0" => Ok(CultNetWireContract::GameCultNetworkingV0),
|
|
_ => Err(anyhow!(
|
|
"{field_name} contains unsupported wire contract {value}"
|
|
)),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn legacy_bytes(input: &str, field_name: &str) -> Result<rmpv::Value> {
|
|
if input.trim().is_empty() {
|
|
return Err(anyhow!("{field_name} must be a non-empty base64url string"));
|
|
}
|
|
Ok(rmpv::Value::Binary(URL_SAFE_NO_PAD.decode(input)?))
|
|
}
|
|
|
|
fn legacy_optional_string_array(input: Option<&[String]>) -> rmpv::Value {
|
|
match input {
|
|
Some(values) => rmpv::Value::Array(
|
|
values
|
|
.iter()
|
|
.map(|value| rmpv::Value::from(value.as_str()))
|
|
.collect(),
|
|
),
|
|
None => rmpv::Value::Nil,
|
|
}
|
|
}
|
|
|
|
fn legacy_optional_schema_kind_array(input: Option<&[CultNetSchemaKind]>) -> rmpv::Value {
|
|
match input {
|
|
Some(values) => rmpv::Value::Array(
|
|
values
|
|
.iter()
|
|
.map(|value| {
|
|
rmpv::Value::from(match value {
|
|
CultNetSchemaKind::WireMessage => "wire_message",
|
|
CultNetSchemaKind::DocumentPayload => "document_payload",
|
|
CultNetSchemaKind::SharedContract => "shared_contract",
|
|
})
|
|
})
|
|
.collect(),
|
|
),
|
|
None => rmpv::Value::Nil,
|
|
}
|
|
}
|
|
|
|
fn legacy_schema_descriptor(schema: &CultNetSchemaDescriptor) -> Result<rmpv::Value> {
|
|
Ok(rmpv::Value::Map(vec![
|
|
(
|
|
rmpv::Value::from("schemaId"),
|
|
rmpv::Value::from(schema.schema_id.as_str()),
|
|
),
|
|
(
|
|
rmpv::Value::from("kind"),
|
|
rmpv::Value::from(match schema.kind {
|
|
CultNetSchemaKind::WireMessage => "wire_message",
|
|
CultNetSchemaKind::DocumentPayload => "document_payload",
|
|
CultNetSchemaKind::SharedContract => "shared_contract",
|
|
}),
|
|
),
|
|
(
|
|
rmpv::Value::from("schemaVersion"),
|
|
schema
|
|
.schema_version
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("documentType"),
|
|
schema
|
|
.document_type
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("title"),
|
|
schema
|
|
.title
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
(
|
|
rmpv::Value::from("wireContracts"),
|
|
rmpv::Value::Array(
|
|
schema
|
|
.wire_contracts
|
|
.iter()
|
|
.map(|value| {
|
|
rmpv::Value::from(match value {
|
|
CultNetWireContract::CultNetSchemaV0 => "cultnet.schema.v0",
|
|
CultNetWireContract::GameCultNetworkingV0 => "gamecult.networking.v0",
|
|
})
|
|
})
|
|
.collect(),
|
|
),
|
|
),
|
|
(
|
|
rmpv::Value::from("contentHash"),
|
|
rmpv::Value::from(schema.content_hash.as_str()),
|
|
),
|
|
(
|
|
rmpv::Value::from("schemaJson"),
|
|
schema
|
|
.schema_json
|
|
.as_deref()
|
|
.map(rmpv::Value::from)
|
|
.unwrap_or(rmpv::Value::Nil),
|
|
),
|
|
]))
|
|
}
|
|
|
|
/// The content hash a write-receipt server binds to a `(actor, messageId)` dedupe key
|
|
/// (contracts/cultnet/interop/write-receipt.md, rule 5): SHA-256 of the MessagePack array
|
|
/// `[operation, schemaId, recordKey, payload]`, `payload` a `bin` for a put and nil for a delete.
|
|
pub fn write_content_hash(
|
|
operation: &str,
|
|
schema_id: &str,
|
|
record_key: &str,
|
|
payload: Option<&[u8]>,
|
|
) -> Result<[u8; 32]> {
|
|
use sha2::{Digest, Sha256};
|
|
let value = rmpv::Value::Array(vec![
|
|
rmpv::Value::from(operation),
|
|
rmpv::Value::from(schema_id),
|
|
rmpv::Value::from(record_key),
|
|
payload.map_or(rmpv::Value::Nil, |payload| rmpv::Value::Binary(payload.to_vec())),
|
|
]);
|
|
let mut bytes = Vec::new();
|
|
rmpv::encode::write_value(&mut bytes, &value)?;
|
|
Ok(Sha256::digest(&bytes).into())
|
|
}
|
|
|
|
/// SHA-256 of a write's `messageId`: what a receipt server remembers in place of the id itself, so an
|
|
/// entry's memory does not depend on the id's length (write-receipt.md, "What is remembered").
|
|
pub fn message_id_digest(message_id: &str) -> [u8; 32] {
|
|
use sha2::{Digest, Sha256};
|
|
Sha256::digest(message_id.as_bytes()).into()
|
|
}
|
|
|
|
/// The `messageId` of a schema frame that does not decode as a message (write-receipt.md, rule 3: a refusal
|
|
/// carries the write's id whenever the frame is readable enough to find it). `None` when the bytes are not
|
|
/// a MessagePack map or hold no non-empty string `messageId`.
|
|
pub fn readable_message_id(bytes: &[u8]) -> Option<String> {
|
|
let value = rmpv::decode::read_value(&mut &*bytes).ok()?;
|
|
let rmpv::Value::Map(entries) = value else {
|
|
return None;
|
|
};
|
|
entries.into_iter().find_map(|(key, value)| match (key.as_str(), value) {
|
|
(Some("messageId"), rmpv::Value::String(id)) => id.into_str().filter(|id| !id.is_empty()),
|
|
_ => None,
|
|
})
|
|
}
|