Skip to content

Commit 26a6f56

Browse files
authored
Fix/v2 crash (#685)
* feat(agent): Add config toggle for Horizon functionality Introduces a `horizon_enabled` boolean flag within `SenderAccountConfig`, populated from the main application configuration (`config.horizon.enabled`). This flag gates Horizon-specific functionality within the TAP agent: - In `SenderAccount`: - Database operations (fetching RAVs, querying/updating denylist) for `SenderType::Horizon` are now conditional on this flag being true. - The `todo!()` for querying unfinalized V2 transactions is now only relevant if Horizon is enabled. - A temporary check (`FIXME`) is added to `deny_sender_if_insolvent` to bypass denial logic for Horizon senders if the feature is disabled via config. - In `SenderAccountsManager`: - Fetching pending V2 (Horizon) sender allocations is skipped if the flag is false. - The `new_receipts_watcher` task for V2/Horizon is only spawned if the flag is true. This allows Horizon-related features, which may still be under development, to be effectively disabled via configuration, simplifying testing and allowing for incremental rollout without impacting existing functionality if Horizon support is not desired or ready. * fix(test): Remove database migration by default * fix(config): Add missing horizon setting to config files fix(config): add missing config field * chore(test): Change test function name to indicate it works for RAV V1 only * fix(testnet): Remove commented code
1 parent 7aab7d9 commit 26a6f56

File tree

12 files changed

+107
-62
lines changed

12 files changed

+107
-62
lines changed

contrib/indexer-service/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ trigger_value_divisor = 500_000
4444

4545
[tap.sender_aggregator_endpoints]
4646
"ACCOUNT0_ADDRESS_PLACEHOLDER" = "http://tap-aggregator:7610"
47+
48+
[horizon]
49+
enabled = false

contrib/indexer-service/start.sh

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ until pg_isready -h postgres -U postgres -d indexer_components_1; do
1919
sleep 2
2020
done
2121

22-
echo "Applying database migrations..."
23-
# Get all the migration UP files and sort them by name
24-
for migration_file in $(find /opt/migrations -name "*.up.sql" | sort); do
25-
echo "Applying migration: $(basename $migration_file)"
26-
psql -h postgres -U postgres postgres -f $migration_file
27-
done
28-
echo "Database migrations completed."
29-
3022
# Get network subgraph deployment ID
3123
NETWORK_DEPLOYMENT=$(curl -s "http://graph-node:8000/subgraphs/name/graph-network" \
3224
-H 'content-type: application/json' \

contrib/tap-agent/start.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ fi
2828

2929
echo "Postgres is ready!"
3030

31-
echo "Ensuring database tables exist..."
32-
for migration_file in $(find /opt/migrations -name "*.up.sql" | sort); do
33-
echo "Applying migration if needed: $(basename $migration_file)"
34-
psql -h postgres -U postgres indexer_components_1 -f $migration_file 2>/dev/null || true
35-
done
36-
echo "Database setup completed."
37-
3831
# Wait for indexer-service to be ready with timeout
3932
echo "Waiting for indexer-service to be ready..."
4033
MAX_ATTEMPTS=30

crates/config/default_values.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ trigger_value_divisor = 10
2727
timestamp_buffer_secs = 60
2828
request_timeout_secs = 5
2929
max_receipts_per_request = 10000
30+
31+
[horizon]
32+
enabled = false

crates/config/maximal-config-example.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,6 @@ hardhat = "100"
168168

169169
[dips.additional_networks]
170170
"eip155:1337" = "hardhat"
171+
172+
[horizon]
173+
enabled = false

crates/config/src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub struct Config {
4242
pub service: ServiceConfig,
4343
pub tap: TapConfig,
4444
pub dips: Option<DipsConfig>,
45+
pub horizon: HorizonConfig,
4546
}
4647

4748
// Newtype wrapping Config to be able use serde_ignored with Figment
@@ -445,6 +446,15 @@ pub struct RavRequestConfig {
445446
pub max_receipts_per_request: u64,
446447
}
447448

449+
/// Configuration for the horizon
450+
/// standard
451+
#[derive(Debug, Default, Deserialize)]
452+
#[cfg_attr(test, derive(PartialEq))]
453+
pub struct HorizonConfig {
454+
/// Whether the horizon is enabled or not
455+
pub enabled: bool,
456+
}
457+
448458
#[cfg(test)]
449459
mod tests {
450460
use std::{

crates/tap-agent/src/agent/sender_account.rs

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ pub struct SenderAccountConfig {
400400
/// Senders that are allowed to spend up to `max_amount_willing_to_lose_grt`
401401
/// over the escrow balance
402402
pub trusted_senders: HashSet<Address>,
403+
404+
#[doc(hidden)]
405+
pub horizon_enabled: bool,
403406
}
404407

405408
impl SenderAccountConfig {
@@ -415,6 +418,7 @@ impl SenderAccountConfig {
415418
rav_request_timeout: config.tap.rav_request.request_timeout_secs,
416419
tap_sender_timeout: config.tap.sender_timeout_secs,
417420
trusted_senders: config.tap.trusted_senders.clone(),
421+
horizon_enabled: config.horizon.enabled,
418422
}
419423
}
420424
}
@@ -623,6 +627,13 @@ impl State {
623627
sender_balance = self.sender_balance.to_u128(),
624628
"Denying sender."
625629
);
630+
// Check if this is horizon like sender and if it is actually enable,
631+
// otherwise just ignore.
632+
// FIXME: This should be removed once full horizon support
633+
// is implemented!
634+
if matches!(self.sender_type, SenderType::Horizon) && !self.config.horizon_enabled {
635+
return;
636+
}
626637

627638
SenderAccount::deny_sender(self.sender_type, &self.pgpool, self.sender).await;
628639
self.denied = true;
@@ -654,16 +665,18 @@ impl State {
654665
.expect("Should not fail to delete from denylist");
655666
}
656667
SenderType::Horizon => {
657-
sqlx::query!(
658-
r#"
668+
if self.config.horizon_enabled {
669+
sqlx::query!(
670+
r#"
659671
DELETE FROM tap_horizon_denylist
660672
WHERE sender_address = $1
661673
"#,
662-
self.sender.encode_hex(),
663-
)
664-
.execute(&self.pgpool)
665-
.await
666-
.expect("Should not fail to delete from denylist");
674+
self.sender.encode_hex(),
675+
)
676+
.execute(&self.pgpool)
677+
.await
678+
.expect("Should not fail to delete from horizon denylist");
679+
}
667680
}
668681
}
669682
self.denied = false;
@@ -798,20 +811,26 @@ impl Actor for SenderAccount {
798811
.map(|record| (record.allocation_id, record.value_aggregate))
799812
.collect(),
800813
// Get all ravs from v2 table
801-
SenderType::Horizon => sqlx::query!(
802-
r#"
814+
SenderType::Horizon => {
815+
if config.horizon_enabled {
816+
sqlx::query!(
817+
r#"
803818
SELECT allocation_id, value_aggregate
804819
FROM tap_horizon_ravs
805820
WHERE payer = $1 AND last AND NOT final;
806821
"#,
807-
sender_id.encode_hex(),
808-
)
809-
.fetch_all(&pgpool)
810-
.await
811-
.expect("Should not fail to fetch from scalar_tap_ravs")
812-
.into_iter()
813-
.map(|record| (record.allocation_id, record.value_aggregate))
814-
.collect(),
822+
sender_id.encode_hex(),
823+
)
824+
.fetch_all(&pgpool)
825+
.await
826+
.expect("Should not fail to fetch from \"horizon\" scalar_tap_ravs")
827+
.into_iter()
828+
.map(|record| (record.allocation_id, record.value_aggregate))
829+
.collect()
830+
} else {
831+
vec![]
832+
}
833+
}
815834
};
816835

817836
// get a list from the subgraph of which subgraphs were already redeemed and were not marked as final
@@ -845,7 +864,11 @@ impl Actor for SenderAccount {
845864
// TODO Implement query for unfinalized v2 transactions
846865
// Depends on Escrow Subgraph Schema
847866
SenderType::Horizon => {
848-
todo!()
867+
if config.horizon_enabled {
868+
todo!("Implement query for unfinalized v2 transactions, It depends on Escrow Subgraph Schema")
869+
}
870+
// if we have any problems, we don't want to filter out
871+
vec![]
849872
}
850873
};
851874

@@ -896,20 +919,28 @@ impl Actor for SenderAccount {
896919
.denied
897920
.expect("Deny status cannot be null"),
898921
// Get deny status from the tap horizon table
899-
SenderType::Horizon => sqlx::query!(
900-
r#"
922+
SenderType::Horizon => {
923+
if config.horizon_enabled {
924+
sqlx::query!(
925+
r#"
901926
SELECT EXISTS (
902927
SELECT 1
903928
FROM tap_horizon_denylist
904929
WHERE sender_address = $1
905930
) as denied
906931
"#,
907-
sender_id.encode_hex(),
908-
)
909-
.fetch_one(&pgpool)
910-
.await?
911-
.denied
912-
.expect("Deny status cannot be null"),
932+
sender_id.encode_hex(),
933+
)
934+
.fetch_one(&pgpool)
935+
.await?
936+
.denied
937+
.expect("Deny status cannot be null")
938+
} else {
939+
// If horizon is enabled,
940+
// just ignore this sender
941+
false
942+
}
943+
}
913944
};
914945

915946
let sender_balance = escrow_accounts
@@ -1419,7 +1450,7 @@ impl SenderAccount {
14191450
)
14201451
.execute(pool)
14211452
.await
1422-
.expect("Should not fail to insert into denylist");
1453+
.expect("Should not fail to insert into \"horizon\" denylist");
14231454
}
14241455
}
14251456

crates/tap-agent/src/agent/sender_accounts_manager.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,17 @@ impl Actor for SenderAccountsManager {
264264
.await;
265265

266266
// v2
267-
let sender_allocation_v2 = select! {
268-
sender_allocation = state.get_pending_sender_allocation_id_v2() => sender_allocation,
269-
_ = tokio::time::sleep(state.config.tap_sender_timeout) => {
270-
panic!("Timeout while getting pending sender allocation ids");
267+
let sender_allocation_v2 = if state.config.horizon_enabled {
268+
select! {
269+
sender_allocation = state.get_pending_sender_allocation_id_v2() => sender_allocation,
270+
_ = tokio::time::sleep(state.config.tap_sender_timeout) => {
271+
panic!("Timeout while getting pending sender allocation ids");
272+
}
271273
}
274+
} else {
275+
HashMap::new()
272276
};
277+
273278
state.sender_ids_v2.extend(sender_allocation_v2.keys());
274279
stream::iter(sender_allocation_v2)
275280
.map(|(sender_id, allocation_ids)| {
@@ -298,19 +303,24 @@ impl Actor for SenderAccountsManager {
298303

299304
// Start the new_receipts_watcher task that will consume from the `pglistener`
300305
// after starting all senders
301-
state.new_receipts_watcher_handle_v2 = Some(tokio::spawn(
302-
new_receipts_watcher()
303-
.actor_cell(myself.get_cell())
304-
.pglistener(pglistener_v2)
305-
.escrow_accounts_rx(escrow_accounts_v2)
306-
.sender_type(SenderType::Horizon)
307-
.maybe_prefix(prefix)
308-
.call(),
309-
));
306+
state.new_receipts_watcher_handle_v2 = None;
307+
308+
if state.config.horizon_enabled {
309+
state.new_receipts_watcher_handle_v2 = Some(tokio::spawn(
310+
new_receipts_watcher()
311+
.actor_cell(myself.get_cell())
312+
.pglistener(pglistener_v2)
313+
.escrow_accounts_rx(escrow_accounts_v2)
314+
.sender_type(SenderType::Horizon)
315+
.maybe_prefix(prefix)
316+
.call(),
317+
));
318+
}
310319

311320
tracing::info!("SenderAccountManager created!");
312321
Ok(state)
313322
}
323+
314324
async fn post_stop(
315325
&self,
316326
_: ActorRef<Self::Msg>,

crates/tap-agent/src/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub fn get_sender_account_config() -> &'static SenderAccountConfig {
9191
escrow_polling_interval: ESCROW_POLLING_INTERVAL,
9292
tap_sender_timeout: Duration::from_secs(63),
9393
trusted_senders: HashSet::new(),
94+
horizon_enabled: true,
9495
}))
9596
}
9697

@@ -127,6 +128,7 @@ pub async fn create_sender_account(
127128
escrow_polling_interval: Duration::default(),
128129
tap_sender_timeout: TAP_SENDER_TIMEOUT,
129130
trusted_senders,
131+
horizon_enabled: false,
130132
}));
131133

132134
let network_subgraph = Box::leak(Box::new(

crates/tap-agent/tests/tap_agent_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub async fn start_agent(
9292
escrow_polling_interval: Duration::from_secs(10),
9393
tap_sender_timeout: Duration::from_secs(30),
9494
trusted_senders: HashSet::new(),
95+
horizon_enabled: false,
9596
}));
9697

9798
let args = SenderAccountsManagerArgs {

0 commit comments

Comments
 (0)