-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdeny_list_check.rs
288 lines (247 loc) · 9.26 KB
/
deny_list_check.rs
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
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0
use std::{
collections::HashSet,
str::FromStr,
sync::{Arc, RwLock},
};
use sqlx::{postgres::PgListener, PgPool};
use tap_core::receipt::{
checks::{Check, CheckError, CheckResult},
state::Checking,
ReceiptWithState,
};
use thegraph_core::alloy::primitives::Address;
use crate::middleware::Sender;
pub struct DenyListCheck {
sender_denylist: Arc<RwLock<HashSet<Address>>>,
_sender_denylist_watcher_handle: Arc<tokio::task::JoinHandle<()>>,
sender_denylist_watcher_cancel_token: tokio_util::sync::CancellationToken,
#[cfg(test)]
notify: std::sync::Arc<tokio::sync::Notify>,
}
impl DenyListCheck {
pub async fn new(pgpool: PgPool) -> Self {
// Listen to pg_notify events. We start it before updating the sender_denylist so that we
// don't miss any updates. PG will buffer the notifications until we start consuming them.
let mut pglistener = PgListener::connect_with(&pgpool.clone()).await.unwrap();
pglistener
.listen("scalar_tap_deny_notification")
.await
.expect(
"should be able to subscribe to Postgres Notify events on the channel \
'scalar_tap_deny_notification'",
);
// Fetch the denylist from the DB
let sender_denylist = Arc::new(RwLock::new(HashSet::new()));
Self::sender_denylist_reload(pgpool.clone(), sender_denylist.clone())
.await
.expect("should be able to fetch the sender_denylist from the DB on startup");
#[cfg(test)]
let notify = std::sync::Arc::new(tokio::sync::Notify::new());
let sender_denylist_watcher_cancel_token = tokio_util::sync::CancellationToken::new();
let sender_denylist_watcher_handle = Arc::new(tokio::spawn(Self::sender_denylist_watcher(
pgpool.clone(),
pglistener,
sender_denylist.clone(),
sender_denylist_watcher_cancel_token.clone(),
#[cfg(test)]
notify.clone(),
)));
Self {
sender_denylist,
_sender_denylist_watcher_handle: sender_denylist_watcher_handle,
sender_denylist_watcher_cancel_token,
#[cfg(test)]
notify,
}
}
async fn sender_denylist_reload(
pgpool: PgPool,
denylist_rwlock: Arc<RwLock<HashSet<Address>>>,
) -> anyhow::Result<()> {
// Fetch the denylist from the DB
let sender_denylist = sqlx::query!(
r#"
SELECT sender_address FROM scalar_tap_denylist
"#
)
.fetch_all(&pgpool)
.await?
.iter()
.map(|row| Address::from_str(&row.sender_address))
.collect::<Result<HashSet<_>, _>>()?;
*(denylist_rwlock.write().unwrap()) = sender_denylist;
Ok(())
}
async fn sender_denylist_watcher(
pgpool: PgPool,
mut pglistener: PgListener,
denylist: Arc<RwLock<HashSet<Address>>>,
cancel_token: tokio_util::sync::CancellationToken,
#[cfg(test)] notify: std::sync::Arc<tokio::sync::Notify>,
) {
#[derive(serde::Deserialize)]
struct DenylistNotification {
tg_op: String,
sender_address: Address,
}
loop {
tokio::select! {
_ = cancel_token.cancelled() => {
break;
}
pg_notification = pglistener.recv() => {
let pg_notification = pg_notification.expect(
"should be able to receive Postgres Notify events on the channel \
'scalar_tap_deny_notification'",
);
let denylist_notification: DenylistNotification =
serde_json::from_str(pg_notification.payload()).expect(
"should be able to deserialize the Postgres Notify event payload as a \
DenylistNotification",
);
match denylist_notification.tg_op.as_str() {
"INSERT" => {
denylist
.write()
.unwrap()
.insert(denylist_notification.sender_address);
}
"DELETE" => {
denylist
.write()
.unwrap()
.remove(&denylist_notification.sender_address);
}
// UPDATE and TRUNCATE are not expected to happen. Reload the entire denylist.
_ => {
tracing::error!(
"Received an unexpected denylist table notification: {}. Reloading entire \
denylist.",
denylist_notification.tg_op
);
Self::sender_denylist_reload(pgpool.clone(), denylist.clone())
.await
.expect("should be able to reload the sender denylist")
}
}
#[cfg(test)]
notify.notify_one();
}
}
}
}
}
#[async_trait::async_trait]
impl Check for DenyListCheck {
async fn check(
&self,
ctx: &tap_core::receipt::Context,
_: &ReceiptWithState<Checking>,
) -> CheckResult {
let Sender(receipt_sender) = ctx
.get::<Sender>()
.ok_or(CheckError::Failed(anyhow::anyhow!("Could not find sender")))?;
// Check that the sender is not denylisted
if self
.sender_denylist
.read()
.unwrap()
.contains(receipt_sender)
{
return Err(CheckError::Failed(anyhow::anyhow!(
"Received a receipt from a denylisted sender: {}",
receipt_sender
)));
}
Ok(())
}
}
impl Drop for DenyListCheck {
fn drop(&mut self) {
// Clean shutdown for the sender_denylist_watcher
// Though since it's not a critical task, we don't wait for it to finish (join).
self.sender_denylist_watcher_cancel_token.cancel();
}
}
#[cfg(test)]
mod tests {
use sqlx::PgPool;
use tap_core::receipt::{checks::Check, Context, ReceiptWithState};
use test_assets::{self, create_signed_receipt, SignedReceiptRequest, TAP_SENDER};
use thegraph_core::alloy::hex::ToHexExt;
use super::*;
async fn new_deny_list_check(pgpool: PgPool) -> DenyListCheck {
// Mock escrow accounts
DenyListCheck::new(pgpool).await
}
#[sqlx::test(migrations = "../../migrations")]
async fn test_sender_denylist(pgpool: PgPool) {
// Add the sender to the denylist
sqlx::query!(
r#"
INSERT INTO scalar_tap_denylist (sender_address)
VALUES ($1)
"#,
TAP_SENDER.1.encode_hex()
)
.execute(&pgpool)
.await
.unwrap();
let signed_receipt = create_signed_receipt(SignedReceiptRequest::builder().build()).await;
let deny_list_check = new_deny_list_check(pgpool.clone()).await;
let checking_receipt = ReceiptWithState::new(signed_receipt);
let mut ctx = Context::new();
ctx.insert(Sender(TAP_SENDER.1));
// Check that the receipt is rejected
assert!(deny_list_check
.check(&ctx, &checking_receipt)
.await
.is_err());
}
#[sqlx::test(migrations = "../../migrations")]
async fn test_sender_denylist_updates(pgpool: PgPool) {
let signed_receipt = create_signed_receipt(SignedReceiptRequest::builder().build()).await;
let deny_list_check = new_deny_list_check(pgpool.clone()).await;
// Check that the receipt is valid
let checking_receipt = ReceiptWithState::new(signed_receipt);
let mut ctx = Context::new();
ctx.insert(Sender(TAP_SENDER.1));
deny_list_check
.check(&ctx, &checking_receipt)
.await
.unwrap();
// Add the sender to the denylist
sqlx::query!(
r#"
INSERT INTO scalar_tap_denylist (sender_address)
VALUES ($1)
"#,
TAP_SENDER.1.encode_hex()
)
.execute(&pgpool)
.await
.unwrap();
deny_list_check.notify.notified().await;
// Check that the receipt is rejected
assert!(deny_list_check
.check(&ctx, &checking_receipt)
.await
.is_err());
// Remove the sender from the denylist
sqlx::query!(
r#"
DELETE FROM scalar_tap_denylist
WHERE sender_address = $1
"#,
TAP_SENDER.1.encode_hex()
)
.execute(&pgpool)
.await
.unwrap();
deny_list_check.notify.notified().await;
// Check that the receipt is valid again
assert!(deny_list_check.check(&ctx, &checking_receipt).await.is_ok());
}
}