pub struct Config {
pub salt: String,
}Expand description
Configuration for generting proof of work Please choose a long, unique value for salt Resistance to dictionary/rainbow attacks depend on uniqueness of the salt
Fields§
§salt: StringImplementations§
Source§impl Config
impl Config
Sourcepub fn prove_work<T>(&self, t: &T, difficulty: u32) -> Result<PoW<T>>where
T: Serialize,
pub fn prove_work<T>(&self, t: &T, difficulty: u32) -> Result<PoW<T>>where
T: Serialize,
Create Proof of Work over item of type T.
Make sure difficulty is not too high. A 64 bit difficulty, for example, takes a long time on a general purpose processor. Returns bincode::Error if serialization fails.
Examples found in repository?
12fn main() {
13 let config = ConfigBuilder::default()
14 .salt("myrandomsaltisnotlongenoug".into())
15 .build()
16 .unwrap();
17
18 let phrase = "ironmansucks";
19
20 const DIFFICULTY: u32 = 1000;
21
22 let work = config.prove_work(&phrase, DIFFICULTY).unwrap();
23 assert!(config.is_valid_proof(&work, &phrase));
24 assert!(config.is_sufficient_difficulty(&work, DIFFICULTY));
25}Sourcepub fn prove_work_serialized<T>(&self, prefix: &[u8], difficulty: u32) -> PoW<T>where
T: Serialize,
pub fn prove_work_serialized<T>(&self, prefix: &[u8], difficulty: u32) -> PoW<T>where
T: Serialize,
Create Proof of Work on an already serialized item of type T. The input is assumed to be serialized using network byte order.
Make sure difficulty is not too high. A 64 bit difficulty, for example, takes a long time on a general purpose processor.
Sourcepub fn calculate<T>(&self, pow: &PoW<T>, t: &T) -> Result<u128>where
T: Serialize,
pub fn calculate<T>(&self, pow: &PoW<T>, t: &T) -> Result<u128>where
T: Serialize,
Calculate the PoW score with the provided input T.
Sourcepub fn calculate_serialized<T>(&self, pow: &PoW<T>, target: &[u8]) -> u128where
T: Serialize,
pub fn calculate_serialized<T>(&self, pow: &PoW<T>, target: &[u8]) -> u128where
T: Serialize,
Calculate the PoW score of an already serialized T and self. The input is assumed to be serialized using network byte order.
Sourcepub fn is_valid_proof<T>(&self, pow: &PoW<T>, t: &T) -> boolwhere
T: Serialize,
pub fn is_valid_proof<T>(&self, pow: &PoW<T>, t: &T) -> boolwhere
T: Serialize,
Verifies that the PoW is indeed generated out of the phrase provided.
Examples found in repository?
12fn main() {
13 let config = ConfigBuilder::default()
14 .salt("myrandomsaltisnotlongenoug".into())
15 .build()
16 .unwrap();
17
18 let phrase = "ironmansucks";
19
20 const DIFFICULTY: u32 = 1000;
21
22 let work = config.prove_work(&phrase, DIFFICULTY).unwrap();
23 assert!(config.is_valid_proof(&work, &phrase));
24 assert!(config.is_sufficient_difficulty(&work, DIFFICULTY));
25}Sourcepub fn is_sufficient_difficulty<T>(
&self,
pow: &PoW<T>,
target_diff: u32,
) -> boolwhere
T: Serialize,
pub fn is_sufficient_difficulty<T>(
&self,
pow: &PoW<T>,
target_diff: u32,
) -> boolwhere
T: Serialize,
Checks if the PoW result is of sufficient difficulty
Examples found in repository?
12fn main() {
13 let config = ConfigBuilder::default()
14 .salt("myrandomsaltisnotlongenoug".into())
15 .build()
16 .unwrap();
17
18 let phrase = "ironmansucks";
19
20 const DIFFICULTY: u32 = 1000;
21
22 let work = config.prove_work(&phrase, DIFFICULTY).unwrap();
23 assert!(config.is_valid_proof(&work, &phrase));
24 assert!(config.is_sufficient_difficulty(&work, DIFFICULTY));
25}