Strand
Append and Read
Append nonempty blocks and inspect their actual signed headers.
Strand::new chooses file storage when a path is configured and memory storage otherwise. Appends return a zero-based sequence number. A header contains version, sequence, length, byte offset, previous-block hash, writer public key, timestamp and signature; it does not directly contain the Merkle root.
This example is checked against source signatures; it was not compiled or run during this documentation pass.
use strand::{Strand, StrandConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempfile::tempdir()?;
let cfg = StrandConfig::new().with_storage(dir.path());
let mut strand = Strand::new(cfg).await?;
// Append a batch of blocks; the returned seq is the new tail index.
for (i, payload) in ["genesis", "block-one", "block-two"].iter().enumerate() {
let seq = strand.append(payload.as_bytes()).await?;
assert_eq!(seq, i as u64);
}
// Random-access read returns the exact bytes that were appended.
let block_one = strand.get(1).await?;
assert_eq!(block_one, b"block-one");
// Read the signed block header separately from its payload.
let header = strand.get_header(2).await?;
println!(
"strand len={} latest_seq={} discovery_key={}",
strand.len(),
2,
hex::encode(strand.discovery_key()),
);
println!(
"header seq={} length={} writer={}",
header.seq,
header.length,
hex::encode(header.writer_public_key),
);
Ok(())
}