main.rs 12 KB

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