block.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. use serde::{Deserialize, Serialize};
  2. use std::hash::{Hash, Hasher};
  3. use super::{metadata::Metadata, participant::Participant, tx::Tx};
  4. use crate::{
  5. crypto::{keypair::PublicKey, schnorr::Signature},
  6. net,
  7. util::serial::{SerialDecodable, SerialEncodable},
  8. };
  9. /// This struct represents a tuple of the form (st, sl, txs, metadata).
  10. /// Each blocks parent hash h may be computed simply as a hash of the parent block.
  11. #[derive(Debug, Clone, Deserialize, Serialize)]
  12. pub struct Block {
  13. /// Previous block hash
  14. pub st: String, // Change this to a proper hash type
  15. /// Slot uid, generated by the beacon
  16. pub sl: u64,
  17. /// Transactions payload
  18. pub txs: Vec<Tx>,
  19. /// Additional block information
  20. pub metadata: Metadata,
  21. }
  22. impl Block {
  23. pub fn new(
  24. st: String,
  25. sl: u64,
  26. txs: Vec<Tx>,
  27. proof: String,
  28. r: String,
  29. s: String,
  30. participants: Vec<Participant>,
  31. ) -> Block {
  32. Block { st, sl, txs, metadata: Metadata::new(proof, r, s, participants) }
  33. }
  34. }
  35. impl PartialEq for Block {
  36. fn eq(&self, other: &Self) -> bool {
  37. self.st == other.st && self.sl == other.sl && self.txs == other.txs
  38. }
  39. }
  40. impl Hash for Block {
  41. fn hash<H: Hasher>(&self, hasher: &mut H) {
  42. format!("{:?}{:?}{:?}", self.st, self.sl, self.txs).hash(hasher);
  43. }
  44. }
  45. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize, SerialEncodable, SerialDecodable)]
  46. pub struct BlockProposal {
  47. /// leader public key
  48. pub public_key: PublicKey,
  49. /// signed block
  50. pub signature: Signature,
  51. /// leader id
  52. pub id: u64,
  53. /// Previous block hash
  54. pub st: String, // Change this to a proper hash type
  55. /// Slot uid, generated by the beacon
  56. pub sl: u64,
  57. /// Transactions payload
  58. pub txs: Vec<Tx>,
  59. }
  60. impl BlockProposal {
  61. pub fn new(
  62. public_key: PublicKey,
  63. signature: Signature,
  64. id: u64,
  65. st: String,
  66. sl: u64,
  67. txs: Vec<Tx>,
  68. ) -> BlockProposal {
  69. BlockProposal { public_key, signature, id, st, sl, txs }
  70. }
  71. }
  72. impl net::Message for BlockProposal {
  73. fn name() -> &'static str {
  74. "proposal"
  75. }
  76. }
  77. pub fn proposal_eq_block(proposal: &BlockProposal, block: &Block) -> bool {
  78. proposal.st == block.st && proposal.sl == block.sl && proposal.txs == block.txs
  79. }