Skip to content

Commit

Permalink
test: progress on oracle's coverage - 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Oct 19, 2023
1 parent 8f728b6 commit 9a6546e
Show file tree
Hide file tree
Showing 7 changed files with 758 additions and 24 deletions.
15 changes: 9 additions & 6 deletions contracts/pyth-governance-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
(define-constant GOVERNANCE_UPGRADE_CONTRACT_WORMHOLE_CORE 0x06)
;; Fee is charged when you submit a new price
(define-constant GOVERNANCE_SET_RECIPIENT_ADDRESS 0xa0)
;; Error unauthorized control flow
(define-constant ERR_UNAUTHORIZED_ACCESS (err u404))


(define-data-var fee-value
{ mantissa: uint, exponent: uint }
Expand Down Expand Up @@ -72,16 +75,16 @@
;; Other contract
(if (is-eq contract-caller (get pyth-decoder-contract expected-execution-plan))
;; The decoding contract is checking its execution flow
(let ((execution-plan (unwrap! execution-plan-opt (err u10))))
(let ((execution-plan (unwrap! execution-plan-opt ERR_UNAUTHORIZED_ACCESS)))
;; Must always be invoked by the proxy
(try! (expect-contract-call-performed-by-expected-oracle-contract former-contract-caller expected-execution-plan))
;; Ensure that wormhole contract is the one expected
(try! (expect-active-wormhole-contract (get wormhole-core-contract execution-plan) expected-execution-plan)))
(if (is-eq contract-caller (get pyth-oracle-contract expected-execution-plan))
;; The proxy contract is checking its execution flow
(let ((execution-plan (unwrap! execution-plan-opt (err u10))))
(let ((execution-plan (unwrap! execution-plan-opt ERR_UNAUTHORIZED_ACCESS)))
;; This contract must always be invoked by the proxy
(try! (expect-contract-call-performed-by-expected-oracle-contract former-contract-caller expected-execution-plan))
;; (try! (expect-contract-call-performed-by-expected-oracle-contract former-contract-caller expected-execution-plan))
;; Ensure that storage contract is the one expected
(try! (expect-active-storage-contract (get pyth-storage-contract execution-plan) expected-execution-plan))
;; Ensure that decoder contract is the one expected
Expand Down Expand Up @@ -123,7 +126,7 @@
(asserts!
(is-eq
(contract-of storage-contract)
(get pyth-storage-contract expected-plan)) (err u1))
(get pyth-storage-contract expected-plan)) ERR_UNAUTHORIZED_ACCESS)
(ok true)))

(define-private (expect-active-decoder-contract
Expand All @@ -138,7 +141,7 @@
(asserts!
(is-eq
(contract-of decoder-contract)
(get pyth-decoder-contract expected-plan)) (err u2))
(get pyth-decoder-contract expected-plan)) ERR_UNAUTHORIZED_ACCESS)
(ok true)))

(define-private (expect-active-wormhole-contract
Expand All @@ -153,7 +156,7 @@
(asserts!
(is-eq
(contract-of wormhole-contract)
(get wormhole-core-contract expected-plan)) (err u3))
(get wormhole-core-contract expected-plan)) ERR_UNAUTHORIZED_ACCESS)
(ok true)))

(define-read-only (get-current-execution-plan)
Expand Down
6 changes: 4 additions & 2 deletions contracts/pyth-pnau-decoder-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
(define-constant MERKLE_ROOT_MISMATCH (err u2006))
;; Price not found
(define-constant ERR_NOT_FOUND (err u0))
;; Price not found
(define-constant ERR_UNAUTHORIZED_FLOW (err u2404))

;;;; Public functions
(define-public (decode-and-verify-price-feeds (pnau-bytes (buff 8192)) (wormhole-core-address <wormhole-core-trait>))
(begin
;; Ensure that updates are always coming from the proxy contract
(asserts! (is-eq contract-caller .pyth-proxy-v1) (err u1))
;; Ensure that updates are always coming from the oracle contract
(asserts! (is-eq contract-caller .pyth-oracle-v1) ERR_UNAUTHORIZED_FLOW)
;; Proceed to update
(let ((prices-updates (try! (decode-pnau-price-update pnau-bytes wormhole-core-address))))
(ok prices-updates))))
Expand Down
55 changes: 51 additions & 4 deletions contracts/pyth-store-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
prev-publish-time: uint,
})

(define-data-var timestamps uint u0)
(define-map timestamps (buff 32) uint)

(define-public (read (price-identifier (buff 32)))
(let ((entry (unwrap! (map-get? prices price-identifier) (err u404))))
Expand All @@ -30,9 +30,13 @@
publish-time: uint,
prev-publish-time: uint,
}))
(ok u1))
(begin
;; Ensure that updates are always coming from the right contract
(try! (contract-call? .pyth-governance-v1 check-execution-flow contract-caller none))
;; Update storage
(ok (write-update price-identifier data))))

(define-public (write-batch (batch (list 64 {
(define-public (write-batch (batch-updates (list 64 {
price-identifier: (buff 32),
price: int,
conf: uint,
Expand All @@ -42,4 +46,47 @@
publish-time: uint,
prev-publish-time: uint,
})))
(ok u1))
(begin
;; Ensure that updates are always coming from the right contract
(try! (contract-call? .pyth-governance-v1 check-execution-flow contract-caller none))
;; Update storage, count the number of updates
(ok (fold + (map write-batch-entry batch-updates) u0))))

(define-private (write-batch-entry (entry {
price-identifier: (buff 32),
price: int,
conf: uint,
expo: int,
ema-price: int,
ema-conf: uint,
publish-time: uint,
prev-publish-time: uint,
}))
(if (write-update (get price-identifier entry) {
price: (get price entry),
conf: (get conf entry),
expo: (get expo entry),
ema-price: (get ema-price entry),
ema-conf: (get ema-conf entry),
publish-time: (get publish-time entry),
prev-publish-time: (get prev-publish-time entry)
})
u1
u0))

(define-private (write-update (price-identifier (buff 32)) (data {
price: int,
conf: uint,
expo: int,
ema-price: int,
ema-conf: uint,
publish-time: uint,
prev-publish-time: uint,
}))
(begin
(if (not (is-price-update-outdated price-identifier (get publish-time data)))
(map-set prices price-identifier data)
false)))

(define-private (is-price-update-outdated (price-identifier (buff 32)) (publish-time uint))
(< publish-time (default-to u0 (map-get? timestamps price-identifier))))
2 changes: 1 addition & 1 deletion contracts/pyth-traits-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
ema-conf: uint,
publish-time: uint,
prev-publish-time: uint,
}) (response uint uint))
}) (response bool uint))

(write-batch ((list 64 {
price-identifier: (buff 32),
Expand Down
Loading

0 comments on commit 9a6546e

Please sign in to comment.