Skip to content

Commit

Permalink
better settings management and handle 404 reponses
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Dec 3, 2024
1 parent 0cf47fb commit d89bf87
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 41 deletions.
4 changes: 2 additions & 2 deletions components/clarinet-deployments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ pub fn update_session_with_deployment_plan(
contracts_asts: Option<&BTreeMap<QualifiedContractIdentifier, ContractAST>>,
forced_min_epoch: Option<StacksEpochId>,
) -> UpdateSessionExecutionResult {
session.advance_chain_tip(10);
update_session_with_genesis_accounts(session, deployment);
// session.advance_chain_tip(10);
// update_session_with_genesis_accounts(session, deployment);

let mut boot_contracts = BTreeMap::new();
if !session.settings.repl_settings.remote_data.enabled {
Expand Down
96 changes: 57 additions & 39 deletions components/clarity-repl/src/repl/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ impl Default for ClarityDatastore {

impl ClarityDatastore {
pub fn new(remote_data_settings: RemoteDataSettings) -> Self {
let id = height_to_id(0);
let block_height = 108;
let id = height_to_id(block_height);
Self {
open_chain_tip: id,
current_chain_tip: id,
store: HashMap::from([(id, HashMap::new())]),
metadata: HashMap::new(),
block_id_lookup: HashMap::from([(id, id)]),
height_at_chain_tip: HashMap::from([(id, 0)]),
height_at_chain_tip: HashMap::from([(id, block_height)]),

remote_data_settings,
remote_chaintip_cache: HashMap::new(),
Expand Down Expand Up @@ -232,16 +233,16 @@ impl ClarityDatastore {
}

#[cfg(not(target_arch = "wasm32"))]
fn fetch_data<T: serde::de::DeserializeOwned>(&self, path: &str) -> Result<T> {
fn fetch_data<T: serde::de::DeserializeOwned>(&self, path: &str) -> Result<Option<T>> {
let url = format!("{}{}", self.remote_data_settings.api_url, path);
println!("fetching: {}", url);
let response =
reqwest::blocking::get(url).unwrap_or_else(|e| panic!("unable to fetch data: {}", e));

let data = response.json::<T>().unwrap_or_else(|e| {
panic!("unable to parse json, error: {}", e);
});

Ok(data)
match response.json::<T>() {
Ok(data) => Ok(Some(data)),
_ => Ok(None),
}
}

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -278,6 +279,9 @@ impl ClarityDatastore {
.fetch_data::<BlockInfoResponse>(&url)
.unwrap_or_else(|e| {
panic!("unable to parse json, error: {}", e);
})
.unwrap_or_else(|| {
panic!("unable to get remote chaintip");
});

let block_hash = data.index_block_hash.replacen("0x", "", 1);
Expand All @@ -294,21 +298,26 @@ impl ClarityDatastore {
panic!("unable to parse json, error: {}", e);
});

let value = if data.data.starts_with("0x") {
data.data[2..].to_string()
} else {
data.data
};

Ok(Some(value))
match data {
Some(data) => {
let value = if data.data.starts_with("0x") {
data.data[2..].to_string()
} else {
data.data
};
Ok(Some(value))
}
None => Ok(None),
}
}

fn fetch_clarity_marf_value(&mut self, key: &str) -> Result<Option<String>> {
let key_hash = TrieHash::from_key(key);
let block_height = self.get_current_block_height();
let remote_chaintip = self.get_remote_chaintip(block_height);
let path = format!(
"/v2/clarity/marf/{}?tip={}&proof=false",
key, remote_chaintip
key_hash, remote_chaintip
);
self.fetch_remote_data(&path)
}
Expand Down Expand Up @@ -365,13 +374,16 @@ impl ClarityBackingStore for ClarityDatastore {
if value.is_some() {
return Ok(value.cloned());
}
uprint!("fetching MARF from network");
let data = self.fetch_clarity_marf_value(key);
if let Ok(Some(value)) = &data {
uprint!("network marf value: {}", value);
self.put(key, value);
if self.remote_data_settings.enabled {
uprint!("fetching MARF from network");
let data = self.fetch_clarity_marf_value(key);
if let Ok(Some(value)) = &data {
uprint!("network marf value: {}", value);
self.put(key, value);
}
return data;
}
data
Ok(None)
} else {
panic!("Block does not exist for current chain tip");
}
Expand Down Expand Up @@ -442,6 +454,7 @@ impl ClarityBackingStore for ClarityDatastore {
key: &str,
value: &str,
) -> Result<()> {
println!("inserting metadata({}, {}", contract, key);
self.metadata
.insert((contract.to_string(), key.to_string()), value.to_string());
Ok(())
Expand Down Expand Up @@ -495,54 +508,59 @@ impl ClarityBackingStore for ClarityDatastore {

impl Default for Datastore {
fn default() -> Self {
Self::new(StacksConstants {
burn_start_height: 0,
pox_prepare_length: 50,
pox_reward_cycle_length: 1050,
pox_rejection_fraction: 0,
})
Self::new(
None,
StacksConstants {
burn_start_height: 1,
pox_prepare_length: 50,
pox_reward_cycle_length: 1050,
pox_rejection_fraction: 0,
},
)
}
}

impl Datastore {
pub fn new(constants: StacksConstants) -> Self {
let bytes = height_to_hashed_bytes(0);
pub fn new(initial_height: Option<u32>, constants: StacksConstants) -> Self {
let block_height = initial_height.unwrap_or(108);
let burn_block_height = 216;
let bytes = height_to_hashed_bytes(block_height);
let id = StacksBlockId(bytes);
let sortition_id = SortitionId(bytes);
let genesis_time = chrono::Utc::now().timestamp() as u64;

let first_burn_block_header_hash = BurnchainHeaderHash([0x00; 32]);
let first_burn_block_header_hash = height_to_burn_block_header_hash(burn_block_height);

let genesis_burn_block = BurnBlockInfo {
burn_block_time: genesis_time,
burn_block_height: 0,
burn_block_height,
};

let genesis_block = StacksBlockInfo {
block_header_hash: BlockHeaderHash([0x00; 32]),
block_header_hash: BlockHeaderHash(bytes),
burn_block_header_hash: first_burn_block_header_hash,
consensus_hash: ConsensusHash([0x00; 20]),
vrf_seed: VRFSeed([0x00; 32]),
consensus_hash: ConsensusHash::from_bytes(&bytes[0..20]).unwrap(),
vrf_seed: VRFSeed(bytes),
stacks_block_time: genesis_time + SECONDS_BETWEEN_STACKS_BLOCKS,
};

let sortition_lookup = HashMap::from([(sortition_id, id)]);
let consensus_hash_lookup = HashMap::from([(genesis_block.consensus_hash, sortition_id)]);
let tenure_blocks_height = HashMap::from([(0, 0)]);
let tenure_blocks_height = HashMap::from([(1, 1)]);
let burn_blocks = HashMap::from([(first_burn_block_header_hash, genesis_burn_block)]);
let stacks_blocks = HashMap::from([(id, genesis_block)]);

Datastore {
genesis_id: id,
burn_chain_height: 0,
burn_chain_height: burn_block_height,
burn_blocks,
stacks_chain_height: 0,
stacks_chain_height: block_height,
stacks_blocks,
sortition_lookup,
consensus_hash_lookup,
tenure_blocks_height,
current_epoch: StacksEpochId::Epoch2_05,
current_epoch_start_height: 0,
current_epoch_start_height: 1,
constants,
}
}
Expand Down

0 comments on commit d89bf87

Please sign in to comment.