utils.rs 4.9 KB

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