Skip to content

Commit d27242a

Browse files
committed
Cleanup of API Server tests
1 parent f41a37f commit d27242a

File tree

12 files changed

+74
-122
lines changed

12 files changed

+74
-122
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api-server/stack-test-suite/tests/v1/block.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ async fn ok(#[case] seed: Seed) {
6262
let block_height = rng.gen_range(1..50);
6363
let n_blocks = rng.gen_range(block_height..100);
6464

65-
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
66-
6765
let chain_config = create_unit_test_config();
6866

6967
let chainstate_blocks = {
@@ -80,9 +78,16 @@ async fn ok(#[case] seed: Seed) {
8078
let block = tf.block(tf.to_chain_block_id(&block_id));
8179

8280
let expected_block = json!({
83-
"previous_block_id": block.prev_block_id().to_hash().encode_hex::<String>(),
84-
"timestamp": block.timestamp(),
85-
"merkle_root": block.merkle_root().encode_hex::<String>(),
81+
"header": {
82+
"previous_block_id": block.prev_block_id(),
83+
"merkle_root": block.merkle_root(),
84+
"witness_merkle_root": block.witness_merkle_root(),
85+
"timestamp": block.timestamp(),
86+
},
87+
"body": {
88+
"reward": block.block_reward().outputs().iter().clone().collect::<Vec<_>>(),
89+
"transactions": block.transactions().iter().map(|tx| tx.transaction()).collect::<Vec<_>>(),
90+
},
8691
});
8792

8893
_ = tx.send((block_id.to_hash().encode_hex::<String>(), expected_block));
@@ -129,22 +134,8 @@ async fn ok(#[case] seed: Seed) {
129134

130135
let body = response.text().await.unwrap();
131136
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
132-
let body = body.as_object().unwrap();
133-
134-
assert_eq!(
135-
body.get("previous_block_id").unwrap(),
136-
&expected_block["previous_block_id"]
137-
);
138-
assert_eq!(body.get("timestamp").unwrap(), &expected_block["timestamp"]);
139-
assert_eq!(
140-
body.get("merkle_root").unwrap(),
141-
&expected_block["merkle_root"]
142-
);
143-
144-
assert!(body.contains_key("transactions"));
145-
146-
// TODO check transactions fields
147-
// assert...
137+
138+
assert_eq!(body, expected_block);
148139

149140
task.abort();
150141
}

api-server/stack-test-suite/tests/v1/block_header.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ async fn ok(#[case] seed: Seed) {
7777
let block_id = chainstate_block_ids[block_height - 1];
7878
let block = tf.block(tf.to_chain_block_id(&block_id));
7979

80-
let expected_block = json!({
80+
let expected_header = json!({
8181
"previous_block_id": block.prev_block_id().to_hash().encode_hex::<String>(),
8282
"timestamp": block.timestamp(),
8383
"merkle_root": block.merkle_root().encode_hex::<String>(),
84+
"witness_merkle_root": block.witness_merkle_root(),
8485
});
8586

86-
_ = tx.send((block_id.to_hash().encode_hex::<String>(), expected_block));
87+
_ = tx.send((block_id.to_hash().encode_hex::<String>(), expected_header));
8788

8889
chainstate_block_ids
8990
.iter()
@@ -113,7 +114,7 @@ async fn ok(#[case] seed: Seed) {
113114
web_server(listener, web_server_state).await
114115
});
115116

116-
let (block_id, expected_block) = rx.await.unwrap();
117+
let (block_id, expected_header) = rx.await.unwrap();
117118
let url = format!("/api/v1/block/{block_id}/header");
118119

119120
// Given that the listener port is open, this will block until a
@@ -127,19 +128,8 @@ async fn ok(#[case] seed: Seed) {
127128

128129
let body = response.text().await.unwrap();
129130
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
130-
let body = body.as_object().unwrap();
131-
132-
assert_eq!(
133-
body.get("previous_block_id").unwrap(),
134-
&expected_block["previous_block_id"]
135-
);
136-
assert_eq!(body.get("timestamp").unwrap(), &expected_block["timestamp"]);
137-
assert_eq!(
138-
body.get("merkle_root").unwrap(),
139-
&expected_block["merkle_root"]
140-
);
141-
142-
assert!(!body.contains_key("transactions"));
131+
132+
assert_eq!(body, expected_header);
143133

144134
task.abort();
145135
}

api-server/stack-test-suite/tests/v1/block_reward.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async fn has_reward(#[case] seed: Seed) {
168168

169169
_ = tx.send((
170170
block_index.block_id().to_hash().encode_hex::<String>(),
171-
block.clone(),
171+
json!(block.block_reward().outputs().iter().clone().collect::<Vec<_>>()),
172172
));
173173

174174
block
@@ -197,7 +197,7 @@ async fn has_reward(#[case] seed: Seed) {
197197
}
198198
});
199199

200-
let (block_id, expected_block) = rx.await.unwrap();
200+
let (block_id, expected_reward) = rx.await.unwrap();
201201
let url = format!("/api/v1/block/{block_id}/reward");
202202

203203
// Given that the listener port is open, this will block until a
@@ -212,13 +212,7 @@ async fn has_reward(#[case] seed: Seed) {
212212
let body = response.text().await.unwrap();
213213
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
214214

215-
let block_reward = serde_json::to_value(expected_block.block_reward())
216-
.unwrap()
217-
.get("reward_outputs")
218-
.unwrap()
219-
.clone();
220-
221-
assert_eq!(body, block_reward);
215+
assert_eq!(body, expected_reward);
222216

223217
task.abort();
224218
}

api-server/stack-test-suite/tests/v1/block_transaction_ids.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async fn ok(#[case] seed: Seed) {
8484

8585
_ = tx.send((
8686
block_id.to_hash().encode_hex::<String>(),
87-
expected_transaction_ids,
87+
json!(expected_transaction_ids),
8888
));
8989

9090
chainstate_block_ids
@@ -131,14 +131,7 @@ async fn ok(#[case] seed: Seed) {
131131
let body = response.text().await.unwrap();
132132
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
133133

134-
let body_transaction_ids =
135-
body.as_object().unwrap().get("transaction_ids").unwrap().as_array().unwrap();
136-
137-
for transaction_id in expected_transaction_ids {
138-
assert!(
139-
body_transaction_ids.contains(&json!(transaction_id.to_hash().encode_hex::<String>()))
140-
);
141-
}
134+
assert_eq!(body, expected_transaction_ids);
142135

143136
task.abort();
144137
}

api-server/stack-test-suite/tests/v1/chain_at_height.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ async fn height_n(#[case] seed: Seed) {
135135

136136
assert_eq!(response.status(), 200);
137137

138-
let expected_block_id = rx.await.unwrap();
139-
140138
let body = response.text().await.unwrap();
141139
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
142140

141+
let expected_block_id = rx.await.unwrap();
142+
143143
assert_eq!(
144144
body.as_str().unwrap(),
145145
expected_block_id.to_hash().encode_hex::<String>()

api-server/stack-test-suite/tests/v1/chain_tip.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ async fn at_genesis() {
3131
let binding = Arc::clone(&chain_config);
3232
let expected_genesis_id = binding.genesis_block().get_id();
3333

34-
_ = tx.send(expected_genesis_id);
34+
_ = tx.send(json!({
35+
"block_height": 0,
36+
"block_id": expected_genesis_id.to_hash().encode_hex::<String>(),
37+
}));
3538

3639
let storage = TransactionalApiServerInMemoryStorage::new(&chain_config);
3740

@@ -54,17 +57,12 @@ async fn at_genesis() {
5457

5558
assert_eq!(response.status(), 200);
5659

57-
let expected_genesis_id = rx.await.unwrap();
58-
5960
let body = response.text().await.unwrap();
6061
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
6162

62-
assert_eq!(body["block_height"].as_u64().unwrap(), 0);
63+
let expected_tip = rx.await.unwrap();
6364

64-
assert_eq!(
65-
body["block_id"].as_str().unwrap(),
66-
expected_genesis_id.to_hash().encode_hex::<String>()
67-
);
65+
assert_eq!(body, expected_tip);
6866

6967
task.abort();
7068
}
@@ -101,7 +99,10 @@ async fn height_n(#[case] seed: Seed) {
10199
// Need the "- 1" to account for the genesis block not in the vec
102100
let expected_block_id = chainstate_block_ids[n_blocks - 1];
103101

104-
_ = tx.send((n_blocks, expected_block_id));
102+
_ = tx.send(json!({
103+
"block_height": n_blocks,
104+
"block_id": expected_block_id,
105+
}));
105106

106107
chainstate_block_ids
107108
.iter()
@@ -141,18 +142,12 @@ async fn height_n(#[case] seed: Seed) {
141142

142143
assert_eq!(response.status(), 200);
143144

144-
let (height, expected_block_id) = rx.await.unwrap();
145-
146145
let body = response.text().await.unwrap();
147146
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
148-
let _body = body.as_object().unwrap();
149147

150-
assert_eq!(body["block_height"].as_u64().unwrap(), height as u64);
148+
let expected_tip = rx.await.unwrap();
151149

152-
assert_eq!(
153-
body["block_id"].as_str().unwrap(),
154-
expected_block_id.to_hash().encode_hex::<String>()
155-
);
150+
assert_eq!(body, expected_tip);
156151

157152
task.abort();
158153
}

api-server/stack-test-suite/tests/v1/mod.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ async fn chain_genesis() {
6161
let chain_config = Arc::new(create_unit_test_config());
6262
let expected_genesis = chain_config.genesis_block().clone();
6363

64-
_ = tx.send(expected_genesis);
64+
_ = tx.send(json!({
65+
"block_id": expected_genesis.get_id(),
66+
"fun_message": expected_genesis.fun_message(),
67+
"timestamp": expected_genesis.timestamp(),
68+
"utxos": expected_genesis.utxos(),
69+
}));
6570

6671
let storage = TransactionalApiServerInMemoryStorage::new(&chain_config);
6772

@@ -84,15 +89,12 @@ async fn chain_genesis() {
8489

8590
assert_eq!(response.status(), 200);
8691

87-
let expected_genesis = rx.await.unwrap();
88-
8992
let body = response.text().await.unwrap();
9093
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
9194

92-
assert_eq!(
93-
body["block_id"].as_str().unwrap(),
94-
expected_genesis.get_id().to_hash().encode_hex::<String>()
95-
);
95+
let expected_genesis = rx.await.unwrap();
96+
97+
assert_eq!(body, expected_genesis);
9698

9799
task.abort();
98100
}

api-server/stack-test-suite/tests/v1/transaction.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ async fn ok(#[case] seed: Seed) {
8686
"version_byte": transaction.version_byte(),
8787
"is_replaceable": transaction.is_replaceable(),
8888
"flags": transaction.flags(),
89+
"inputs": transaction.inputs(),
90+
"outputs": transaction.outputs(),
8991
});
9092

9193
_ = tx.send((
@@ -141,18 +143,18 @@ async fn ok(#[case] seed: Seed) {
141143
assert_eq!(body.get("block_id").unwrap(), &block_id);
142144
assert_eq!(
143145
body.get("version_byte").unwrap(),
144-
expected_transaction.get("version_byte").unwrap()
146+
&expected_transaction["version_byte"]
145147
);
146148
assert_eq!(
147149
body.get("is_replaceable").unwrap(),
148-
expected_transaction.get("is_replaceable").unwrap()
150+
&expected_transaction["is_replaceable"]
149151
);
152+
assert_eq!(body.get("flags").unwrap(), &expected_transaction["flags"]);
153+
assert_eq!(body.get("inputs").unwrap(), &expected_transaction["inputs"]);
150154
assert_eq!(
151-
body.get("flags").unwrap(),
152-
expected_transaction.get("flags").unwrap()
155+
body.get("outputs").unwrap(),
156+
&expected_transaction["outputs"]
153157
);
154158

155-
// TODO check inputs and outputs
156-
157159
task.abort();
158160
}

api-server/stack-test-suite/tests/v1/transaction_merkle_path.rs

+4-23
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ async fn ok(#[case] seed: Seed) {
409409
.unwrap()
410410
.into_hashes();
411411

412-
let expected_transaction = json!({
412+
let expected_path = json!({
413413
"block_id": block_id.to_hash().encode_hex::<String>(),
414414
"transaction_index": transaction_index,
415415
"merkle_root": block.merkle_root().encode_hex::<String>(),
@@ -419,7 +419,7 @@ async fn ok(#[case] seed: Seed) {
419419

420420
_ = tx.send((
421421
transaction_id.to_hash().encode_hex::<String>(),
422-
expected_transaction,
422+
expected_path,
423423
));
424424

425425
chainstate_block_ids
@@ -450,7 +450,7 @@ async fn ok(#[case] seed: Seed) {
450450
web_server(listener, web_server_state).await
451451
});
452452

453-
let (transaction_id, expected_transaction) = rx.await.unwrap();
453+
let (transaction_id, expected_path) = rx.await.unwrap();
454454
let url = format!("/api/v1/transaction/{transaction_id}/merkle-path");
455455

456456
// Given that the listener port is open, this will block until a
@@ -464,27 +464,8 @@ async fn ok(#[case] seed: Seed) {
464464

465465
let body = response.text().await.unwrap();
466466
let body: serde_json::Value = serde_json::from_str(&body).unwrap();
467-
let body = body.as_object().unwrap();
468-
469-
assert_eq!(
470-
body.get("block_id").unwrap(),
471-
expected_transaction.get("block_id").unwrap()
472-
);
473-
assert_eq!(
474-
body.get("transaction_index").unwrap(),
475-
expected_transaction.get("transaction_index").unwrap()
476-
);
477-
assert_eq!(
478-
body.get("merkle_root").unwrap(),
479-
expected_transaction.get("merkle_root").unwrap()
480-
);
481467

482-
for (index, hash) in body.get("merkle_path").unwrap().as_array().unwrap().iter().enumerate() {
483-
assert_eq!(
484-
hash,
485-
expected_transaction.get("merkle_path").unwrap().get(index).unwrap()
486-
);
487-
}
468+
assert_eq!(body, expected_path);
488469

489470
task.abort();
490471
}

api-server/web-server/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ api-server-common = { path = "../api-server-common" }
1212
common = { path = "../../common/" }
1313
crypto = { path = "../../crypto/" }
1414
logging = { path = "../../logging" }
15+
serialization = { path = "../../serialization" }
1516

1617
axum.workspace = true
1718
clap = { workspace = true, features = ["derive"] }

0 commit comments

Comments
 (0)