瀏覽代碼

added real test data from mock hermes calls

Ayush Suresh 4 月之前
父節點
當前提交
b9cd40244e

+ 20 - 13
target_chains/stylus/contracts/pyth-receiver/src/integration_tests.rs

@@ -3,6 +3,7 @@
 mod test {
     use crate::PythReceiver;
     use crate::error::PythReceiverError;
+    use crate::test_data;
     use alloy_primitives::{address, U256, FixedBytes};
     use stylus_sdk::testing::TestVM;
     use pythnet_sdk::wire::v1::PYTHNET_ACCUMULATOR_UPDATE_MAGIC;
@@ -53,10 +54,7 @@ mod test {
     }
 
     fn create_valid_update_data() -> Vec<u8> {
-        let mut data = Vec::new();
-        data.extend_from_slice(PYTHNET_ACCUMULATOR_UPDATE_MAGIC);
-        data.extend_from_slice(&[0u8; 100]);
-        data
+        test_data::good_update1()
     }
 
     fn create_invalid_magic_data() -> Vec<u8> {
@@ -252,15 +250,24 @@ mod test {
         assert!(initial_result.is_err());
         assert!(matches!(initial_result.unwrap_err(), PythReceiverError::PriceUnavailable));
 
-        // let update_data = create_valid_update_data();
-        // let update_result = contract.update_price_feeds(update_data);
-
-        // let price_result = contract.get_price_unsafe(TEST_PRICE_ID);
-        // assert!(price_result.is_ok());
+    }
 
-        // let price_info = price_result.unwrap();
-        // assert_eq!(price_info.0.to::<u64>(), TEST_PUBLISH_TIME);
-        // assert_eq!(price_info.2.to::<i64>(), TEST_PRICE);
-        // assert_eq!(price_info.3.to::<u64>(), TEST_CONF);
+    #[test]
+    fn test_realistic_test_data_functions() {
+        let good_update_data = test_data::good_update1();
+        assert!(!good_update_data.is_empty());
+        assert!(good_update_data.len() > 100);
+
+        let vaa_data = test_data::good_vm1();
+        assert!(!vaa_data.is_empty());
+        assert!(vaa_data.len() > 50);
+
+        let price_update_data = test_data::test_price_update1();
+        assert!(!price_update_data.is_empty());
+        assert!(price_update_data.len() > 100);
+
+        let price_update2_data = test_data::test_price_update2();
+        assert!(!price_update2_data.is_empty());
+        assert!(price_update2_data.len() > 100);
     }
 }

+ 3 - 0
target_chains/stylus/contracts/pyth-receiver/src/lib.rs

@@ -7,7 +7,10 @@ extern crate alloc;
 
 mod structs;
 mod error;
+#[cfg(test)]
 mod integration_tests;
+#[cfg(test)]
+mod test_data;
 
 use alloc::vec::Vec;
 use stylus_sdk::{alloy_primitives::{U16, U32, U256, U64, I32, I64, FixedBytes, Address},

+ 211 - 0
target_chains/stylus/contracts/pyth-receiver/src/test_data.rs

@@ -0,0 +1,211 @@
+use alloy_primitives::U256;
+
+fn from_cairo_byte_array_data(data: &[U256], num_last_bytes: usize) -> Vec<u8> {
+    let mut result = Vec::new();
+    
+    if data.is_empty() {
+        return result;
+    }
+    
+    for (i, &item) in data.iter().enumerate() {
+        let buf = item.to_be_bytes::<32>();
+        
+        if i == data.len() - 1 {
+            if num_last_bytes > 0 {
+                let start_idx = 32 - num_last_bytes;
+                result.extend_from_slice(&buf[start_idx..]);
+            }
+        } else {
+            result.extend_from_slice(&buf[1..]);
+        }
+    }
+    
+    result
+}
+
+pub fn good_update1() -> Vec<u8> {
+    let bytes = [
+        U256::from_str_radix("141887862745809943100717722154781668316147089807066324001213790862261653767", 10).unwrap(),
+        U256::from_str_radix("451230040559159019530944948086670994623010697390864133264612902902585665886", 10).unwrap(),
+        U256::from_str_radix("355897384610106978643111834734000274494997301794613218547634257521495150151", 10).unwrap(),
+        U256::from_str_radix("140511063638834349363702006999356227863549404051701803148734324248522745879", 10).unwrap(),
+        U256::from_str_radix("435849190784772134907557391544163070978531038970298390345939133663347953446", 10).unwrap(),
+        U256::from_str_radix("416390591179833928094641114955594939466104495718036761707729297119441316151", 10).unwrap(),
+        U256::from_str_radix("360454929416220920336539568461651500076647166763464050800345920693176904002", 10).unwrap(),
+        U256::from_str_radix("316054999864337699543932294956493808847640383114707243342262764542081441331", 10).unwrap(),
+        U256::from_str_radix("325277902980160684959962429721294603784343718796390808940252812862355246813", 10).unwrap(),
+        U256::from_str_radix("43683235854839458868457367619068018785880460427473556950900276498953667", 10).unwrap(),
+        U256::from_str_radix("448289429405712011882317781416869052550573589492688760675666957663813001522", 10).unwrap(),
+        U256::from_str_radix("118081463902430977133121147164253483958565039026724621562859841189218059803", 10).unwrap(),
+        U256::from_str_radix("194064310618695309465615383754562031677972810736048112738513050109934134235", 10).unwrap(),
+        U256::from_str_radix("133901765334590923121691219814784557892214901646312752962904032795881821509", 10).unwrap(),
+        U256::from_str_radix("404227501001709279944936006741063968912686453006275462577777397594240621266", 10).unwrap(),
+        U256::from_str_radix("81649001731335394114026683805238949464016657447685509824621946636993704965", 10).unwrap(),
+        U256::from_str_radix("32402065226491532148674904435794801976788068837745943243341272676331333141", 10).unwrap(),
+        U256::from_str_radix("431262841416902409381606630149292665102873776020834630861578112749151562174", 10).unwrap(),
+        U256::from_str_radix("6164523115980545628843981978797257048781800754033825701059814297149591186", 10).unwrap(),
+        U256::from_str_radix("408761574582108996678203805090470134287794603493622537384530614829262728153", 10).unwrap(),
+        U256::from_str_radix("185368533577943244707350150853170361880334596276529206938783888784867529821", 10).unwrap(),
+        U256::from_str_radix("173578821500714074579643724957224629379984215847383417303110192934676518530", 10).unwrap(),
+        U256::from_str_radix("90209855380378362490166376523380463998928070428866100240907090599465187835", 10).unwrap(),
+        U256::from_str_radix("97758466908511588082569287391708453107999243934457382895073183209581711489", 10).unwrap(),
+        U256::from_str_radix("132725011490528489913736834798247512772139171145730373610858422315799224432", 10).unwrap(),
+        U256::from_str_radix("117123868005849140967825260063167768530251411611975150066586827543934313288", 10).unwrap(),
+        U256::from_str_radix("408149062252618928234854115279677715692278734600386004492580987016428761675", 10).unwrap(),
+        U256::from_str_radix("164529520317122600276020522906605877985809506451193373524142111430138855019", 10).unwrap(),
+        U256::from_str_radix("444793051809958482843529748761971363435331354795896511243191618771787268378", 10).unwrap(),
+        U256::from_str_radix("247660009137502548346315865368477795392972486141407800140910365553760622080", 10).unwrap(),
+        U256::from_str_radix("3281582060272565111592312037403686940429019548922889497694300188", 10).unwrap(),
+        U256::from_str_radix("93649805131515836129946966966350066506512123780266587069413066350925286142", 10).unwrap(),
+        U256::from_str_radix("394112423559676785086098106350541172262729583743734966358666094809121292390", 10).unwrap(),
+        U256::from_str_radix("35403101004688876764673991514113473446030702766599795822870037077688984558", 10).unwrap(),
+        U256::from_str_radix("99366103604611980443183454746643823071419076016677225828619807954313149423", 10).unwrap(),
+        U256::from_str_radix("10381657217606191031071521950784155484751645280452344547752823767622424055", 10).unwrap(),
+        U256::from_str_radix("391045354044274401116419632681482293741435113770205621235865697077178955228", 10).unwrap(),
+        U256::from_str_radix("311250087759201408758984550959714865999349469611700431708031036894849650573", 10).unwrap(),
+        U256::from_str_radix("59953730895385399344628932835545900304309851622811198425230584225200786697", 10).unwrap(),
+        U256::from_str_radix("226866843267230707879834616967256711063296411939069440476882347301771901839", 10).unwrap(),
+        U256::from_str_radix("95752383404870925303422787", 10).unwrap(),
+    ];
+    from_cairo_byte_array_data(&bytes, 11)
+}
+
+pub fn good_vm1() -> Vec<u8> {
+    let bytes = [
+        U256::from_str_radix("1766847066033410293701000231337210964058791470455465385734308943533652138", 10).unwrap(),
+        U256::from_str_radix("250126301534699068413432844632573953364878937343368310395142095034982913232", 10).unwrap(),
+        U256::from_str_radix("374780571002258088211231890250917843593951765403462661483498298003400611238", 10).unwrap(),
+        U256::from_str_radix("23190137343211334092589308306056431640588154666326612124726174150537328574", 10).unwrap(),
+        U256::from_str_radix("238750269065878649216923353030193912502813798896051725498208457553032584635", 10).unwrap(),
+        U256::from_str_radix("29844190303057534696518006438077948796328243878877072296680853158289181326", 10).unwrap(),
+        U256::from_str_radix("106329507856770018708432343978518079724691760719405501795955774399597471533", 10).unwrap(),
+        U256::from_str_radix("50779865592261858016477142415230454208001695486195806892438697217059319645", 10).unwrap(),
+        U256::from_str_radix("448669871976126446102256476358498380455807705600424321390063431836375575318", 10).unwrap(),
+        U256::from_str_radix("115682669871397824853706713833773246708114483862317474710603223566297521279", 10).unwrap(),
+        U256::from_str_radix("301634766618012930739391408723909107532790832406455099966028276947414082504", 10).unwrap(),
+        U256::from_str_radix("104473166230846104217366042152018649207811514257244625711402436055500423094", 10).unwrap(),
+        U256::from_str_radix("64445621634231668761998815864645440965239569561546522651415024970517905416", 10).unwrap(),
+        U256::from_str_radix("192317190225976528694195501079591384434869624408066864018183189813956862386", 10).unwrap(),
+        U256::from_str_radix("289982656017597431343118552054719821766658675456661448685110903889153449006", 10).unwrap(),
+        U256::from_str_radix("218840601196095059731241556733624112758673153548932709011933806481899680620", 10).unwrap(),
+        U256::from_str_radix("430933799927481265070475198137531816946660368757134588278434352703899277070", 10).unwrap(),
+        U256::from_str_radix("69322998883710289192076494057541346430050879299268159627180404869988632073", 10).unwrap(),
+        U256::from_str_radix("23862615839737051269352321086490452186237833007444069999578906611768140646", 10).unwrap(),
+        U256::from_str_radix("444634264607471510688862284107804392707600799506487897206707262445172121289", 10).unwrap(),
+        U256::from_str_radix("438038196736233160320436150616293672539386464061037100698335568417587662951", 10).unwrap(),
+        U256::from_str_radix("4682255185797880874381673193118803274635247527626050223938224759013169366", 10).unwrap(),
+        U256::from_str_radix("337620725992972686809095065321563509600769533202700218393281926304544120094", 10).unwrap(),
+        U256::from_str_radix("106657917096532484607371891267699639824731774168349872862335217581425289654", 10).unwrap(),
+        U256::from_str_radix("71240348385993236445536577509595968468284689483611375124653855125285401592", 10).unwrap(),
+        U256::from_str_radix("347603391821038175842934311068097986460257977131947418186118379296987051086", 10).unwrap(),
+        U256::from_str_radix("414263571545410645948841360836383289766662078574048514890988877286444618669", 10).unwrap(),
+        U256::from_str_radix("250301638008739107522011802538487063969565433276260914336890309092111026583", 10).unwrap(),
+        U256::from_str_radix("43192785595291340058788190601908070333310658291317702311902081", 10).unwrap(),
+        U256::from_str_radix("52685537088250779930155363779405986390839624071318818148325576008719597568", 10).unwrap(),
+        U256::from_str_radix("14615204155786886573933667335033405822686404253588533", 10).unwrap(),
+    ];
+    from_cairo_byte_array_data(&bytes, 22)
+}
+
+pub fn test_price_update1() -> Vec<u8> {
+    let bytes = [
+        U256::from_str_radix("141887862745809943100421399774809552050876420277163116849842965275903806689", 10).unwrap(),
+        U256::from_str_radix("210740906737592158039211995620336526131859667363627655742687286503264782608", 10).unwrap(),
+        U256::from_str_radix("437230063624699337579360546580839669896712252828825008570863758867641146081", 10).unwrap(),
+        U256::from_str_radix("3498691308882995183871222184377409432186747119716981166996399082193594993", 10).unwrap(),
+        U256::from_str_radix("1390200166945919815453709407753165121175395927094647129599868236", 10).unwrap(),
+        U256::from_str_radix("222819573728193325268644030206737371345667885599602384508424089704440116301", 10).unwrap(),
+        U256::from_str_radix("341318259000017461738706238280879290398059773267212529438772847337449455616", 10).unwrap(),
+        U256::from_str_radix("1275126645346645395843037504005879519843596923369759718556759844520336145", 10).unwrap(),
+        U256::from_str_radix("363528783578153760894082184744116718493621815898909809604883433584616420886", 10).unwrap(),
+        U256::from_str_radix("301537311768214106147206781423041990995720118715322906821301413003463484347", 10).unwrap(),
+        U256::from_str_radix("83150006264761451992768264969047148434524798781124754530141755679159432208", 10).unwrap(),
+        U256::from_str_radix("96387772316726941183358990094337324283641753573556594738287498821253761827", 10).unwrap(),
+        U256::from_str_radix("395908154570808692326126405856049827157095768069251211022053821585519235652", 10).unwrap(),
+        U256::from_str_radix("87135893730137265929093180553063146337041045646221968026289709394440932141", 10).unwrap(),
+        U256::from_str_radix("245333243912241114598596888050489286502591033459250287888834", 10).unwrap(),
+    ];
+    from_cairo_byte_array_data(&bytes, 25)
+}
+
+pub fn test_price_update2() -> Vec<u8> {
+    let bytes = [
+        U256::from_str_radix("141887862745809943100421399774809552050874823427618844548942380383465221086", 10).unwrap(),
+        U256::from_str_radix("106893583704677921907497845070624642590618427233243792006390965895909696183", 10).unwrap(),
+        U256::from_str_radix("126617671723931969110123875642449115250793288301361049879364132884271078113", 10).unwrap(),
+        U256::from_str_radix("3498691308882995183871222184377409432186747119716981166996399082193594993", 10).unwrap(),
+        U256::from_str_radix("1390200461185063661704370212555794334034815850290352693418762308", 10).unwrap(),
+        U256::from_str_radix("419598057710749587537080281518289024699150505326900462079484531390510117965", 10).unwrap(),
+        U256::from_str_radix("341318259000017461738706238280879290398059773267212529438780607147892801536", 10).unwrap(),
+        U256::from_str_radix("1437437604754599821041091415535991441313586347841485651963630208563420739", 10).unwrap(),
+        U256::from_str_radix("305222830440467078008666830004555943609735125691441831219591213494068931362", 10).unwrap(),
+        U256::from_str_radix("358396406696718360717615797531477055540194104082154743994717297650279402646", 10).unwrap(),
+        U256::from_str_radix("429270385827211102844129651648706540139690432947840438198166022904666187018", 10).unwrap(),
+        U256::from_str_radix("343946166212648899477337159288779715507980257611242783073384876024451565860", 10).unwrap(),
+        U256::from_str_radix("67853010773876862913176476530730880916439012004585961528150130218675908823", 10).unwrap(),
+        U256::from_str_radix("370855179649505412564259994413632062925303311800103998016489412083011059699", 10).unwrap(),
+        U256::from_str_radix("1182295126766215829784496273374889928477877265080355104888778", 10).unwrap(),
+    ];
+    from_cairo_byte_array_data(&bytes, 25)
+}
+
+pub fn pyth_set_fee() -> Vec<u8> {
+    let bytes = [
+        U256::from_str_radix("1766847064779993955862540543984267022910717161432209540262366788014689913", 10).unwrap(),
+        U256::from_str_radix("322968519187498395396360816568387642032723484530650782503164941848016432477", 10).unwrap(),
+        U256::from_str_radix("49565958604199796163020368", 10).unwrap(),
+        U256::from_str_radix("8072278384728444780182694421117884443886221966887092226", 10).unwrap(),
+    ];
+    from_cairo_byte_array_data(&bytes, 23)
+}
+
+pub fn pyth_set_data_sources() -> Vec<u8> {
+    let bytes = [
+        U256::from_str_radix("1766847064779993795984967344618836356750759980724568847727566676204733945", 10).unwrap(),
+        U256::from_str_radix("319252252405206634291073190903653114488682078063415369176250618646860635118", 10).unwrap(),
+        U256::from_str_radix("223938022913800988696085410923418445187967252047785407181969631814277398528", 10).unwrap(),
+        U256::from_str_radix("301", 10).unwrap(),
+    ];
+    from_cairo_byte_array_data(&bytes, 14)
+}
+
+pub fn create_realistic_price_update_test() -> Vec<u8> {
+    test_price_update1()
+}
+
+pub fn create_realistic_vaa_test() -> Vec<u8> {
+    good_vm1()
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_conversion_logic() {
+        let test_data = [U256::from(0x010203u32)];
+        let result = from_cairo_byte_array_data(&test_data, 3);
+        assert_eq!(result, vec![0x01, 0x02, 0x03]);
+    }
+
+    #[test]
+    fn test_good_update1_not_empty() {
+        let data = good_update1();
+        assert!(!data.is_empty());
+        assert!(data.len() > 100);
+    }
+
+    #[test]
+    fn test_test_price_update1_not_empty() {
+        let data = test_price_update1();
+        assert!(!data.is_empty());
+        assert!(data.len() > 100);
+    }
+
+    #[test]
+    fn test_good_vm1_not_empty() {
+        let data = good_vm1();
+        assert!(!data.is_empty());
+        assert!(data.len() > 50);
+    }
+}