瀏覽代碼

Add test for partial update behaviour

Tom Pointon 3 年之前
父節點
當前提交
7fbd842d8d
共有 1 個文件被更改,包括 66 次插入0 次删除
  1. 66 0
      ethereum/test/pyth.js

+ 66 - 0
ethereum/test/pyth.js

@@ -254,6 +254,72 @@ contract("Pyth", function () {
         assert.equal(second.price.emaConf, 2);
         assert.equal(second.attestation_time, 1647273460);
     })
+
+    it("should only cache updates for new prices", async function() {
+        // This test sends two batches of updates, for the same Price IDs. The second batch contains
+        // different price values to the first batch, but only the first and last updates in
+        // the second batch have a newer timestamp than those in the first batch, and so these 
+        // are the only two which should be cached.
+
+        const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
+
+        let secondBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000073dfffffffb00000000000005470000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000622f65f5503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000450fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000622f65f4503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000659fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000622f65f45032574800020181ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bDfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000622f65f5";
+
+        let all_price_ids = ["0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
+            "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
+            "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd",
+            "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
+        ];
+
+        // Send the first batch
+        await attest(initialized, rawBatchPriceAttestation);
+        let prices_after_first_update = {};
+        for (var i = 0; i < all_price_ids.length; i++) {
+            const price_id = all_price_ids[i];
+            prices_after_first_update[price_id] = await initialized.methods.latestPriceInfo(price_id).call();
+        }
+
+        // Send the second batch
+        await attest(initialized, secondBatchPriceAttestation);
+        let prices_after_second_update = {};
+        for (var i = 0; i < all_price_ids.length; i++) {
+            const price_id = all_price_ids[i];
+            prices_after_second_update[price_id] = await initialized.methods.latestPriceInfo(price_id).call();
+        }
+
+        // Price IDs which have newer timestamps
+        let new_price_updates = [
+            "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
+            "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
+        ];
+
+        // Price IDs which have older timestamps
+        let old_price_updates = [
+            "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
+            "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd"];
+
+        // Check that the new price updates have been updated
+        for (var i = 0; i < new_price_updates.length; i++) {
+            const price_id = new_price_updates[i];
+            assert.notEqual(prices_after_first_update[price_id].price.price, prices_after_second_update[price_id].price.price);
+            assert.notEqual(prices_after_first_update[price_id].attestation_time, prices_after_second_update[price_id].attestation_time);
+        }
+
+        // Check that the old price updates have been discarded
+        for (var i = 0; i < old_price_updates.length; i++) {
+            const price_id = old_price_updates[i];
+            assert.equal(prices_after_first_update[price_id].price.price, prices_after_second_update[price_id].price.price);
+            assert.equal(prices_after_first_update[price_id].price.conf, prices_after_second_update[price_id].price.conf);
+            assert.equal(prices_after_first_update[price_id].price.expo, prices_after_second_update[price_id].price.expo);
+            assert.equal(prices_after_first_update[price_id].price.status.toString(), prices_after_second_update[price_id].price.status.toString());
+            assert.equal(prices_after_first_update[price_id].price.numPublishers, prices_after_second_update[price_id].price.numPublishers);
+            assert.equal(prices_after_first_update[price_id].price.maxNumPublishers, prices_after_second_update[price_id].price.maxNumPublishers);
+            assert.equal(prices_after_first_update[price_id].price.emaPrice, prices_after_second_update[price_id].price.emaPrice);
+            assert.equal(prices_after_first_update[price_id].price.emaConf, prices_after_second_update[price_id].price.emaConf);
+            assert.equal(prices_after_first_update[price_id].attestation_time, prices_after_second_update[price_id].attestation_time);
+            assert.equal(prices_after_first_update[price_id].arrival_time, prices_after_second_update[price_id].arrival_time);
+        }
+    })
 });
 
 const signAndEncodeVM = async function (