block.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use std::hash::{Hash, Hasher};
  2. use super::metadata::Metadata;
  3. use darkfi::{tx::Transaction, util::serial::Encodable};
  4. /// This struct represents a tuple of the form (st, sl, txs, metadata).
  5. /// Each blocks parent hash h may be computed simply as a hash of the parent block.
  6. #[derive(Debug, Clone)]
  7. pub struct Block {
  8. /// Previous block hash
  9. pub st: String,
  10. /// Slot uid, generated by the beacon
  11. pub sl: u64,
  12. /// Transactions payload
  13. pub txs: Vec<Transaction>,
  14. /// Additional block information
  15. pub metadata: Metadata,
  16. }
  17. impl Block {
  18. pub fn new(
  19. st: String,
  20. sl: u64,
  21. txs: Vec<Transaction>,
  22. proof: String,
  23. r: String,
  24. s: String,
  25. ) -> Block {
  26. Block { st, sl, txs, metadata: Metadata::new(proof, r, s) }
  27. }
  28. pub fn signature_encode(&self) -> Vec<u8> {
  29. let mut encoded_block = Vec::new();
  30. let mut len = 0;
  31. len += self.st.encode(&mut encoded_block).unwrap();
  32. len += self.sl.encode(&mut encoded_block).unwrap();
  33. len += self.txs.encode(&mut encoded_block).unwrap();
  34. assert_eq!(len, encoded_block.len());
  35. encoded_block
  36. }
  37. }
  38. impl PartialEq for Block {
  39. fn eq(&self, other: &Self) -> bool {
  40. self.st == other.st && self.sl == other.sl && self.txs == other.txs
  41. }
  42. }
  43. impl Hash for Block {
  44. fn hash<H: Hasher>(&self, hasher: &mut H) {
  45. format!("{:?}{:?}{:?}", self.st, self.sl, self.txs).hash(hasher);
  46. }
  47. }