Skip to content

Commit

Permalink
feat: fmt code
Browse files Browse the repository at this point in the history
  • Loading branch information
yubing744 committed Sep 2, 2024
1 parent 416ce8c commit a1f34f5
Showing 1 changed file with 94 additions and 93 deletions.
187 changes: 94 additions & 93 deletions examples/bitseed_generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use cosmwasm_std::{
to_json_binary, Binary, Deps, Env, StdResult, Response, MessageInfo, DepsMut,
entry_point,
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use serde::{Deserialize, Serialize};

Expand All @@ -10,137 +9,142 @@ pub struct InstantiateMsg {}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
InscribeGenerate(InputData),
InscribeVerify { input: InputData, output: OutputData },
IndexerGenerate(InputData),
InscribeGenerate(InputData),
InscribeVerify {
input: InputData,
output: OutputData,
},
IndexerGenerate(InputData),
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct InputData {
seed: String,
user_input: String,
attributes: Vec<AttributeGenerateRule>,
seed: String,
user_input: String,
attributes: Vec<AttributeGenerateRule>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct AttributeGenerateRule {
key: String,
rule_type: AttributeRuleType,
parameters: Vec<String>,
key: String,
rule_type: AttributeRuleType,
parameters: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum AttributeRuleType {
Range,
List,
Range,
List,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct OutputData {
amount: u32,
attributes: Vec<Attribute>,
content: Binary,
amount: u32,
attributes: Vec<Attribute>,
content: Binary,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Attribute {
key: String,
value: String,
key: String,
value: String,
}


#[entry_point]
pub fn instantiate(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> StdResult<Response> {
Ok(Response::default())
Ok(Response::default())
}

#[entry_point]
pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::InscribeGenerate(input) => to_json_binary(&inscribe_generate(input)?),
QueryMsg::InscribeVerify { input, output } => to_json_binary(&inscribe_verify(input, output)?),
QueryMsg::IndexerGenerate(input) => to_json_binary(&indexer_generate(input)?),
}
match msg {
QueryMsg::InscribeGenerate(input) => to_json_binary(&inscribe_generate(input)?),
QueryMsg::InscribeVerify { input, output } => {
to_json_binary(&inscribe_verify(input, output)?)
}
QueryMsg::IndexerGenerate(input) => to_json_binary(&indexer_generate(input)?),
}
}

fn inscribe_generate(input: InputData) -> StdResult<OutputData> {
let hash_value = hash_str_uint32(&format!("{}{}", input.seed, input.user_input));

let mut attributes = vec![
Attribute {
key: "id".to_string(),
value: input.user_input.clone(),
}
];

for attr in input.attributes {
let value = match attr.rule_type {
AttributeRuleType::Range => {
if attr.parameters.len() == 2 {
if let (Ok(min), Ok(max)) = (attr.parameters[0].parse::<u32>(), attr.parameters[1].parse::<u32>()) {
let random_value = min + (hash_value % (max - min + 1));
random_value.to_string()
} else {
continue; // Skip this attribute if parsing fails
}
} else {
continue; // Skip this attribute if parameters are incorrect
}
},
AttributeRuleType::List => {
if !attr.parameters.is_empty() {
let index = (hash_value as usize) % attr.parameters.len();
attr.parameters[index].clone()
} else {
continue; // Skip this attribute if the list is empty
}
},
};

attributes.push(Attribute {
key: attr.key,
value,
});
}

Ok(OutputData {
amount: 1000,
attributes,
content: Binary::default(),
})
let hash_value = hash_str_uint32(&format!("{}{}", input.seed, input.user_input));

let mut attributes = vec![Attribute {
key: "id".to_string(),
value: input.user_input.clone(),
}];

for attr in input.attributes {
let value = match attr.rule_type {
AttributeRuleType::Range => {
if attr.parameters.len() == 2 {
if let (Ok(min), Ok(max)) = (
attr.parameters[0].parse::<u32>(),
attr.parameters[1].parse::<u32>(),
) {
let random_value = min + (hash_value % (max - min + 1));
random_value.to_string()
} else {
continue; // Skip this attribute if parsing fails
}
} else {
continue; // Skip this attribute if parameters are incorrect
}
}
AttributeRuleType::List => {
if !attr.parameters.is_empty() {
let index = (hash_value as usize) % attr.parameters.len();
attr.parameters[index].clone()
} else {
continue; // Skip this attribute if the list is empty
}
}
};

attributes.push(Attribute {
key: attr.key,
value,
});
}

Ok(OutputData {
amount: 1000,
attributes,
content: Binary::default(),
})
}

fn inscribe_verify(input: InputData, output: OutputData) -> StdResult<bool> {
let generated_output = inscribe_generate(input)?;
Ok(generated_output == output)
let generated_output = inscribe_generate(input)?;
Ok(generated_output == output)
}

fn indexer_generate(input: InputData) -> StdResult<OutputData> {
inscribe_generate(input)
inscribe_generate(input)
}

fn hash_str_uint32(str: &str) -> u32 {
let mut hash: u32 = 0x811c9dc5;
let prime: u32 = 0x1000193;
let mut hash: u32 = 0x811c9dc5;
let prime: u32 = 0x1000193;

for &byte in str.as_bytes() {
hash ^= u32::from(byte);
hash = hash.wrapping_mul(prime);
}
for &byte in str.as_bytes() {
hash ^= u32::from(byte);
hash = hash.wrapping_mul(prime);
}

hash
hash
}

#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::from_json;
use cosmwasm_std::testing::{mock_dependencies, mock_env};

#[test]
fn test_inscribe_generate() {
Expand Down Expand Up @@ -204,17 +208,15 @@ mod tests {
let input = InputData {
seed: "test_seed".to_string(),
user_input: "test_user_input".to_string(),
attributes: vec![
AttributeGenerateRule {
key: "test_range".to_string(),
rule_type: AttributeRuleType::Range,
parameters: vec!["1".to_string(), "10".to_string()],
},
],
attributes: vec![AttributeGenerateRule {
key: "test_range".to_string(),
rule_type: AttributeRuleType::Range,
parameters: vec!["1".to_string(), "10".to_string()],
}],
};

// Generate expected output
let expected_output = inscribe_generate( input.clone()).unwrap();
let expected_output = inscribe_generate(input.clone()).unwrap();

// Test verification with correct output
let query_msg = QueryMsg::InscribeVerify {
Expand All @@ -238,5 +240,4 @@ mod tests {
let result: bool = from_json(&binary_response).unwrap();
assert!(!result);
}

}

0 comments on commit a1f34f5

Please sign in to comment.