utils.rs 5.1 KB

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