main.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. use async_std::sync::Arc;
  2. use std::{fs::File, io::Write};
  3. use darkfi::{
  4. blockchain::{
  5. blockstore::{BlockOrderStore, BlockStore, HeaderStore},
  6. metadatastore::StreamletMetadataStore,
  7. txstore::TxStore,
  8. Blockchain,
  9. },
  10. consensus::{
  11. block::{Block, BlockProposal, Header, ProposalChain},
  12. metadata::{Metadata, StreamletMetadata},
  13. participant::Participant,
  14. state::{ConsensusState, ValidatorState},
  15. vote::Vote,
  16. TESTNET_GENESIS_HASH_BYTES,
  17. },
  18. crypto::{merkle_node::MerkleNode, token_list::DrkTokenList},
  19. node::Client,
  20. tx::Transaction,
  21. util::{expand_path, serial::serialize, time::Timestamp},
  22. wallet::walletdb::init_wallet,
  23. Result,
  24. };
  25. #[derive(Debug)]
  26. struct ParticipantInfo {
  27. _address: String,
  28. _joined: u64,
  29. _voted: Option<u64>,
  30. _quarantined: Option<u64>,
  31. }
  32. impl ParticipantInfo {
  33. pub fn new(participant: &Participant) -> ParticipantInfo {
  34. let _address = participant.address.to_string();
  35. let _joined = participant.joined;
  36. let _voted = participant.voted;
  37. let _quarantined = participant.quarantined;
  38. ParticipantInfo { _address, _joined, _voted, _quarantined }
  39. }
  40. }
  41. #[derive(Debug)]
  42. struct VoteInfo {
  43. _proposal: blake3::Hash,
  44. _slot: u64,
  45. _address: String,
  46. }
  47. impl VoteInfo {
  48. pub fn new(vote: &Vote) -> VoteInfo {
  49. let _proposal = vote.proposal;
  50. let _slot = vote.slot;
  51. let _address = vote.address.to_string();
  52. VoteInfo { _proposal, _slot, _address }
  53. }
  54. }
  55. #[derive(Debug)]
  56. struct StreamletMetadataInfo {
  57. _votes: Vec<VoteInfo>,
  58. _notarized: bool,
  59. _finalized: bool,
  60. _participants: Vec<ParticipantInfo>,
  61. }
  62. impl StreamletMetadataInfo {
  63. pub fn new(metadata: &StreamletMetadata) -> StreamletMetadataInfo {
  64. let mut _votes = Vec::new();
  65. for vote in &metadata.votes {
  66. _votes.push(VoteInfo::new(&vote));
  67. }
  68. let _notarized = metadata.notarized;
  69. let _finalized = metadata.finalized;
  70. let mut _participants = Vec::new();
  71. for participant in &metadata.participants {
  72. _participants.push(ParticipantInfo::new(&participant));
  73. }
  74. StreamletMetadataInfo { _votes, _notarized, _finalized, _participants }
  75. }
  76. }
  77. #[derive(Debug)]
  78. struct MetadataInfo {
  79. _address: String,
  80. }
  81. impl MetadataInfo {
  82. pub fn new(metadata: &Metadata) -> MetadataInfo {
  83. let _address = metadata.address.to_string();
  84. MetadataInfo { _address }
  85. }
  86. }
  87. #[derive(Debug)]
  88. struct ProposalInfo {
  89. _block: BlockInfo,
  90. _sm: StreamletMetadataInfo,
  91. }
  92. impl ProposalInfo {
  93. pub fn new(proposal: &BlockProposal) -> ProposalInfo {
  94. let _header = proposal.block.header.headerhash();
  95. let mut _txs = vec![];
  96. for tx in &proposal.block.txs {
  97. let hash = blake3::hash(&serialize(tx));
  98. _txs.push(hash);
  99. }
  100. let _metadata = MetadataInfo::new(&proposal.block.metadata);
  101. let _block =
  102. BlockInfo { _hash: _header, _magic: proposal.block.magic, _header, _txs, _metadata };
  103. let _sm = StreamletMetadataInfo::new(&proposal.block.sm);
  104. ProposalInfo { _block, _sm }
  105. }
  106. }
  107. #[derive(Debug)]
  108. struct ProposalInfoChain {
  109. _proposals: Vec<ProposalInfo>,
  110. }
  111. impl ProposalInfoChain {
  112. pub fn new(proposals: &ProposalChain) -> ProposalInfoChain {
  113. let mut _proposals = Vec::new();
  114. for proposal in &proposals.proposals {
  115. _proposals.push(ProposalInfo::new(&proposal));
  116. }
  117. ProposalInfoChain { _proposals }
  118. }
  119. }
  120. #[derive(Debug)]
  121. struct ConsensusInfo {
  122. _genesis_ts: Timestamp,
  123. _proposals: Vec<ProposalInfoChain>,
  124. }
  125. impl ConsensusInfo {
  126. pub fn new(consensus: &ConsensusState) -> ConsensusInfo {
  127. let _genesis_ts = consensus.genesis_ts.clone();
  128. let mut _proposals = Vec::new();
  129. for proposal in &consensus.proposals {
  130. _proposals.push(ProposalInfoChain::new(&proposal));
  131. }
  132. ConsensusInfo { _genesis_ts, _proposals }
  133. }
  134. }
  135. #[derive(Debug)]
  136. struct HeaderInfo {
  137. _hash: blake3::Hash,
  138. _version: u8,
  139. _state: blake3::Hash,
  140. _epoch: u64,
  141. _slot: u64,
  142. _timestamp: Timestamp,
  143. _root: MerkleNode,
  144. }
  145. impl HeaderInfo {
  146. pub fn new(_hash: blake3::Hash, header: &Header) -> HeaderInfo {
  147. let _version = header.version;
  148. let _state = header.state;
  149. let _epoch = header.epoch;
  150. let _slot = header.slot;
  151. let _timestamp = header.timestamp;
  152. let _root = header.root;
  153. HeaderInfo { _hash, _version, _state, _epoch, _slot, _timestamp, _root }
  154. }
  155. }
  156. #[derive(Debug)]
  157. struct HeaderStoreInfo {
  158. _headers: Vec<HeaderInfo>,
  159. }
  160. impl HeaderStoreInfo {
  161. pub fn new(headerstore: &HeaderStore) -> HeaderStoreInfo {
  162. let mut _headers = Vec::new();
  163. let result = headerstore.get_all();
  164. match result {
  165. Ok(iter) => {
  166. for (hash, header) in iter.iter() {
  167. _headers.push(HeaderInfo::new(hash.clone(), &header));
  168. }
  169. }
  170. Err(e) => println!("Error: {:?}", e),
  171. }
  172. HeaderStoreInfo { _headers }
  173. }
  174. }
  175. #[derive(Debug)]
  176. struct BlockInfo {
  177. _hash: blake3::Hash,
  178. _magic: [u8; 4],
  179. _header: blake3::Hash,
  180. _txs: Vec<blake3::Hash>,
  181. _metadata: MetadataInfo,
  182. }
  183. impl BlockInfo {
  184. pub fn new(_hash: blake3::Hash, block: &Block) -> BlockInfo {
  185. let _magic = block.magic;
  186. let _header = block.header;
  187. let _txs = block.txs.clone();
  188. let _metadata = MetadataInfo::new(&block.metadata);
  189. BlockInfo { _hash, _magic, _header, _txs, _metadata }
  190. }
  191. }
  192. #[derive(Debug)]
  193. struct BlockInfoChain {
  194. _blocks: Vec<BlockInfo>,
  195. }
  196. impl BlockInfoChain {
  197. pub fn new(blockstore: &BlockStore) -> BlockInfoChain {
  198. let mut _blocks = Vec::new();
  199. let result = blockstore.get_all();
  200. match result {
  201. Ok(iter) => {
  202. for (hash, block) in iter.iter() {
  203. _blocks.push(BlockInfo::new(hash.clone(), &block));
  204. }
  205. }
  206. Err(e) => println!("Error: {:?}", e),
  207. }
  208. BlockInfoChain { _blocks }
  209. }
  210. }
  211. #[derive(Debug)]
  212. struct OrderInfo {
  213. _slot: u64,
  214. _hash: blake3::Hash,
  215. }
  216. impl OrderInfo {
  217. pub fn new(_slot: u64, _hash: blake3::Hash) -> OrderInfo {
  218. OrderInfo { _slot, _hash }
  219. }
  220. }
  221. #[derive(Debug)]
  222. struct BlockOrderStoreInfo {
  223. _order: Vec<OrderInfo>,
  224. }
  225. impl BlockOrderStoreInfo {
  226. pub fn new(orderstore: &BlockOrderStore) -> BlockOrderStoreInfo {
  227. let mut _order = Vec::new();
  228. let result = orderstore.get_all();
  229. match result {
  230. Ok(iter) => {
  231. for (slot, hash) in iter.iter() {
  232. _order.push(OrderInfo::new(slot.clone(), hash.clone()));
  233. }
  234. }
  235. Err(e) => println!("Error: {:?}", e),
  236. }
  237. BlockOrderStoreInfo { _order }
  238. }
  239. }
  240. #[derive(Debug)]
  241. struct TxInfo {
  242. _hash: blake3::Hash,
  243. _payload: Transaction,
  244. }
  245. impl TxInfo {
  246. pub fn new(_hash: blake3::Hash, tx: &Transaction) -> TxInfo {
  247. let _payload = tx.clone();
  248. TxInfo { _hash, _payload }
  249. }
  250. }
  251. #[derive(Debug)]
  252. struct TxStoreInfo {
  253. _transactions: Vec<TxInfo>,
  254. }
  255. impl TxStoreInfo {
  256. pub fn new(txstore: &TxStore) -> TxStoreInfo {
  257. let mut _transactions = Vec::new();
  258. let result = txstore.get_all();
  259. match result {
  260. Ok(iter) => {
  261. for (hash, tx) in iter.iter() {
  262. _transactions.push(TxInfo::new(hash.clone(), &tx));
  263. }
  264. }
  265. Err(e) => println!("Error: {:?}", e),
  266. }
  267. TxStoreInfo { _transactions }
  268. }
  269. }
  270. #[derive(Debug)]
  271. struct HashedMetadataInfo {
  272. _block: blake3::Hash,
  273. _metadata: StreamletMetadataInfo,
  274. }
  275. impl HashedMetadataInfo {
  276. pub fn new(_block: blake3::Hash, metadata: &StreamletMetadata) -> HashedMetadataInfo {
  277. let _metadata = StreamletMetadataInfo::new(&metadata);
  278. HashedMetadataInfo { _block, _metadata }
  279. }
  280. }
  281. #[derive(Debug)]
  282. struct MetadataStoreInfo {
  283. _metadata: Vec<HashedMetadataInfo>,
  284. }
  285. impl MetadataStoreInfo {
  286. pub fn new(metadatastore: &StreamletMetadataStore) -> MetadataStoreInfo {
  287. let mut _metadata = Vec::new();
  288. let result = metadatastore.get_all();
  289. match result {
  290. Ok(iter) => {
  291. for (hash, m) in iter.iter() {
  292. _metadata.push(HashedMetadataInfo::new(hash.clone(), &m));
  293. }
  294. }
  295. Err(e) => println!("Error: {:?}", e),
  296. }
  297. MetadataStoreInfo { _metadata }
  298. }
  299. }
  300. #[derive(Debug)]
  301. struct BlockchainInfo {
  302. _headers: HeaderStoreInfo,
  303. _blocks: BlockInfoChain,
  304. _order: BlockOrderStoreInfo,
  305. _transactions: TxStoreInfo,
  306. _metadata: MetadataStoreInfo,
  307. }
  308. impl BlockchainInfo {
  309. pub fn new(blockchain: &Blockchain) -> BlockchainInfo {
  310. let _headers = HeaderStoreInfo::new(&blockchain.headers);
  311. let _blocks = BlockInfoChain::new(&blockchain.blocks);
  312. let _order = BlockOrderStoreInfo::new(&blockchain.order);
  313. let _transactions = TxStoreInfo::new(&blockchain.transactions);
  314. let _metadata = MetadataStoreInfo::new(&blockchain.streamlet_metadata);
  315. BlockchainInfo { _headers, _blocks, _order, _transactions, _metadata }
  316. }
  317. }
  318. #[derive(Debug)]
  319. struct StateInfo {
  320. _address: String,
  321. _consensus: ConsensusInfo,
  322. _blockchain: BlockchainInfo,
  323. }
  324. impl StateInfo {
  325. pub fn new(state: &ValidatorState) -> StateInfo {
  326. let _address = state.address.to_string();
  327. let _consensus = ConsensusInfo::new(&state.consensus);
  328. let _blockchain = BlockchainInfo::new(&state.blockchain);
  329. StateInfo { _address, _consensus, _blockchain }
  330. }
  331. }
  332. async fn generate(name: &str, folder: &str) -> Result<()> {
  333. let genesis_ts = Timestamp(1648383795);
  334. let genesis_data = *TESTNET_GENESIS_HASH_BYTES;
  335. let pass = "changeme";
  336. // Initialize or load wallet
  337. let path = folder.to_owned() + "/wallet.db";
  338. let wallet = init_wallet(&path, &pass).await?;
  339. let address = wallet.get_default_address().await?;
  340. let tokenlist = Arc::new(DrkTokenList::new(&[
  341. ("drk", include_bytes!("../../../../contrib/token/darkfi_token_list.min.json")),
  342. ("btc", include_bytes!("../../../../contrib/token/bitcoin_token_list.min.json")),
  343. ("eth", include_bytes!("../../../../contrib/token/erc20_token_list.min.json")),
  344. ("sol", include_bytes!("../../../../contrib/token/solana_token_list.min.json")),
  345. ])?);
  346. let client = Arc::new(Client::new(wallet, tokenlist).await?);
  347. // Initialize or load sled database
  348. let path = folder.to_owned() + "/blockchain/testnet";
  349. let db_path = expand_path(&path).unwrap();
  350. let sled_db = sled::open(&db_path)?;
  351. // Data export
  352. println!("Exporting data for {:?} - {:?}", name, address.to_string());
  353. let state =
  354. ValidatorState::new(&sled_db, genesis_ts, genesis_data, client, vec![], vec![]).await?;
  355. let info = StateInfo::new(&*state.read().await);
  356. let info_string = format!("{:#?}", info);
  357. let path = name.to_owned() + "_testnet_db";
  358. let mut file = File::create(path)?;
  359. file.write(info_string.as_bytes())?;
  360. drop(sled_db);
  361. Ok(())
  362. }
  363. #[async_std::main]
  364. async fn main() -> Result<()> {
  365. // darkfid0
  366. generate("darkfid0", "../../../contrib/localnet/darkfid0").await?;
  367. // darkfid1
  368. generate("darkfid1", "../../../contrib/localnet/darkfid1").await?;
  369. // darkfid2
  370. generate("darkfid2", "../../../contrib/localnet/darkfid2").await?;
  371. // faucetd
  372. generate("faucetd", "../../../contrib/localnet/faucetd").await?;
  373. Ok(())
  374. }