lead_info.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_sdk::{
  19. crypto::{schnorr::Signature, Keypair, PublicKey},
  20. pasta::pallas,
  21. };
  22. use darkfi_serial::{SerialDecodable, SerialEncodable};
  23. use log::error;
  24. use crate::{
  25. zk::proof::{Proof, VerifyingKey},
  26. Result,
  27. };
  28. // TODO: Replace 'Lead' terms with 'Producer' to make it more clear that
  29. // we refer to block producer.
  30. /// This struct represents [`Block`](super::Block) leader information used by the consensus protocol.
  31. #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  32. pub struct LeadInfo {
  33. /// Block producer signature
  34. pub signature: Signature,
  35. /// Block producer public_key
  36. pub public_key: PublicKey, // TODO: remove this(to be derived by proof)
  37. /// Block producer slot competing coins public inputs
  38. pub public_inputs: Vec<pallas::Base>,
  39. /// Leader coin creation slot
  40. pub coin_slot: u64,
  41. /// Leader coin creation eta
  42. pub coin_eta: pallas::Base,
  43. /// Leader NIZK proof
  44. pub proof: LeadProof,
  45. /// Slot offset block producer used
  46. pub offset: u64,
  47. /// Block producer leaders count
  48. pub leaders: u64,
  49. }
  50. impl Default for LeadInfo {
  51. /// Default LeadInfo used in genesis block generation
  52. fn default() -> Self {
  53. let keypair = Keypair::default();
  54. let signature = Signature::dummy();
  55. let public_inputs = vec![];
  56. let coin_slot = 0;
  57. let coin_eta = pallas::Base::zero();
  58. let proof = LeadProof::default();
  59. let offset = 0;
  60. let leaders = 0;
  61. Self {
  62. signature,
  63. public_key: keypair.public,
  64. public_inputs,
  65. coin_slot,
  66. coin_eta,
  67. proof,
  68. offset,
  69. leaders,
  70. }
  71. }
  72. }
  73. impl LeadInfo {
  74. #[allow(clippy::too_many_arguments)]
  75. pub fn new(
  76. signature: Signature,
  77. public_key: PublicKey,
  78. public_inputs: Vec<pallas::Base>,
  79. coin_slot: u64,
  80. coin_eta: pallas::Base,
  81. proof: LeadProof,
  82. offset: u64,
  83. leaders: u64,
  84. ) -> Self {
  85. Self { signature, public_key, public_inputs, coin_slot, coin_eta, proof, offset, leaders }
  86. }
  87. }
  88. /// Wrapper over the Proof, for future additions.
  89. #[derive(Default, Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  90. pub struct LeadProof {
  91. /// Leadership proof
  92. pub proof: Proof,
  93. }
  94. impl LeadProof {
  95. pub fn verify(&self, vk: &VerifyingKey, public_inputs: &[pallas::Base]) -> Result<()> {
  96. if let Err(e) = self.proof.verify(vk, public_inputs) {
  97. error!(target: "consensus::lead_info", "Verification of consensus lead proof failed: {}", e);
  98. return Err(e.into())
  99. }
  100. Ok(())
  101. }
  102. }
  103. impl From<Proof> for LeadProof {
  104. fn from(proof: Proof) -> Self {
  105. Self { proof }
  106. }
  107. }