metadata.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use crate::{
  2. util::serial::{deserialize, serialize, SerialDecodable, SerialEncodable},
  3. Result,
  4. };
  5. use super::{participant::Participant, util::Timestamp, vote::Vote};
  6. const SLED_METADATA_TREE: &[u8] = b"_metadata";
  7. /// This struct represents additional Block information used by the consensus protocol.
  8. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  9. pub struct Metadata {
  10. /// Block creation timestamp
  11. pub timestamp: Timestamp,
  12. /// Block information used by Ouroboros consensus
  13. pub om: OuroborosMetadata,
  14. /// Block information used by Streamlet consensus
  15. pub sm: StreamletMetadata,
  16. }
  17. impl Metadata {
  18. pub fn new(
  19. timestamp: Timestamp,
  20. proof: String,
  21. r: String,
  22. s: String,
  23. participants: Vec<Participant>,
  24. ) -> Metadata {
  25. Metadata {
  26. timestamp,
  27. om: OuroborosMetadata::new(proof, r, s),
  28. sm: StreamletMetadata::new(participants),
  29. }
  30. }
  31. }
  32. /// This struct represents Block information used by Ouroboros consensus protocol.
  33. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  34. pub struct OuroborosMetadata {
  35. /// Proof the stakeholder is the block owner
  36. pub proof: String,
  37. /// Random seed for VRF
  38. pub r: String,
  39. /// Block owner signature
  40. pub s: String,
  41. }
  42. impl OuroborosMetadata {
  43. pub fn new(proof: String, r: String, s: String) -> OuroborosMetadata {
  44. OuroborosMetadata { proof, r, s }
  45. }
  46. }
  47. /// This struct represents Block information used by Streamlet consensus protocol.
  48. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  49. pub struct StreamletMetadata {
  50. /// Epoch votes
  51. pub votes: Vec<Vote>,
  52. /// Block notarization flag
  53. pub notarized: bool,
  54. /// Block finalization flag
  55. pub finalized: bool,
  56. /// Nodes participated in the voting process
  57. pub participants: Vec<Participant>,
  58. }
  59. impl StreamletMetadata {
  60. pub fn new(participants: Vec<Participant>) -> StreamletMetadata {
  61. StreamletMetadata { votes: Vec::new(), notarized: false, finalized: false, participants }
  62. }
  63. }
  64. #[derive(Debug)]
  65. pub struct MetadataStore(sled::Tree);
  66. impl MetadataStore {
  67. pub fn new(db: &sled::Db) -> Result<Self> {
  68. let tree = db.open_tree(SLED_METADATA_TREE)?;
  69. Ok(Self(tree))
  70. }
  71. /// Insert metadata into the metadatastore.
  72. /// The block hash for the metadata is used as the key, where value is the serialized metadata.
  73. pub fn insert(&self, metadata: &Metadata, block: blake3::Hash) -> Result<()> {
  74. self.0.insert(block.as_bytes(), serialize(metadata))?;
  75. Ok(())
  76. }
  77. /// Retrieve all metadata.
  78. /// Be carefull as this will try to load everything in memory.
  79. pub fn get_all(&self) -> Result<Vec<Option<(blake3::Hash, Metadata)>>> {
  80. let mut metadata = Vec::new();
  81. let mut iterator = self.0.into_iter().enumerate();
  82. while let Some((_, r)) = iterator.next() {
  83. let (k, v) = r.unwrap();
  84. let hash_bytes: [u8; 32] = k.as_ref().try_into().unwrap();
  85. let m = deserialize(&v)?;
  86. metadata.push(Some((hash_bytes.into(), m)));
  87. }
  88. Ok(metadata)
  89. }
  90. }