|
| 1 | +use crate::{ |
| 2 | + error::{Error, TickError}, |
| 3 | + follower, |
| 4 | + sentry_interface::{campaigns::all_campaigns, Validator, Validators}, |
| 5 | + SentryApi, |
| 6 | +}; |
| 7 | +use primitives::{adapter::Adapter, channel_v5::Channel, config::Config, util::ApiUrl, ChannelId}; |
| 8 | +// use slog::{error, info, Logger}; |
| 9 | +use slog::Logger; |
| 10 | +use std::collections::{hash_map::Entry, HashSet}; |
| 11 | + |
| 12 | +pub async fn channel_tick<A: Adapter + 'static>( |
| 13 | + adapter: A, |
| 14 | + config: &Config, |
| 15 | + logger: &Logger, |
| 16 | + channel: Channel, |
| 17 | + validators: Validators, |
| 18 | +) -> Result<ChannelId, Error<A::AdapterError>> { |
| 19 | + let tick = channel |
| 20 | + .find_validator(adapter.whoami()) |
| 21 | + .ok_or(Error::ChannelNotIntendedForUs)?; |
| 22 | + |
| 23 | + let sentry = SentryApi::init( |
| 24 | + adapter, |
| 25 | + logger.clone(), |
| 26 | + config.clone(), |
| 27 | + (channel, validators), |
| 28 | + )?; |
| 29 | + // `GET /channel/:id/spender/all` |
| 30 | + let all_spenders = sentry.get_all_spenders().await?; |
| 31 | + |
| 32 | + // `GET /channel/:id/accounting` |
| 33 | + // Validation #1: |
| 34 | + // sum(Accounting.spenders) == sum(Accounting.earners) |
| 35 | + let accounting = sentry.get_accounting(channel.id()).await?; |
| 36 | + |
| 37 | + // Validation #2: |
| 38 | + // spender.spender_leaf.total_deposit >= accounting.balances.spenders[spender.address] |
| 39 | + if !all_spenders.iter().all(|(address, spender)| { |
| 40 | + spender.total_deposited |
| 41 | + >= accounting |
| 42 | + .balances |
| 43 | + .spenders |
| 44 | + .get(address) |
| 45 | + .cloned() |
| 46 | + .unwrap_or_default() |
| 47 | + }) { |
| 48 | + return Err(Error::Validation); |
| 49 | + } |
| 50 | + |
| 51 | + // TODO: Add timeout |
| 52 | + let _tick_result = match tick { |
| 53 | + primitives::Validator::Leader(_v) => todo!(), |
| 54 | + primitives::Validator::Follower(_v) => { |
| 55 | + follower::tick(&sentry, channel, accounting.balances) |
| 56 | + .await |
| 57 | + .map_err(|err| Error::FollowerTick(channel.id(), TickError::Tick(err)))? |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + // Validation #3 |
| 62 | + // Accounting.balances != NewState.balances |
| 63 | + |
| 64 | + // Validation #4 |
| 65 | + // OUTPACE Rules: |
| 66 | + |
| 67 | + Ok(channel.id()) |
| 68 | +} |
| 69 | + |
| 70 | +/// Fetches all `Campaign`s from Sentry and builds the `Channel`s to be processed |
| 71 | +/// along side all the `Validator`s' url & auth token |
| 72 | +pub async fn collect_channels<A: Adapter + 'static>( |
| 73 | + adapter: A, |
| 74 | + sentry_url: &ApiUrl, |
| 75 | + _config: &Config, |
| 76 | + _logger: &Logger, |
| 77 | +) -> Result<(HashSet<Channel>, Validators), reqwest::Error> { |
| 78 | + let whoami = adapter.whoami(); |
| 79 | + |
| 80 | + let campaigns = all_campaigns(sentry_url, whoami).await?; |
| 81 | + let channels = campaigns |
| 82 | + .iter() |
| 83 | + .map(|campaign| campaign.channel) |
| 84 | + .collect::<HashSet<_>>(); |
| 85 | + |
| 86 | + let validators = campaigns |
| 87 | + .into_iter() |
| 88 | + .fold(Validators::new(), |mut acc, campaign| { |
| 89 | + for validator_desc in campaign.validators.iter() { |
| 90 | + // if Validator is already there, we can just skip it |
| 91 | + // remember, the campaigns are ordered by `created DESC` |
| 92 | + // so we will always get the latest Validator url first |
| 93 | + match acc.entry(validator_desc.id) { |
| 94 | + Entry::Occupied(_) => continue, |
| 95 | + Entry::Vacant(entry) => { |
| 96 | + // try to parse the url of the Validator Desc |
| 97 | + let validator_url = validator_desc.url.parse::<ApiUrl>(); |
| 98 | + // and also try to find the Auth token in the config |
| 99 | + |
| 100 | + // if there was an error with any of the operations, skip this `ValidatorDesc` |
| 101 | + let auth_token = adapter.get_auth(&validator_desc.id); |
| 102 | + |
| 103 | + // only if `ApiUrl` parsing is `Ok` & Auth Token is found in the `Adapter` |
| 104 | + if let (Ok(url), Ok(auth_token)) = (validator_url, auth_token) { |
| 105 | + // add an entry for propagation |
| 106 | + entry.insert(Validator { |
| 107 | + url, |
| 108 | + token: auth_token, |
| 109 | + }); |
| 110 | + } |
| 111 | + // otherwise it will try to do the same things on the next encounter of this `ValidatorId` |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + acc |
| 117 | + }); |
| 118 | + |
| 119 | + Ok((channels, validators)) |
| 120 | +} |
0 commit comments