utils.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 log::info;
  19. use darkfi::{
  20. error::TxVerifyFailed,
  21. net::{P2p, P2pPtr, Settings, SESSION_ALL},
  22. tx::Transaction,
  23. validator::ValidatorPtr,
  24. Result,
  25. };
  26. use darkfi_consensus_contract::{
  27. model::ConsensusGenesisStakeParamsV1, ConsensusFunction::GenesisStakeV1,
  28. };
  29. use darkfi_money_contract::{model::MoneyTokenMintParamsV1, MoneyFunction::GenesisMintV1};
  30. use darkfi_sdk::crypto::{CONSENSUS_CONTRACT_ID, MONEY_CONTRACT_ID};
  31. use darkfi_serial::deserialize;
  32. use crate::proto::{ProtocolBlock, ProtocolProposal, ProtocolSync, ProtocolTx};
  33. /// Auxiliary function to calculate the total amount of minted tokens in provided
  34. /// genesis transactions set. This includes both staked and normal tokens.
  35. /// If a non-genesis transaction is found, execution fails.
  36. pub fn genesis_txs_total(txs: &[Transaction]) -> Result<u64> {
  37. let mut total = 0;
  38. for tx in txs {
  39. // Transaction must contain a single Consensus::GenesisStake or Money::GenesisMint call
  40. if tx.calls.len() != 1 {
  41. return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
  42. }
  43. let call = &tx.calls[0];
  44. let function = call.data[0];
  45. if !(call.contract_id == *CONSENSUS_CONTRACT_ID || call.contract_id == *MONEY_CONTRACT_ID) ||
  46. (call.contract_id == *CONSENSUS_CONTRACT_ID && function != GenesisStakeV1 as u8) ||
  47. (call.contract_id == *MONEY_CONTRACT_ID && function != GenesisMintV1 as u8)
  48. {
  49. return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
  50. }
  51. let value = if function == GenesisStakeV1 as u8 {
  52. let params: ConsensusGenesisStakeParamsV1 = deserialize(&call.data[1..])?;
  53. params.input.value
  54. } else {
  55. let params: MoneyTokenMintParamsV1 = deserialize(&call.data[1..])?;
  56. params.input.value
  57. };
  58. total += value;
  59. }
  60. Ok(total)
  61. }
  62. /// Auxiliary function to generate the sync P2P network and register all its protocols.
  63. pub async fn spawn_sync_p2p(settings: &Settings, validator: &ValidatorPtr) -> P2pPtr {
  64. info!(target: "darkfid", "Registering sync network P2P protocols...");
  65. let p2p = P2p::new(settings.clone()).await;
  66. let registry = p2p.protocol_registry();
  67. let _validator = validator.clone();
  68. registry
  69. .register(SESSION_ALL, move |channel, p2p| {
  70. let validator = _validator.clone();
  71. async move { ProtocolBlock::init(channel, validator, p2p).await.unwrap() }
  72. })
  73. .await;
  74. let _validator = validator.clone();
  75. registry
  76. .register(SESSION_ALL, move |channel, _p2p| {
  77. let validator = _validator.clone();
  78. async move { ProtocolSync::init(channel, validator).await.unwrap() }
  79. })
  80. .await;
  81. let _validator = validator.clone();
  82. registry
  83. .register(SESSION_ALL, move |channel, p2p| {
  84. let validator = _validator.clone();
  85. async move { ProtocolTx::init(channel, validator, p2p).await.unwrap() }
  86. })
  87. .await;
  88. p2p
  89. }
  90. /// Auxiliary function to generate the consensus P2P network and register all its protocols.
  91. pub async fn spawn_consensus_p2p(settings: &Settings, validator: &ValidatorPtr) -> P2pPtr {
  92. info!(target: "darkfid", "Registering consensus network P2P protocols...");
  93. let p2p = P2p::new(settings.clone()).await;
  94. let registry = p2p.protocol_registry();
  95. let _validator = validator.clone();
  96. registry
  97. .register(SESSION_ALL, move |channel, p2p| {
  98. let validator = _validator.clone();
  99. async move { ProtocolProposal::init(channel, validator, p2p).await.unwrap() }
  100. })
  101. .await;
  102. p2p
  103. }