-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathintegration_tests.rs
More file actions
306 lines (287 loc) · 11.7 KB
/
integration_tests.rs
File metadata and controls
306 lines (287 loc) · 11.7 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
mod common;
use crate::common::{create_mock_user_decryption_request_tx, init_kms_worker};
use alloy::{
hex::{self, FromHex},
primitives::{FixedBytes, U256},
providers::{ProviderBuilder, mock::Asserter},
sol_types::SolValue,
};
use connector_utils::{
tests::{
db::requests::{
InsertRequestOptions, check_no_uncompleted_request_in_db, insert_rand_request,
},
rand::{rand_digest, rand_sns_ct},
setup::{
DbInstance, S3_CT_DIGEST, S3_CT_HANDLE, S3Instance, TestInstanceBuilder,
init_host_chains_acl_contracts_mock,
},
},
types::{
GatewayEventKind, KmsGrpcResponse, KmsResponse, KmsResponseKind, db::EventType,
kms_response,
},
};
use fhevm_gateway_bindings::gateway_config::GatewayConfig::Coprocessor;
use kms_grpc::kms::v1::{
CrsGenResult, Empty, InitiateResharingResponse, KeyGenPreprocResult, KeyGenResult,
PublicDecryptionResponse, PublicDecryptionResponsePayload, RequestId, UserDecryptionResponse,
UserDecryptionResponsePayload,
};
use kms_worker::core::Config;
use mocktail::{MockSet, server::MockServer};
use rstest::rstest;
use sqlx::{Pool, Postgres};
use std::{collections::HashMap, time::Duration};
use tokio_util::sync::CancellationToken;
use tracing::{info, warn};
#[rstest]
#[case::public_decryption(EventType::PublicDecryptionRequest, false)]
#[case::user_decryption(EventType::UserDecryptionRequest, false)]
#[case::prep_keygen(EventType::PrepKeygenRequest, false)]
#[case::keygen(EventType::KeygenRequest, false)]
#[case::crsgen(EventType::CrsgenRequest, false)]
#[case::prss_init(EventType::PrssInit, false)]
#[case::key_reshare_same_set(EventType::KeyReshareSameSet, false)]
#[case::public_decryption_already_sent(EventType::PublicDecryptionRequest, true)]
#[case::user_decryption_already_sent(EventType::UserDecryptionRequest, true)]
#[case::prep_keygen_already_sent(EventType::PrepKeygenRequest, true)]
#[case::keygen_already_sent(EventType::KeygenRequest, true)]
#[case::crsgen_already_sent(EventType::CrsgenRequest, true)]
#[timeout(Duration::from_secs(60))]
#[tokio::test]
async fn test_processing_request(
#[case] event_type: EventType,
#[case] already_sent: bool,
) -> anyhow::Result<()> {
// Setup real DB and S3 instance
let test_instance = TestInstanceBuilder::default()
.with_db(DbInstance::setup().await?)
.with_s3(S3Instance::setup().await?)
.build();
// Mocking Gateway
let asserter = Asserter::new();
let mut sns_ct = rand_sns_ct();
sns_ct.ctHandle = FixedBytes::<32>::from_hex(S3_CT_HANDLE)?;
sns_ct.snsCiphertextDigest = FixedBytes::<32>::from_hex(S3_CT_DIGEST)?;
let mut insert_options = InsertRequestOptions::new()
.with_already_sent(already_sent)
.with_sns_ct_materials(vec![sns_ct.clone()]);
if matches!(event_type, EventType::UserDecryptionRequest) {
// Mocking `get_transaction_by_hash` call result
let tx_hash = rand_digest();
let mock_tx = create_mock_user_decryption_request_tx(tx_hash, sns_ct.ctHandle)?;
insert_options = insert_options.with_tx_hash(tx_hash);
asserter.push_success(&mock_tx);
}
let get_copro_call_response = Coprocessor {
s3BucketUrl: format!("{}/ct128", test_instance.s3_url()),
..Default::default()
};
asserter.push_success(&get_copro_call_response.abi_encode());
let gateway_mock_provider = ProviderBuilder::new()
.disable_recommended_fillers()
.connect_mocked_client(asserter.clone());
info!("Gateway mock started!");
// Mocking Host chain
let acl_contracts_mock = match &event_type {
EventType::PublicDecryptionRequest => {
init_host_chains_acl_contracts_mock(sns_ct.ctHandle.as_slice(), vec![true])
}
EventType::UserDecryptionRequest => {
init_host_chains_acl_contracts_mock(sns_ct.ctHandle.as_slice(), vec![true, true])
}
_ => HashMap::new(),
};
// Insert request in DB to trigger kms_worker job
let request = insert_rand_request(test_instance.db(), event_type, insert_options).await?;
// Mocking KMS responses
let kms_mocks = prepare_mocks(&request, already_sent);
let kms_mock_server =
MockServer::new_grpc("kms_service.v1.CoreServiceEndpoint").with_mocks(kms_mocks);
kms_mock_server.start().await?;
info!("KMS mock server started!");
// Starting kms_worker
let config = Config {
kms_core_endpoints: vec![kms_mock_server.base_url().unwrap().to_string()],
..Default::default()
};
let kms_worker = init_kms_worker(
config,
gateway_mock_provider,
acl_contracts_mock,
test_instance.db(),
)
.await?;
let cancel_token = CancellationToken::new();
let kms_worker_task = tokio::spawn(kms_worker.start(cancel_token.clone()));
info!("KmsWorker started!");
// Waiting for kms_worker to process the request
match &request {
GatewayEventKind::PrssInit(_) | GatewayEventKind::KeyReshareSameSet(_) => {
while let Err(e) =
check_no_uncompleted_request_in_db(test_instance.db(), event_type).await
{
warn!("Still requests in DB: {e}");
tokio::time::sleep(Duration::from_millis(200)).await;
}
}
_ => {
let response = wait_for_response_in_db(test_instance.db(), &request).await?;
check_response_data(&request, response)?;
check_no_uncompleted_request_in_db(test_instance.db(), event_type).await?;
}
}
// Stopping the test
cancel_token.cancel();
kms_worker_task.await.unwrap();
Ok(())
}
fn prepare_mocks(req: &GatewayEventKind, already_sent: bool) -> MockSet {
let mut kms_mocks = MockSet::new();
// Gets the request ID and endpoints for the given request type
let (request_id_u256, req_endpoint, resp_endpoint) = match req {
GatewayEventKind::PublicDecryption(r) => {
(r.decryptionId, "PublicDecrypt", "GetPublicDecryptionResult")
}
GatewayEventKind::UserDecryption(r) => {
(r.decryptionId, "UserDecrypt", "GetUserDecryptionResult")
}
GatewayEventKind::PrepKeygen(r) => {
(r.prepKeygenId, "KeyGenPreproc", "GetKeyGenPreprocResult")
}
GatewayEventKind::Keygen(r) => (r.keyId, "KeyGen", "GetKeyGenResult"),
GatewayEventKind::Crsgen(r) => (r.crsId, "CrsGen", "GetCrsGenResult"),
GatewayEventKind::PrssInit(id) => (*id, "Init", ""),
GatewayEventKind::KeyReshareSameSet(r) => (r.keyId, "InitiateResharing", ""),
};
let request_id = u256_to_request_id(request_id_u256);
// No mock if `already_sent` to ensure this request is skipped on kms_worker side
if !already_sent {
// Mock initial KMS response to initial GRPC request
kms_mocks.mock(|when, then| {
when.path(format!(
"/kms_service.v1.CoreServiceEndpoint/{req_endpoint}"
));
match req {
GatewayEventKind::KeyReshareSameSet(_) => {
then.pb(InitiateResharingResponse::default())
}
// KMS returns `Empty` for all kind of requests except `KeyReshareSameSet`
_ => then.pb(Empty::default()),
};
});
}
// Mock response of result polling
kms_mocks.mock(|when, then| {
when.path(format!(
"/kms_service.v1.CoreServiceEndpoint/{resp_endpoint}"
));
match req {
GatewayEventKind::PublicDecryption(_) => then.pb(PublicDecryptionResponse {
payload: Some(PublicDecryptionResponsePayload::default()),
..Default::default()
}),
GatewayEventKind::UserDecryption(_) => then.pb(UserDecryptionResponse {
payload: Some(UserDecryptionResponsePayload::default()),
..Default::default()
}),
GatewayEventKind::PrepKeygen(_) => then.pb(KeyGenPreprocResult {
preprocessing_id: request_id,
..Default::default()
}),
GatewayEventKind::Keygen(_) => then.pb(KeyGenResult {
request_id,
..Default::default()
}),
GatewayEventKind::Crsgen(_) => then.pb(CrsGenResult {
request_id,
..Default::default()
}),
_ => then.pb(Empty::default()),
};
});
kms_mocks
}
async fn wait_for_response_in_db(
db: &Pool<Postgres>,
req: &GatewayEventKind,
) -> anyhow::Result<KmsResponse> {
info!("Waiting for response to be stored in DB...");
let query = match req {
GatewayEventKind::PublicDecryption(_) => "SELECT * FROM public_decryption_responses",
GatewayEventKind::UserDecryption(_) => "SELECT * FROM user_decryption_responses",
GatewayEventKind::PrepKeygen(_) => "SELECT * FROM prep_keygen_responses",
GatewayEventKind::Keygen(_) => "SELECT * FROM keygen_responses",
GatewayEventKind::Crsgen(_) => "SELECT * FROM crsgen_responses",
_ => unimplemented!(),
};
let response = loop {
let result = sqlx::query(query).fetch_all(db).await?;
if result.is_empty() {
warn!("Not yet...");
tokio::time::sleep(Duration::from_millis(200)).await;
} else {
match req {
GatewayEventKind::PublicDecryption(_) => {
break kms_response::from_public_decryption_row(&result[0])?;
}
GatewayEventKind::UserDecryption(_) => {
break kms_response::from_user_decryption_row(&result[0])?;
}
GatewayEventKind::PrepKeygen(_) => {
break kms_response::from_prep_keygen_row(&result[0])?;
}
GatewayEventKind::Keygen(_) => {
break kms_response::from_keygen_row(&result[0])?;
}
GatewayEventKind::Crsgen(_) => {
break kms_response::from_crsgen_row(&result[0])?;
}
_ => unimplemented!(),
};
}
};
info!("OK!");
Ok(response)
}
fn check_response_data(request: &GatewayEventKind, response: KmsResponse) -> anyhow::Result<()> {
info!("Checking response data...");
let expected_response = match request {
GatewayEventKind::PublicDecryption(r) => KmsGrpcResponse::PublicDecryption {
decryption_id: r.decryptionId,
grpc_response: PublicDecryptionResponse {
payload: Some(PublicDecryptionResponsePayload::default()),
..Default::default()
},
},
GatewayEventKind::UserDecryption(r) => KmsGrpcResponse::UserDecryption {
decryption_id: r.decryptionId,
grpc_response: UserDecryptionResponse {
payload: Some(UserDecryptionResponsePayload::default()),
..Default::default()
},
},
GatewayEventKind::PrepKeygen(r) => KmsGrpcResponse::PrepKeygen(KeyGenPreprocResult {
preprocessing_id: u256_to_request_id(r.prepKeygenId),
..Default::default()
}),
GatewayEventKind::Keygen(r) => KmsGrpcResponse::Keygen(KeyGenResult {
request_id: u256_to_request_id(r.keyId),
..Default::default()
}),
GatewayEventKind::Crsgen(r) => KmsGrpcResponse::Crsgen(CrsGenResult {
request_id: u256_to_request_id(r.crsId),
..Default::default()
}),
_ => unimplemented!(),
};
assert_eq!(response.kind, KmsResponseKind::process(expected_response)?);
info!("OK!");
Ok(())
}
fn u256_to_request_id(value: U256) -> Option<RequestId> {
Some(RequestId {
request_id: hex::encode(value.to_be_bytes::<32>()),
})
}