hyli-model 0.14.0

Hyli datamodel
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::collections::BTreeMap;
use std::sync::RwLock;

use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use sha3::{Digest, Sha3_256};
use strum::IntoDiscriminant;
use strum_macros::{EnumDiscriminants, IntoStaticStr};
use utoipa::{
    openapi::{ArrayBuilder, ObjectBuilder, RefOr, Schema},
    PartialSchema, ToSchema,
};

use crate::{api::APIRegisterContract, *};

#[derive(
    Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize,
)]
pub struct Transaction {
    pub version: u32,
    pub transaction_data: TransactionData,
}

impl Transaction {
    pub fn metadata(&self, parent_data_proposal_hash: DataProposalHash) -> TransactionMetadata {
        TransactionMetadata {
            version: self.version,
            transaction_kind: self.transaction_data.discriminant(),
            id: TxId(parent_data_proposal_hash, self.hashed()),
        }
    }
}

impl DataSized for Transaction {
    fn estimate_size(&self) -> usize {
        match &self.transaction_data {
            TransactionData::Blob(tx) => tx.estimate_size(),
            TransactionData::Proof(tx) => tx.estimate_size(),
            TransactionData::VerifiedProof(tx) => tx.proof_size,
        }
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)]
pub struct TransactionMetadata {
    pub version: u32,
    pub transaction_kind: TransactionKind,
    pub id: TxId,
}

#[derive(
    EnumDiscriminants,
    Debug,
    Serialize,
    Deserialize,
    Clone,
    PartialEq,
    Eq,
    BorshSerialize,
    BorshDeserialize,
    IntoStaticStr,
)]
#[strum_discriminants(derive(Default, BorshSerialize, BorshDeserialize))]
#[strum_discriminants(name(TransactionKind))]
pub enum TransactionData {
    #[strum_discriminants(default)]
    Blob(BlobTransaction),
    Proof(ProofTransaction),
    VerifiedProof(VerifiedProofTransaction),
}

impl Default for TransactionData {
    fn default() -> Self {
        TransactionData::Blob(BlobTransaction::default())
    }
}

#[derive(
    Serialize,
    Deserialize,
    ToSchema,
    Default,
    PartialEq,
    Eq,
    Clone,
    BorshSerialize,
    BorshDeserialize,
)]
pub struct ProofTransaction {
    pub contract_name: ContractName,
    pub program_id: ProgramId,
    pub verifier: Verifier,
    pub proof: ProofData,
}

impl ProofTransaction {
    pub fn estimate_size(&self) -> usize {
        borsh::to_vec(self).unwrap_or_default().len()
    }
}

#[derive(
    Default, Serialize, Deserialize, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize,
)]
pub struct VerifiedProofTransaction {
    pub contract_name: ContractName,
    pub program_id: ProgramId,
    pub verifier: Verifier,
    pub proof: Option<ProofData>, // Kept only on the local lane for indexing purposes
    pub proof_hash: ProofDataHash,
    pub proof_size: usize,
    pub proven_blobs: Vec<BlobProofOutput>,
    pub is_recursive: bool,
}

impl std::fmt::Debug for VerifiedProofTransaction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VerifiedProofTransaction")
            .field("contract_name", &self.contract_name)
            .field("proof_hash", &self.proof_hash)
            .field("proof_size", &self.proof_size)
            .field("proof", &"[HIDDEN]")
            .field(
                "proof_len",
                &match &self.proof {
                    Some(v) => v.0.len(),
                    None => 0,
                },
            )
            .field("proven_blobs", &self.proven_blobs)
            .finish()
    }
}

impl std::fmt::Debug for ProofTransaction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ProofTransaction")
            .field("contract_name", &self.contract_name)
            .field("proof", &"[HIDDEN]")
            .field("proof_len", &self.proof.0.len())
            .finish()
    }
}

impl Transaction {
    pub fn wrap(data: TransactionData) -> Self {
        Transaction {
            version: 1,
            transaction_data: data,
        }
    }
}

impl From<TransactionData> for Transaction {
    fn from(data: TransactionData) -> Self {
        Transaction::wrap(data)
    }
}

impl From<BlobTransaction> for Transaction {
    fn from(tx: BlobTransaction) -> Self {
        Transaction::wrap(TransactionData::Blob(tx))
    }
}

impl From<ProofTransaction> for Transaction {
    fn from(tx: ProofTransaction) -> Self {
        Transaction::wrap(TransactionData::Proof(tx))
    }
}

impl From<VerifiedProofTransaction> for Transaction {
    fn from(tx: VerifiedProofTransaction) -> Self {
        Transaction::wrap(TransactionData::VerifiedProof(tx))
    }
}

impl Hashed<TxHash> for Transaction {
    fn hashed(&self) -> TxHash {
        match &self.transaction_data {
            TransactionData::Blob(tx) => tx.hashed(),
            TransactionData::Proof(tx) => tx.hashed(),
            TransactionData::VerifiedProof(tx) => tx.hashed(),
        }
    }
}

impl Hashed<TxHash> for ProofTransaction {
    fn hashed(&self) -> TxHash {
        let mut hasher = Sha3_256::new();
        hasher.update(self.contract_name.0.as_bytes());
        hasher.update(self.program_id.0.clone());
        hasher.update(self.verifier.0.as_bytes());
        hasher.update(self.proof.hashed().0);
        let hash_bytes = hasher.finalize();
        TxHash(hash_bytes.to_vec())
    }
}
impl Hashed<TxHash> for VerifiedProofTransaction {
    fn hashed(&self) -> TxHash {
        let mut hasher = Sha3_256::new();
        hasher.update(self.contract_name.0.as_bytes());
        hasher.update(self.program_id.0.clone());
        hasher.update(self.verifier.0.as_bytes());
        hasher.update(&self.proof_hash.0);
        let hash_bytes = hasher.finalize();
        TxHash(hash_bytes.to_vec())
    }
}

#[derive(Serialize, Deserialize, Default, BorshSerialize, BorshDeserialize)]
#[readonly::make]
pub struct BlobTransaction {
    pub identity: Identity,
    pub blobs: Vec<Blob>,
    // FIXME: add a nonce or something to prevent BlobTransaction to share the same hash
    #[borsh(skip)]
    #[serde(skip_serializing, skip_deserializing)]
    hash_cache: RwLock<Option<TxHash>>,
    #[borsh(skip)]
    #[serde(skip_serializing, skip_deserializing)]
    blobshash_cache: RwLock<Option<BlobsHashes>>,
}

impl BlobTransaction {
    pub fn new(identity: impl Into<Identity>, blobs: Vec<Blob>) -> Self {
        BlobTransaction {
            identity: identity.into(),
            blobs,
            hash_cache: RwLock::new(None),
            blobshash_cache: RwLock::new(None),
        }
    }
}

// Custom implem to skip the cached fields
impl std::fmt::Debug for BlobTransaction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BlobTransaction")
            .field("identity", &self.identity)
            .field("blobs", &self.blobs)
            .finish()
    }
}

impl PartialSchema for BlobTransaction {
    fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
        RefOr::T(Schema::Object(
            ObjectBuilder::new()
                .property("identity", Identity::schema())
                .property("blobs", ArrayBuilder::new().items(Blob::schema()).build())
                .required("identity")
                .required("blobs")
                .build(),
        ))
    }
}

impl ToSchema for BlobTransaction {}

impl Clone for BlobTransaction {
    fn clone(&self) -> Self {
        BlobTransaction {
            identity: self.identity.clone(),
            blobs: self.blobs.clone(),
            hash_cache: RwLock::new(self.hash_cache.read().unwrap().clone()),
            blobshash_cache: RwLock::new(self.blobshash_cache.read().unwrap().clone()),
        }
    }
}

impl PartialEq for BlobTransaction {
    fn eq(&self, other: &Self) -> bool {
        self.identity == other.identity && self.blobs == other.blobs
    }
}

impl Eq for BlobTransaction {}

impl BlobTransaction {
    pub fn estimate_size(&self) -> usize {
        borsh::to_vec(self).unwrap_or_default().len()
    }
}

impl Hashed<TxHash> for BlobTransaction {
    fn hashed(&self) -> TxHash {
        if let Some(hash) = self.hash_cache.read().unwrap().clone() {
            return hash;
        }
        let mut hasher = Sha3_256::new();
        hasher.update(self.identity.0.as_bytes());
        for blob in self.blobs.iter() {
            hasher.update(blob.hashed().0);
        }
        let hash_bytes = hasher.finalize();
        let tx_hash = TxHash(hash_bytes.to_vec());
        *self.hash_cache.write().unwrap() = Some(tx_hash.clone());
        tx_hash
    }
}

impl BlobTransaction {
    pub fn blobs_hash(&self) -> BlobsHashes {
        if let Some(hash) = self.blobshash_cache.read().unwrap().clone() {
            return hash;
        }
        let hash: BlobsHashes = (&self.blobs).into();
        self.blobshash_cache.write().unwrap().replace(hash.clone());
        hash
    }

    pub fn validate_identity(&self) -> Result<(), anyhow::Error> {
        // Checks that there is a blob that proves the identity
        let Some((identity, identity_contract_name)) = self.identity.0.rsplit_once("@") else {
            anyhow::bail!("Transaction identity {} is not correctly formed. It should be in the form <id>@<contract_id_name>", self.identity.0);
        };

        if identity.is_empty() || identity_contract_name.is_empty() {
            anyhow::bail!(
                "Transaction identity {}@{} must not have empty parts",
                identity,
                identity_contract_name
            );
        }

        // Check that there is at least one blob that has identity_contract_name as contract name
        if !self
            .blobs
            .iter()
            .any(|blob| blob.contract_name.0 == identity_contract_name)
        {
            anyhow::bail!(
                "Can't find blob that proves the identity on contract '{}'",
                identity_contract_name
            );
        }
        Ok(())
    }
}

impl From<APIRegisterContract> for BlobTransaction {
    fn from(payload: APIRegisterContract) -> Self {
        let mut blobs = vec![RegisterContractAction {
            verifier: payload.verifier,
            program_id: payload.program_id,
            state_commitment: payload.state_commitment,
            contract_name: payload.contract_name.clone(),
            timeout_window: payload
                .timeout_window
                .map(|(a, b)| TimeoutWindow::timeout(BlockHeight(a), BlockHeight(b))),
            constructor_metadata: payload.constructor_metadata.clone(),
        }
        .as_blob("hyli".into())];

        if let Some(constructor_metadata) = &payload.constructor_metadata {
            blobs.push(Blob {
                contract_name: payload.contract_name,
                data: BlobData(constructor_metadata.clone()),
            });
        }

        BlobTransaction::new("hyli@hyli", blobs)
    }
}

#[derive(
    Debug,
    Default,
    Clone,
    Serialize,
    Deserialize,
    ToSchema,
    Eq,
    PartialEq,
    Hash,
    BorshSerialize,
    BorshDeserialize,
)]
pub struct BlobsHashes {
    pub hashes: BTreeMap<BlobIndex, BlobHash>,
}

impl From<&Vec<Blob>> for BlobsHashes {
    fn from(iter: &Vec<Blob>) -> Self {
        BlobsHashes {
            hashes: iter
                .iter()
                .enumerate()
                .map(|(index, blob)| (BlobIndex(index), blob.hashed()))
                .collect(),
        }
    }
}

impl From<&IndexedBlobs> for BlobsHashes {
    fn from(iter: &IndexedBlobs) -> Self {
        BlobsHashes {
            hashes: iter
                .iter()
                .map(|(index, blob)| (*index, blob.hashed()))
                .collect(),
        }
    }
}

impl BlobsHashes {
    pub fn includes_all(&self, other: &BlobsHashes) -> bool {
        for (index, hash) in other.hashes.iter() {
            if !self
                .hashes
                .iter()
                .any(|(other_index, other_hash)| index == other_index && hash == other_hash)
            {
                return false;
            }
        }
        true
    }
}

impl std::fmt::Display for BlobsHashes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (BlobIndex(index), hash) in self.hashes.iter() {
            write!(f, "[{index}]: {hash}")?;
        }
        Ok(())
    }
}