solana_conditional_liquidity/
origin.rsuse solana_program::{account_info::AccountInfo, pubkey, pubkey::Pubkey};
use crate::is_invoked_by_segmenter;
pub type Origin = u16;
pub mod origin_identity {
use super::Origin;
pub const UNKNOWN: Origin = 0;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VerifyOriginResult {
InvokedBySegmenter(Origin),
NotInvokedBySegmenter,
}
pub fn verify_origin(
claimed_origin: Origin,
registry: &AccountInfo<'_>,
segmenter: &AccountInfo<'_>,
) -> VerifyOriginResult {
if !is_invoked_by_segmenter(registry, segmenter) {
return VerifyOriginResult::NotInvokedBySegmenter;
}
if registry.key != &ORIGIN_VERIFYING_SEGMENTER_REGISTRY {
return VerifyOriginResult::InvokedBySegmenter(origin_identity::UNKNOWN);
}
VerifyOriginResult::InvokedBySegmenter(claimed_origin)
}
pub const ORIGIN_VERIFYING_SEGMENTER_REGISTRY: Pubkey =
pubkey!("Reg1Y127DNKYUTf3LinfEs3oiSiywJsyAobJMjqYqDE");
#[cfg(test)]
mod tests {
use std::{cell::RefCell, rc::Rc};
use base64::Engine;
use solana_program::system_program;
use crate::Registry;
use super::*;
#[test]
fn test_verify_origin() {
let mut registry_data = base64::engine::general_purpose::STANDARD
.decode("L65u9ri2/NoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADmpy9pZ0kSP2HFqsTSMjxFxburQjxQioc8A4BaVKbWtBAWsWrJs+pByyagNT2RTZ5E/wZAQB0FNhQpY/WTUev0FyJOST1AtAGCROxwJ16TmEm/91X11Lmzcymw9zcmibQnqAzYRp+mQPsmo+2htSt8O7nGFhRpTDQdq9qCtx9jA==")
.unwrap();
let mut registry_lamports = 15200640;
let registry = AccountInfo {
key: &pubkey!("Reg1Y127DNKYUTf3LinfEs3oiSiywJsyAobJMjqYqDE"),
is_signer: false,
is_writable: false,
lamports: Rc::new(RefCell::new(&mut registry_lamports)),
data: Rc::new(RefCell::new(&mut registry_data)),
owner: &Registry::PROGRAM_ID,
executable: false,
rent_epoch: 18446744073709551615,
};
let mut segmenter_lamports = 0;
let mut segmenter_data = [];
let segmenter = AccountInfo {
key: &pubkey!("F2Me9XknvkPYjvoEgTXXKqARg58Ezo6ZmGhpYdS3UTmF"),
is_signer: true,
is_writable: false,
lamports: Rc::new(RefCell::new(&mut segmenter_lamports)),
data: Rc::new(RefCell::new(&mut segmenter_data)),
owner: &system_program::id(),
executable: false,
rent_epoch: 18446744073709551615,
};
assert_eq!(
verify_origin(1, ®istry, &segmenter),
VerifyOriginResult::InvokedBySegmenter(1)
);
let mut segmenter_lamports = 0;
let mut segmenter_data = [];
let segmenter = AccountInfo {
key: &pubkey!("F2Me9XknvkPYjvoEgTXXKqARg58Ezo6ZmGhpYdS3UTmF"),
is_signer: false,
is_writable: false,
lamports: Rc::new(RefCell::new(&mut segmenter_lamports)),
data: Rc::new(RefCell::new(&mut segmenter_data)),
owner: &system_program::id(),
executable: false,
rent_epoch: 18446744073709551615,
};
assert_eq!(
verify_origin(1, ®istry, &segmenter),
VerifyOriginResult::NotInvokedBySegmenter
);
let mut segmenter_lamports = 0;
let mut segmenter_data = [];
let segmenter = AccountInfo {
key: &Pubkey::new_unique(),
is_signer: true,
is_writable: false,
lamports: Rc::new(RefCell::new(&mut segmenter_lamports)),
data: Rc::new(RefCell::new(&mut segmenter_data)),
owner: &system_program::id(),
executable: false,
rent_epoch: 18446744073709551615,
};
assert_eq!(
verify_origin(1, ®istry, &segmenter),
VerifyOriginResult::NotInvokedBySegmenter
);
}
#[test]
fn test_verify_origin_non_verifying_registry() {
let mut registry_data = base64::engine::general_purpose::STANDARD
.decode("L65u9ri2/NoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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: false,
lamports: Rc::new(RefCell::new(&mut registry_lamports)),
data: Rc::new(RefCell::new(&mut registry_data)),
owner: &Registry::PROGRAM_ID,
executable: false,
rent_epoch: 18446744073709551615,
};
let mut segmenter_lamports = 0;
let mut segmenter_data = [];
let segmenter = AccountInfo {
key: &pubkey!("F2Me9XknvkPYjvoEgTXXKqARg58Ezo6ZmGhpYdS3UTmF"),
is_signer: true,
is_writable: false,
lamports: Rc::new(RefCell::new(&mut segmenter_lamports)),
data: Rc::new(RefCell::new(&mut segmenter_data)),
owner: &system_program::id(),
executable: false,
rent_epoch: 18446744073709551615,
};
assert_eq!(
verify_origin(1, ®istry, &segmenter),
VerifyOriginResult::InvokedBySegmenter(origin_identity::UNKNOWN)
);
let mut segmenter_lamports = 0;
let mut segmenter_data = [];
let segmenter = AccountInfo {
key: &pubkey!("F2Me9XknvkPYjvoEgTXXKqARg58Ezo6ZmGhpYdS3UTmF"),
is_signer: false,
is_writable: false,
lamports: Rc::new(RefCell::new(&mut segmenter_lamports)),
data: Rc::new(RefCell::new(&mut segmenter_data)),
owner: &system_program::id(),
executable: false,
rent_epoch: 18446744073709551615,
};
assert_eq!(
verify_origin(1, ®istry, &segmenter),
VerifyOriginResult::NotInvokedBySegmenter
);
let mut segmenter_lamports = 0;
let mut segmenter_data = [];
let segmenter = AccountInfo {
key: &Pubkey::new_unique(),
is_signer: true,
is_writable: false,
lamports: Rc::new(RefCell::new(&mut segmenter_lamports)),
data: Rc::new(RefCell::new(&mut segmenter_data)),
owner: &system_program::id(),
executable: false,
rent_epoch: 18446744073709551615,
};
assert_eq!(
verify_origin(1, ®istry, &segmenter),
VerifyOriginResult::NotInvokedBySegmenter
);
}
}