|
| 1 | +// Copyright (c) Zefchain Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#![cfg_attr(target_arch = "wasm32", no_main)] |
| 5 | + |
| 6 | +mod state; |
| 7 | + |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use linera_sdk::{ |
| 11 | + linera_base_types::{BlobContent, CryptoHash, DataBlobHash, WithServiceAbi}, |
| 12 | + Service, ServiceRuntime, |
| 13 | +}; |
| 14 | +use publish_read_data_blob::{Operation, PublishReadDataBlobAbi, ServiceQuery}; |
| 15 | + |
| 16 | +pub struct PublishReadDataBlobService { |
| 17 | + runtime: Arc<ServiceRuntime<Self>>, |
| 18 | +} |
| 19 | + |
| 20 | +linera_sdk::service!(PublishReadDataBlobService); |
| 21 | + |
| 22 | +impl WithServiceAbi for PublishReadDataBlobService { |
| 23 | + type Abi = PublishReadDataBlobAbi; |
| 24 | +} |
| 25 | + |
| 26 | +impl Service for PublishReadDataBlobService { |
| 27 | + type Parameters = (); |
| 28 | + |
| 29 | + async fn new(runtime: ServiceRuntime<Self>) -> Self { |
| 30 | + PublishReadDataBlobService { |
| 31 | + runtime: Arc::new(runtime), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + async fn handle_query(&self, query: ServiceQuery) -> Vec<u8> { |
| 36 | + match query { |
| 37 | + ServiceQuery::PublishDataBlob(data) => { |
| 38 | + self.runtime |
| 39 | + .schedule_operation(&Operation::CreateDataBlob(data)); |
| 40 | + Vec::new() |
| 41 | + } |
| 42 | + ServiceQuery::ReadDataBlob(hash, expected_data) => { |
| 43 | + self.runtime |
| 44 | + .schedule_operation(&Operation::ReadDataBlob(hash, expected_data)); |
| 45 | + Vec::new() |
| 46 | + } |
| 47 | + ServiceQuery::PublishAndCreateOneOperation(data) => { |
| 48 | + self.runtime |
| 49 | + .schedule_operation(&Operation::CreateAndReadDataBlob(data)); |
| 50 | + Vec::new() |
| 51 | + } |
| 52 | + ServiceQuery::PublishAndCreateTwoOperations(data) => { |
| 53 | + // First operation: create the blob |
| 54 | + self.runtime |
| 55 | + .schedule_operation(&Operation::CreateDataBlob(data.clone())); |
| 56 | + |
| 57 | + // Compute the blob_id from the data |
| 58 | + let content = BlobContent::new_data(data.clone()); |
| 59 | + let hash = DataBlobHash(CryptoHash::new(&content)); |
| 60 | + |
| 61 | + // Second operation: read the blob with verification |
| 62 | + self.runtime |
| 63 | + .schedule_operation(&Operation::ReadDataBlob(hash, data)); |
| 64 | + Vec::new() |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments