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
use quick_protobuf::deserialize_from_slice;
use solana_program::account_info::AccountInfo;
use solana_program::program_error::ProgramError;
use switchboard_protos::protos::aggregator_state::mod_AggregatorState;
use solana_program::pubkey::Pubkey;

pub use switchboard_protos::protos::aggregator_state::AggregatorState;
pub use switchboard_protos::protos::aggregator_state::RoundResult;

/// Returns whether the current open round is considered valid for usage.
pub fn is_current_round_valid(aggregator: &AggregatorState) -> Result<bool, ProgramError> {
    let maybe_round = aggregator.current_round_result.clone();
    if maybe_round.is_none() {
        return Ok(false);
    }
    let round = maybe_round.unwrap();
    let configs = aggregator.configs.as_ref().ok_or(ProgramError::InvalidAccountData)?;
    if round.num_success < configs.min_confirmations {
        return Ok(false);
    }
    // Taking account for negative feed values.
    let max_resp: f64 = 1.0 * round.min_response.abs().max(round.max_response.abs());
    let min_resp: f64 = 1.0 * round.min_response.abs().min(round.max_response.abs());
    if min_resp / max_resp * 100.0 < configs.min_round_agreement_percentage.into() {
        return Ok(false);
    }
    Ok(true)
}

/// Given a Switchboard data feed account, this methos will parse the account state.
///
/// Returns a ProgramError if the AccountInfo is unable to be borrowed or the
/// account is not initialized as an aggregator.
pub fn get_aggregator<'a>(switchboard_feed: &'a AccountInfo<'a>) -> Result<AggregatorState, ProgramError> {
    let state_buffer = switchboard_feed.try_borrow_data()?;
    let aggregator_state: AggregatorState =
        deserialize_from_slice(&state_buffer[1..]).map_err(|_| ProgramError::InvalidAccountData)?;
    Ok(aggregator_state)
}

/// Returns the most recent resolution round that is considered valid for the aggregator.
///
/// An aggregator round is valid if the number of successful confirmations in the round is
/// greater than `min_confirmations` and the variance of responses of the round
/// is within `min_round_agreement_percentage`.
pub fn get_aggregator_result<'a>(aggregator: &AggregatorState) -> Result<RoundResult, ProgramError> {
    let mut maybe_round = aggregator.current_round_result.clone();
    if !is_current_round_valid(&aggregator)? {
        maybe_round = aggregator.last_round_result.clone();
    }
    maybe_round.ok_or(ProgramError::InvalidAccountData)
}

#[cfg(test)]
mod tests {
    use super::*;

    pub fn new_account_info<'a>(
        owner: &'a Pubkey,
        key: &'a Pubkey,
        lamports: &'a mut u64,
        data: &'a mut [u8],
    ) -> AccountInfo<'a> {
        AccountInfo::new(
            key,      // key: &'a Pubkey,
            false,    // is_signer: bool,
            true,     // is_writable: bool,
            lamports, // lamports: &'a mut u64,
            data,     // data: &'a mut [u8],
            owner,    // owner: &'a Pubkey,
            false,    // executable: bool,
            100,      // rent_epoch: Epoch
        )
    }

    pub fn create_aggregator(current_round: RoundResult, last_round: RoundResult) -> AggregatorState {
        AggregatorState {
            version: 1,
            configs: Some(mod_AggregatorState::Configs {
                min_confirmations: 10,
                min_update_delay_seconds: 10,
                min_round_agreement_percentage: 95,
                locked: false,
            }),
            fulfillment_manager_pubkey: Vec::new(),
            job_definition_pubkeys: Vec::new(),
            agreement: None,
            current_round_result: Some(current_round),
            last_round_result: Some(last_round),
        }
    }

    #[test]
    fn test_reject_current_on_variance() {
        let current_round = RoundResult {
            num_success: 20,
            num_error: 5,
            mean: 50.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 25.0,
            max_response: 75.0
        };
        let last_round = RoundResult {
            num_success: 30,
            num_error: 0,
            mean: 100.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 50.0,
            max_response: 150.0
        };
        let aggregator = create_aggregator(current_round, last_round.clone());
        assert_eq!(get_aggregator_result(&aggregator).unwrap(), last_round.clone());
    }

    #[test]
    fn test_accept_current_on_variance() {
        let current_round = RoundResult {
            num_success: 20,
            num_error: 5,
            mean: 97.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 96.0,
            max_response: 100.0
        };
        let last_round = RoundResult {
            num_success: 30,
            num_error: 0,
            mean: 100.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 50.0,
            max_response: 150.0
        };
        let aggregator = create_aggregator(current_round.clone(), last_round.clone());
        assert_eq!(get_aggregator_result(&aggregator).unwrap(), current_round.clone());
    }


    #[test]
    fn test_reject_current_on_variance_negative() {
        let current_round = RoundResult {
            num_success: 20,
            num_error: 5,
            mean: -50.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: -25.0,
            max_response: -75.0,
        };
        let last_round = RoundResult {
            num_success: 30,
            num_error: 0,
            mean: -100.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: -150.0,
            max_response: -50.0,
        };
        let aggregator = create_aggregator(current_round, last_round.clone());
        assert_eq!(get_aggregator_result(&aggregator).unwrap(), last_round.clone());
    }

    #[test]
    fn test_accept_current_on_variance_negative() {
        let current_round = RoundResult {
            num_success: 20,
            num_error: 5,
            mean: -97.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: -96.0,
            max_response: -100.0,
        };
        let last_round = RoundResult {
            num_success: 30,
            num_error: 0,
            mean: -100.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: -150.0,
            max_response: -50.0,
        };
        let aggregator = create_aggregator(current_round.clone(), last_round.clone());
        assert_eq!(get_aggregator_result(&aggregator).unwrap(), current_round.clone());
    }

    #[test]
    fn test_reject_current_on_sucess_count() {
        let current_round = RoundResult {
            num_success: 2,
            num_error: 5,
            mean: 97.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 96.0,
            max_response: 100.0
        };
        let last_round = RoundResult {
            num_success: 30,
            num_error: 0,
            mean: 100.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 100.0,
            max_response: 100.0
        };
        let aggregator = create_aggregator(current_round.clone(), last_round.clone());
        assert_eq!(get_aggregator_result(&aggregator).unwrap(), last_round.clone());
    }

    #[test]
    fn test_accept_current_on_sucess_count() {
        let current_round = RoundResult {
            num_success: 20,
            num_error: 5,
            mean: 97.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 96.0,
            max_response: 100.0
        };
        let last_round = RoundResult {
            num_success: 30,
            num_error: 0,
            mean: 100.0,
            round_open_slot: 1,
            round_open_timestamp: 1,
            min_response: 100.0,
            max_response: 100.0
        };
        let aggregator = create_aggregator(current_round.clone(), last_round.clone());
        assert_eq!(get_aggregator_result(&aggregator).unwrap(), current_round.clone());
    }
}