block.rs 1.6 KB

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