Bläddra i källkod

ethereum: Add a setContractConfig test, clarify verifyPythVM errors

Stan Drozd 3 år sedan
förälder
incheckning
cb266322f5
2 ändrade filer med 61 tillägg och 5 borttagningar
  1. 7 5
      ethereum/contracts/pyth/Pyth.sol
  2. 54 0
      ethereum/test/pyth.js

+ 7 - 5
ethereum/contracts/pyth/Pyth.sol

@@ -28,7 +28,9 @@ contract Pyth is PythGetters, PythSetters, AbstractPyth {
         (IWormhole.VM memory vm, bool valid, string memory reason) = wormhole().parseAndVerifyVM(encodedVm);
 
         require(valid, reason);
-        require(verifyPythVM(vm), "invalid emitter");
+
+	(bool pythValid, string memory pythReason) = verifyPythVM(vm);
+	require(pythValid, pythReason);
 
         PythInternalStructs.BatchPriceAttestation memory batch = parseBatchPriceAttestation(vm.payload);
 
@@ -73,14 +75,14 @@ contract Pyth is PythGetters, PythSetters, AbstractPyth {
         return info;
     }
 
-    function verifyPythVM(IWormhole.VM memory vm) private view returns (bool valid) {
+    function verifyPythVM(IWormhole.VM memory vm) private view returns (bool valid, string memory reason) {
         if (vm.emitterChainId != pyth2WormholeChainId()) {
-            return false;
+            return (false, "Invalid Chain ID");
         }
         if (vm.emitterAddress != pyth2WormholeEmitter()) {
-            return false;
+            return (false, "Invalid Emitter");
         }
-        return true;
+        return (true, "");
     }
 
 

+ 54 - 0
ethereum/test/pyth.js

@@ -362,6 +362,60 @@ contract("Pyth", function () {
             );
         }
     });
+
+    it("should set chain/emitter/wormhole IDs and immediately use them", async function () {
+        let attestationTime = 1647273460; // re-used for publishTime
+        let publishTime = 1647273465; // re-used for publishTime
+        let rawBatch = generateRawBatchAttestation(attestationTime, publishTime, 1337);
+
+        // Switch only chain ID to wrong value
+        await this.pythProxy.setContractConfig(testPyth2WormholeChainId + 1, testPyth2WormholeEmitter, (await Wormhole.deployed()).address);
+
+        let wrongChainIdFails = false;
+
+        try {
+            // Should result in expected failure
+            await updatePriceFeeds(this.pythProxy, [rawBatch]);
+        } catch (e) {
+            wrongChainIdFails = true;
+        }
+
+        assert.equal(wrongChainIdFails, true);
+
+        // Switch only emitter to wrong value
+        await this.pythProxy.setContractConfig(testPyth2WormholeChainId, testGovernanceContract, (await Wormhole.deployed()).address);
+
+        let wrongEmitterFails = false;
+
+        try {
+        // Should result in expected failure
+        await updatePriceFeeds(this.pythProxy, [rawBatch]);
+        } catch (e) {
+            wrongEmitterFails = true;
+        }
+
+        assert.equal(wrongEmitterFails, true);
+
+        // Switch only wormhole address to a wrong value
+        await this.pythProxy.setContractConfig(testPyth2WormholeChainId, testPyth2WormholeEmitter, testSigner2.address);
+
+        let wrongWormholeAddrFails = false;
+
+        try {
+            // Should result in expected failure
+            await updatePriceFeeds(this.pythProxy, [rawBatch]);
+        } catch (e) {
+            wrongWormholeAddrFails = true;
+        }
+
+        assert.equal(wrongWormholeAddrFails, true);
+
+        // Switch everything back to normal
+        await this.pythProxy.setContractConfig(testPyth2WormholeChainId, testPyth2WormholeEmitter, (await Wormhole.deployed()).address);
+
+        // Should be successful again
+        await updatePriceFeeds(this.pythProxy, [rawBatch]);
+    });
 });
 
 const signAndEncodeVM = async function (