Encodes this span directly into its raw wire format. [`crate::Batch::encode`] and protocol-level fixture builders call this after writing the batch type byte. Encoding from the derived caches avoids cloning them into an owned [`crate::RawSpanBatch`]. Returns [`SpanBatchError::EmptySpanBatch`] when no block has been appended.
(&self, w: &mut dyn bytes::BufMut)
| 250 | /// |
| 251 | /// Returns [`SpanBatchError::EmptySpanBatch`] when no block has been appended. |
| 252 | pub fn encode(&self, w: &mut dyn bytes::BufMut) -> Result<(), SpanBatchError> { |
| 253 | if self.batches.is_empty() { |
| 254 | return Err(SpanBatchError::EmptySpanBatch); |
| 255 | } |
| 256 | let span_start = &self.batches[0]; |
| 257 | let span_end = &self.batches[self.batches.len() - 1]; |
| 258 | |
| 259 | // The wire prefix anchors the span to its first L2 timestamp, parent, |
| 260 | // and final L1 origin. |
| 261 | SpanBatchPrefix { |
| 262 | rel_timestamp: span_start.timestamp - self.genesis_timestamp, |
| 263 | l1_origin_num: span_end.epoch_num, |
| 264 | parent_check: self.parent_check, |
| 265 | l1_origin_check: self.l1_origin_check, |
| 266 | } |
| 267 | .encode_prefix(w); |
| 268 | |
| 269 | // Encode directly from the derived caches instead of cloning them into |
| 270 | // an intermediate RawSpanBatch payload. |
| 271 | let block_count = self.batches.len(); |
| 272 | let mut varint_buf = [0u8; 10]; |
| 273 | w.put_slice(unsigned_varint::encode::u64(block_count as u64, &mut varint_buf)); |
| 274 | SpanBatchBits::encode(w, block_count, &self.origin_bits)?; |
| 275 | for block_tx_count in &self.block_tx_counts { |
| 276 | w.put_slice(unsigned_varint::encode::u64(*block_tx_count, &mut varint_buf)); |
| 277 | } |
| 278 | self.txs.encode(w) |
| 279 | } |
| 280 | |
| 281 | /// Converts all [`SpanBatchElement`]s after the L2 safe head to [`SingleBatch`]es. The |
| 282 | /// resulting [`SingleBatch`]es do not contain a parent hash, as it is populated by the |