Skip to content

Commit c4ea964

Browse files
committed
test: extend capabilities to create invalid transactions
1 parent 5f9a8f4 commit c4ea964

File tree

4 files changed

+66
-43
lines changed

4 files changed

+66
-43
lines changed

crates/common/src/transaction_builder.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ impl UncommittedTransaction<'_> {
2626
let hc = self
2727
.builder
2828
.hashchains
29-
.get_mut(&self.transaction.id)
30-
.expect("Hashchain should be found");
29+
.entry(self.transaction.id.clone())
30+
.or_insert_with(Hashchain::empty);
3131

3232
hc.add_entry(self.transaction.entry.clone())
3333
.expect("Adding transaction entry to hashchain should work");
@@ -114,16 +114,16 @@ impl TransactionBuilder {
114114
}
115115
}
116116

117-
pub fn create_account_with_random_key(
117+
pub fn create_account_with_random_key_signed(
118118
&mut self,
119119
id: &str,
120120
service_id: &str,
121121
) -> UncommittedTransaction {
122-
let random_signing_key = SigningKey::new_ed25519();
123-
self.create_account_for_existing_service(id, service_id, random_signing_key)
122+
let account_signing_key = SigningKey::new_ed25519();
123+
self.create_account_signed(id, service_id, account_signing_key)
124124
}
125125

126-
pub fn create_account_for_existing_service(
126+
pub fn create_account_signed(
127127
&mut self,
128128
id: &str,
129129
service_id: &str,
@@ -136,6 +136,16 @@ impl TransactionBuilder {
136136
self.create_account(id, service_id, &service_signing_key, signing_key)
137137
}
138138

139+
pub fn create_account_with_random_key(
140+
&mut self,
141+
id: &str,
142+
service_id: &str,
143+
service_signing_key: &SigningKey,
144+
) -> UncommittedTransaction {
145+
let account_signing_key = SigningKey::new_ed25519();
146+
self.create_account(id, service_id, service_signing_key, account_signing_key)
147+
}
148+
139149
pub fn create_account(
140150
&mut self,
141151
id: &str,
@@ -203,11 +213,9 @@ impl TransactionBuilder {
203213
signing_key: &SigningKey,
204214
key_idx: usize,
205215
) -> UncommittedTransaction {
206-
let Some(hc) = self.hashchains.get(id) else {
207-
panic!("No existing hashchain found for {}", id)
208-
};
216+
let last_hash = self.hashchains.get(id).map_or(Digest::zero(), Hashchain::last_hash);
209217

210-
let entry = HashchainEntry::new_add_key(key, hc.last_hash(), signing_key, key_idx);
218+
let entry = HashchainEntry::new_add_key(key, last_hash, signing_key, key_idx);
211219

212220
UncommittedTransaction {
213221
transaction: Transaction {
@@ -238,9 +246,9 @@ impl TransactionBuilder {
238246
signing_key: &SigningKey,
239247
key_idx: usize,
240248
) -> UncommittedTransaction {
241-
let hc = self.hashchains.get(id).expect("No existing hashchain found for");
249+
let last_hash = self.hashchains.get(id).map_or(Digest::zero(), Hashchain::last_hash);
242250

243-
let entry = HashchainEntry::new_revoke_key(key, hc.last_hash(), signing_key, key_idx);
251+
let entry = HashchainEntry::new_revoke_key(key, last_hash, signing_key, key_idx);
244252

245253
UncommittedTransaction {
246254
transaction: Transaction {
@@ -359,15 +367,10 @@ impl TransactionBuilder {
359367
signing_key: &SigningKey,
360368
key_idx: usize,
361369
) -> UncommittedTransaction {
362-
let hc = self.hashchains.get(id).expect("Hashchain to add data on should be found");
363-
364-
let entry = HashchainEntry::new_add_data(
365-
data,
366-
data_signature,
367-
hc.last_hash(),
368-
signing_key,
369-
key_idx,
370-
);
370+
let last_hash = self.hashchains.get(id).map_or(Digest::zero(), Hashchain::last_hash);
371+
372+
let entry =
373+
HashchainEntry::new_add_data(data, data_signature, last_hash, signing_key, key_idx);
371374

372375
UncommittedTransaction {
373376
transaction: Transaction {

crates/common/src/tree/mod.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ mod tests {
3939
};
4040
assert!(insert_proof.verify().is_ok());
4141

42-
let account_tx = tx_builder.create_account_with_random_key("acc_1", "service_1").commit();
42+
let account_tx =
43+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").commit();
4344

4445
let Proof::Insert(insert_proof) = tree.process_transaction(account_tx).unwrap() else {
4546
panic!("Processing transaction did not return the expected insert proof");
@@ -63,8 +64,14 @@ mod tests {
6364
let mut tree = KeyDirectoryTree::new(Arc::new(MockTreeStore::default()));
6465
let mut tx_builder = TransactionBuilder::new();
6566

67+
let service_signing_key = SigningKey::new_ed25519();
68+
6669
let invalid_account_tx = tx_builder
67-
.create_account_with_random_key("acc_1", "service_id_that_does_not_exist")
70+
.create_account_with_random_key(
71+
"acc_1",
72+
"service_id_that_does_not_exist",
73+
&service_signing_key,
74+
)
6875
.build();
6976

7077
let insertion_result = tree.process_transaction(invalid_account_tx);
@@ -108,9 +115,10 @@ mod tests {
108115
let mut tx_builder = TransactionBuilder::new();
109116

110117
let service_tx = tx_builder.register_service_with_random_keys("service_1").commit();
111-
let account_tx = tx_builder.create_account_with_random_key("acc_1", "service_1").commit();
118+
let account_tx =
119+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").commit();
112120
let account_with_same_id_tx =
113-
tx_builder.create_account_with_random_key("acc_1", "service_1").build();
121+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").build();
114122

115123
let Proof::Insert(insert_proof) = tree.process_transaction(service_tx).unwrap() else {
116124
panic!("Processing service registration failed")
@@ -132,7 +140,8 @@ mod tests {
132140
let mut tx_builder = TransactionBuilder::new();
133141

134142
let service_tx = tx_builder.register_service_with_random_keys("service_1").commit();
135-
let acc_tx = tx_builder.create_account_with_random_key("acc_1", "service_1").commit();
143+
let acc_tx =
144+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").commit();
136145

137146
tree.process_transaction(service_tx).unwrap();
138147
tree.process_transaction(acc_tx).unwrap();
@@ -161,8 +170,8 @@ mod tests {
161170

162171
// This is a signing key not known to the storage yet
163172
let random_signing_key = SigningKey::new_ed25519();
164-
// This transaction shall be invalid, because it is signed with a unknown key
165-
let invalid_key_tx = tx_builder.add_random_key("acc_1", &random_signing_key, 0).commit();
173+
// This transaction shall be invalid, because it is signed with an unknown key
174+
let invalid_key_tx = tx_builder.add_random_key("acc_1", &random_signing_key, 0).build();
166175

167176
let result = tree.process_transaction(invalid_key_tx);
168177
assert!(result.is_err());
@@ -187,8 +196,10 @@ mod tests {
187196
let mut tx_builder = TransactionBuilder::new();
188197

189198
let service_tx = tx_builder.register_service_with_random_keys("service_1").commit();
190-
let acc1_tx = tx_builder.create_account_with_random_key("acc_1", "service_1").commit();
191-
let acc2_tx = tx_builder.create_account_with_random_key("acc_2", "service_1").commit();
199+
let acc1_tx =
200+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").commit();
201+
let acc2_tx =
202+
tx_builder.create_account_with_random_key_signed("acc_2", "service_1").commit();
192203

193204
tree.process_transaction(service_tx).unwrap();
194205

@@ -224,8 +235,10 @@ mod tests {
224235
let mut tx_builder = TransactionBuilder::new();
225236

226237
let service_tx = tx_builder.register_service_with_random_keys("service_1").commit();
227-
let acc1_tx = tx_builder.create_account_with_random_key("acc_1", "service_1").commit();
228-
let acc2_tx = tx_builder.create_account_with_random_key("acc_2", "service_1").commit();
238+
let acc1_tx =
239+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").commit();
240+
let acc2_tx =
241+
tx_builder.create_account_with_random_key_signed("acc_2", "service_1").commit();
229242

230243
tree.process_transaction(service_tx).unwrap();
231244
tree.process_transaction(acc1_tx).unwrap();
@@ -239,7 +252,7 @@ mod tests {
239252
let last_proof = tree.process_transaction(add_key_to_2_tx).unwrap();
240253

241254
// Update account_2 using the correct key index
242-
let Proof::Insert(insert_proof) = last_proof else {
255+
let Proof::Update(update_proof) = last_proof else {
243256
panic!("Expetced insert proof for transaction");
244257
};
245258

@@ -251,7 +264,10 @@ mod tests {
251264

252265
assert!(matches!(get_result1, Found(hc, _) if &hc == test_hashchain_acc1));
253266
assert!(matches!(get_result2, Found(hc, _) if &hc == test_hashchain_acc2));
254-
assert_eq!(insert_proof.new_root, tree.get_commitment().unwrap());
267+
assert_eq!(
268+
Digest::from(update_proof.new_root),
269+
tree.get_commitment().unwrap()
270+
);
255271
}
256272

257273
#[test]
@@ -260,7 +276,8 @@ mod tests {
260276
let mut tx_builder = TransactionBuilder::new();
261277

262278
let service_tx = tx_builder.register_service_with_random_keys("service_1").commit();
263-
let account1_tx = tx_builder.create_account_with_random_key("acc_1", "service_1").commit();
279+
let account1_tx =
280+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").commit();
264281

265282
tree.process_transaction(service_tx).unwrap();
266283

@@ -277,8 +294,10 @@ mod tests {
277294
let mut tx_builder = TransactionBuilder::new();
278295

279296
let service_tx = tx_builder.register_service_with_random_keys("service_1").commit();
280-
let account1_tx = tx_builder.create_account_with_random_key("acc_1", "service_1").commit();
281-
let account2_tx = tx_builder.create_account_with_random_key("acc_2", "service_2").commit();
297+
let account1_tx =
298+
tx_builder.create_account_with_random_key_signed("acc_1", "service_1").commit();
299+
let account2_tx =
300+
tx_builder.create_account_with_random_key_signed("acc_2", "service_1").commit();
282301

283302
tree.process_transaction(service_tx).unwrap();
284303

crates/node_types/prover/src/prover/tests.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ fn create_mock_transactions(service_id: String) -> Vec<Transaction> {
2222
vec![
2323
transaction_builder.register_service_with_random_keys(&service_id).commit(),
2424
transaction_builder
25-
.create_account_with_random_key("user1@example.com", &service_id)
25+
.create_account_with_random_key_signed("user1@example.com", &service_id)
2626
.commit(),
2727
transaction_builder
28-
.create_account_with_random_key("user2@example.com", &service_id)
28+
.create_account_with_random_key_signed("user2@example.com", &service_id)
2929
.commit(),
3030
transaction_builder.add_random_key_verified_with_root("user1@example.com").commit(),
3131
]
@@ -54,8 +54,9 @@ async fn test_process_transactions() {
5454
let mut transaction_builder = TransactionBuilder::new();
5555
let register_service_transaction =
5656
transaction_builder.register_service_with_random_keys("test_service").commit();
57-
let create_account_transaction =
58-
transaction_builder.create_account_with_random_key("test_account", "test_service").commit();
57+
let create_account_transaction = transaction_builder
58+
.create_account_with_random_key_signed("test_account", "test_service")
59+
.commit();
5960

6061
let proof = prover.process_transaction(register_service_transaction).await.unwrap();
6162
assert!(matches!(proof, Proof::Insert(_)));
@@ -96,7 +97,7 @@ async fn test_execute_block_with_invalid_tx() {
9697

9798
let transactions = vec![
9899
tx_builder.register_service_with_random_keys("service_id").commit(),
99-
tx_builder.create_account_with_random_key("account_id", "service_id").commit(),
100+
tx_builder.create_account_with_random_key_signed("account_id", "service_id").commit(),
100101
// add new key, so it will be index = 1
101102
tx_builder.add_key_verified_with_root("account_id", new_key_vk.clone()).commit(),
102103
// revoke new key again

crates/tests/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async fn test_light_client_prover_talking() -> Result<()> {
9191
for _ in 0..num_new_accounts {
9292
let random_user_id = format!("{}@gmail.com", i);
9393
let new_acc = transaction_builder
94-
.create_account_with_random_key(random_user_id.as_str(), "test_service")
94+
.create_account_with_random_key_signed(random_user_id.as_str(), "test_service")
9595
.commit();
9696
match prover.clone().validate_and_queue_update(new_acc).await {
9797
Ok(_) => {

0 commit comments

Comments
 (0)