main.rs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. use std::{fs::File, io::Write};
  2. use darkfi::{
  3. consensus::{
  4. block::{Block, BlockOrderStore, BlockProposal, BlockStore},
  5. blockchain::{Blockchain, ProposalsChain},
  6. metadata::{Metadata, OuroborosMetadata, StreamletMetadata, StreamletMetadataStore},
  7. participant::Participant,
  8. state::{ConsensusState, ValidatorState},
  9. tx::{Tx, TxStore},
  10. util::Timestamp,
  11. vote::Vote,
  12. },
  13. util::expand_path,
  14. Result,
  15. };
  16. #[derive(Debug)]
  17. struct ParticipantInfo {
  18. _id: u64,
  19. _joined: u64,
  20. _voted: Option<u64>,
  21. }
  22. impl ParticipantInfo {
  23. pub fn new(participant: &Participant) -> ParticipantInfo {
  24. let _id = participant.id;
  25. let _joined = participant.joined;
  26. let _voted = participant.voted;
  27. ParticipantInfo { _id, _joined, _voted }
  28. }
  29. }
  30. #[derive(Debug)]
  31. struct VoteInfo {
  32. _proposal: blake3::Hash,
  33. _sl: u64,
  34. _id: u64,
  35. }
  36. impl VoteInfo {
  37. pub fn new(vote: &Vote) -> VoteInfo {
  38. let _proposal = vote.proposal;
  39. let _sl = vote.sl;
  40. let _id = vote.id;
  41. VoteInfo { _proposal, _sl, _id }
  42. }
  43. }
  44. #[derive(Debug)]
  45. struct StreamletMetadataInfo {
  46. _votes: Vec<VoteInfo>,
  47. _notarized: bool,
  48. _finalized: bool,
  49. _participants: Vec<ParticipantInfo>,
  50. }
  51. impl StreamletMetadataInfo {
  52. pub fn new(metadata: &StreamletMetadata) -> StreamletMetadataInfo {
  53. let mut _votes = Vec::new();
  54. for vote in &metadata.votes {
  55. _votes.push(VoteInfo::new(&vote));
  56. }
  57. let _notarized = metadata.notarized;
  58. let _finalized = metadata.finalized;
  59. let mut _participants = Vec::new();
  60. for participant in &metadata.participants {
  61. _participants.push(ParticipantInfo::new(&participant));
  62. }
  63. StreamletMetadataInfo { _votes, _notarized, _finalized, _participants }
  64. }
  65. }
  66. #[derive(Debug)]
  67. struct OuroborosMetadataInfo {
  68. _proof: String,
  69. _r: String,
  70. _s: String,
  71. }
  72. impl OuroborosMetadataInfo {
  73. pub fn new(metadata: &OuroborosMetadata) -> OuroborosMetadataInfo {
  74. let _proof = metadata.proof.clone();
  75. let _r = metadata.r.clone();
  76. let _s = metadata.s.clone();
  77. OuroborosMetadataInfo { _proof, _r, _s }
  78. }
  79. }
  80. #[derive(Debug)]
  81. struct MetadataInfo {
  82. _timestamp: Timestamp,
  83. _om: OuroborosMetadataInfo,
  84. }
  85. impl MetadataInfo {
  86. pub fn new(metadata: &Metadata) -> MetadataInfo {
  87. let _timestamp = metadata.timestamp.clone();
  88. let _om = OuroborosMetadataInfo::new(&metadata.om);
  89. MetadataInfo { _timestamp, _om }
  90. }
  91. }
  92. #[derive(Debug)]
  93. struct ProposalInfo {
  94. _id: u64,
  95. _st: blake3::Hash,
  96. _sl: u64,
  97. _txs: Vec<Tx>,
  98. _metadata: MetadataInfo,
  99. _sm: StreamletMetadataInfo,
  100. }
  101. impl ProposalInfo {
  102. pub fn new(proposal: &BlockProposal) -> ProposalInfo {
  103. let _id = proposal.id;
  104. let _st = proposal.st;
  105. let _sl = proposal.sl;
  106. let _txs = proposal.txs.clone();
  107. let _metadata = MetadataInfo::new(&proposal.metadata);
  108. let _sm = StreamletMetadataInfo::new(&proposal.sm);
  109. ProposalInfo { _id, _st, _sl, _txs, _metadata, _sm }
  110. }
  111. }
  112. #[derive(Debug)]
  113. struct ProposalsInfoChain {
  114. _proposals: Vec<ProposalInfo>,
  115. }
  116. impl ProposalsInfoChain {
  117. pub fn new(proposals: &ProposalsChain) -> ProposalsInfoChain {
  118. let mut _proposals = Vec::new();
  119. for proposal in &proposals.proposals {
  120. _proposals.push(ProposalInfo::new(&proposal));
  121. }
  122. ProposalsInfoChain { _proposals }
  123. }
  124. }
  125. #[derive(Debug)]
  126. struct ConsensusInfo {
  127. _genesis: Timestamp,
  128. _proposals: Vec<ProposalsInfoChain>,
  129. }
  130. impl ConsensusInfo {
  131. pub fn new(consensus: &ConsensusState) -> ConsensusInfo {
  132. let _genesis = consensus.genesis.clone();
  133. let mut _proposals = Vec::new();
  134. for proposal in &consensus.proposals {
  135. _proposals.push(ProposalsInfoChain::new(&proposal));
  136. }
  137. ConsensusInfo { _genesis, _proposals }
  138. }
  139. }
  140. #[derive(Debug)]
  141. struct BlockInfo {
  142. _hash: blake3::Hash,
  143. _st: blake3::Hash,
  144. _sl: u64,
  145. _txs: Vec<blake3::Hash>,
  146. }
  147. impl BlockInfo {
  148. pub fn new(_hash: blake3::Hash, block: &Block) -> BlockInfo {
  149. let _st = block.st;
  150. let _sl = block.sl;
  151. let _txs = block.txs.clone();
  152. BlockInfo { _hash, _st, _sl, _txs }
  153. }
  154. }
  155. #[derive(Debug)]
  156. struct BlockInfoChain {
  157. _blocks: Vec<BlockInfo>,
  158. }
  159. impl BlockInfoChain {
  160. pub fn new(blockstore: &BlockStore) -> BlockInfoChain {
  161. let mut _blocks = Vec::new();
  162. let result = blockstore.get_all();
  163. match result {
  164. Ok(iter) => {
  165. for item in iter.iter() {
  166. match item {
  167. Some((hash, block)) => _blocks.push(BlockInfo::new(hash.clone(), &block)),
  168. None => (),
  169. };
  170. }
  171. }
  172. Err(e) => println!("Error: {:?}", e),
  173. }
  174. BlockInfoChain { _blocks }
  175. }
  176. }
  177. #[derive(Debug)]
  178. struct OrderInfo {
  179. _sl: u64,
  180. _hash: blake3::Hash,
  181. }
  182. impl OrderInfo {
  183. pub fn new(_sl: u64, _hash: blake3::Hash) -> OrderInfo {
  184. OrderInfo { _sl, _hash }
  185. }
  186. }
  187. #[derive(Debug)]
  188. struct BlockOrderStoreInfo {
  189. _order: Vec<OrderInfo>,
  190. }
  191. impl BlockOrderStoreInfo {
  192. pub fn new(orderstore: &BlockOrderStore) -> BlockOrderStoreInfo {
  193. let mut _order = Vec::new();
  194. let result = orderstore.get_all();
  195. match result {
  196. Ok(iter) => {
  197. for item in iter.iter() {
  198. match item {
  199. Some((slot, hash)) => {
  200. _order.push(OrderInfo::new(slot.clone(), hash.clone()))
  201. }
  202. None => (),
  203. };
  204. }
  205. }
  206. Err(e) => println!("Error: {:?}", e),
  207. }
  208. BlockOrderStoreInfo { _order }
  209. }
  210. }
  211. #[derive(Debug)]
  212. struct TxInfo {
  213. _hash: blake3::Hash,
  214. _payload: String,
  215. }
  216. impl TxInfo {
  217. pub fn new(_hash: blake3::Hash, tx: &Tx) -> TxInfo {
  218. let _payload = tx.payload.clone();
  219. TxInfo { _hash, _payload }
  220. }
  221. }
  222. #[derive(Debug)]
  223. struct TxStoreInfo {
  224. _transactions: Vec<TxInfo>,
  225. }
  226. impl TxStoreInfo {
  227. pub fn new(txstore: &TxStore) -> TxStoreInfo {
  228. let mut _transactions = Vec::new();
  229. let result = txstore.get_all();
  230. match result {
  231. Ok(iter) => {
  232. for item in iter.iter() {
  233. match item {
  234. Some((hash, tx)) => _transactions.push(TxInfo::new(hash.clone(), &tx)),
  235. None => (),
  236. };
  237. }
  238. }
  239. Err(e) => println!("Error: {:?}", e),
  240. }
  241. TxStoreInfo { _transactions }
  242. }
  243. }
  244. #[derive(Debug)]
  245. struct HashedMetadataInfo {
  246. _block: blake3::Hash,
  247. _metadata: StreamletMetadataInfo,
  248. }
  249. impl HashedMetadataInfo {
  250. pub fn new(_block: blake3::Hash, metadata: &StreamletMetadata) -> HashedMetadataInfo {
  251. let _metadata = StreamletMetadataInfo::new(&metadata);
  252. HashedMetadataInfo { _block, _metadata }
  253. }
  254. }
  255. #[derive(Debug)]
  256. struct MetadataStoreInfo {
  257. _metadata: Vec<HashedMetadataInfo>,
  258. }
  259. impl MetadataStoreInfo {
  260. pub fn new(metadatastore: &StreamletMetadataStore) -> MetadataStoreInfo {
  261. let mut _metadata = Vec::new();
  262. let result = metadatastore.get_all();
  263. match result {
  264. Ok(iter) => {
  265. for item in iter.iter() {
  266. match item {
  267. Some((hash, m)) => {
  268. _metadata.push(HashedMetadataInfo::new(hash.clone(), &m))
  269. }
  270. None => (),
  271. };
  272. }
  273. }
  274. Err(e) => println!("Error: {:?}", e),
  275. }
  276. MetadataStoreInfo { _metadata }
  277. }
  278. }
  279. #[derive(Debug)]
  280. struct BlockchainInfo {
  281. _blocks: BlockInfoChain,
  282. _order: BlockOrderStoreInfo,
  283. _transactions: TxStoreInfo,
  284. _metadata: MetadataStoreInfo,
  285. }
  286. impl BlockchainInfo {
  287. pub fn new(blockchain: &Blockchain) -> BlockchainInfo {
  288. let _blocks = BlockInfoChain::new(&blockchain.blocks);
  289. let _order = BlockOrderStoreInfo::new(&blockchain.order);
  290. let _transactions = TxStoreInfo::new(&blockchain.transactions);
  291. let _metadata = MetadataStoreInfo::new(&blockchain.streamlet_metadata);
  292. BlockchainInfo { _blocks, _order, _transactions, _metadata }
  293. }
  294. }
  295. #[derive(Debug)]
  296. struct StateInfo {
  297. _id: u64,
  298. _consensus: ConsensusInfo,
  299. _blockchain: BlockchainInfo,
  300. }
  301. impl StateInfo {
  302. pub fn new(state: &ValidatorState) -> StateInfo {
  303. let _id = state.id;
  304. let _consensus = ConsensusInfo::new(&state.consensus);
  305. let _blockchain = BlockchainInfo::new(&state.blockchain);
  306. StateInfo { _id, _consensus, _blockchain }
  307. }
  308. }
  309. fn main() -> Result<()> {
  310. let nodes = 4;
  311. let genesis = 1648383795;
  312. for i in 0..nodes {
  313. let path = format!("~/.config/darkfi/validatord_db_{:?}", i);
  314. let database_path = expand_path(&path).unwrap();
  315. println!("Export data from sled database: {:?}", database_path);
  316. let state = ValidatorState::new(database_path, i, genesis).unwrap();
  317. let info = StateInfo::new(&*state.read().unwrap());
  318. let info_string = format!("{:#?}", info);
  319. let path = format!("validatord_state_{:?}", i);
  320. let mut file = File::create(path)?;
  321. file.write(info_string.as_bytes())?;
  322. }
  323. Ok(())
  324. }