interplane.Get Omega
Read in your language

spark-rsi-mv

Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger

Brought by drakestapleton

branch main Branches Tags History

spark-rsi-mv / src / ledger / record.rs

148 lines · 4602 bytes Raw Blame History
1use serde::{Deserialize, Serialize};
2use sha2::{Digest, Sha256};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
6pub enum BlockType {
7 Genesis,
8 Evaluation,
9 Promotion,
10 Rollback,
11 PromotionEvidence,
12}
13
14impl std::fmt::Display for BlockType {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 match self {
17 BlockType::Genesis => write!(f, "GENESIS"),
18 BlockType::Evaluation => write!(f, "EVALUATION"),
19 BlockType::Promotion => write!(f, "PROMOTION"),
20 BlockType::Rollback => write!(f, "ROLLBACK"),
21 BlockType::PromotionEvidence => write!(f, "PROMOTION_EVIDENCE"),
22 }
23 }
24}
25
26impl std::str::FromStr for BlockType {
27 type Err = String;
28 fn from_str(s: &str) -> Result<Self, Self::Err> {
29 match s {
30 "GENESIS" => Ok(BlockType::Genesis),
31 "EVALUATION" => Ok(BlockType::Evaluation),
32 "PROMOTION" => Ok(BlockType::Promotion),
33 "ROLLBACK" => Ok(BlockType::Rollback),
34 "PROMOTION_EVIDENCE" => Ok(BlockType::PromotionEvidence),
35 _ => Err(format!("Unknown BlockType: {}", s)),
36 }
37 }
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
41pub struct PromotionEvidencePayload {
42 pub meta_candidate_block_hash: String,
43 pub downstream_cycle_id: String,
44 pub downstream_ledger_block_hash: String,
45 pub capability_improvement_proof: String,
46 pub final_classification: String,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
50pub struct LedgerBlock {
51 pub sequence: u64,
52 pub block_type: BlockType,
53 pub timestamp_utc: String,
54 pub prev_block_hash: String,
55 pub payload_json: String,
56 pub payload_digest: String,
57 pub blob_hashes: Vec<String>,
58 pub block_hash: String,
59}
60
61impl LedgerBlock {
62 pub const GENESIS_PREV_HASH: &'static str =
63 "0000000000000000000000000000000000000000000000000000000000000000";
64
65 pub fn new(
66 sequence: u64,
67 block_type: BlockType,
68 timestamp_utc: String,
69 prev_block_hash: String,
70 payload_json: String,
71 blob_hashes: Vec<String>,
72 ) -> Self {
73 let payload_digest = Self::compute_payload_digest(&payload_json);
74 let block_hash = Self::compute_block_hash(
75 sequence,
76 block_type,
77 &timestamp_utc,
78 &prev_block_hash,
79 &payload_digest,
80 &blob_hashes,
81 );
82
83 Self {
84 sequence,
85 block_type,
86 timestamp_utc,
87 prev_block_hash,
88 payload_json,
89 payload_digest,
90 blob_hashes,
91 block_hash,
92 }
93 }
94
95 pub fn compute_payload_digest(payload_json: &str) -> String {
96 let mut hasher = Sha256::new();
97 hasher.update(payload_json.as_bytes());
98 format!("{:x}", hasher.finalize())
99 }
100
101 pub fn compute_block_hash(
102 sequence: u64,
103 block_type: BlockType,
104 timestamp_utc: &str,
105 prev_block_hash: &str,
106 payload_digest: &str,
107 blob_hashes: &[String],
108 ) -> String {
109 let mut hasher = Sha256::new();
110 hasher.update(sequence.to_le_bytes());
111 hasher.update(block_type.to_string().as_bytes());
112 hasher.update(timestamp_utc.as_bytes());
113 hasher.update(prev_block_hash.as_bytes());
114 hasher.update(payload_digest.as_bytes());
115 for bh in blob_hashes {
116 hasher.update(bh.as_bytes());
117 }
118 format!("{:x}", hasher.finalize())
119 }
120
121 pub fn verify_integrity(&self) -> Result<(), String> {
122 let expected_payload_digest = Self::compute_payload_digest(&self.payload_json);
123 if self.payload_digest != expected_payload_digest {
124 return Err(format!(
125 "Payload digest mismatch at sequence {}: stored={}, computed={}",
126 self.sequence, self.payload_digest, expected_payload_digest
127 ));
128 }
129
130 let expected_block_hash = Self::compute_block_hash(
131 self.sequence,
132 self.block_type,
133 &self.timestamp_utc,
134 &self.prev_block_hash,
135 &self.payload_digest,
136 &self.blob_hashes,
137 );
138
139 if self.block_hash != expected_block_hash {
140 return Err(format!(
141 "Block hash mismatch at sequence {}: stored={}, computed={}",
142 self.sequence, self.block_hash, expected_block_hash
143 ));
144 }
145
146 Ok(())
147 }
148}