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

Clarity <-> MARF #1084

Merged
merged 33 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cfc088d
Merge branch 'feature/materialized-view-commitment' into feature/clar…
kantai Aug 5, 2019
4c96008
the calm before a storm of refactoring of contexts.rs
kantai Aug 6, 2019
a780c38
round out the implementation of ClarityDatabase wrapper. massive refa…
kantai Aug 6, 2019
9e056b4
SQLite dumb k/v storage. Refactor clarity.rs to use.
kantai Aug 6, 2019
4fde1db
get rid of compiler warning on comment
kantai Aug 6, 2019
0b1d209
get rid of initial begin() behavior of globalcontext. supply execute …
kantai Aug 7, 2019
24b23f1
Merge remote-tracking branch 'origin/develop' into feature/clarity-marfs
kantai Aug 7, 2019
ab76b2b
Merge remote-tracking branch 'origin/feature/materialized-view-commit…
kantai Aug 7, 2019
ed0929b
eval was using an in-memory env, but it should actually read the db
kantai Aug 7, 2019
aeef102
refactor type checker to use the new DB interface
kantai Aug 7, 2019
1328e0e
change k/v store trait to use mutable gets. add k/v store wrapper for…
kantai Aug 7, 2019
3eb9f42
vm::tests::assets and vm::tests::contracts now both test both the Has…
kantai Aug 7, 2019
c078917
refactor Asset/Token named things, separate the claritydb struct from…
kantai Aug 8, 2019
1d32fe8
Merge remote-tracking branch 'origin/feature/materialized-view-commit…
kantai Aug 8, 2019
c2e421e
add some comments to the MarfedKV
kantai Aug 8, 2019
c7cf89c
use MARFValue for .insert()
kantai Aug 8, 2019
a8782bd
use &str in the k/v interface. use marfvalue for generating hash
kantai Aug 8, 2019
5e63a61
&String to &str
kantai Aug 8, 2019
a92bbe6
Merge remote-tracking branch 'origin/develop' into feature/clarity-marfs
kantai Aug 9, 2019
cf66b43
use marfs in the cli invocation
kantai Aug 9, 2019
c342c0a
rename define to define-private and define-constant #968, macro-ize f…
kantai Aug 12, 2019
914890e
let marf.get() work when called on an unopened FileStorage instance, …
kantai Aug 12, 2019
d4b5fe1
use the get_name() function in docs. add parsing for qualified contra…
kantai Aug 13, 2019
e0ad8f3
Merge remote-tracking branch 'origin/develop' into feature/clarity-marfs
kantai Aug 13, 2019
c60055e
adopt C-like function names: #989
kantai Aug 13, 2019
1e72e7b
use insert_batch for marf k/v, extend keyvaluestore trait with put_all.
kantai Aug 13, 2019
f768264
dont commit if there are no edits
kantai Aug 14, 2019
9112554
use Vecs instead of VecDeques in contexts, rollbacks
kantai Aug 15, 2019
1c0340b
token/asset -> ft/nft. clarify AnalysisDatabase::make_key.
kantai Aug 15, 2019
c5f3d9f
add test of forking behavior in the Clarity/MARF integration.
kantai Aug 15, 2019
53d651c
set .chain_tup to .open_chain_tip on begin()
kantai Aug 15, 2019
156a97e
add error messages to MARF panics
kantai Aug 16, 2019
40d98e1
correct typo
kantai Aug 16, 2019
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
16 changes: 8 additions & 8 deletions sample-programs/names.clar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(define burn-address 'SP000000000000000000002Q6VF78)
(define (price-function (name int))
(define-constant burn-address 'SP000000000000000000002Q6VF78)
(define-private (price-function (name int))
(if (< name 100000) 1000 100))

(define-map name-map
Expand All @@ -13,7 +13,7 @@
(name-price int))
(if (is-ok? (contract-call! tokens token-transfer
burn-address name-price))
(begin (insert-entry! preorder-map
(begin (map-insert! preorder-map
(tuple (name-hash name-hash))
(tuple (paid name-price)
(buyer tx-sender)))
Expand All @@ -26,11 +26,11 @@
(salt int))
(let ((preorder-entry
(expects! ;; name _must_ have been preordered.
(fetch-entry preorder-map
(tuple (name-hash (hash160 (xor name salt)))))
(map-get preorder-map
(tuple (name-hash (hash160 (xor name salt)))))
(err "no preorder found")))
(name-entry
(fetch-entry name-map (tuple (name name)))))
(map-get name-map (tuple (name name)))))
(if (and
;; name shouldn't *already* exist
(is-none? name-entry)
Expand All @@ -41,10 +41,10 @@
(eq? tx-sender
(get buyer preorder-entry)))
(if (and
(insert-entry! name-map
(map-insert! name-map
(tuple (name name))
(tuple (owner recipient-principal)))
(delete-entry! preorder-map
(map-delete! preorder-map
(tuple (name-hash (hash160 (xor name salt))))))
(ok 0)
(err "failed to insert new name entry"))
Expand Down
10 changes: 5 additions & 5 deletions sample-programs/tokens.clar
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(define-map tokens ((account principal)) ((balance int)))
(define (get-balance (account principal))
(default-to 0 (get balance (fetch-entry tokens (tuple (account account))))))
(define-private (get-balance (account principal))
(default-to 0 (get balance (map-get tokens (tuple (account account))))))

(define (token-credit! (account principal) (amount int))
(define-private (token-credit! (account principal) (amount int))
(if (<= amount 0)
(err "must move positive balance")
(let ((current-amount (get-balance account)))
(begin
(set-entry! tokens (tuple (account account))
(map-set! tokens (tuple (account account))
(tuple (balance (+ amount current-amount))))
(ok amount)))))

Expand All @@ -16,7 +16,7 @@
(if (or (> amount balance) (<= amount 0))
(err "must transfer positive balance and possess funds")
(begin
(set-entry! tokens (tuple (account tx-sender))
(map-set! tokens (tuple (account tx-sender))
(tuple (balance (- balance amount))))
(token-credit! to amount)))))

Expand Down
17 changes: 11 additions & 6 deletions src/chainstate/stacks/index/marf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ use chainstate::stacks::index::Error as Error;

use util::log;


/// Merklized Adaptive-Radix Forest -- a collection of Merklized Adaptive-Radix Tries.
pub struct MARF {
storage: TrieFileStorage,
Expand Down Expand Up @@ -459,7 +458,7 @@ impl MARF {
/// Instantiate the MARF using a TrieFileStorage instance, from the given path on disk.
/// This will have the side-effect of instantiating a new fork table from the tries encoded on
/// disk. Performant code should call this method sparingly.
pub fn from_path(path: &String) -> Result<MARF, Error> {
pub fn from_path(path: &str) -> Result<MARF, Error> {
let mut file_storage = TrieFileStorage::new(path)?;
match fs::metadata(path) {
Ok(_) => {},
Expand All @@ -476,11 +475,12 @@ impl MARF {
}

/// Resolve a key from the MARF to a MARFValue with respect to the given block height.
pub fn get(&mut self, block_hash: &BlockHeaderHash, key: &String) -> Result<Option<MARFValue>, Error> {
pub fn get(&mut self, block_hash: &BlockHeaderHash, key: &str) -> Result<Option<MARFValue>, Error> {
let cur_block_hash = self.storage.get_cur_block();
let cur_block_rw = self.storage.readwrite();

let path = TriePath::from_key(key);

let leaf_opt_res = MARF::get_path(&mut self.storage, block_hash, &path)?;

// restore
Expand All @@ -499,7 +499,7 @@ impl MARF {
/// Insert the given (key, value) pair into the MARF. Inserting the same key twice silently
/// overwrites the existing key. Succeeds if there are no storage errors.
/// Must be called after a call to .begin() (will fail otherwise)
pub fn insert(&mut self, key: &String, value: &String) -> Result<(), Error> {
pub fn insert(&mut self, key: &str, value: MARFValue) -> Result<(), Error> {
match self.open_chain_tip {
None => {
Err(Error::WriteNotBegunError)
Expand All @@ -508,9 +508,9 @@ impl MARF {
let cur_block_hash = self.storage.get_cur_block();
let cur_block_rw = self.storage.readwrite();

let marf_value = MARFValue::from_value(value);
let marf_leaf = TrieLeaf::from_value(&vec![], marf_value);
let marf_leaf = TrieLeaf::from_value(&vec![], value);
let path = TriePath::from_key(key);

MARF::insert_leaf(&mut self.storage, block_hash, &path, &marf_leaf)?;

// restore
Expand Down Expand Up @@ -598,6 +598,11 @@ impl MARF {
Ok(())
}

/// Get open chain tip
pub fn get_open_chain_tip(&self) -> Option<&BlockHeaderHash> {
self.open_chain_tip.as_ref()
}

/// Get all known chain tips
pub fn chain_tips(&mut self) -> Vec<BlockHeaderHash> {
self.storage.chain_tips()
Expand Down
2 changes: 1 addition & 1 deletion src/chainstate/stacks/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl MARFValue {
}

/// Construct from a String that encodes a value inserted into the underlying data store
pub fn from_value(s: &String) -> MARFValue {
pub fn from_value(s: &str) -> MARFValue {
let mut tmp = [0u8; 32];

let mut hasher = TrieHasher::new();
Expand Down
2 changes: 1 addition & 1 deletion src/chainstate/stacks/index/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl_byte_array_newtype!(TriePath, u8, 32);
pub const TRIEPATH_MAX_LEN : usize = 32;

impl TriePath {
pub fn from_key(k: &String) -> TriePath {
pub fn from_key(k: &str) -> TriePath {
let h = TrieHash::from_data(k.as_bytes());
let mut hb = [0u8; TRIEPATH_MAX_LEN];
hb.copy_from_slice(h.as_bytes());
Expand Down
4 changes: 2 additions & 2 deletions src/chainstate/stacks/index/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,13 +1186,13 @@ mod test {

// Block #1
m.begin(&block_0, &block_1).unwrap();
let r = m.insert(&k1, &old_v);
let r = m.insert(&k1, MARFValue::from_value(&old_v));
let (_, root_hash_1) = Trie::read_root(m.borrow_storage_backend()).unwrap();
m.commit().unwrap();

// Block #2
m.begin(&block_1, &block_2).unwrap();
let r = m.insert(&k1, &new_v);
let r = m.insert(&k1, MARFValue::from_value(&new_v));
let (_, root_hash_2) = Trie::read_root(m.borrow_storage_backend()).unwrap();
m.commit().unwrap();

Expand Down
19 changes: 15 additions & 4 deletions src/chainstate/stacks/index/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ pub struct TrieFileStorage {
}

impl TrieFileStorage {
pub fn new(dir_path: &String) -> Result<TrieFileStorage, Error> {
pub fn new(dir_path: &str) -> Result<TrieFileStorage, Error> {
match fs::metadata(dir_path) {
Ok(md) => {
if !md.is_dir() {
Expand All @@ -408,15 +408,16 @@ impl TrieFileStorage {
}
}

let partially_written_state = TrieFileStorage::scan_tmp_blocks(dir_path)?;
let dir_path = dir_path.to_string();
let partially_written_state = TrieFileStorage::scan_tmp_blocks(&dir_path)?;
jcnelson marked this conversation as resolved.
Show resolved Hide resolved
if partially_written_state.len() > 0 {
return Err(Error::PartialWriteError);
}

let fork_table = TrieFileStorage::read_fork_table(dir_path, &TrieFileStorage::block_sentinel())?;
let fork_table = TrieFileStorage::read_fork_table(&dir_path, &TrieFileStorage::block_sentinel())?;

let ret = TrieFileStorage {
dir_path: dir_path.clone(),
dir_path,
readonly: false,

last_extended: None,
Expand Down Expand Up @@ -891,6 +892,16 @@ impl TrieFileStorage {
pub fn open_block(&mut self, bhh: &BlockHeaderHash, readwrite: bool) -> Result<(), Error> {
let block_fork_ptr = self.fork_table.get_fork_ptr(bhh)?;

let sentinel = TrieFileStorage::block_sentinel();
if *bhh == sentinel {
jcnelson marked this conversation as resolved.
Show resolved Hide resolved
// just reset to newly opened state
self.cur_block_fd = None;
self.cur_block = sentinel;
self.cur_block_fork_ptr = None;
self.readonly = !readwrite;
return Ok(());
}

if Some(*bhh) == self.last_extended {
// nothing to do -- we're already ready.
// just clear out.
Expand Down
Loading