Selaa lähdekoodia

research/pallas_constants: Use rustfmt to format output.

parazyd 3 vuotta sitten
vanhempi
sitoutus
81a2f6a0fa

+ 1 - 0
script/research/pallas_constants/Cargo.toml

@@ -10,6 +10,7 @@ edition = "2021"
 [workspace]
 
 [dependencies]
+anyhow = "1.0.71"
 darkfi = {path = "../../../", features = ["blockchain"]}
 darkfi-sdk = {path = "../../../src/sdk"}
 

+ 52 - 17
script/research/pallas_constants/src/main.rs

@@ -16,26 +16,57 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use std::{
+    io,
+    io::Write,
+    process::{Command, Stdio},
+};
+
+use anyhow::Result;
 use darkfi::consensus::lead_coin::LeadCoin;
 use darkfi_sdk::{crypto::pasta_prelude::PrimeField, pasta::pallas};
 
 /// Generate a string represenation of a pallas::Base constant
-fn to_constant(name: &str, pallas: pallas::Base) -> String {
-    let repr = pallas.to_repr();
-    let mut res = [0; 4];
+fn to_constant(name: &str, x: pallas::Base, public: bool) -> String {
+    let repr = x.to_repr();
+    let mut res = [0_u64; 4];
+
     res[0] = u64::from_le_bytes(repr[0..8].try_into().unwrap());
     res[1] = u64::from_le_bytes(repr[8..16].try_into().unwrap());
     res[2] = u64::from_le_bytes(repr[16..24].try_into().unwrap());
     res[3] = u64::from_le_bytes(repr[24..32].try_into().unwrap());
 
-    format!("    const {name}: pallas::Base = pallas::Base::from_raw({res:?});")
+    let p = if public { "pub" } else { "" };
+
+    format!("{p} const {name}: pallas::Base = pallas::Base::from_raw({res:?});\n")
 }
 
 /// Generate constants for corresponding pallas::Base.
-fn main() {
-    let headstart = to_constant("HEADSTART", LeadCoin::headstart());
-    println!("Constants:");
-    println!("{headstart}");
+fn main() -> Result<()> {
+    let mut source = String::new();
+    source.push_str(&to_constant("HEADSTART", LeadCoin::headstart(), false));
+
+    let mut cmd = Command::new("rustfmt");
+    cmd.stdin(Stdio::piped()).stdout(Stdio::piped());
+    cmd.args(["--edition=2021"]);
+
+    let mut child = cmd.spawn()?;
+    let mut child_stdin = child.stdin.take().unwrap();
+    let mut child_stdout = child.stdout.take().unwrap();
+
+    let stdin_handle = std::thread::spawn(move || {
+        let _ = child_stdin.write_all(source.as_bytes());
+        source
+    });
+
+    let mut output = vec![];
+    io::copy(&mut child_stdout, &mut output)?;
+
+    let _ = stdin_handle.join().unwrap();
+    let output = String::from_utf8(output)?;
+    print!("{}", output);
+
+    Ok(())
 }
 
 #[cfg(test)]
@@ -43,16 +74,20 @@ mod tests {
     use darkfi::consensus::lead_coin::LeadCoin;
     use darkfi_sdk::pasta::pallas;
 
-    const HEADSTART: pallas::Base = pallas::Base::from_raw([
-        11731824086999220879,
-        11830614503713258191,
-        737869762948382064,
-        46116860184273879,
-    ]);
-
     #[test]
-    fn test_headstart() {
+    fn consistency() {
+        let zero = pallas::Base::zero();
+        let zero_arr = [0, 0, 0, 0];
+
+        let one = pallas::Base::one();
+        let one_arr = [1, 0, 0, 0];
+
         let headstart = LeadCoin::headstart();
-        assert_eq!(headstart, HEADSTART);
+        let headstart_arr =
+            [11731824086999220879, 11830614503713258191, 737869762948382064, 46116860184273879];
+
+        assert_eq!(zero, pallas::Base::from_raw(zero_arr));
+        assert_eq!(one, pallas::Base::from_raw(one_arr));
+        assert_eq!(headstart, pallas::Base::from_raw(headstart_arr));
     }
 }