main.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. use async_std::sync::Arc;
  2. use std::{fs::File, io::Write};
  3. use darkfi::{
  4. blockchain::{
  5. blockstore::{BlockOrderStore, BlockStore},
  6. metadatastore::StreamletMetadataStore,
  7. txstore::TxStore,
  8. Blockchain,
  9. },
  10. consensus::{
  11. block::{Block, BlockProposal, ProposalChain},
  12. metadata::{Metadata, OuroborosMetadata, StreamletMetadata},
  13. participant::Participant,
  14. state::{ConsensusState, ValidatorState},
  15. vote::Vote,
  16. TESTNET_GENESIS_HASH_BYTES,
  17. },
  18. crypto::token_list::DrkTokenList,
  19. node::Client,
  20. tx::Transaction,
  21. util::{expand_path, 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. }
  31. impl ParticipantInfo {
  32. pub fn new(participant: &Participant) -> ParticipantInfo {
  33. let _address = participant.address.to_string();
  34. let _joined = participant.joined;
  35. let _voted = participant.voted;
  36. ParticipantInfo { _address, _joined, _voted }
  37. }
  38. }
  39. #[derive(Debug)]
  40. struct VoteInfo {
  41. _proposal: blake3::Hash,
  42. _sl: u64,
  43. _address: String,
  44. }
  45. impl VoteInfo {
  46. pub fn new(vote: &Vote) -> VoteInfo {
  47. let _proposal = vote.proposal;
  48. let _sl = vote.sl;
  49. let _address = vote.address.to_string();
  50. VoteInfo { _proposal, _sl, _address }
  51. }
  52. }
  53. #[derive(Debug)]
  54. struct StreamletMetadataInfo {
  55. _votes: Vec<VoteInfo>,
  56. _notarized: bool,
  57. _finalized: bool,
  58. _participants: Vec<ParticipantInfo>,
  59. }
  60. impl StreamletMetadataInfo {
  61. pub fn new(metadata: &StreamletMetadata) -> StreamletMetadataInfo {
  62. let mut _votes = Vec::new();
  63. for vote in &metadata.votes {
  64. _votes.push(VoteInfo::new(&vote));
  65. }
  66. let _notarized = metadata.notarized;
  67. let _finalized = metadata.finalized;
  68. let mut _participants = Vec::new();
  69. for participant in &metadata.participants {
  70. _participants.push(ParticipantInfo::new(&participant));
  71. }
  72. StreamletMetadataInfo { _votes, _notarized, _finalized, _participants }
  73. }
  74. }
  75. #[derive(Debug)]
  76. struct OuroborosMetadataInfo {
  77. _proof: String,
  78. _r: String,
  79. _s: String,
  80. }
  81. impl OuroborosMetadataInfo {
  82. pub fn new(metadata: &OuroborosMetadata) -> OuroborosMetadataInfo {
  83. let _proof = metadata.proof.clone();
  84. let _r = metadata.r.clone();
  85. let _s = metadata.s.clone();
  86. OuroborosMetadataInfo { _proof, _r, _s }
  87. }
  88. }
  89. #[derive(Debug)]
  90. struct MetadataInfo {
  91. _timestamp: Timestamp,
  92. _om: OuroborosMetadataInfo,
  93. }
  94. impl MetadataInfo {
  95. pub fn new(metadata: &Metadata) -> MetadataInfo {
  96. let _timestamp = metadata.timestamp.clone();
  97. let _om = OuroborosMetadataInfo::new(&metadata.om);
  98. MetadataInfo { _timestamp, _om }
  99. }
  100. }
  101. #[derive(Debug)]
  102. struct ProposalInfo {
  103. _address: String,
  104. _st: blake3::Hash,
  105. _sl: u64,
  106. _txs: Vec<Transaction>,
  107. _metadata: MetadataInfo,
  108. _sm: StreamletMetadataInfo,
  109. }
  110. impl ProposalInfo {
  111. pub fn new(proposal: &BlockProposal) -> ProposalInfo {
  112. let _address = proposal.address.to_string();
  113. let _st = proposal.block.st;
  114. let _sl = proposal.block.sl;
  115. let _txs = proposal.block.txs.clone();
  116. let _metadata = MetadataInfo::new(&proposal.block.metadata);
  117. let _sm = StreamletMetadataInfo::new(&proposal.block.sm);
  118. ProposalInfo { _address, _st, _sl, _txs, _metadata, _sm }
  119. }
  120. }
  121. #[derive(Debug)]
  122. struct ProposalInfoChain {
  123. _proposals: Vec<ProposalInfo>,
  124. }
  125. impl ProposalInfoChain {
  126. pub fn new(proposals: &ProposalChain) -> ProposalInfoChain {
  127. let mut _proposals = Vec::new();
  128. for proposal in &proposals.proposals {
  129. _proposals.push(ProposalInfo::new(&proposal));
  130. }
  131. ProposalInfoChain { _proposals }
  132. }
  133. }
  134. #[derive(Debug)]
  135. struct ConsensusInfo {
  136. _genesis_ts: Timestamp,
  137. _proposals: Vec<ProposalInfoChain>,
  138. }
  139. impl ConsensusInfo {
  140. pub fn new(consensus: &ConsensusState) -> ConsensusInfo {
  141. let _genesis_ts = consensus.genesis_ts.clone();
  142. let mut _proposals = Vec::new();
  143. for proposal in &consensus.proposals {
  144. _proposals.push(ProposalInfoChain::new(&proposal));
  145. }
  146. ConsensusInfo { _genesis_ts, _proposals }
  147. }
  148. }
  149. #[derive(Debug)]
  150. struct BlockInfo {
  151. _hash: blake3::Hash,
  152. _st: blake3::Hash,
  153. _sl: u64,
  154. _txs: Vec<blake3::Hash>,
  155. }
  156. impl BlockInfo {
  157. pub fn new(_hash: blake3::Hash, block: &Block) -> BlockInfo {
  158. let _st = block.st;
  159. let _sl = block.sl;
  160. let _txs = block.txs.clone();
  161. BlockInfo { _hash, _st, _sl, _txs }
  162. }
  163. }
  164. #[derive(Debug)]
  165. struct BlockInfoChain {
  166. _blocks: Vec<BlockInfo>,
  167. }
  168. impl BlockInfoChain {
  169. pub fn new(blockstore: &BlockStore) -> BlockInfoChain {
  170. let mut _blocks = Vec::new();
  171. let result = blockstore.get_all();
  172. match result {
  173. Ok(iter) => {
  174. for (hash, block) in iter.iter() {
  175. _blocks.push(BlockInfo::new(hash.clone(), &block));
  176. }
  177. }
  178. Err(e) => println!("Error: {:?}", e),
  179. }
  180. BlockInfoChain { _blocks }
  181. }
  182. }
  183. #[derive(Debug)]
  184. struct OrderInfo {
  185. _sl: u64,
  186. _hash: blake3::Hash,
  187. }
  188. impl OrderInfo {
  189. pub fn new(_sl: u64, _hash: blake3::Hash) -> OrderInfo {
  190. OrderInfo { _sl, _hash }
  191. }
  192. }
  193. #[derive(Debug)]
  194. struct BlockOrderStoreInfo {
  195. _order: Vec<OrderInfo>,
  196. }
  197. impl BlockOrderStoreInfo {
  198. pub fn new(orderstore: &BlockOrderStore) -> BlockOrderStoreInfo {
  199. let mut _order = Vec::new();
  200. let result = orderstore.get_all();
  201. match result {
  202. Ok(iter) => {
  203. for (slot, hash) in iter.iter() {
  204. _order.push(OrderInfo::new(slot.clone(), hash.clone()));
  205. }
  206. }
  207. Err(e) => println!("Error: {:?}", e),
  208. }
  209. BlockOrderStoreInfo { _order }
  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 HashedMetadataInfo {
  244. _block: blake3::Hash,
  245. _metadata: StreamletMetadataInfo,
  246. }
  247. impl HashedMetadataInfo {
  248. pub fn new(_block: blake3::Hash, metadata: &StreamletMetadata) -> HashedMetadataInfo {
  249. let _metadata = StreamletMetadataInfo::new(&metadata);
  250. HashedMetadataInfo { _block, _metadata }
  251. }
  252. }
  253. #[derive(Debug)]
  254. struct MetadataStoreInfo {
  255. _metadata: Vec<HashedMetadataInfo>,
  256. }
  257. impl MetadataStoreInfo {
  258. pub fn new(metadatastore: &StreamletMetadataStore) -> MetadataStoreInfo {
  259. let mut _metadata = Vec::new();
  260. let result = metadatastore.get_all();
  261. match result {
  262. Ok(iter) => {
  263. for (hash, m) in iter.iter() {
  264. _metadata.push(HashedMetadataInfo::new(hash.clone(), &m));
  265. }
  266. }
  267. Err(e) => println!("Error: {:?}", e),
  268. }
  269. MetadataStoreInfo { _metadata }
  270. }
  271. }
  272. #[derive(Debug)]
  273. struct BlockchainInfo {
  274. _blocks: BlockInfoChain,
  275. _order: BlockOrderStoreInfo,
  276. _transactions: TxStoreInfo,
  277. _metadata: MetadataStoreInfo,
  278. }
  279. impl BlockchainInfo {
  280. pub fn new(blockchain: &Blockchain) -> BlockchainInfo {
  281. let _blocks = BlockInfoChain::new(&blockchain.blocks);
  282. let _order = BlockOrderStoreInfo::new(&blockchain.order);
  283. let _transactions = TxStoreInfo::new(&blockchain.transactions);
  284. let _metadata = MetadataStoreInfo::new(&blockchain.streamlet_metadata);
  285. BlockchainInfo { _blocks, _order, _transactions, _metadata }
  286. }
  287. }
  288. #[derive(Debug)]
  289. struct StateInfo {
  290. _address: String,
  291. _consensus: ConsensusInfo,
  292. _blockchain: BlockchainInfo,
  293. }
  294. impl StateInfo {
  295. pub fn new(state: &ValidatorState) -> StateInfo {
  296. let _address = state.address.to_string();
  297. let _consensus = ConsensusInfo::new(&state.consensus);
  298. let _blockchain = BlockchainInfo::new(&state.blockchain);
  299. StateInfo { _address, _consensus, _blockchain }
  300. }
  301. }
  302. #[async_std::main]
  303. async fn main() -> Result<()> {
  304. let nodes = 4;
  305. let genesis_ts = Timestamp(1648383795);
  306. let genesis_data = *TESTNET_GENESIS_HASH_BYTES;
  307. let pass = "changeme";
  308. for i in 0..nodes {
  309. // Initialize or load wallet
  310. let path = format!("../../../tmp/node{:?}/wallet.db", i);
  311. let wallet = init_wallet(&path, &pass).await?;
  312. let address = wallet.get_default_address().await?;
  313. let tokenlist = Arc::new(DrkTokenList::new(&[
  314. ("drk", include_bytes!("../../../../contrib/token/darkfi_token_list.min.json")),
  315. ("btc", include_bytes!("../../../../contrib/token/bitcoin_token_list.min.json")),
  316. ("eth", include_bytes!("../../../../contrib/token/erc20_token_list.min.json")),
  317. ("sol", include_bytes!("../../../../contrib/token/solana_token_list.min.json")),
  318. ])?);
  319. let client = Arc::new(Client::new(wallet, tokenlist).await?);
  320. // Initialize or load sled database
  321. let path = format!("../../../tmp/node{:?}/blockchain/testnet", i);
  322. let db_path = expand_path(&path).unwrap();
  323. let sled_db = sled::open(&db_path)?;
  324. // Data export
  325. println!("Exporting data for node{:?} - {:?}", i, address.to_string());
  326. let state =
  327. ValidatorState::new(&sled_db, genesis_ts, genesis_data, client, vec![], vec![]).await?;
  328. let info = StateInfo::new(&*state.read().await);
  329. let info_string = format!("{:#?}", info);
  330. let path = format!("node{:?}_testnet_db", i);
  331. let mut file = File::create(path)?;
  332. file.write(info_string.as_bytes())?;
  333. drop(sled_db);
  334. }
  335. Ok(())
  336. }