main.rs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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::{fs::File, io::Write};
  19. use darkfi::{
  20. blockchain::{
  21. block_store::{BlockOrderStore, BlockStore, HeaderStore},
  22. slot_checkpoint_store::SlotCheckpointStore,
  23. tx_store::TxStore,
  24. Blockchain,
  25. },
  26. consensus::{
  27. block::{Block, Header},
  28. constants::TESTNET_GENESIS_HASH_BYTES,
  29. lead_info::LeadInfo,
  30. validator::ValidatorState,
  31. },
  32. tx::Transaction,
  33. util::{path::expand_path, time::Timestamp},
  34. wallet::walletdb::init_wallet,
  35. Result,
  36. };
  37. use darkfi_sdk::crypto::MerkleNode;
  38. #[derive(Debug)]
  39. struct LeadInfoInfo {
  40. _signature: String,
  41. _public_key: String,
  42. _public_inputs: Vec<String>,
  43. _coin_slot: u64,
  44. _coin_eta: String,
  45. _proof: String,
  46. _leaders: u64,
  47. }
  48. impl LeadInfoInfo {
  49. pub fn new(lead_info: &LeadInfo) -> LeadInfoInfo {
  50. let _signature = format!("{:?}", lead_info.signature);
  51. let _public_key = lead_info.public_key.to_string();
  52. let mut _public_inputs = vec![];
  53. for public_input in &lead_info.public_inputs {
  54. _public_inputs.push(format!("{:?}", public_input));
  55. }
  56. let _coin_slot = lead_info.coin_slot;
  57. let _coin_eta = format!("{:?}", lead_info.coin_eta);
  58. let _proof = format!("{:?}", lead_info.proof);
  59. let _leaders = lead_info.leaders;
  60. LeadInfoInfo {
  61. _signature,
  62. _public_key,
  63. _public_inputs,
  64. _coin_slot,
  65. _coin_eta,
  66. _proof,
  67. _leaders,
  68. }
  69. }
  70. }
  71. #[derive(Debug)]
  72. struct HeaderInfo {
  73. _hash: blake3::Hash,
  74. _version: u8,
  75. _previous: blake3::Hash,
  76. _epoch: u64,
  77. _slot: u64,
  78. _timestamp: Timestamp,
  79. _root: MerkleNode,
  80. }
  81. impl HeaderInfo {
  82. pub fn new(_hash: blake3::Hash, header: &Header) -> HeaderInfo {
  83. let _version = header.version;
  84. let _previous = header.previous;
  85. let _epoch = header.epoch;
  86. let _slot = header.slot;
  87. let _timestamp = header.timestamp;
  88. let _root = header.root;
  89. HeaderInfo { _hash, _version, _previous, _epoch, _slot, _timestamp, _root }
  90. }
  91. }
  92. #[derive(Debug)]
  93. struct HeaderStoreInfo {
  94. _headers: Vec<HeaderInfo>,
  95. }
  96. impl HeaderStoreInfo {
  97. pub fn new(headerstore: &HeaderStore) -> HeaderStoreInfo {
  98. let mut _headers = Vec::new();
  99. let result = headerstore.get_all();
  100. match result {
  101. Ok(iter) => {
  102. for (hash, header) in iter.iter() {
  103. _headers.push(HeaderInfo::new(hash.clone(), &header));
  104. }
  105. }
  106. Err(e) => println!("Error: {:?}", e),
  107. }
  108. HeaderStoreInfo { _headers }
  109. }
  110. }
  111. #[derive(Debug)]
  112. struct BlockInfo {
  113. _hash: blake3::Hash,
  114. _magic: [u8; 4],
  115. _header: blake3::Hash,
  116. _txs: Vec<blake3::Hash>,
  117. _lead_info: LeadInfoInfo,
  118. }
  119. impl BlockInfo {
  120. pub fn new(_hash: blake3::Hash, block: &Block) -> BlockInfo {
  121. let _magic = block.magic;
  122. let _header = block.header;
  123. let _txs = block.txs.clone();
  124. let _lead_info = LeadInfoInfo::new(&block.lead_info);
  125. BlockInfo { _hash, _magic, _header, _txs, _lead_info }
  126. }
  127. }
  128. #[derive(Debug)]
  129. struct BlockInfoChain {
  130. _blocks: Vec<BlockInfo>,
  131. }
  132. impl BlockInfoChain {
  133. pub fn new(blockstore: &BlockStore) -> BlockInfoChain {
  134. let mut _blocks = Vec::new();
  135. let result = blockstore.get_all();
  136. match result {
  137. Ok(iter) => {
  138. for (hash, block) in iter.iter() {
  139. _blocks.push(BlockInfo::new(hash.clone(), &block));
  140. }
  141. }
  142. Err(e) => println!("Error: {:?}", e),
  143. }
  144. BlockInfoChain { _blocks }
  145. }
  146. }
  147. #[derive(Debug)]
  148. struct OrderInfo {
  149. _slot: u64,
  150. _hash: blake3::Hash,
  151. }
  152. impl OrderInfo {
  153. pub fn new(_slot: u64, _hash: blake3::Hash) -> OrderInfo {
  154. OrderInfo { _slot, _hash }
  155. }
  156. }
  157. #[derive(Debug)]
  158. struct BlockOrderStoreInfo {
  159. _order: Vec<OrderInfo>,
  160. }
  161. impl BlockOrderStoreInfo {
  162. pub fn new(orderstore: &BlockOrderStore) -> BlockOrderStoreInfo {
  163. let mut _order = Vec::new();
  164. let result = orderstore.get_all();
  165. match result {
  166. Ok(iter) => {
  167. for (slot, hash) in iter.iter() {
  168. _order.push(OrderInfo::new(slot.clone(), hash.clone()));
  169. }
  170. }
  171. Err(e) => println!("Error: {:?}", e),
  172. }
  173. BlockOrderStoreInfo { _order }
  174. }
  175. }
  176. #[derive(Debug)]
  177. struct SlotCheckpointInfo {
  178. _slot: u64,
  179. _eta: String,
  180. _sigma1: String,
  181. _sigma2: String,
  182. }
  183. impl SlotCheckpointInfo {
  184. pub fn new(_slot: u64, _eta: String, _sigma1: String, _sigma2: String) -> SlotCheckpointInfo {
  185. SlotCheckpointInfo { _slot, _eta, _sigma1, _sigma2 }
  186. }
  187. }
  188. #[derive(Debug)]
  189. struct SlotCheckpointStoreInfo {
  190. _slot_checkpoints: Vec<SlotCheckpointInfo>,
  191. }
  192. impl SlotCheckpointStoreInfo {
  193. pub fn new(slotcheckpointstore: &SlotCheckpointStore) -> SlotCheckpointStoreInfo {
  194. let mut _slot_checkpoints = Vec::new();
  195. let result = slotcheckpointstore.get_all();
  196. match result {
  197. Ok(iter) => {
  198. for slot_checkpoint in iter.iter() {
  199. _slot_checkpoints.push(SlotCheckpointInfo::new(
  200. slot_checkpoint.slot,
  201. format!("{:?}", slot_checkpoint.eta),
  202. format!("{:?}", slot_checkpoint.sigma1),
  203. format!("{:?}", slot_checkpoint.sigma2),
  204. ));
  205. }
  206. }
  207. Err(e) => println!("Error: {:?}", e),
  208. }
  209. SlotCheckpointStoreInfo { _slot_checkpoints }
  210. }
  211. }
  212. #[derive(Debug)]
  213. struct TxInfo {
  214. _hash: blake3::Hash,
  215. _payload: Transaction,
  216. }
  217. impl TxInfo {
  218. pub fn new(_hash: blake3::Hash, tx: &Transaction) -> TxInfo {
  219. let _payload = tx.clone();
  220. TxInfo { _hash, _payload }
  221. }
  222. }
  223. #[derive(Debug)]
  224. struct TxStoreInfo {
  225. _transactions: Vec<TxInfo>,
  226. }
  227. impl TxStoreInfo {
  228. pub fn new(txstore: &TxStore) -> TxStoreInfo {
  229. let mut _transactions = Vec::new();
  230. let result = txstore.get_all();
  231. match result {
  232. Ok(iter) => {
  233. for (hash, tx) in iter.iter() {
  234. _transactions.push(TxInfo::new(hash.clone(), &tx));
  235. }
  236. }
  237. Err(e) => println!("Error: {:?}", e),
  238. }
  239. TxStoreInfo { _transactions }
  240. }
  241. }
  242. #[derive(Debug)]
  243. struct BlockchainInfo {
  244. _headers: HeaderStoreInfo,
  245. _blocks: BlockInfoChain,
  246. _order: BlockOrderStoreInfo,
  247. _slot_checkpoints: SlotCheckpointStoreInfo,
  248. _transactions: TxStoreInfo,
  249. }
  250. impl BlockchainInfo {
  251. pub fn new(blockchain: &Blockchain) -> BlockchainInfo {
  252. let _headers = HeaderStoreInfo::new(&blockchain.headers);
  253. let _blocks = BlockInfoChain::new(&blockchain.blocks);
  254. let _order = BlockOrderStoreInfo::new(&blockchain.order);
  255. let _slot_checkpoints = SlotCheckpointStoreInfo::new(&blockchain.slot_checkpoints);
  256. let _transactions = TxStoreInfo::new(&blockchain.transactions);
  257. BlockchainInfo { _headers, _blocks, _order, _slot_checkpoints, _transactions }
  258. }
  259. }
  260. #[derive(Debug)]
  261. struct StateInfo {
  262. _blockchain: BlockchainInfo,
  263. }
  264. impl StateInfo {
  265. pub fn new(state: &ValidatorState) -> StateInfo {
  266. let _blockchain = BlockchainInfo::new(&state.blockchain);
  267. StateInfo { _blockchain }
  268. }
  269. }
  270. async fn generate(localnet: &str, name: &str) -> Result<()> {
  271. println!("Exporting data for {name}...");
  272. // Node folder
  273. let folder = localnet.to_owned() + name;
  274. // Consensus configuration
  275. let bootstrap_ts = Timestamp(1648383795);
  276. let genesis_ts = Timestamp(1648383795);
  277. let genesis_data = *TESTNET_GENESIS_HASH_BYTES;
  278. let initial_distribution = 1000;
  279. let pass = "changeme";
  280. // Initialize or load wallet
  281. let path = folder.to_owned() + "/wallet.db";
  282. let wallet = init_wallet(&path, &pass).await?;
  283. // Initialize or load sled database
  284. let path = folder.to_owned() + "/blockchain/testnet";
  285. let db_path = expand_path(&path).unwrap();
  286. let sled_db = sled::open(&db_path)?;
  287. // Data export
  288. let state = ValidatorState::new(
  289. &sled_db,
  290. bootstrap_ts,
  291. genesis_ts,
  292. genesis_data,
  293. initial_distribution,
  294. wallet,
  295. vec![],
  296. false,
  297. )
  298. .await?;
  299. let info = StateInfo::new(&*state.read().await);
  300. let info_string = format!("{:#?}", info);
  301. let path = name.to_owned() + "_testnet_db";
  302. let mut file = File::create(path)?;
  303. file.write(info_string.as_bytes())?;
  304. drop(sled_db);
  305. Ok(())
  306. }
  307. #[async_std::main]
  308. async fn main() -> Result<()> {
  309. // Localnet folder
  310. let localnet = "../../../contrib/localnet/darkfid/";
  311. println!("Localnet folder: {localnet}");
  312. // darkfid0
  313. generate(localnet, "darkfid0").await?;
  314. // darkfid1
  315. generate(localnet, "darkfid1").await?;
  316. // darkfid2
  317. generate(localnet, "darkfid2").await?;
  318. // faucetd
  319. generate(localnet, "faucetd").await?;
  320. Ok(())
  321. }