From d89bf87a1644ac767e81d2654240b26df5643532 Mon Sep 17 00:00:00 2001 From: Hugo CAILLARD <911307+hugocaillard@users.noreply.github.com> Date: Tue, 3 Dec 2024 19:31:49 +0100 Subject: [PATCH] better settings management and handle 404 reponses --- components/clarinet-deployments/src/lib.rs | 4 +- components/clarity-repl/src/repl/datastore.rs | 96 +++++++++++-------- 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/components/clarinet-deployments/src/lib.rs b/components/clarinet-deployments/src/lib.rs index 1cde83de4..3cf813529 100644 --- a/components/clarinet-deployments/src/lib.rs +++ b/components/clarinet-deployments/src/lib.rs @@ -124,8 +124,8 @@ pub fn update_session_with_deployment_plan( contracts_asts: Option<&BTreeMap>, forced_min_epoch: Option, ) -> 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 { diff --git a/components/clarity-repl/src/repl/datastore.rs b/components/clarity-repl/src/repl/datastore.rs index 7e4687deb..1a3a7f82b 100644 --- a/components/clarity-repl/src/repl/datastore.rs +++ b/components/clarity-repl/src/repl/datastore.rs @@ -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(), @@ -232,16 +233,16 @@ impl ClarityDatastore { } #[cfg(not(target_arch = "wasm32"))] - fn fetch_data(&self, path: &str) -> Result { + fn fetch_data(&self, path: &str) -> Result> { 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::().unwrap_or_else(|e| { - panic!("unable to parse json, error: {}", e); - }); - - Ok(data) + match response.json::() { + Ok(data) => Ok(Some(data)), + _ => Ok(None), + } } #[cfg(target_arch = "wasm32")] @@ -278,6 +279,9 @@ impl ClarityDatastore { .fetch_data::(&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); @@ -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> { + 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) } @@ -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"); } @@ -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(()) @@ -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, 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, } }