Bladeren bron

script/research: added tool to retrieve bs58encoded consensus headstart pallas

aggstam 3 jaren geleden
bovenliggende
commit
958db02b22

+ 3 - 0
script/research/consensus_headstart/.gitignore

@@ -0,0 +1,3 @@
+/target
+Cargo.lock
+rustfmt.toml

+ 21 - 0
script/research/consensus_headstart/Cargo.toml

@@ -0,0 +1,21 @@
+[package]
+name = "consensus_headstart"
+version = "0.4.1"
+description = "Command-line tool to extract currently configured consensus headstart parameter"
+authors = ["Dyne.org foundation <foundation@dyne.org>"]
+repository = "https://github.com/darkrenaissance/darkfi"
+license = "AGPL-3.0-only"
+edition = "2021"
+
+[workspace]
+
+[dependencies]
+bs58 = "0.4.0"
+darkfi = {path = "../../../", features = ["blockchain"]}
+darkfi-serial = {path = "../../../src/serial"}
+
+[dev-dependencies]
+darkfi-sdk = {path = "../../../src/sdk"}
+
+[patch.crates-io]
+halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v3"}

+ 53 - 0
script/research/consensus_headstart/src/main.rs

@@ -0,0 +1,53 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2023 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+use darkfi::consensus::lead_coin::LeadCoin;
+use darkfi_serial::serialize;
+
+/// Extract currently configured consensus headstart parameter
+/// pallas::Base representation and print its bs58 encoding.
+fn main() {
+    let headstart = bs58::encode(&serialize(&LeadCoin::headstart())).into_string();
+    println!("Currently configured headstart(encoded): {headstart}");
+}
+
+#[cfg(test)]
+mod tests {
+    use darkfi::{consensus::lead_coin::LeadCoin, Error, Result};
+    use darkfi_sdk::{crypto::pasta_prelude::PrimeField, pasta::pallas};
+    use darkfi_serial::serialize;
+
+    #[test]
+    fn test_conversion() -> Result<()> {
+        let headstart = LeadCoin::headstart();
+        let encoded = bs58::encode(&serialize(&headstart)).into_string();
+        let pallas = from_bs58str(&encoded)?;
+        assert_eq!(headstart, pallas);
+
+        Ok(())
+    }
+
+    fn from_bs58str(s: &str) -> Result<pallas::Base> {
+        let decoded = bs58::decode(s).into_vec()?;
+        if decoded.len() != 32 {
+            return Err(Error::DecodeError("Decoded bs58 string is not 32 bytes long"))
+        }
+        let bytes: [u8; 32] = decoded.try_into().unwrap();
+        Ok(pallas::Base::from_repr(bytes).unwrap())
+    }
+}