main.rs 9.5 KB

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