library_blockchain/
blockchain_executive.rs

1#![no_main]
2// #![deny(rust_2018_idioms)]
3// #![warn(rust_2018_idioms)]
4
5use env_logger::{Builder, Target};
6extern crate core; // Required to use the `core` crate in Rust 2015.
7use core::any;
8use serde_json::{json};
9use  library_utils::{*, stringtou128::string_to_u128};
10use std::env;
11use std::env::set_var;
12use std::fs::File;
13use std::io::BufReader;
14use log::{log_enabled, info, Level};
15
16use crate::{Blockchain, factory::blockchain_factory, CustomError};
17
18//pub mod sample;
19
20
21#[deny(elided_lifetimes_in_paths)]
22#[allow(dead_code)]
23#[allow(unused_mut)]
24
25/// DIFFICULTY=0x000fffffffffffffffffffffffffffff cargo run
26/// RUST_LOG=INFO DIFFICULTY=0x00ffffffffffffffffffffffffffffff time cargo run file sample-blocks.json 
27
28pub fn main() -> Result<(), CustomError> {
29
30  
31    blockchain_init_env_logger(true);
32
33    info!("Starting Up...");
34   
35    let difficulty_str = blockchain_var_ret_difficulty("0x00ffffffffffffffffffffffffffffff");
36    if difficulty_str.is_empty() {
37        set_var("DIFFICULTY", "0x00ffffffffffffffffffffffffffffff");
38    }
39
40    let difficulty = string_to_u128(&difficulty_str);
41    let mut args: Vec<String> = env::args().collect();
42    let mut mode = String::default();
43    let mut file_name = String::default();
44    let blockchain = Blockchain::new();
45
46    if (&args).len() <= 1 {
47        println!("** Please select a runner mode\n Help(file path transaction_list, or macrojson transaction_list)\n Default is cargo run macrojson **\n");
48        args.push("macrojson".to_owned());
49    } else {
50        mode = (&args[1]).trim().to_lowercase();
51    }
52    if &mode == "file" {
53        file_name = (&args[2]).trim().to_lowercase();
54        let file = File::open(&file_name).unwrap();
55        println!("**************************************************************");
56        blockchain_factory(blockchain, difficulty, || {
57            blockchain_sample_trx_json_data_block_from_file(&file)
58        })?;
59                
60    } else if &mode == "macrojson" {
61        println!("Selected mode is macrojson\n");
62        blockchain_factory(blockchain, difficulty, || {
63            blockchain_sample_trx_json_data_from_module()
64        })?;
65        
66    } else if &mode == "stringjson" {
67        println!("Selected mode is stringjson\n");
68        blockchain_factory(blockchain, difficulty, || {
69            blockchain_sample_trx_json_data_from_string()
70        })?;
71        
72    } else {
73        println!("The mode is not selected! Default is macrojson\n");
74        blockchain_factory(blockchain, difficulty, || {
75            blockchain_sample_trx_json_data_from_module()
76        })?;
77        
78    }
79    Ok(())
80}
81
82pub fn blockchain_init_env_logger(is_enable:bool) {
83        
84    let mut builder = Builder::from_default_env();
85    builder.target(Target::Stdout);
86    if is_enable{
87       builder.init();
88    }
89        
90    if log_enabled!(Level::Info) {            
91        info!("------------Welcome to env_logger------------");
92    }
93    else  {
94        println!("----------env_logger have not been activated----------");
95    }
96}
97
98pub fn blockchain_sample_trx_json_data_block_from_file(file: &File) -> Result<serde_json::Value, CustomError> {
99    println!("Selected mode is file!");
100
101    let reader = BufReader::new(file);
102    let serde_values_transactions = serde_json::from_reader(reader)?;
103    
104
105    return Ok(serde_values_transactions);
106}
107
108
109
110
111pub fn blockchain_sample_trx_json_data_from_module() -> Result<serde_json::Value, CustomError> {
112    Ok(json!({
113    "blocks":[{
114        "block1":[{
115            "transactions":[{
116                "transaction1":[{
117                        "inputs":[{
118                           
119                        }],
120                        "outputs":[{
121                            "to_addr": "Alice",
122                            "value": "50"
123                        },{
124                            "to_addr": "Bob",
125                            "value": "10"
126                        }]
127                }] 
128            }]
129        }],
130        "block2":[{
131            "transactions":[{
132                "transaction1":[{
133                        "inputs":[{                          
134                        }],
135                        "outputs":[{
136                            "to_addr": "Alice",
137                            "value": "1000"
138                        },{
139                            "to_addr": "Bob",
140                            "value": "1000"
141                        }]
142                }] ,
143                "transaction2":[{
144                        "inputs":[{                    
145                            "to_addr": "Alice",
146                            "value": "50"
147                        },{
148                            "to_addr": "Bob",
149                            "value": "10"
150                        }],
151                        "outputs":[{
152                            "to_addr": "Alice",
153                            "value": "40"
154                        },{
155                            "to_addr": "Bob",
156                            "value": "20"
157                        }]
158                }]
159            }]
160        }]
161      }]
162    }))
163}
164
165pub fn blockchain_sample_trx_json_data_from_string() -> Result<serde_json::Value, CustomError> {
166    let json = r#"{
167    "blocks":[{    
168        "block1":[{
169            "transactions":[{
170                    "transaction1":[{
171                        "inputs":[{                          
172                        }],
173                        "outputs":[{
174                            "to_addr": "Alice",
175                            "value": "1000"
176                        },{
177                            "to_addr": "Bob",
178                            "value": "1000"
179                        }]
180                    }]
181            }]
182        }]
183    }]
184    }
185    "#;
186
187    let js = serde_json::from_str(json).unwrap();
188
189    Ok(js)
190}
191
192
193pub fn blockchain_var_ret_difficulty(difficulty_arg: &str) -> String {
194    match env::var("DIFFICULTY") {
195        Ok(val) => val,
196        Err(e) => {
197            eprintln!("\n🦀{e}! We used default difficulty🦀\n");
198            env::var("DIFFICULTY").unwrap_or(difficulty_arg.to_owned())
199        }
200    }
201}