main.rs 9.7 KB

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