Skip to content

Commit

Permalink
feat: support clarity 3
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Jun 26, 2024
1 parent a80e4b5 commit 34a83c0
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 22 deletions.
File renamed without changes.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion components/clarinet-cli/src/generate/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl GetChangesForNewProject {
use_current_dir: bool,
telemetry_enabled: bool,
) -> Self {
println!("project_path: {project_path}, project_name: {project_name}");
let project_path = if use_current_dir {
project_path.clone()
} else {
Expand Down
53 changes: 53 additions & 0 deletions components/clarity-repl/src/analysis/ast_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,23 @@ pub trait ASTVisitor<'a> {
.unwrap_or(&DEFAULT_NAME),
args.get(1).unwrap_or(&DEFAULT_EXPR),
),
GetStacksBlockInfo => self.traverse_get_stacks_block_info(
expr,
args.get(0)
.unwrap_or(&DEFAULT_EXPR)
.match_atom()
.unwrap_or(&DEFAULT_NAME),
args.get(1).unwrap_or(&DEFAULT_EXPR),
),
GetTenureInfo => self.traverse_get_tenure_info(
expr,
args.get(0)
.unwrap_or(&DEFAULT_EXPR)
.match_atom()
.unwrap_or(&DEFAULT_NAME),
args.get(1).unwrap_or(&DEFAULT_EXPR),
),

ConsError => self.traverse_err(expr, args.get(0).unwrap_or(&DEFAULT_EXPR)),
ConsOkay => self.traverse_ok(expr, args.get(0).unwrap_or(&DEFAULT_EXPR)),
ConsSome => self.traverse_some(expr, args.get(0).unwrap_or(&DEFAULT_EXPR)),
Expand Down Expand Up @@ -1465,6 +1482,42 @@ pub trait ASTVisitor<'a> {
true
}

fn traverse_get_stacks_block_info(
&mut self,
expr: &'a SymbolicExpression,
prop_name: &'a ClarityName,
block: &'a SymbolicExpression,
) -> bool {
self.traverse_expr(block) && self.visit_get_stacks_block_info(expr, prop_name, block)
}

fn visit_get_stacks_block_info(
&mut self,
expr: &'a SymbolicExpression,
prop_name: &'a ClarityName,
block: &'a SymbolicExpression,
) -> bool {
true
}

fn traverse_get_tenure_info(
&mut self,
expr: &'a SymbolicExpression,
prop_name: &'a ClarityName,
block: &'a SymbolicExpression,
) -> bool {
self.traverse_expr(block) && self.visit_get_tenure_info(expr, prop_name, block)
}

fn visit_get_tenure_info(
&mut self,
expr: &'a SymbolicExpression,
prop_name: &'a ClarityName,
block: &'a SymbolicExpression,
) -> bool {
true
}

fn traverse_err(
&mut self,
expr: &'a SymbolicExpression,
Expand Down
73 changes: 61 additions & 12 deletions components/clarity-repl/src/repl/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use clarity::types::chainstate::StacksAddress;
use clarity::types::chainstate::StacksBlockId;
use clarity::types::chainstate::VRFSeed;
use clarity::types::StacksEpochId;
use clarity::types::PEER_VERSION_EPOCH_2_1;
use clarity::util::hash::Sha512Trunc256Sum;
use clarity::vm::analysis::AnalysisDatabase;
use clarity::vm::database::BurnStateDB;
Expand All @@ -19,6 +20,8 @@ use clarity::vm::StacksEpoch;
use pox_locking::handle_contract_call_special_cases;
use sha2::{Digest, Sha512_256};

use super::interpreter::BLOCK_LIMIT_MAINNET;

#[derive(Clone, Debug)]
pub struct Datastore {
store: HashMap<StacksBlockId, HashMap<String, String>>,
Expand Down Expand Up @@ -51,7 +54,6 @@ pub struct StacksConstants {
pub pox_prepare_length: u32,
pub pox_reward_cycle_length: u32,
pub pox_rejection_fraction: u64,
pub epoch_21_start_height: u32,
}

#[derive(Clone, Debug)]
Expand All @@ -64,6 +66,8 @@ pub struct BurnDatastore {
current_chain_tip: StacksBlockId,
chain_height: u32,
height_at_chain_tip: HashMap<StacksBlockId, u32>,
current_epoch: StacksEpochId,
current_epoch_start_height: u32,
constants: StacksConstants,
genesis_time: u64,
}
Expand Down Expand Up @@ -302,7 +306,7 @@ impl ClarityBackingStore for Datastore {
}

impl BurnDatastore {
pub fn new(constants: StacksConstants) -> BurnDatastore {
pub fn new(constants: StacksConstants) -> Self {
let bytes = height_to_hashed_bytes(0);
let id = StacksBlockId(bytes);
let sortition_id = SortitionId(bytes);
Expand Down Expand Up @@ -349,6 +353,8 @@ impl BurnDatastore {
current_chain_tip: id,
chain_height: 0,
height_at_chain_tip,
current_epoch: StacksEpochId::Epoch2_05,
current_epoch_start_height: 0,
constants,
genesis_time,
}
Expand Down Expand Up @@ -380,6 +386,11 @@ impl BurnDatastore {
self.open_chain_tip = height_to_id(self.chain_height);
self.current_chain_tip = self.open_chain_tip;
}

pub fn set_current_epoch(&mut self, epoch: StacksEpochId) {
self.current_epoch = epoch;
self.current_epoch_start_height = self.chain_height;
}
}

impl HeadersDB for BurnDatastore {
Expand All @@ -399,6 +410,7 @@ impl HeadersDB for BurnDatastore {
fn get_stacks_block_header_hash_for_block(
&self,
id_bhh: &StacksBlockId,
_epoch_id: &StacksEpochId,
) -> Option<BlockHeaderHash> {
self.store.get(id_bhh).map(|id| id.block_header_hash)
}
Expand All @@ -410,32 +422,63 @@ impl HeadersDB for BurnDatastore {
self.store.get(id_bhh).map(|id| id.burn_block_header_hash)
}

fn get_consensus_hash_for_block(&self, id_bhh: &StacksBlockId) -> Option<ConsensusHash> {
fn get_consensus_hash_for_block(
&self,
id_bhh: &StacksBlockId,
_epoch_id: &StacksEpochId,
) -> Option<ConsensusHash> {
self.store.get(id_bhh).map(|id| id.consensus_hash)
}
fn get_vrf_seed_for_block(&self, id_bhh: &StacksBlockId) -> Option<VRFSeed> {
fn get_vrf_seed_for_block(
&self,
id_bhh: &StacksBlockId,
_epoch_id: &StacksEpochId,
) -> Option<VRFSeed> {
self.store.get(id_bhh).map(|id| id.vrf_seed)
}
fn get_burn_block_time_for_block(&self, id_bhh: &StacksBlockId) -> Option<u64> {
fn get_stacks_block_time_for_block(&self, id_bhh: &StacksBlockId) -> Option<u64> {
self.store.get(id_bhh).map(|id| id.burn_block_time)
}
fn get_burn_block_time_for_block(
&self,
id_bhh: &StacksBlockId,
_epoch_id: Option<&StacksEpochId>,
) -> Option<u64> {
self.store.get(id_bhh).map(|id| id.burn_block_time)
}
fn get_burn_block_height_for_block(&self, id_bhh: &StacksBlockId) -> Option<u32> {
self.store.get(id_bhh).map(|id| id.burn_block_height)
}
fn get_miner_address(&self, id_bhh: &StacksBlockId) -> Option<StacksAddress> {
fn get_miner_address(
&self,
id_bhh: &StacksBlockId,
_epoch_id: &StacksEpochId,
) -> Option<StacksAddress> {
self.store.get(id_bhh).map(|id| id.miner)
}
fn get_burnchain_tokens_spent_for_block(&self, id_bhh: &StacksBlockId) -> Option<u128> {
fn get_burnchain_tokens_spent_for_block(
&self,
id_bhh: &StacksBlockId,
_epoch_id: &StacksEpochId,
) -> Option<u128> {
self.store
.get(id_bhh)
.map(|id| id.burnchain_tokens_spent_for_block)
}
fn get_burnchain_tokens_spent_for_winning_block(&self, id_bhh: &StacksBlockId) -> Option<u128> {
fn get_burnchain_tokens_spent_for_winning_block(
&self,
id_bhh: &StacksBlockId,
_epoch_id: &StacksEpochId,
) -> Option<u128> {
self.store
.get(id_bhh)
.map(|id| id.get_burnchain_tokens_spent_for_winning_block)
}
fn get_tokens_earned_for_block(&self, id_bhh: &StacksBlockId) -> Option<u128> {
fn get_tokens_earned_for_block(
&self,
id_bhh: &StacksBlockId,
_epoch_id: &StacksEpochId,
) -> Option<u128> {
self.store.get(id_bhh).map(|id| id.tokens_earned_for_block)
}
}
Expand Down Expand Up @@ -481,7 +524,7 @@ impl BurnStateDB for BurnDatastore {

/// Returns the height of the burnchain when the Stacks chain started running.
fn get_burn_start_height(&self) -> u32 {
0
self.constants.burn_start_height
}

fn get_pox_prepare_length(&self) -> u32 {
Expand Down Expand Up @@ -523,11 +566,17 @@ impl BurnStateDB for BurnDatastore {
/// The epoch is defined as by a start and end height. This returns
/// the epoch enclosing `height`.
fn get_stacks_epoch(&self, _height: u32) -> Option<StacksEpoch> {
None
Some(StacksEpoch {
epoch_id: self.current_epoch,
start_height: self.current_epoch_start_height.into(),
end_height: u64::MAX,
block_limit: BLOCK_LIMIT_MAINNET,
network_epoch: PEER_VERSION_EPOCH_2_1,
})
}

fn get_stacks_epoch_by_epoch_id(&self, _epoch_id: &StacksEpochId) -> Option<StacksEpoch> {
None
self.get_stacks_epoch(0)
}

/// Get the PoX payout addresses for a given burnchain block
Expand Down
5 changes: 2 additions & 3 deletions components/clarity-repl/src/repl/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ impl ClarityInterpreter {
pub fn new(tx_sender: StandardPrincipalData, repl_settings: Settings) -> Self {
let constants = StacksConstants {
burn_start_height: 0,
pox_prepare_length: 0,
pox_reward_cycle_length: 0,
pox_prepare_length: 50,
pox_reward_cycle_length: 1050,
pox_rejection_fraction: 0,
epoch_21_start_height: 0,
};
Self {
tx_sender,
Expand Down
2 changes: 2 additions & 0 deletions components/clarity-repl/src/repl/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,11 +951,13 @@ impl Session {
))
}
};
self.update_epoch(epoch);
output.push(green!(format!("Epoch updated to: {epoch}")));
}

pub fn update_epoch(&mut self, epoch: StacksEpochId) {
self.current_epoch = epoch;
self.interpreter.burn_datastore.set_current_epoch(epoch);
if epoch >= StacksEpochId::Epoch30 {
self.interpreter.set_tenure_height();
}
Expand Down

0 comments on commit 34a83c0

Please sign in to comment.