metadata.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. use darkfi_serial::{SerialDecodable, SerialEncodable};
  2. use rand::rngs::OsRng;
  3. use super::Participant;
  4. use crate::{
  5. crypto::{
  6. address::Address,
  7. keypair::Keypair,
  8. lead_proof,
  9. leadcoin::LeadCoin,
  10. proof::{Proof, ProvingKey, VerifyingKey},
  11. schnorr::Signature,
  12. types::*,
  13. },
  14. VerifyResult,
  15. };
  16. /// This struct represents [`Block`](super::Block) information used by the consensus protocol.
  17. #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  18. pub struct Metadata {
  19. /// Block owner signature
  20. pub signature: Signature,
  21. /// Block owner address
  22. pub address: Address,
  23. /// Response of global random oracle, or it's emulation.
  24. pub eta: [u8; 32],
  25. /// Leader NIZK proof
  26. pub proof: LeadProof,
  27. /// Nodes participating in the consensus process
  28. pub participants: Vec<Participant>,
  29. }
  30. impl Default for Metadata {
  31. fn default() -> Self {
  32. let keypair = Keypair::random(&mut OsRng);
  33. let address = Address::from(keypair.public);
  34. let signature = Signature::dummy();
  35. let eta: [u8; 32] = *blake3::hash(b"let there be dark!").as_bytes();
  36. let proof = LeadProof::default();
  37. let participants = vec![];
  38. Self { signature, address, eta, proof, participants }
  39. }
  40. }
  41. impl Metadata {
  42. pub fn new(
  43. signature: Signature,
  44. address: Address,
  45. eta: [u8; 32],
  46. proof: LeadProof,
  47. participants: Vec<Participant>,
  48. ) -> Self {
  49. Self { signature, address, eta, proof, participants }
  50. }
  51. }
  52. /// Wrapper over the Proof, for future additions.
  53. #[derive(Default, Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  54. pub struct LeadProof {
  55. /// Leadership proof
  56. pub proof: Proof,
  57. }
  58. impl LeadProof {
  59. pub fn new(pk: &ProvingKey, coin: LeadCoin) -> Self {
  60. let proof = lead_proof::create_lead_proof(pk, coin).unwrap();
  61. Self { proof }
  62. }
  63. pub fn verify(&self, vk: VerifyingKey, public_inputs: &[DrkCircuitField]) -> VerifyResult<()> {
  64. lead_proof::verify_lead_proof(&vk, &self.proof, public_inputs)
  65. }
  66. }
  67. impl From<Proof> for LeadProof {
  68. fn from(proof: Proof) -> Self {
  69. Self { proof }
  70. }
  71. }