|
| 1 | +#[cfg(feature = "async-std")] |
| 2 | +use async_std::main as async_main; |
| 3 | +use hypercore::{ |
| 4 | + Hypercore, HypercoreBuilder, HypercoreError, PartialKeypair, RequestBlock, RequestUpgrade, |
| 5 | + Storage, |
| 6 | +}; |
| 7 | +use random_access_disk::RandomAccessDisk; |
| 8 | +use random_access_memory::RandomAccessMemory; |
| 9 | +use tempfile::Builder; |
| 10 | +#[cfg(feature = "tokio")] |
| 11 | +use tokio::main as async_main; |
| 12 | + |
| 13 | +/// Example on how to replicate a (disk) hypercore to another (memory) hypercore. |
| 14 | +/// NB: The replication functions used here are low-level, built for use in the wire |
| 15 | +/// protocol. |
| 16 | +#[async_main] |
| 17 | +async fn main() { |
| 18 | + // For the purposes of this example, first create a |
| 19 | + // temporary directory to hold hypercore. |
| 20 | + let dir = Builder::new() |
| 21 | + .prefix("examples_replication") |
| 22 | + .tempdir() |
| 23 | + .unwrap() |
| 24 | + .into_path(); |
| 25 | + |
| 26 | + // Create a disk storage, overwriting existing values. |
| 27 | + let overwrite = true; |
| 28 | + let storage = Storage::new_disk(&dir, overwrite) |
| 29 | + .await |
| 30 | + .expect("Could not create disk storage"); |
| 31 | + |
| 32 | + // Build a new disk hypercore |
| 33 | + let mut origin_hypercore = HypercoreBuilder::new(storage) |
| 34 | + .build() |
| 35 | + .await |
| 36 | + .expect("Could not create disk hypercore"); |
| 37 | + |
| 38 | + // Append values to the hypercore |
| 39 | + let batch: &[&[u8]] = &[b"Hello, ", b"from ", b"replicated ", b"hypercore!"]; |
| 40 | + origin_hypercore.append_batch(batch).await.unwrap(); |
| 41 | + |
| 42 | + // Store the public key |
| 43 | + let origin_public_key = origin_hypercore.key_pair().public; |
| 44 | + |
| 45 | + // Create a peer of the origin hypercore using the public key |
| 46 | + let mut replicated_hypercore = HypercoreBuilder::new( |
| 47 | + Storage::new_memory() |
| 48 | + .await |
| 49 | + .expect("Could not create memory storage"), |
| 50 | + ) |
| 51 | + .key_pair(PartialKeypair { |
| 52 | + public: origin_public_key, |
| 53 | + secret: None, |
| 54 | + }) |
| 55 | + .build() |
| 56 | + .await |
| 57 | + .expect("Could not create memory hypercore"); |
| 58 | + |
| 59 | + // Replicate the four values in random order |
| 60 | + replicate_index(&mut origin_hypercore, &mut replicated_hypercore, 3).await; |
| 61 | + replicate_index(&mut origin_hypercore, &mut replicated_hypercore, 0).await; |
| 62 | + replicate_index(&mut origin_hypercore, &mut replicated_hypercore, 2).await; |
| 63 | + replicate_index(&mut origin_hypercore, &mut replicated_hypercore, 1).await; |
| 64 | + |
| 65 | + // Print values from replicated hypercore, converting binary back to string |
| 66 | + println!( |
| 67 | + "{}{}{}{}", |
| 68 | + format_res(replicated_hypercore.get(0).await), |
| 69 | + format_res(replicated_hypercore.get(1).await), |
| 70 | + format_res(replicated_hypercore.get(2).await), |
| 71 | + format_res(replicated_hypercore.get(3).await) |
| 72 | + ); // prints "Hello, from replicated hypercore!" |
| 73 | +} |
| 74 | + |
| 75 | +async fn replicate_index( |
| 76 | + origin_hypercore: &mut Hypercore<RandomAccessDisk>, |
| 77 | + replicated_hypercore: &mut Hypercore<RandomAccessMemory>, |
| 78 | + request_index: u64, |
| 79 | +) { |
| 80 | + let missing_nodes = origin_hypercore |
| 81 | + .missing_nodes(request_index) |
| 82 | + .await |
| 83 | + .expect("Could not get missing nodes"); |
| 84 | + let upgrade_start = replicated_hypercore.info().contiguous_length; |
| 85 | + let upgrade_length = origin_hypercore.info().contiguous_length - upgrade_start; |
| 86 | + |
| 87 | + let proof = origin_hypercore |
| 88 | + .create_proof( |
| 89 | + Some(RequestBlock { |
| 90 | + index: request_index, |
| 91 | + nodes: missing_nodes, |
| 92 | + }), |
| 93 | + None, |
| 94 | + None, |
| 95 | + Some(RequestUpgrade { |
| 96 | + start: upgrade_start, |
| 97 | + length: upgrade_length, |
| 98 | + }), |
| 99 | + ) |
| 100 | + .await |
| 101 | + .expect("Creating proof error") |
| 102 | + .expect("Could not get proof"); |
| 103 | + // Then the proof is verified and applied to the replicated party. |
| 104 | + assert!(replicated_hypercore |
| 105 | + .verify_and_apply_proof(&proof) |
| 106 | + .await |
| 107 | + .expect("Verifying and applying proof failed")); |
| 108 | +} |
| 109 | + |
| 110 | +fn format_res(res: Result<Option<Vec<u8>>, HypercoreError>) -> String { |
| 111 | + match res { |
| 112 | + Ok(Some(bytes)) => String::from_utf8(bytes).expect("Shouldn't fail in example"), |
| 113 | + Ok(None) => "Got None in feed".to_string(), |
| 114 | + Err(e) => format!("Error getting value from feed, reason = {e:?}"), |
| 115 | + } |
| 116 | +} |
0 commit comments