main.rs 10 KB

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