1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use holo_hash::AgentPubKey;
use holo_hash::DhtOpHash;
use holochain_keystore::AgentPubKeyExt;
use holochain_keystore::MetaLairClient;
use holochain_serialized_bytes::prelude::*;
use holochain_sqlite::prelude::*;
use holochain_sqlite::rusqlite::named_params;
use holochain_sqlite::rusqlite::OptionalExtension;
use holochain_sqlite::rusqlite::Transaction;
use holochain_zome_types::signature::Signature;
use holochain_zome_types::Timestamp;
use holochain_zome_types::ValidationStatus;
use mutations::StateMutationResult;
use crate::mutations;
use crate::prelude::from_blob;
use crate::prelude::StateQueryResult;
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
SerializedBytes,
)]
pub struct ValidationReceipt {
pub dht_op_hash: DhtOpHash,
pub validation_status: ValidationStatus,
pub validator: AgentPubKey,
pub when_integrated: Timestamp,
}
impl ValidationReceipt {
pub async fn sign(
self,
keystore: &MetaLairClient,
) -> holochain_keystore::LairResult<SignedValidationReceipt> {
let signature = self.validator.sign(keystore, self.clone()).await?;
Ok(SignedValidationReceipt {
receipt: self,
validator_signature: signature,
})
}
}
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
SerializedBytes,
)]
pub struct SignedValidationReceipt {
pub receipt: ValidationReceipt,
pub validator_signature: Signature,
}
pub fn list_receipts(
txn: &Transaction,
op_hash: &DhtOpHash,
) -> StateQueryResult<Vec<SignedValidationReceipt>> {
let mut stmt = txn.prepare(
"
SELECT blob FROM ValidationReceipt WHERE op_hash = :op_hash
",
)?;
let iter = stmt.query_and_then(
named_params! {
":op_hash": op_hash
},
|row| from_blob::<SignedValidationReceipt>(row.get("blob")?),
)?;
iter.collect()
}
pub fn count_valid(txn: &Transaction, op_hash: &DhtOpHash) -> DatabaseResult<usize> {
let count: usize = txn
.query_row(
"SELECT COUNT(hash) FROM ValidationReceipt WHERE op_hash = :op_hash",
named_params! {
":op_hash": op_hash
},
|row| row.get(0),
)
.optional()?
.unwrap_or(0);
Ok(count)
}
pub fn add_if_unique(
txn: &mut Transaction,
receipt: SignedValidationReceipt,
) -> StateMutationResult<()> {
mutations::insert_validation_receipt(txn, receipt)
}
#[cfg(test)]
mod tests {
use super::*;
use fixt::prelude::*;
use holo_hash::HasHash;
use holochain_types::dht_op::DhtOp;
use holochain_types::dht_op::DhtOpHashed;
use holochain_zome_types::fixt::*;
async fn fake_vr(
dht_op_hash: &DhtOpHash,
keystore: &MetaLairClient,
) -> SignedValidationReceipt {
let agent = keystore.new_sign_keypair_random().await.unwrap();
let receipt = ValidationReceipt {
dht_op_hash: dht_op_hash.clone(),
validation_status: ValidationStatus::Valid,
validator: agent,
when_integrated: Timestamp::now(),
};
receipt.sign(keystore).await.unwrap()
}
#[tokio::test(flavor = "multi_thread")]
async fn test_validation_receipts_db_populate_and_list() -> StateMutationResult<()> {
observability::test_run().ok();
let test_env = crate::test_utils::test_cell_env();
let env = test_env.env();
let keystore = crate::test_utils::test_keystore();
let op = DhtOpHashed::from_content_sync(DhtOp::RegisterAgentActivity(
fixt!(Signature),
fixt!(Header),
));
let test_op_hash = op.as_hash().clone();
env.conn()
.unwrap()
.with_commit_sync(|txn| mutations::insert_op(txn, op, true))
.unwrap();
let vr1 = fake_vr(&test_op_hash, &keystore).await;
let vr2 = fake_vr(&test_op_hash, &keystore).await;
{
env.conn().unwrap().with_commit_sync(|txn| {
add_if_unique(txn, vr1.clone())?;
add_if_unique(txn, vr1.clone())?;
add_if_unique(txn, vr2.clone())
})?;
env.conn()
.unwrap()
.with_commit_sync(|txn| add_if_unique(txn, vr1.clone()))?;
}
let mut g = env.conn().unwrap();
g.with_reader_test(|reader| {
assert_eq!(2, count_valid(&reader, &test_op_hash).unwrap());
let mut list = list_receipts(&reader, &test_op_hash).unwrap();
list.sort_by(|a, b| {
a.receipt
.validator
.partial_cmp(&b.receipt.validator)
.unwrap()
});
let mut expects = vec![vr1, vr2];
expects.sort_by(|a, b| {
a.receipt
.validator
.partial_cmp(&b.receipt.validator)
.unwrap()
});
assert_eq!(expects, list);
});
Ok(())
}
}