@feltdb/core 0.4.11 → 0.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/commands.js +5 -2
- package/dist/cli/index.js +1 -1
- package/dist/create/cli.js +16 -1
- package/dist/create/create.js +30 -16
- package/dist/create/docker-compose-generator.js +48 -9
- package/dist/create/package-versions.js +1 -1
- package/dist/create/server-source/Cargo.lock +2238 -0
- package/dist/create/server-source/Cargo.toml +7 -0
- package/dist/create/server-source/crates/feltdb/Cargo.lock +175 -0
- package/dist/create/server-source/crates/feltdb/Cargo.toml +79 -0
- package/dist/create/server-source/crates/feltdb/benches/baselines/gate-13-redux.json +370 -0
- package/dist/create/server-source/crates/feltdb/benches/gate13_baseline.rs +589 -0
- package/dist/create/server-source/crates/feltdb/benches/gate13_phase_7_1_release_economics.rs +259 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_13_redux.rs +446 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_13_regression_runner.rs +272 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_concurrent_writer_scaling.rs +378 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_production_admission_revalidation.rs +414 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc2_admission_contract.rs +486 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc_root_cause.rs +273 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync1_queued_prototype.rs +587 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync_economics.rs +513 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14b_causal_backlog_scaling.rs +395 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_contract_test.rs +469 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_scaling.rs +409 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14d_combined_dimension_scaling.rs +627 -0
- package/dist/create/server-source/crates/feltdb/benches/phase_7_1_2_optimization_benchmark.rs +383 -0
- package/dist/create/server-source/crates/feltdb/benches/phase_7_1_3_crossover_analysis.rs +298 -0
- package/dist/create/server-source/crates/feltdb/src/acceptance_tests.rs +1698 -0
- package/dist/create/server-source/crates/feltdb/src/acquisition.rs +286 -0
- package/dist/create/server-source/crates/feltdb/src/admission.rs +192 -0
- package/dist/create/server-source/crates/feltdb/src/admission_contract_tests.rs +477 -0
- package/dist/create/server-source/crates/feltdb/src/adversarial_transport.rs +566 -0
- package/dist/create/server-source/crates/feltdb/src/analytics.rs +475 -0
- package/dist/create/server-source/crates/feltdb/src/application.rs +2244 -0
- package/dist/create/server-source/crates/feltdb/src/application_runtime.rs +1070 -0
- package/dist/create/server-source/crates/feltdb/src/authorization.rs +1030 -0
- package/dist/create/server-source/crates/feltdb/src/bin/feltdb_node.rs +266 -0
- package/dist/create/server-source/crates/feltdb/src/capabilities/mod.rs +8 -0
- package/dist/create/server-source/crates/feltdb/src/capabilities/search.rs +418 -0
- package/dist/create/server-source/crates/feltdb/src/capabilities/vector.rs +482 -0
- package/dist/create/server-source/crates/feltdb/src/capability.rs +903 -0
- package/dist/create/server-source/crates/feltdb/src/cardinality_diagnostics.rs +261 -0
- package/dist/create/server-source/crates/feltdb/src/cardinality_endpoint.rs +78 -0
- package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier.rs +2214 -0
- package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier_phase_7_1.rs +194 -0
- package/dist/create/server-source/crates/feltdb/src/concurrency_fuzzing.rs +427 -0
- package/dist/create/server-source/crates/feltdb/src/consistency_contract.rs +453 -0
- package/dist/create/server-source/crates/feltdb/src/content_distribution.rs +465 -0
- package/dist/create/server-source/crates/feltdb/src/convergence.rs +618 -0
- package/dist/create/server-source/crates/feltdb/src/crash_atomic_boundary.rs +418 -0
- package/dist/create/server-source/crates/feltdb/src/crash_injection.rs +380 -0
- package/dist/create/server-source/crates/feltdb/src/crash_recovery_tests.rs +362 -0
- package/dist/create/server-source/crates/feltdb/src/cron.rs +294 -0
- package/dist/create/server-source/crates/feltdb/src/distributed_indexing.rs +474 -0
- package/dist/create/server-source/crates/feltdb/src/distributed_tests.rs +1003 -0
- package/dist/create/server-source/crates/feltdb/src/distributed_transactions.rs +536 -0
- package/dist/create/server-source/crates/feltdb/src/durability_guarantees.rs +364 -0
- package/dist/create/server-source/crates/feltdb/src/durable_dedup_set.rs +235 -0
- package/dist/create/server-source/crates/feltdb/src/durable_operation_log.rs +207 -0
- package/dist/create/server-source/crates/feltdb/src/durable_sync.rs +316 -0
- package/dist/create/server-source/crates/feltdb/src/execution.rs +426 -0
- package/dist/create/server-source/crates/feltdb/src/in_process_transport.rs +219 -0
- package/dist/create/server-source/crates/feltdb/src/indexing.rs +779 -0
- package/dist/create/server-source/crates/feltdb/src/lib.rs +2838 -0
- package/dist/create/server-source/crates/feltdb/src/materialization.rs +184 -0
- package/dist/create/server-source/crates/feltdb/src/metrics.rs +267 -0
- package/dist/create/server-source/crates/feltdb/src/multi_node_convergence.rs +311 -0
- package/dist/create/server-source/crates/feltdb/src/observability.rs +366 -0
- package/dist/create/server-source/crates/feltdb/src/operation.rs +251 -0
- package/dist/create/server-source/crates/feltdb/src/operation_algebra.rs +438 -0
- package/dist/create/server-source/crates/feltdb/src/operation_log.rs +344 -0
- package/dist/create/server-source/crates/feltdb/src/partition_reconciliation.rs +477 -0
- package/dist/create/server-source/crates/feltdb/src/peer_registry.rs +166 -0
- package/dist/create/server-source/crates/feltdb/src/permutation_scheduler.rs +261 -0
- package/dist/create/server-source/crates/feltdb/src/persistence_reality.rs +560 -0
- package/dist/create/server-source/crates/feltdb/src/phase1b_acceptance.rs +3226 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c1_acceptance.rs +201 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c2_acceptance.rs +263 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c3_acceptance.rs +484 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c_atomicity_proof.rs +216 -0
- package/dist/create/server-source/crates/feltdb/src/phase5_integration.rs +281 -0
- package/dist/create/server-source/crates/feltdb/src/phase5_scenarios.rs +323 -0
- package/dist/create/server-source/crates/feltdb/src/phase6_adversarial_scenarios.rs +573 -0
- package/dist/create/server-source/crates/feltdb/src/phase6_convergence_validator.rs +404 -0
- package/dist/create/server-source/crates/feltdb/src/phase6_persistence.rs +418 -0
- package/dist/create/server-source/crates/feltdb/src/phase_1c_real_tcp.rs +381 -0
- package/dist/create/server-source/crates/feltdb/src/phase_1c_three_node.rs +523 -0
- package/dist/create/server-source/crates/feltdb/src/phase_2a_failures.rs +334 -0
- package/dist/create/server-source/crates/feltdb/src/phase_2b_network.rs +306 -0
- package/dist/create/server-source/crates/feltdb/src/phase_2c_cascading.rs +355 -0
- package/dist/create/server-source/crates/feltdb/src/phase_3_durability.rs +395 -0
- package/dist/create/server-source/crates/feltdb/src/phase_4_baseline.rs +346 -0
- package/dist/create/server-source/crates/feltdb/src/phase_5_soak.rs +430 -0
- package/dist/create/server-source/crates/feltdb/src/production_api.rs +400 -0
- package/dist/create/server-source/crates/feltdb/src/provenance.rs +207 -0
- package/dist/create/server-source/crates/feltdb/src/query_performance.rs +217 -0
- package/dist/create/server-source/crates/feltdb/src/references.rs +404 -0
- package/dist/create/server-source/crates/feltdb/src/replay_fuzzing.rs +401 -0
- package/dist/create/server-source/crates/feltdb/src/replication_manager.rs +160 -0
- package/dist/create/server-source/crates/feltdb/src/replication_protocol.rs +132 -0
- package/dist/create/server-source/crates/feltdb/src/routing.rs +409 -0
- package/dist/create/server-source/crates/feltdb/src/sharding.rs +523 -0
- package/dist/create/server-source/crates/feltdb/src/state_contract.rs +3118 -0
- package/dist/create/server-source/crates/feltdb/src/state_hash.rs +291 -0
- package/dist/create/server-source/crates/feltdb/src/state_transition_store.rs +376 -0
- package/dist/create/server-source/crates/feltdb/src/storage.rs +267 -0
- package/dist/create/server-source/crates/feltdb/src/submission.rs +477 -0
- package/dist/create/server-source/crates/feltdb/src/sync.rs +483 -0
- package/dist/create/server-source/crates/feltdb/src/sync_contract.rs +822 -0
- package/dist/create/server-source/crates/feltdb/src/tcp_transport.rs +366 -0
- package/dist/create/server-source/crates/feltdb/src/transaction_api.rs +473 -0
- package/dist/create/server-source/crates/feltdb/src/transaction_invariants.rs +949 -0
- package/dist/create/server-source/crates/feltdb/src/transactions.rs +646 -0
- package/dist/create/server-source/crates/feltdb/src/trigger.rs +390 -0
- package/dist/create/server-source/crates/feltdb/src/worker_mesh.rs +928 -0
- package/dist/create/server-source/crates/feltdb/src/workflow.rs +713 -0
- package/dist/create/server-source/crates/feltdb/src/workflow_acceptance_tests.rs +510 -0
- package/dist/create/server-source/crates/feltdb/src/workflow_integration.rs +354 -0
- package/dist/create/server-source/crates/feltdb/src/workflow_runtime.rs +594 -0
- package/dist/create/server-source/crates/feltdb/src/workload.rs +1310 -0
- package/dist/create/server-source/crates/feltdb-server/Cargo.toml +23 -0
- package/dist/create/server-source/crates/feltdb-server/README.md +79 -0
- package/dist/create/server-source/crates/feltdb-server/src/app_state.rs +73 -0
- package/dist/create/server-source/crates/feltdb-server/src/application_contract.rs +425 -0
- package/dist/create/server-source/crates/feltdb-server/src/artifacts.rs +449 -0
- package/dist/create/server-source/crates/feltdb-server/src/audit.rs +59 -0
- package/dist/create/server-source/crates/feltdb-server/src/auth.rs +308 -0
- package/dist/create/server-source/crates/feltdb-server/src/authorization.rs +228 -0
- package/dist/create/server-source/crates/feltdb-server/src/backup.rs +416 -0
- package/dist/create/server-source/crates/feltdb-server/src/causal.rs +218 -0
- package/dist/create/server-source/crates/feltdb-server/src/certification.rs +223 -0
- package/dist/create/server-source/crates/feltdb-server/src/clock.rs +132 -0
- package/dist/create/server-source/crates/feltdb-server/src/cluster.rs +165 -0
- package/dist/create/server-source/crates/feltdb-server/src/connections.rs +1044 -0
- package/dist/create/server-source/crates/feltdb-server/src/content.rs +111 -0
- package/dist/create/server-source/crates/feltdb-server/src/identity.rs +250 -0
- package/dist/create/server-source/crates/feltdb-server/src/key_management.rs +78 -0
- package/dist/create/server-source/crates/feltdb-server/src/key_provider.rs +247 -0
- package/dist/create/server-source/crates/feltdb-server/src/leases.rs +123 -0
- package/dist/create/server-source/crates/feltdb-server/src/lib.rs +25 -0
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +8715 -0
- package/dist/create/server-source/crates/feltdb-server/src/metrics.rs +97 -0
- package/dist/create/server-source/crates/feltdb-server/src/portable_bundle.rs +350 -0
- package/dist/create/server-source/crates/feltdb-server/src/principals.rs +510 -0
- package/dist/create/server-source/crates/feltdb-server/src/providers.rs +269 -0
- package/dist/create/server-source/crates/feltdb-server/src/releases.rs +2563 -0
- package/dist/create/server-source/crates/feltdb-server/src/sessions.rs +156 -0
- package/dist/create/server-source/crates/feltdb-server/src/tenancy.rs +1097 -0
- package/dist/create/server-source/crates/feltdb-server/src/versions.rs +264 -0
- package/dist/create/server-source/crates/feltdb-wasm/Cargo.toml +31 -0
- package/dist/create/server-source/crates/feltdb-wasm/src/lib.rs +1086 -0
- package/dist/create/server-source/crates/feltdb-wasm/test.db +0 -0
- package/dist/flowspec.d.ts +2 -0
- package/dist/flowspec.d.ts.map +1 -1
- package/dist/flowspec.js +28 -4
- package/dist/state-contract.d.ts +7 -1
- package/dist/state-contract.d.ts.map +1 -1
- package/dist/studio/components/ApplicationDesigner.d.ts.map +1 -1
- package/dist/studio/components/index.js +1 -1
- package/dist/studio/{components-q43NSTTH.js → components-bHTARBin.js} +160 -89
- package/dist/studio/index.js +31 -28
- package/dist/studio-app/assets/{index-DZpBoVMp.js → index-BqKquLuU.js} +8 -8
- package/dist/studio-app/index.html +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/// Cryptographic State Hashing
|
|
2
|
+
///
|
|
3
|
+
/// Produces SHA-256 hashes of canonical state serializations.
|
|
4
|
+
/// Invariant: same state → same hash, different state → different hash (except negligible crypto collision)
|
|
5
|
+
|
|
6
|
+
use serde_json::{json, Value};
|
|
7
|
+
use std::collections::BTreeMap;
|
|
8
|
+
|
|
9
|
+
/// Canonical state representation for hashing
|
|
10
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
11
|
+
pub struct CanonicalState {
|
|
12
|
+
/// Records indexed by (collection, record_id)
|
|
13
|
+
pub records: BTreeMap<(String, String), Value>,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
impl CanonicalState {
|
|
17
|
+
pub fn new() -> Self {
|
|
18
|
+
Self {
|
|
19
|
+
records: BTreeMap::new(),
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Add or update a record
|
|
24
|
+
pub fn set_record(&mut self, collection: String, record_id: String, data: Value) {
|
|
25
|
+
self.records.insert((collection, record_id), data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Get a record
|
|
29
|
+
pub fn get_record(&self, collection: &str, record_id: &str) -> Option<&Value> {
|
|
30
|
+
self.records.get(&(collection.to_string(), record_id.to_string()))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Canonical serialization for hashing (deterministic ordering)
|
|
34
|
+
pub fn to_canonical_json(&self) -> Value {
|
|
35
|
+
let mut state_map = serde_json::Map::new();
|
|
36
|
+
|
|
37
|
+
for ((collection, record_id), data) in &self.records {
|
|
38
|
+
let key = format!("{}:{}", collection, record_id);
|
|
39
|
+
state_map.insert(key, data.clone());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
Value::Object(state_map)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Compute cryptographic hash of canonical state
|
|
46
|
+
pub fn hash(&self) -> StateHash {
|
|
47
|
+
let canonical = self.to_canonical_json();
|
|
48
|
+
let json_str = canonical.to_string();
|
|
49
|
+
StateHash::compute(&json_str)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// Get the size of the canonical state (for metrics)
|
|
53
|
+
pub fn size_bytes(&self) -> usize {
|
|
54
|
+
self.to_canonical_json().to_string().len()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
impl Default for CanonicalState {
|
|
59
|
+
fn default() -> Self {
|
|
60
|
+
Self::new()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// SHA-256 hash of state
|
|
65
|
+
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
66
|
+
pub struct StateHash(String);
|
|
67
|
+
|
|
68
|
+
impl StateHash {
|
|
69
|
+
/// Compute hash from canonical JSON string
|
|
70
|
+
pub fn compute(canonical_json: &str) -> Self {
|
|
71
|
+
use sha2::{Digest, Sha256};
|
|
72
|
+
|
|
73
|
+
let mut hasher = Sha256::new();
|
|
74
|
+
hasher.update(canonical_json.as_bytes());
|
|
75
|
+
let result = hasher.finalize();
|
|
76
|
+
|
|
77
|
+
let hash_hex = format!("{:x}", result);
|
|
78
|
+
StateHash(hash_hex)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Create from string (for testing/recovery)
|
|
82
|
+
pub fn from_hex(hex: String) -> Self {
|
|
83
|
+
StateHash(hex)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// Get hex representation
|
|
87
|
+
pub fn as_hex(&self) -> &str {
|
|
88
|
+
&self.0
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Verify a canonical state produces this hash
|
|
92
|
+
pub fn verify(&self, canonical_json: &str) -> bool {
|
|
93
|
+
Self::compute(canonical_json) == *self
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
impl std::fmt::Display for StateHash {
|
|
98
|
+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
99
|
+
write!(f, "{}", self.0)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[cfg(test)]
|
|
104
|
+
mod tests {
|
|
105
|
+
use super::*;
|
|
106
|
+
|
|
107
|
+
#[test]
|
|
108
|
+
fn test_canonical_state_creation() {
|
|
109
|
+
let mut state = CanonicalState::new();
|
|
110
|
+
state.set_record(
|
|
111
|
+
"users".to_string(),
|
|
112
|
+
"user1".to_string(),
|
|
113
|
+
json!({"name": "Alice", "balance": 100}),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
assert_eq!(state.records.len(), 1);
|
|
117
|
+
let record = state.get_record("users", "user1");
|
|
118
|
+
assert!(record.is_some());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#[test]
|
|
122
|
+
fn test_state_hash_deterministic() {
|
|
123
|
+
let mut state1 = CanonicalState::new();
|
|
124
|
+
state1.set_record(
|
|
125
|
+
"users".to_string(),
|
|
126
|
+
"user1".to_string(),
|
|
127
|
+
json!({"name": "Alice", "balance": 100}),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
let mut state2 = CanonicalState::new();
|
|
131
|
+
state2.set_record(
|
|
132
|
+
"users".to_string(),
|
|
133
|
+
"user1".to_string(),
|
|
134
|
+
json!({"name": "Alice", "balance": 100}),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
let hash1 = state1.hash();
|
|
138
|
+
let hash2 = state2.hash();
|
|
139
|
+
|
|
140
|
+
assert_eq!(hash1, hash2, "Same state must produce same hash");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
#[test]
|
|
144
|
+
fn test_state_hash_collision_resistant() {
|
|
145
|
+
let mut state1 = CanonicalState::new();
|
|
146
|
+
state1.set_record(
|
|
147
|
+
"users".to_string(),
|
|
148
|
+
"user1".to_string(),
|
|
149
|
+
json!({"name": "Alice", "balance": 100}),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
let mut state2 = CanonicalState::new();
|
|
153
|
+
state2.set_record(
|
|
154
|
+
"users".to_string(),
|
|
155
|
+
"user1".to_string(),
|
|
156
|
+
json!({"name": "Alice", "balance": 101}),
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
let hash1 = state1.hash();
|
|
160
|
+
let hash2 = state2.hash();
|
|
161
|
+
|
|
162
|
+
assert_ne!(hash1, hash2, "Different state must produce different hash");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#[test]
|
|
166
|
+
fn test_state_hash_field_order_invariant() {
|
|
167
|
+
let mut state1 = CanonicalState::new();
|
|
168
|
+
state1.set_record(
|
|
169
|
+
"users".to_string(),
|
|
170
|
+
"user1".to_string(),
|
|
171
|
+
json!({"name": "Alice", "balance": 100}),
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
let mut state2 = CanonicalState::new();
|
|
175
|
+
state2.set_record(
|
|
176
|
+
"users".to_string(),
|
|
177
|
+
"user1".to_string(),
|
|
178
|
+
json!({"balance": 100, "name": "Alice"}),
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
let hash1 = state1.hash();
|
|
182
|
+
let hash2 = state2.hash();
|
|
183
|
+
|
|
184
|
+
assert_eq!(hash1, hash2, "Field order must not affect hash");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[test]
|
|
188
|
+
fn test_state_hash_multiple_records() {
|
|
189
|
+
let mut state = CanonicalState::new();
|
|
190
|
+
state.set_record(
|
|
191
|
+
"users".to_string(),
|
|
192
|
+
"user1".to_string(),
|
|
193
|
+
json!({"name": "Alice"}),
|
|
194
|
+
);
|
|
195
|
+
state.set_record(
|
|
196
|
+
"users".to_string(),
|
|
197
|
+
"user2".to_string(),
|
|
198
|
+
json!({"name": "Bob"}),
|
|
199
|
+
);
|
|
200
|
+
state.set_record(
|
|
201
|
+
"accounts".to_string(),
|
|
202
|
+
"acc1".to_string(),
|
|
203
|
+
json!({"balance": 1000}),
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
let hash = state.hash();
|
|
207
|
+
|
|
208
|
+
assert_eq!(hash.as_hex().len(), 64, "SHA-256 hex is 64 chars");
|
|
209
|
+
assert!(hash.verify(&state.to_canonical_json().to_string()));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
#[test]
|
|
213
|
+
fn test_state_hash_record_update() {
|
|
214
|
+
let mut state = CanonicalState::new();
|
|
215
|
+
state.set_record(
|
|
216
|
+
"users".to_string(),
|
|
217
|
+
"user1".to_string(),
|
|
218
|
+
json!({"balance": 100}),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
let hash_before = state.hash();
|
|
222
|
+
|
|
223
|
+
state.set_record(
|
|
224
|
+
"users".to_string(),
|
|
225
|
+
"user1".to_string(),
|
|
226
|
+
json!({"balance": 200}),
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
let hash_after = state.hash();
|
|
230
|
+
|
|
231
|
+
assert_ne!(hash_before, hash_after, "Hash changes when state updates");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#[test]
|
|
235
|
+
fn test_state_hash_empty_state() {
|
|
236
|
+
let state = CanonicalState::new();
|
|
237
|
+
let hash = state.hash();
|
|
238
|
+
|
|
239
|
+
assert_eq!(hash.as_hex().len(), 64);
|
|
240
|
+
assert!(hash.verify(&state.to_canonical_json().to_string()));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
#[test]
|
|
244
|
+
fn test_state_hash_verify() {
|
|
245
|
+
let mut state = CanonicalState::new();
|
|
246
|
+
state.set_record(
|
|
247
|
+
"users".to_string(),
|
|
248
|
+
"user1".to_string(),
|
|
249
|
+
json!({"name": "Alice"}),
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
let hash = state.hash();
|
|
253
|
+
let canonical_json = state.to_canonical_json().to_string();
|
|
254
|
+
|
|
255
|
+
assert!(hash.verify(&canonical_json));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#[test]
|
|
259
|
+
fn test_canonical_json_ordering() {
|
|
260
|
+
let mut state1 = CanonicalState::new();
|
|
261
|
+
state1.set_record("a".to_string(), "1".to_string(), json!(1));
|
|
262
|
+
state1.set_record("c".to_string(), "3".to_string(), json!(3));
|
|
263
|
+
state1.set_record("b".to_string(), "2".to_string(), json!(2));
|
|
264
|
+
|
|
265
|
+
let mut state2 = CanonicalState::new();
|
|
266
|
+
state2.set_record("c".to_string(), "3".to_string(), json!(3));
|
|
267
|
+
state2.set_record("a".to_string(), "1".to_string(), json!(1));
|
|
268
|
+
state2.set_record("b".to_string(), "2".to_string(), json!(2));
|
|
269
|
+
|
|
270
|
+
let json1 = state1.to_canonical_json().to_string();
|
|
271
|
+
let json2 = state2.to_canonical_json().to_string();
|
|
272
|
+
|
|
273
|
+
assert_eq!(
|
|
274
|
+
json1, json2,
|
|
275
|
+
"Insertion order must not affect canonical JSON"
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
#[test]
|
|
280
|
+
fn test_state_size_bytes() {
|
|
281
|
+
let mut state = CanonicalState::new();
|
|
282
|
+
state.set_record(
|
|
283
|
+
"users".to_string(),
|
|
284
|
+
"user1".to_string(),
|
|
285
|
+
json!({"name": "Alice", "balance": 100}),
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
let size = state.size_bytes();
|
|
289
|
+
assert!(size > 0);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/// State Transition Store
|
|
2
|
+
///
|
|
3
|
+
/// Persistent storage of executed state transitions.
|
|
4
|
+
/// Transitions are immutable once committed; this is the authoritative record of
|
|
5
|
+
/// what state changes have occurred and in what order.
|
|
6
|
+
|
|
7
|
+
use crate::transactions::{StateTransition, StateVersion};
|
|
8
|
+
use std::collections::HashMap;
|
|
9
|
+
use std::sync::{Arc, Mutex};
|
|
10
|
+
|
|
11
|
+
/// Persisted state transition record
|
|
12
|
+
#[derive(Clone, Debug)]
|
|
13
|
+
pub struct StateTransitionRecord {
|
|
14
|
+
pub store_index: u64,
|
|
15
|
+
pub transition: StateTransition,
|
|
16
|
+
pub timestamp_ms: u64,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// In-memory state transition store (for testing/development)
|
|
20
|
+
/// For production, implement FileStateTransitionStore or DatabaseStateTransitionStore
|
|
21
|
+
#[derive(Clone)]
|
|
22
|
+
pub struct MemoryStateTransitionStore {
|
|
23
|
+
records: Arc<Mutex<Vec<StateTransitionRecord>>>,
|
|
24
|
+
by_transaction_id: Arc<Mutex<HashMap<String, u64>>>,
|
|
25
|
+
by_state_version: Arc<Mutex<HashMap<String, u64>>>,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
impl MemoryStateTransitionStore {
|
|
29
|
+
pub fn new() -> Self {
|
|
30
|
+
Self {
|
|
31
|
+
records: Arc::new(Mutex::new(Vec::new())),
|
|
32
|
+
by_transaction_id: Arc::new(Mutex::new(HashMap::new())),
|
|
33
|
+
by_state_version: Arc::new(Mutex::new(HashMap::new())),
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Record a committed state transition
|
|
38
|
+
/// Returns the store index assigned to this transition
|
|
39
|
+
pub fn record_transition(&self, transition: StateTransition) -> Result<u64, String> {
|
|
40
|
+
let mut records = self.records.lock().unwrap();
|
|
41
|
+
let mut by_txn = self.by_transaction_id.lock().unwrap();
|
|
42
|
+
let mut by_version = self.by_state_version.lock().unwrap();
|
|
43
|
+
|
|
44
|
+
// Ensure transition is successful before recording
|
|
45
|
+
if !transition.is_successful() {
|
|
46
|
+
return Err("Cannot record unsuccessful transition".to_string());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Index by transaction_id if present
|
|
50
|
+
if let Some(ref txn_id) = transition.transaction_id {
|
|
51
|
+
if by_txn.contains_key(txn_id) {
|
|
52
|
+
return Err(format!("Transaction already recorded: {}", txn_id));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let store_index = records.len() as u64;
|
|
57
|
+
let timestamp_ms = std::time::SystemTime::now()
|
|
58
|
+
.duration_since(std::time::UNIX_EPOCH)
|
|
59
|
+
.unwrap()
|
|
60
|
+
.as_millis() as u64;
|
|
61
|
+
|
|
62
|
+
let record = StateTransitionRecord {
|
|
63
|
+
store_index,
|
|
64
|
+
transition: transition.clone(),
|
|
65
|
+
timestamp_ms,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// Index by transaction_id
|
|
69
|
+
if let Some(ref txn_id) = transition.transaction_id {
|
|
70
|
+
by_txn.insert(txn_id.clone(), store_index);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Index by to_version (state_hash for quick lookup)
|
|
74
|
+
by_version.insert(transition.to_version.state_hash.clone(), store_index);
|
|
75
|
+
|
|
76
|
+
records.push(record);
|
|
77
|
+
|
|
78
|
+
Ok(store_index)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Get a transition by transaction ID
|
|
82
|
+
pub fn get_by_transaction_id(&self, transaction_id: &str) -> Option<StateTransitionRecord> {
|
|
83
|
+
let by_txn = self.by_transaction_id.lock().unwrap();
|
|
84
|
+
if let Some(&store_index) = by_txn.get(transaction_id) {
|
|
85
|
+
let records = self.records.lock().unwrap();
|
|
86
|
+
records.get(store_index as usize).cloned()
|
|
87
|
+
} else {
|
|
88
|
+
None
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// Get a transition by store index
|
|
93
|
+
pub fn get_by_index(&self, store_index: u64) -> Option<StateTransitionRecord> {
|
|
94
|
+
let records = self.records.lock().unwrap();
|
|
95
|
+
records.get(store_index as usize).cloned()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Get transitions in range [start_index, end_index)
|
|
99
|
+
pub fn get_range(&self, start_index: u64, end_index: u64) -> Vec<StateTransitionRecord> {
|
|
100
|
+
let records = self.records.lock().unwrap();
|
|
101
|
+
records
|
|
102
|
+
.iter()
|
|
103
|
+
.skip(start_index as usize)
|
|
104
|
+
.take((end_index - start_index) as usize)
|
|
105
|
+
.cloned()
|
|
106
|
+
.collect()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Find transitions that led from a specific state version
|
|
110
|
+
pub fn get_from_version(&self, state_hash: &str) -> Vec<StateTransitionRecord> {
|
|
111
|
+
let records = self.records.lock().unwrap();
|
|
112
|
+
records
|
|
113
|
+
.iter()
|
|
114
|
+
.filter(|r| r.transition.from_version.state_hash == state_hash)
|
|
115
|
+
.cloned()
|
|
116
|
+
.collect()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/// Find the transition that led to a specific state version
|
|
120
|
+
pub fn get_to_version(&self, state_hash: &str) -> Option<StateTransitionRecord> {
|
|
121
|
+
let by_version = self.by_state_version.lock().unwrap();
|
|
122
|
+
if let Some(&store_index) = by_version.get(state_hash) {
|
|
123
|
+
let records = self.records.lock().unwrap();
|
|
124
|
+
records.get(store_index as usize).cloned()
|
|
125
|
+
} else {
|
|
126
|
+
None
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// Get the current size of the store (number of transitions)
|
|
131
|
+
pub fn size(&self) -> u64 {
|
|
132
|
+
let records = self.records.lock().unwrap();
|
|
133
|
+
records.len() as u64
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// Iterate over all transitions in the store
|
|
137
|
+
pub fn iter(&self) -> Vec<StateTransitionRecord> {
|
|
138
|
+
let records = self.records.lock().unwrap();
|
|
139
|
+
records.clone()
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// Check if a transaction has been recorded
|
|
143
|
+
pub fn contains_transaction_id(&self, transaction_id: &str) -> bool {
|
|
144
|
+
let by_txn = self.by_transaction_id.lock().unwrap();
|
|
145
|
+
by_txn.contains_key(transaction_id)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// Get the chain of transitions from a starting state to the current state
|
|
149
|
+
/// Returns transitions in order
|
|
150
|
+
pub fn get_transition_chain(&self, from_state_hash: &str) -> Vec<StateTransitionRecord> {
|
|
151
|
+
let records = self.records.lock().unwrap();
|
|
152
|
+
let mut chain = Vec::new();
|
|
153
|
+
let mut current_hash = from_state_hash.to_string();
|
|
154
|
+
|
|
155
|
+
for record in records.iter() {
|
|
156
|
+
if record.transition.from_version.state_hash == current_hash {
|
|
157
|
+
current_hash = record.transition.to_version.state_hash.clone();
|
|
158
|
+
chain.push(record.clone());
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
chain
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// Truncate the store at the given index (for recovery/compaction)
|
|
166
|
+
/// Keeps records with store_index < truncate_at
|
|
167
|
+
pub fn truncate(&self, truncate_at: u64) -> Result<(), String> {
|
|
168
|
+
let mut records = self.records.lock().unwrap();
|
|
169
|
+
let mut by_txn = self.by_transaction_id.lock().unwrap();
|
|
170
|
+
let mut by_version = self.by_state_version.lock().unwrap();
|
|
171
|
+
|
|
172
|
+
if truncate_at > records.len() as u64 {
|
|
173
|
+
return Err("Truncate index beyond store size".to_string());
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
let removed: Vec<_> = records.drain(truncate_at as usize..).collect();
|
|
177
|
+
for record in removed {
|
|
178
|
+
if let Some(ref txn_id) = record.transition.transaction_id {
|
|
179
|
+
by_txn.remove(txn_id);
|
|
180
|
+
}
|
|
181
|
+
by_version.remove(&record.transition.to_version.state_hash);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
Ok(())
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
impl Default for MemoryStateTransitionStore {
|
|
189
|
+
fn default() -> Self {
|
|
190
|
+
Self::new()
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#[cfg(test)]
|
|
195
|
+
mod tests {
|
|
196
|
+
use super::*;
|
|
197
|
+
use crate::convergence::VectorClock;
|
|
198
|
+
use crate::transactions::{ConsistencyContract, OperationCommand, OperationId, Operation};
|
|
199
|
+
use std::collections::HashMap;
|
|
200
|
+
|
|
201
|
+
fn create_test_transition(from_hash: &str, to_hash: &str, txn_id: Option<&str>) -> StateTransition {
|
|
202
|
+
let mut from_vc = VectorClock::new();
|
|
203
|
+
from_vc.increment("node1");
|
|
204
|
+
let from_version = StateVersion::new(from_vc, from_hash.to_string());
|
|
205
|
+
|
|
206
|
+
let mut to_vc = VectorClock::new();
|
|
207
|
+
to_vc.increment("node1");
|
|
208
|
+
to_vc.increment("node1");
|
|
209
|
+
let to_version = StateVersion::new(to_vc, to_hash.to_string());
|
|
210
|
+
|
|
211
|
+
let mut op = Operation::new(
|
|
212
|
+
OperationId::new("node1".to_string(), 1),
|
|
213
|
+
from_version.clone(),
|
|
214
|
+
"Test".to_string(),
|
|
215
|
+
OperationCommand {
|
|
216
|
+
op_type: "UpdateField".to_string(),
|
|
217
|
+
collection: "test".to_string(),
|
|
218
|
+
record_id: "rec1".to_string(),
|
|
219
|
+
fields: {
|
|
220
|
+
let mut m = HashMap::new();
|
|
221
|
+
m.insert("field".to_string(), serde_json::json!("value"));
|
|
222
|
+
m
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
"user".to_string(),
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
if let Some(txn_id_str) = txn_id {
|
|
229
|
+
op = op.with_transaction_id(txn_id_str.to_string());
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
StateTransition::new(
|
|
233
|
+
from_version,
|
|
234
|
+
vec![op],
|
|
235
|
+
to_version,
|
|
236
|
+
ConsistencyContract::causal(),
|
|
237
|
+
)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
#[test]
|
|
241
|
+
fn test_record_and_retrieve() {
|
|
242
|
+
let store = MemoryStateTransitionStore::new();
|
|
243
|
+
let transition = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
244
|
+
|
|
245
|
+
let idx = store.record_transition(transition).unwrap();
|
|
246
|
+
|
|
247
|
+
assert_eq!(idx, 0);
|
|
248
|
+
assert!(store.get_by_transaction_id("txn1").is_some());
|
|
249
|
+
assert_eq!(store.size(), 1);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
#[test]
|
|
253
|
+
fn test_duplicate_transaction_rejection() {
|
|
254
|
+
let store = MemoryStateTransitionStore::new();
|
|
255
|
+
let transition1 = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
256
|
+
let transition2 = create_test_transition("hash1", "hash2", Some("txn1"));
|
|
257
|
+
|
|
258
|
+
let _ = store.record_transition(transition1).unwrap();
|
|
259
|
+
let result = store.record_transition(transition2);
|
|
260
|
+
|
|
261
|
+
assert!(result.is_err());
|
|
262
|
+
assert!(result.unwrap_err().contains("already recorded"));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
#[test]
|
|
266
|
+
fn test_get_by_index() {
|
|
267
|
+
let store = MemoryStateTransitionStore::new();
|
|
268
|
+
let transition1 = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
269
|
+
let transition2 = create_test_transition("hash1", "hash2", Some("txn2"));
|
|
270
|
+
|
|
271
|
+
store.record_transition(transition1).unwrap();
|
|
272
|
+
store.record_transition(transition2).unwrap();
|
|
273
|
+
|
|
274
|
+
let record1 = store.get_by_index(0).unwrap();
|
|
275
|
+
assert_eq!(record1.store_index, 0);
|
|
276
|
+
|
|
277
|
+
let record2 = store.get_by_index(1).unwrap();
|
|
278
|
+
assert_eq!(record2.store_index, 1);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
#[test]
|
|
282
|
+
fn test_get_from_version() {
|
|
283
|
+
let store = MemoryStateTransitionStore::new();
|
|
284
|
+
let transition1 = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
285
|
+
let transition2 = create_test_transition("hash0", "hash2", Some("txn2"));
|
|
286
|
+
let transition3 = create_test_transition("hash1", "hash3", Some("txn3"));
|
|
287
|
+
|
|
288
|
+
store.record_transition(transition1).unwrap();
|
|
289
|
+
store.record_transition(transition2).unwrap();
|
|
290
|
+
store.record_transition(transition3).unwrap();
|
|
291
|
+
|
|
292
|
+
let from_hash0 = store.get_from_version("hash0");
|
|
293
|
+
assert_eq!(from_hash0.len(), 2);
|
|
294
|
+
|
|
295
|
+
let from_hash1 = store.get_from_version("hash1");
|
|
296
|
+
assert_eq!(from_hash1.len(), 1);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
#[test]
|
|
300
|
+
fn test_get_to_version() {
|
|
301
|
+
let store = MemoryStateTransitionStore::new();
|
|
302
|
+
let transition = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
303
|
+
|
|
304
|
+
store.record_transition(transition).unwrap();
|
|
305
|
+
|
|
306
|
+
let record = store.get_to_version("hash1").unwrap();
|
|
307
|
+
assert_eq!(record.transition.to_version.state_hash, "hash1");
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
#[test]
|
|
311
|
+
fn test_get_transition_chain() {
|
|
312
|
+
let store = MemoryStateTransitionStore::new();
|
|
313
|
+
let t1 = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
314
|
+
let t2 = create_test_transition("hash1", "hash2", Some("txn2"));
|
|
315
|
+
let t3 = create_test_transition("hash2", "hash3", Some("txn3"));
|
|
316
|
+
|
|
317
|
+
store.record_transition(t1).unwrap();
|
|
318
|
+
store.record_transition(t2).unwrap();
|
|
319
|
+
store.record_transition(t3).unwrap();
|
|
320
|
+
|
|
321
|
+
let chain = store.get_transition_chain("hash0");
|
|
322
|
+
assert_eq!(chain.len(), 3);
|
|
323
|
+
assert_eq!(chain[0].transition.to_version.state_hash, "hash1");
|
|
324
|
+
assert_eq!(chain[1].transition.to_version.state_hash, "hash2");
|
|
325
|
+
assert_eq!(chain[2].transition.to_version.state_hash, "hash3");
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
#[test]
|
|
329
|
+
fn test_contains_transaction_id() {
|
|
330
|
+
let store = MemoryStateTransitionStore::new();
|
|
331
|
+
let transition = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
332
|
+
|
|
333
|
+
store.record_transition(transition).unwrap();
|
|
334
|
+
|
|
335
|
+
assert!(store.contains_transaction_id("txn1"));
|
|
336
|
+
assert!(!store.contains_transaction_id("txn999"));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
#[test]
|
|
340
|
+
fn test_truncate() {
|
|
341
|
+
let store = MemoryStateTransitionStore::new();
|
|
342
|
+
let t1 = create_test_transition("hash0", "hash1", Some("txn1"));
|
|
343
|
+
let t2 = create_test_transition("hash1", "hash2", Some("txn2"));
|
|
344
|
+
let t3 = create_test_transition("hash2", "hash3", Some("txn3"));
|
|
345
|
+
|
|
346
|
+
store.record_transition(t1).unwrap();
|
|
347
|
+
store.record_transition(t2).unwrap();
|
|
348
|
+
store.record_transition(t3).unwrap();
|
|
349
|
+
|
|
350
|
+
assert_eq!(store.size(), 3);
|
|
351
|
+
store.truncate(2).unwrap();
|
|
352
|
+
assert_eq!(store.size(), 2);
|
|
353
|
+
|
|
354
|
+
assert!(store.contains_transaction_id("txn1"));
|
|
355
|
+
assert!(store.contains_transaction_id("txn2"));
|
|
356
|
+
assert!(!store.contains_transaction_id("txn3"));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
#[test]
|
|
360
|
+
fn test_range_query() {
|
|
361
|
+
let store = MemoryStateTransitionStore::new();
|
|
362
|
+
for i in 0..5 {
|
|
363
|
+
let t = create_test_transition(
|
|
364
|
+
&format!("hash{}", i),
|
|
365
|
+
&format!("hash{}", i + 1),
|
|
366
|
+
Some(&format!("txn{}", i)),
|
|
367
|
+
);
|
|
368
|
+
store.record_transition(t).unwrap();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
let range = store.get_range(1, 4);
|
|
372
|
+
assert_eq!(range.len(), 3);
|
|
373
|
+
assert_eq!(range[0].store_index, 1);
|
|
374
|
+
assert_eq!(range[2].store_index, 3);
|
|
375
|
+
}
|
|
376
|
+
}
|