Skip to content

Commit

Permalink
refactor: consolidate datastore and burn datastore data (#1562)
Browse files Browse the repository at this point in the history
* refactor: consolidate datastore and burn datastore data

* feat: realistic burn and stacks block times

* tests: add unit tests

* tests: fix lsp tests

* refactor: remove dead code

* fix: prevent calling set_epoch with the value multiple times

* refactor: address review in value_to_string

* refactor: use constants in ddatastore for time between blocks

* fix: ensure datastore is successfully updated on new block

* fix: ensure datastore is consistently updated on new blocks
  • Loading branch information
hugocaillard authored Sep 30, 2024
1 parent 8fc863d commit c56b2d4
Show file tree
Hide file tree
Showing 8 changed files with 695 additions and 507 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion components/clarinet-cli/src/frontend/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ pub fn main() {
contract.epoch,
contract.clarity_version,
);
let mut analysis_db = AnalysisDatabase::new(&mut session.interpreter.datastore);
let mut analysis_db = AnalysisDatabase::new(&mut session.interpreter.clarity_datastore);
let mut analysis_diagnostics = match analysis::run_analysis(
&mut contract_analysis,
&mut analysis_db,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("the sdk handle all clarity version", () => {

// `tenure-height` was introduced in clarity 3
let resOk3 = simnet.execute("(print tenure-height)");
expect(resOk3.result).toStrictEqual(Cl.uint(1));
expect(resOk3.result).toStrictEqual(Cl.uint(2));

// `block-height` was removed in clarity 3
expect(() => simnet.execute("(print block-height)")).toThrowError(
Expand Down
2 changes: 1 addition & 1 deletion components/clarity-events/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn main() {
};

{
let mut analysis_db = session.interpreter.datastore.as_analysis_db();
let mut analysis_db = session.interpreter.clarity_datastore.as_analysis_db();
let cost_track = LimitedCostTracker::new_free();
let type_checker = TypeChecker::new(&mut analysis_db, cost_track, true);
let settings = Settings::default();
Expand Down
69 changes: 37 additions & 32 deletions components/clarity-repl/src/repl/clarity_values.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use clarity::vm::{
types::{CharType, SequenceData},
Value,
Expand All @@ -22,47 +24,50 @@ pub fn uint8_to_value(mut value: &[u8]) -> Value {

pub fn value_to_string(value: &Value) -> String {
match value {
Value::Principal(principal_data) => {
format!("'{principal_data}")
}
Value::Principal(principal_data) => format!("'{}", principal_data),
Value::Tuple(tup_data) => {
let mut data = Vec::new();
for (name, value) in tup_data.data_map.iter() {
data.push(format!("{}: {}", &**name, value_to_string(value)))
let mut data = String::new();
for (name, value) in &tup_data.data_map {
write!(&mut data, "{}: {}, ", name, value_to_string(value)).unwrap();
}
format!("{{ {} }}", data.join(", "))
format!("{{ {} }}", data.trim_end_matches(", "))
}
Value::Optional(opt_data) => match opt_data.data {
Some(ref x) => format!("(some {})", value_to_string(x)),
Value::Optional(opt_data) => match &opt_data.data {
Some(x) => format!("(some {})", value_to_string(x)),
None => "none".to_string(),
},
Value::Response(res_data) => match res_data.committed {
true => format!("(ok {})", value_to_string(&res_data.data)),
false => format!("(err {})", value_to_string(&res_data.data)),
},
Value::Sequence(SequenceData::String(CharType::ASCII(data))) => {
format!("\"{}\"", String::from_utf8(data.data.clone()).unwrap())
Value::Response(res_data) => {
let committed = if res_data.committed { "ok" } else { "err" };
format!("({} {})", committed, value_to_string(&res_data.data))
}
Value::Sequence(SequenceData::String(CharType::UTF8(data))) => {
let mut result = String::new();
for c in data.data.iter() {
if c.len() > 1 {
// escape extended charset
result.push_str(&format!("\\u{{{}}}", hash::to_hex(&c[..])));
} else {
result.push(c[0] as char)
}
}
format!("u\"{result}\"")
Value::Sequence(SequenceData::String(CharType::ASCII(ascii_data))) => {
format!("\"{}\"", String::from_utf8_lossy(&ascii_data.data))
}
Value::Sequence(SequenceData::String(CharType::UTF8(utf8_data))) => {
let result = utf8_data
.data
.iter()
.map(|c| {
if c.len() > 1 {
format!("\\u{{{}}}", hash::to_hex(&c[..]))
} else {
(c[0] as char).to_string()
}
})
.collect::<Vec<_>>()
.join("");
format!("u\"{}\"", result)
}
Value::Sequence(SequenceData::List(list_data)) => {
let mut data = Vec::new();
for value in list_data.data.iter() {
data.push(value_to_string(value))
}
format!("(list {})", data.join(" "))
let data = list_data
.data
.iter()
.map(value_to_string)
.collect::<Vec<_>>()
.join(" ");
format!("(list {})", data)
}
_ => format!("{value}"),
_ => format!("{}", value),
}
}

Expand Down
Loading

0 comments on commit c56b2d4

Please sign in to comment.