Jelajahi Sumber

Enable and fix Rust unit tests in Tilt for p2w-sdk and p2w-attest (#184)

* Enable and fix Rust unit tests in Tilt for p2w-sdk and p2w-attest

commit-id:cae6ee05

* p2w-sdk/rust/src/lib.rs: comment about hex payload generation

commit-id:f9f3a249

* p2w-sdk/rust/src/lib.rs: Comment about hex payload generation

commit-id:352e74f9
Stanisław Drozd 3 tahun lalu
induk
melakukan
035021905a

+ 6 - 0
Dockerfile.wasm

@@ -24,6 +24,12 @@ ARG SED_REMOVE_INVALID_REFERENCE="/^\s*wasm.__wbg_systeminstruction_free(ptr);$/
 
 # TODO: it appears that wasm-pack ignores our lockfiles even with --locked
 
+# Run unit tests
+RUN --mount=type=cache,target=/root/.cache \
+	--mount=type=cache,target=third_party/pyth/p2w-sdk/rust/target \
+    cd third_party/pyth/p2w-sdk/rust \
+    && cargo test --locked
+
 # Compile pyth2wormhole
 RUN --mount=type=cache,target=/root/.cache \
 	--mount=type=cache,target=third_party/pyth/p2w-sdk/rust/target \

+ 1 - 0
third_party/pyth/Dockerfile.p2w-attest

@@ -10,6 +10,7 @@ RUN --mount=type=cache,target=/root/.cache \
     --mount=type=cache,target=target \
     --mount=type=cache,target=pyth2wormhole/target \
     cargo build --manifest-path ./pyth2wormhole/Cargo.toml --package pyth2wormhole-client && \
+    cargo test --manifest-path ./pyth2wormhole/Cargo.toml --package pyth2wormhole-client && \
     mv pyth2wormhole/target/debug/pyth2wormhole-client /usr/local/bin/pyth2wormhole-client && \
     chmod a+rx /usr/src/pyth/*.py
 

+ 10 - 4
third_party/pyth/p2w-sdk/rust/src/lib.rs

@@ -451,6 +451,9 @@ impl PriceAttestation {
     }
 }
 
+/// This test suite of the format doubles as a test payload generator;
+/// print statements help provide plausible serialized data on demand
+/// using `cargo test -- --nocapture`.
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -466,7 +469,7 @@ mod tests {
         PriceAttestation {
             product_id:          Pubkey::new_from_array(product_id_bytes),
             price_id:            Pubkey::new_from_array(price_id_bytes),
-            price:               (0xdeadbeefdeadbabe as u64) as i64,
+            price:               0x2bad2feed7,
             price_type:          PriceType::Price,
             ema_price:           Rational {
                 val:   -42,
@@ -482,7 +485,9 @@ mod tests {
             status:              PriceStatus::Trading,
             confidence_interval: 101,
             corp_act:            CorpAction::NoCorpAct,
-            timestamp:           123456789i64,
+            timestamp:           (0xdeadbeeffadedeedu64) as i64,
+	    num_publishers: 123212u32,
+	    max_num_publishers: 321232u32
         }
     }
 
@@ -517,8 +522,8 @@ mod tests {
 
     #[test]
     fn test_batch_serde() -> Result<(), ErrBox> {
-        let attestations: Vec<_> = (0..65535)
-            .map(|i| mock_attestation(Some([(i % 256) as u8; 32]), None))
+        let attestations: Vec<_> = (1..=10)
+            .map(|i| mock_attestation(Some([(i % 256) as u8; 32]), Some([(255 - (i % 256)) as u8; 32])))
             .collect();
 
         let batch_attestation = BatchPriceAttestation {
@@ -526,6 +531,7 @@ mod tests {
         };
 
         let serialized = batch_attestation.serialize()?;
+        println!("Batch hex Bytes: {:02X?}", serialized);
 
         let deserialized: BatchPriceAttestation =
             BatchPriceAttestation::deserialize(serialized.as_slice())?;