Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advance chaintips changes #1506

Merged
merged 18 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions components/clarinet-sdk-wasm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ impl SDK {
session.interpreter.get_block_height()
}

#[wasm_bindgen(getter, js_name=stacksBlockHeight)]
pub fn stacks_block_height(&mut self) -> u32 {
let session = self.get_session_mut();
session.interpreter.get_block_height()
}

#[wasm_bindgen(getter, js_name=burnBlockHeight)]
pub fn burn_block_height(&mut self) -> u32 {
let session = self.get_session_mut();
session.interpreter.get_burn_block_height()
}

#[wasm_bindgen(getter, js_name=currentEpoch)]
pub fn current_epoch(&mut self) -> String {
let session = self.get_session_mut();
Expand Down Expand Up @@ -911,14 +923,39 @@ impl SDK {

#[wasm_bindgen(js_name=mineEmptyBlock)]
pub fn mine_empty_block(&mut self) -> u32 {
let session = self.get_session_mut();
session.advance_chain_tip(1)
self.mine_empty_burn_block()
}

#[wasm_bindgen(js_name=mineEmptyBlocks)]
pub fn mine_empty_blocks(&mut self, count: Option<u32>) -> u32 {
self.mine_empty_burn_blocks(count)
}
#[wasm_bindgen(js_name=mineEmptyStacksBlock)]
pub fn mine_empty_stacks_block(&mut self) -> Result<u32, String> {
let session = self.get_session_mut();
match session.advance_stacks_chaintip(1) {
Ok(new_height) => Ok(new_height),
Err(_) => Err("use mineEmptyBurnBlock in epoch lower than 3.0".to_string()),
}
}

#[wasm_bindgen(js_name=mineEmptyStacksBlocks)]
pub fn mine_empty_stacks_blocks(&mut self, count: Option<u32>) -> Result<u32, String> {
let session = self.get_session_mut();
match session.advance_stacks_chaintip(count.unwrap_or(1)) {
Ok(new_height) => Ok(new_height),
Err(_) => Err("use mineEmptyBurnBlocks in epoch lower than 3.0".to_string()),
}
}

#[wasm_bindgen(js_name=mineEmptyBurnBlock)]
pub fn mine_empty_burn_block(&mut self) -> u32 {
let session = self.get_session_mut();
session.advance_burn_chaintip(1)
}
#[wasm_bindgen(js_name=mineEmptyBurnBlocks)]
pub fn mine_empty_burn_blocks(&mut self, count: Option<u32>) -> u32 {
let session = self.get_session_mut();
session.advance_chain_tip(count.unwrap_or(1))
session.advance_burn_chaintip(count.unwrap_or(1))
}

#[wasm_bindgen(js_name=runSnippet)]
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-sdk/node/tests/fixtures/Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ epoch = 2.4
[contracts.block-height-tests]
path = 'contracts/block-height-tests.clar'
clarity_version = 2
epoch = 2.4
epoch = 2.4
tippenein marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions components/clarinet-sdk/node/tests/fixtures/Clarinet3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[project]
name = 'fixtures'
description = 'sample project to test clarinet-sdk wasm'
telemetry = false
cache_dir = './.cache'
requirements = []

[contracts.multiplier-trait]
path = 'contracts/multiplier-trait.clar'
clarity_version = 3
epoch = 3.0

[contracts.multiplier-contract]
path = 'contracts/multiplier-contract.clar'
clarity_version = 3
epoch = 3.0

[contracts.counter]
path = 'contracts/counter.clar'
clarity_version = 3
epoch = 3.0
33 changes: 33 additions & 0 deletions components/clarinet-sdk/node/tests/simnet-usage-epoch3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from "node:fs";
import path from "node:path";
import { describe, expect, it, beforeEach, afterEach } from "vitest";

// test the built package and not the source code
// makes it simpler to handle wasm build
import { Simnet, initSimnet } from "..";

let simnet: Simnet;

beforeEach(async () => {
simnet = await initSimnet("tests/fixtures/Clarinet.toml");
simnet.setEpoch("3.0");
});

describe("basic simnet interactions", () => {
tippenein marked this conversation as resolved.
Show resolved Hide resolved
it("initialize simnet", () => {
expect(simnet.blockHeight).toBe(1);
});

it("can mine empty blocks", () => {
const blockHeight = simnet.stacksBlockHeight;
const burnBlockHeight = simnet.burnBlockHeight;
simnet.mineEmptyStacksBlock();
expect(simnet.stacksBlockHeight).toBe(blockHeight + 1);
expect(simnet.burnBlockHeight).toBe(burnBlockHeight);
simnet.mineEmptyStacksBlocks(4);
expect(simnet.stacksBlockHeight).toBe(blockHeight + 5);
simnet.mineEmptyBurnBlocks(4);
expect(simnet.burnBlockHeight).toBe(burnBlockHeight + 4);
expect(simnet.stacksBlockHeight).toBe(blockHeight + 9);
})
})
tippenein marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions components/clarinet-sdk/node/tests/simnet-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ describe("basic simnet interactions", () => {
simnet.mineEmptyBlocks(4);
expect(simnet.blockHeight).toBe(blockHeight + 5);
});
it("can not mine empty stacks block in pre-3.0", () => {
expect(() => simnet.mineEmptyStacksBlock()).toThrowError(
"use mineEmptyBurnBlock in epoch lower than 3.0"
);
})
tippenein marked this conversation as resolved.
Show resolved Hide resolved

it("exposes devnet stacks accounts", () => {
const accounts = simnet.getAccounts();
Expand Down
10 changes: 9 additions & 1 deletion components/clarity-repl/src/repl/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,14 @@ impl BurnDatastore {
}
}

pub fn advance_chain_tip(&mut self, count: u32) {
pub fn get_current_epoch(&self) -> StacksEpochId {
self.current_epoch
}

pub fn get_current_block_height(&self) -> u32 {
self.chain_height
}
pub fn advance_chain_tip(&mut self, count: u32) -> u32 {
let cur_height = self.chain_height;
let current_lookup_id = *self
.block_id_lookup
Expand All @@ -399,6 +406,7 @@ impl BurnDatastore {
self.chain_height += count;
self.open_chain_tip = height_to_id(self.chain_height);
self.current_chain_tip = self.open_chain_tip;
self.chain_height
}

pub fn set_current_epoch(&mut self, epoch: StacksEpochId) {
Expand Down
83 changes: 75 additions & 8 deletions components/clarity-repl/src/repl/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,12 +1098,20 @@ impl ClarityInterpreter {
self.tx_sender.clone()
}

pub fn advance_chain_tip(&mut self, count: u32) -> u32 {
self.burn_datastore.advance_chain_tip(count);
let new_height = self.datastore.advance_chain_tip(count);
pub fn advance_burn_chaintip(&mut self, count: u32) -> u32 {
let new_height = self.burn_datastore.advance_chain_tip(count);
let _ = self.datastore.advance_chain_tip(count);
self.set_tenure_height();
new_height
}
pub fn advance_stacks_chaintip(&mut self, count: u32) -> Result<u32, String> {
let current_epoch = self.burn_datastore.get_current_epoch();
if current_epoch < StacksEpochId::Epoch30 {
Err("only burn chain height can be advanced in epoch lower than 3.0".to_string())
tippenein marked this conversation as resolved.
Show resolved Hide resolved
} else {
Ok(self.datastore.advance_chain_tip(count))
}
}

pub fn set_tenure_height(&mut self) {
let block_height = self.get_block_height();
Expand All @@ -1122,6 +1130,10 @@ impl ClarityInterpreter {
self.datastore.get_current_block_height()
}

pub fn get_burn_block_height(&mut self) -> u32 {
self.burn_datastore.get_current_block_height()
}

fn credit_token(&mut self, account: String, token: String, value: u128) {
self.accounts.insert(account.clone());
match self.tokens.entry(token) {
Expand Down Expand Up @@ -1184,6 +1196,7 @@ impl ClarityInterpreter {
#[cfg(test)]
mod tests {
use super::*;
use crate::analysis::Settings as AnalysisSettings;
use crate::{
repl::session::BOOT_CONTRACTS_DATA, test_fixtures::clarity_contract::ClarityContractBuilder,
};
Expand Down Expand Up @@ -1259,14 +1272,68 @@ mod tests {
assert_eq!(interpreter.get_block_height(), 0);
}

#[test]
fn test_advance_stacks_chaintip_pre_epoch_3() {
let mut interpreter =
ClarityInterpreter::new(StandardPrincipalData::transient(), Settings::default());
interpreter
.burn_datastore
.set_current_epoch(StacksEpochId::Epoch2_05);
let count = 5;
let initial_block_height = interpreter.get_burn_block_height();
assert_ne!(interpreter.advance_stacks_chaintip(count), Ok(count));
assert_eq!(interpreter.get_burn_block_height(), initial_block_height);
assert_eq!(interpreter.get_block_height(), initial_block_height);
}
#[test]
fn test_advance_stacks_chaintip() {
let wasm_settings = Settings {
analysis: AnalysisSettings::default(),
clarity_wasm_mode: true,
show_timings: false,
};
let mut interpreter =
ClarityInterpreter::new(StandardPrincipalData::transient(), wasm_settings);
interpreter
.burn_datastore
.set_current_epoch(StacksEpochId::Epoch30);
let count = 5;
let initial_block_height = interpreter.get_burn_block_height();
assert_eq!(interpreter.advance_stacks_chaintip(count), Ok(count));
assert_eq!(interpreter.get_burn_block_height(), initial_block_height);
assert_eq!(interpreter.get_block_height(), initial_block_height + count);
}
#[test]
fn test_advance_chain_tip_pre_epoch3() {
let mut interpreter =
ClarityInterpreter::new(StandardPrincipalData::transient(), Settings::default());
interpreter
.burn_datastore
.set_current_epoch(StacksEpochId::Epoch2_05);
let count = 5;
let initial_block_height = interpreter.get_block_height();
interpreter.advance_burn_chaintip(count);
assert_eq!(interpreter.get_block_height(), initial_block_height + count);
assert_eq!(
interpreter.get_burn_block_height(),
initial_block_height + count
);
}
#[test]
fn test_advance_chain_tip() {
let mut interpreter =
ClarityInterpreter::new(StandardPrincipalData::transient(), Settings::default());
interpreter
.burn_datastore
.set_current_epoch(StacksEpochId::Epoch30);
let count = 5;
let initial_block_height = interpreter.get_block_height();
interpreter.advance_chain_tip(count);
interpreter.advance_burn_chaintip(count);
assert_eq!(interpreter.get_block_height(), initial_block_height + count);
assert_eq!(
interpreter.get_burn_block_height(),
initial_block_height + count
);
}

#[test]
Expand Down Expand Up @@ -1730,7 +1797,7 @@ mod tests {
),
);

interpreter.advance_chain_tip(10);
interpreter.advance_burn_chaintip(10);

let result = interpreter.call_contract_fn(
&contract_id,
Expand Down Expand Up @@ -1762,7 +1829,7 @@ mod tests {
let mut interpreter =
ClarityInterpreter::new(StandardPrincipalData::transient(), Settings::default());

interpreter.advance_chain_tip(1);
interpreter.advance_burn_chaintip(1);

let snippet = [
"(define-read-only (get-height)",
Expand Down Expand Up @@ -1815,7 +1882,7 @@ mod tests {
let mut interpreter =
ClarityInterpreter::new(StandardPrincipalData::transient(), Settings::default());

interpreter.advance_chain_tip(1);
interpreter.advance_burn_chaintip(1);

let snippet = [
"(define-read-only (get-height)",
Expand Down Expand Up @@ -1868,7 +1935,7 @@ mod tests {
),
);

interpreter.advance_chain_tip(10);
interpreter.advance_burn_chaintip(10);

let result = interpreter.call_contract_fn(
&contract_id,
Expand Down
Loading
Loading