Skip to content

Commit 223d137

Browse files
author
Joshua Mir
committed
Dat storage and index minirefactor
1 parent 690572c commit 223d137

File tree

1 file changed

+19
-17
lines changed
  • datdot-node/pallets/datdot/src

1 file changed

+19
-17
lines changed

datdot-node/pallets/datdot/src/lib.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,9 @@ decl_storage! {
396396
trait Store for Module<T: Trait> as DatVerify {
397397
// A vec of free indeces, with the last item usable for `len`
398398
pub DatId get(fn next_id): DatIdVec;
399-
// Each dat archive has a public key
400-
pub DatKey get(fn public_key): map hasher(twox_64_concat) DatIdIndex => Public;
399+
// Each dat archive has a public key, size, tree root hash, and signature
400+
pub Dat get(fn public_key): map hasher(twox_64_concat) DatIdIndex => (Public, DatSize, H256, Signature);
401+
pub DatIndex get(fn dat_index): map hasher(twox_64_concat) Public => DatIdIndex;
401402
// Each dat archive has a tree size
402403
// TODO: remove calls to this when expecting indeces
403404
pub TreeSize get(fn tree_size): map hasher(twox_64_concat) Public => DatSize;
@@ -504,8 +505,10 @@ decl_module!{
504505
#[weight = (100000, Pays::No)]
505506
fn unregister_data(origin, index: DatIdIndex){
506507
let account = ensure_signed(origin)?;
507-
let pubkey = <DatKey>::get(index);
508-
//only allow owner to unregister
508+
let dat = <Dat>::get(index);
509+
let pubkey = dat.0;
510+
//only allow publisher to unregister
511+
//todo, consider potential abuse here: (pinning others' content)
509512
ensure!(
510513
<UserRequestsMap<T>>::get(&pubkey) == account,
511514
Error::<T>::PermissionError
@@ -523,10 +526,8 @@ decl_module!{
523526
},
524527
None => (),
525528
}
526-
<DatKey>::remove(&index);
529+
<Dat>::remove(&index);
527530
<UserRequestsMap<T>>::remove(&pubkey);
528-
<TreeSize>::remove(&pubkey);
529-
<MerkleRoot>::remove(&pubkey);
530531
//inefficient - fixme later.
531532
for (hoster_index, dat_index, _) in <HostedMap>::iter() {
532533
if dat_index == index {
@@ -658,7 +659,7 @@ decl_module!{
658659
.using_encoded(|mut b| u64::decode(&mut b))
659660
.expect("hash must be of correct size; Qed");
660661
let random_index = new_random % last_index;
661-
let dat_pubkey = DatKey::get(&random_index);
662+
let dat_pubkey = Dat::get(&random_index).0;
662663
// DO SOMETHING HERE
663664

664665
// SOMETHING DONE
@@ -711,8 +712,9 @@ decl_module!{
711712
let new_random = (T::Randomness::random(b"dat_verify_init"), nonce)
712713
.using_encoded(|mut b| u64::decode(&mut b))
713714
.expect("hash must be of correct size; Qed");
714-
let dat_pubkey = <DatKey>::get(dat_id);
715-
let dat_tree_len = <TreeSize>::get(&dat_pubkey);
715+
let dat = <Dat>::get(dat_id);
716+
let dat_pubkey = dat.0;
717+
let dat_tree_len = dat.1;
716718
let mut random_leaf = 0;
717719
if dat_tree_len != 0 { // avoid 0 divisor
718720
random_leaf = new_random % dat_tree_len;
@@ -942,9 +944,11 @@ impl<T: Trait> Module<T> {
942944
}
943945
native::info!("{:#?}", tree_size);
944946
let mut dat_vec : Vec<DatIdIndex> = <DatId>::get();
945-
match <MerkleRoot>::contains_key(&pubkey){
946-
true => (),
947-
false => {
947+
<DatIndex>::try_mutate_exists(&pubkey, |index_opt| match index_opt.take() {
948+
Some(index) => {
949+
Ok((pubkey, tree_size, root_hash, sig))
950+
},
951+
None => {
948952
native::info!("Dat Keys Vec: {:#?}", dat_vec);
949953
match dat_vec.pop() {
950954
Some(x) => {
@@ -964,12 +968,10 @@ impl<T: Trait> Module<T> {
964968
},
965969
}
966970
//register new unknown dats
967-
<DatKey>::insert(&lowest_free_index, &pubkey)
971+
Err(<Dat>::insert(&lowest_free_index, (pubkey, tree_size, root_hash, sig)))
968972
},
969-
}
970-
<MerkleRoot>::insert(&pubkey, (root_hash, sig));
973+
});
971974
<DatId>::put(dat_vec);
972-
<TreeSize>::insert(&pubkey, tree_size);
973975
<UserRequestsMap<T>>::insert(&pubkey, &account);
974976
Self::deposit_event(RawEvent::SomethingStored(lowest_free_index, pubkey));
975977
}

0 commit comments

Comments
 (0)