main.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. tx::Tx,
  16. util::Timestamp,
  17. vote::Vote,
  18. TESTNET_GENESIS_HASH_BYTES,
  19. },
  20. node::Client,
  21. util::expand_path,
  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<Tx>,
  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 item in iter.iter() {
  175. match item {
  176. Some((hash, block)) => _blocks.push(BlockInfo::new(hash.clone(), &block)),
  177. None => (),
  178. };
  179. }
  180. }
  181. Err(e) => println!("Error: {:?}", e),
  182. }
  183. BlockInfoChain { _blocks }
  184. }
  185. }
  186. #[derive(Debug)]
  187. struct OrderInfo {
  188. _sl: u64,
  189. _hash: blake3::Hash,
  190. }
  191. impl OrderInfo {
  192. pub fn new(_sl: u64, _hash: blake3::Hash) -> OrderInfo {
  193. OrderInfo { _sl, _hash }
  194. }
  195. }
  196. #[derive(Debug)]
  197. struct BlockOrderStoreInfo {
  198. _order: Vec<OrderInfo>,
  199. }
  200. impl BlockOrderStoreInfo {
  201. pub fn new(orderstore: &BlockOrderStore) -> BlockOrderStoreInfo {
  202. let mut _order = Vec::new();
  203. let result = orderstore.get_all();
  204. match result {
  205. Ok(iter) => {
  206. for item in iter.iter() {
  207. match item {
  208. Some((slot, hash)) => {
  209. _order.push(OrderInfo::new(slot.clone(), hash.clone()))
  210. }
  211. None => (),
  212. };
  213. }
  214. }
  215. Err(e) => println!("Error: {:?}", e),
  216. }
  217. BlockOrderStoreInfo { _order }
  218. }
  219. }
  220. #[derive(Debug)]
  221. struct TxInfo {
  222. _hash: blake3::Hash,
  223. _payload: Tx,
  224. }
  225. impl TxInfo {
  226. pub fn new(_hash: blake3::Hash, tx: &Tx) -> TxInfo {
  227. let _payload = tx.clone();
  228. TxInfo { _hash, _payload }
  229. }
  230. }
  231. #[derive(Debug)]
  232. struct TxStoreInfo {
  233. _transactions: Vec<TxInfo>,
  234. }
  235. impl TxStoreInfo {
  236. pub fn new(txstore: &TxStore) -> TxStoreInfo {
  237. let mut _transactions = Vec::new();
  238. let result = txstore.get_all();
  239. match result {
  240. Ok(iter) => {
  241. for item in iter.iter() {
  242. match item {
  243. Some((hash, tx)) => _transactions.push(TxInfo::new(hash.clone(), &tx)),
  244. None => (),
  245. };
  246. }
  247. }
  248. Err(e) => println!("Error: {:?}", e),
  249. }
  250. TxStoreInfo { _transactions }
  251. }
  252. }
  253. #[derive(Debug)]
  254. struct HashedMetadataInfo {
  255. _block: blake3::Hash,
  256. _metadata: StreamletMetadataInfo,
  257. }
  258. impl HashedMetadataInfo {
  259. pub fn new(_block: blake3::Hash, metadata: &StreamletMetadata) -> HashedMetadataInfo {
  260. let _metadata = StreamletMetadataInfo::new(&metadata);
  261. HashedMetadataInfo { _block, _metadata }
  262. }
  263. }
  264. #[derive(Debug)]
  265. struct MetadataStoreInfo {
  266. _metadata: Vec<HashedMetadataInfo>,
  267. }
  268. impl MetadataStoreInfo {
  269. pub fn new(metadatastore: &StreamletMetadataStore) -> MetadataStoreInfo {
  270. let mut _metadata = Vec::new();
  271. let result = metadatastore.get_all();
  272. match result {
  273. Ok(iter) => {
  274. for item in iter.iter() {
  275. match item {
  276. Some((hash, m)) => {
  277. _metadata.push(HashedMetadataInfo::new(hash.clone(), &m))
  278. }
  279. None => (),
  280. };
  281. }
  282. }
  283. Err(e) => println!("Error: {:?}", e),
  284. }
  285. MetadataStoreInfo { _metadata }
  286. }
  287. }
  288. #[derive(Debug)]
  289. struct BlockchainInfo {
  290. _blocks: BlockInfoChain,
  291. _order: BlockOrderStoreInfo,
  292. _transactions: TxStoreInfo,
  293. _metadata: MetadataStoreInfo,
  294. }
  295. impl BlockchainInfo {
  296. pub fn new(blockchain: &Blockchain) -> BlockchainInfo {
  297. let _blocks = BlockInfoChain::new(&blockchain.blocks);
  298. let _order = BlockOrderStoreInfo::new(&blockchain.order);
  299. let _transactions = TxStoreInfo::new(&blockchain.transactions);
  300. let _metadata = MetadataStoreInfo::new(&blockchain.streamlet_metadata);
  301. BlockchainInfo { _blocks, _order, _transactions, _metadata }
  302. }
  303. }
  304. #[derive(Debug)]
  305. struct StateInfo {
  306. _address: String,
  307. _consensus: ConsensusInfo,
  308. _blockchain: BlockchainInfo,
  309. }
  310. impl StateInfo {
  311. pub fn new(state: &ValidatorState) -> StateInfo {
  312. let _address = state.address.to_string();
  313. let _consensus = ConsensusInfo::new(&state.consensus);
  314. let _blockchain = BlockchainInfo::new(&state.blockchain);
  315. StateInfo { _address, _consensus, _blockchain }
  316. }
  317. }
  318. #[async_std::main]
  319. async fn main() -> Result<()> {
  320. let nodes = 4;
  321. let genesis_ts = Timestamp(1648383795);
  322. let genesis_data = *TESTNET_GENESIS_HASH_BYTES;
  323. let pass = "changeme";
  324. for i in 0..nodes {
  325. // Initialize or load wallet
  326. let path = format!("../../../tmp/node{:?}/wallet.db", i);
  327. let wallet = init_wallet(&path, &pass).await?;
  328. let address = wallet.get_default_address().await?;
  329. let client = Arc::new(Client::new(wallet).await?);
  330. // Initialize or load sled database
  331. let path = format!("../../../tmp/node{:?}/blockchain/testnet", i);
  332. let db_path = expand_path(&path).unwrap();
  333. let sled_db = sled::open(&db_path)?;
  334. // Data export
  335. println!("Exporting data for node{:?} - {:?}", i, address.to_string());
  336. let state =
  337. ValidatorState::new(&sled_db, genesis_ts, genesis_data, client, vec![], vec![]).await?;
  338. let info = StateInfo::new(&*state.read().await);
  339. let info_string = format!("{:#?}", info);
  340. let path = format!("node{:?}_testnet_db", i);
  341. let mut file = File::create(path)?;
  342. file.write(info_string.as_bytes())?;
  343. drop(sled_db);
  344. }
  345. Ok(())
  346. }