Skip to content

Commit

Permalink
Correct choosing the next ctr value
Browse files Browse the repository at this point in the history
  • Loading branch information
rootmos committed Sep 22, 2020
1 parent 85c2e82 commit adf8c1d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
25 changes: 11 additions & 14 deletions engine/examples/commandline/README.md
Expand Up @@ -56,59 +56,56 @@ Create a new chain by encrypting some data and get back the unique identifier
of the newly created encrypted record containing our plain-text data:
```shell
> stronghold encrypt --pass foo --plain secret-text
6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj
f3Duq2bWjC_zjUH376mPXWd6ntSgX6jv
```
(Note that if you haven't/don't want to install the executable you can still
run this as: `cargo run -- encrypt --pass foo --plain "secret text"`.)

To read and decrypt the record we use the `read` command:
```shell
> stronghold read --pass foo --id 6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj
> stronghold read --pass foo --id f3Duq2bWjC_zjUH376mPXWd6ntSgX6jv
Plain: "secret-text"
```

In order to make the following examples less trivial, we create another entry:
```shell
> stronghold encrypt --pass foo --plain another-secret-is-42
vPdgV4h3iNIntR8e9xoxYKDQLByqY92k
b14oVFarvNbMHyFfpWP8ml43Ifwh_EAZ
```
And now we can list the two records we currently have stored:
```shell
> stronghold list --pass foo
Id: 6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Id: vPdgV4h3iNIntR8e9xoxYKDQLByqY92k, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Id: f3Duq2bWjC_zjUH376mPXWd6ntSgX6jv, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Id: b14oVFarvNbMHyFfpWP8ml43Ifwh_EAZ, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
```

When we grow tired of keeping the record we can `revoke` it:
```shell
> stronghold revoke --pass foo --id 6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj
> stronghold revoke --pass foo --id f3Duq2bWjC_zjUH376mPXWd6ntSgX6jv
```
And running the `list` command again we see that it has disappeared:
```shell
> stronghold list --pass foo
Id: vPdgV4h3iNIntR8e9xoxYKDQLByqY92k, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Id: 6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Id: b14oVFarvNbMHyFfpWP8ml43Ifwh_EAZ, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
```
But! The record is not actually removed until a garbage collection of the
chain has taken place.
Here's how you can see all records stored (not only the valid/unrevoked
records):
```shell
> stronghold list --pass foo --all
Id: 6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj
Id: vPdgV4h3iNIntR8e9xoxYKDQLByqY92k
Id: b14oVFarvNbMHyFfpWP8ml43Ifwh_EAZ
Id: f3Duq2bWjC_zjUH376mPXWd6ntSgX6jv
```
So let's make sure it's actually removed:
```shell
> stronghold garbage_collect --pass foo
Id: 6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Id: vPdgV4h3iNIntR8e9xoxYKDQLByqY92k, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Id: b14oVFarvNbMHyFfpWP8ml43Ifwh_EAZ, Hint: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
```
And check that it has in fact been removed:
```shell
> stronghold list --pass foo --all
Id: vPdgV4h3iNIntR8e9xoxYKDQLByqY92k
Id: 6Jaz9ncDC65yj6q1wffPsd1pbUU54mPj
Id: b14oVFarvNbMHyFfpWP8ml43Ifwh_EAZ
```
## Usage
```
Expand Down
2 changes: 1 addition & 1 deletion engine/examples/commandline/src/connection.rs
Expand Up @@ -75,7 +75,7 @@ pub fn send(req: CRequest) -> Option<CResult> {
State::storage_map()
.write()
.expect(line_error!())
.retain(|id, _| id.0 != del.kind() && id.1 != del.id());
.retain(|id, _| id.0 != del.kind() || id.1 != del.id());

CResult::Delete
}
Expand Down
8 changes: 4 additions & 4 deletions engine/examples/commandline/src/main.rs
Expand Up @@ -166,12 +166,12 @@ fn purge_command(matches: &ArgMatches) {
let id = RecordId::try_from(id).expect("Couldn't build a new Id");

client.revoke_record(id);
client.perform_gc();
serialize_to_snapshot(&get_snapshot_path(), pass, client);

let client = deserialize_from_snapshot(&snapshot, pass);
client.perform_gc();
assert!(client.db.take(|db| db.all().find(|i| i == &id).is_none()));

let snapshot = get_snapshot_path();
serialize_to_snapshot(&snapshot, pass, client);
serialize_to_snapshot(&get_snapshot_path(), pass, client);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions engine/vault/src/vault.rs
Expand Up @@ -170,10 +170,10 @@ impl<P: BoxProvider> DBView<P> {

/// Converts the `DBView` into a `DBWriter` for a specific record.
pub fn writer(&self, record: RecordId) -> DBWriter<P> {
let ctr = self.chains.get(&record.0)
.and_then(|r| r.highest_ctr()).unwrap_or(0u64.into());
let next_ctr = self.chains.get(&record.0)
.and_then(|r| r.highest_ctr()).map(|v| v + 1).unwrap_or(0u64.into());

DBWriter { view: self, chain: record.0, ctr }
DBWriter { view: self, chain: record.0, next_ctr }
}

/// Garbage collect the records.
Expand Down Expand Up @@ -250,13 +250,13 @@ impl<'a, P: BoxProvider> DBReader<'a, P> {
pub struct DBWriter<'a, P: BoxProvider> {
view: &'a DBView<P>,
chain: ChainId,
ctr: Val,
next_ctr: Val,
}

impl<'a, P: BoxProvider> DBWriter<'a, P> {
fn next_ctr(&mut self) -> Val {
let c = self.ctr;
self.ctr += 1;
let c = self.next_ctr;
self.next_ctr += 1;
c
}

Expand Down

0 comments on commit adf8c1d

Please sign in to comment.