Skip to content

Commit

Permalink
chore: specify principal len in wire format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Dec 8, 2023
1 parent 13b1529 commit 7f8a9eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
16 changes: 11 additions & 5 deletions contracts/pyth-governance-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update fee-recipient address
(let ((updated-data (unwrap! (from-consensus-buff? principal (get body ptgm)) ERR_UNEXPECTED_ACTION_PAYLOAD)))
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set fee-recipient-address updated-data)
(ok updated-data))))

Expand All @@ -180,7 +180,7 @@
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (unwrap! (from-consensus-buff? principal (get body ptgm)) ERR_UNEXPECTED_ACTION_PAYLOAD)))
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { wormhole-core-contract: updated-data }))
(ok (var-get current-execution-plan)))))

Expand All @@ -195,7 +195,7 @@
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (unwrap! (from-consensus-buff? principal (get body ptgm)) ERR_UNEXPECTED_ACTION_PAYLOAD)))
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { pyth-oracle-contract: updated-data }))
(ok (var-get current-execution-plan)))))

Expand All @@ -210,7 +210,7 @@
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (unwrap! (from-consensus-buff? principal (get body ptgm)) ERR_UNEXPECTED_ACTION_PAYLOAD)))
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { pyth-decoder-contract: updated-data }))
(ok (var-get current-execution-plan)))))

Expand All @@ -225,7 +225,7 @@
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (unwrap! (from-consensus-buff? principal (get body ptgm)) ERR_UNEXPECTED_ACTION_PAYLOAD)))
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { pyth-storage-contract: updated-data }))
(ok (var-get current-execution-plan)))))

Expand Down Expand Up @@ -387,6 +387,12 @@
emitter-address: (get value cursor-emitter-address)
})))

(define-private (parse-principal (pgtm-body (buff 8192)))
(let ((cursor-pgtm-body (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new pgtm-body none))
(cursor-principal-len (try! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-8 (get next cursor-pgtm-body))))
(principal-bytes (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 slice (get next cursor-principal-len) (some (get value cursor-principal-len))))
(new-principal (unwrap! (from-consensus-buff? principal principal-bytes) ERR_UNEXPECTED_ACTION_PAYLOAD)))
(ok new-principal)))

(define-private (parse-and-verify-prices-data-sources (pgtm-body (buff 8192)))
(let ((cursor-pgtm-body (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new pgtm-body none))
Expand Down
61 changes: 31 additions & 30 deletions unit-tests/pyth/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,32 @@ export namespace pyth {
};
}

function serializePrincipal(payload: {
address: string;
contractName?: string;
}): Uint8Array[] {
const components = [];
if (payload.contractName) {
let principal = contractPrincipalCV(
payload.address,
payload.contractName,
);
let serializedPrincipal = clarityValueToBuffer(principal);
let v = Buffer.alloc(1);
v.writeUint8(serializedPrincipal.length, 0);
components.push(v);
components.push(serializedPrincipal);
} else {
let principal = principalCV(payload.address);
let serializedPrincipal = clarityValueToBuffer(principal);
let v = Buffer.alloc(1);
v.writeUint8(serializedPrincipal.length, 0);
components.push(v);
components.push(clarityValueToBuffer(principal));
}
return components;
}

export function serializePtgmVaaPayloadToBuffer(
payload: PtgmVaaPayload,
): Uint8Array {
Expand All @@ -324,40 +350,15 @@ export namespace pyth {
components.push(bigintToBuffer(payload.updateFeeValue.mantissa, 8));
components.push(bigintToBuffer(payload.updateFeeValue.exponent, 8));
} else if (payload.updateFeeRecipient) {
if (payload.updateFeeRecipient.contractName) {
let principal = contractPrincipalCV(
payload.updateFeeRecipient.address,
payload.updateFeeRecipient.contractName,
);
components.push(clarityValueToBuffer(principal));
} else {
let principal = principalCV(payload.updateFeeRecipient.address);
components.push(clarityValueToBuffer(principal));
}
components.push(...serializePrincipal(payload.updateFeeRecipient));
} else if (payload.updateOracleContract) {
let principal = contractPrincipalCV(
payload.updateOracleContract.address,
payload.updateOracleContract.contractName,
);
components.push(clarityValueToBuffer(principal));
components.push(...serializePrincipal(payload.updateOracleContract));
} else if (payload.updateWormholeContract) {
let principal = contractPrincipalCV(
payload.updateWormholeContract.address,
payload.updateWormholeContract.contractName,
);
components.push(clarityValueToBuffer(principal));
components.push(...serializePrincipal(payload.updateWormholeContract));
} else if (payload.updateStoreContract) {
let principal = contractPrincipalCV(
payload.updateStoreContract.address,
payload.updateStoreContract.contractName,
);
components.push(clarityValueToBuffer(principal));
components.push(...serializePrincipal(payload.updateStoreContract));
} else if (payload.updateDecoderContract) {
let principal = contractPrincipalCV(
payload.updateDecoderContract.address,
payload.updateDecoderContract.contractName,
);
components.push(clarityValueToBuffer(principal));
components.push(...serializePrincipal(payload.updateDecoderContract));
} else if (payload.updateStalePriceThreshold) {
components.push(
bigintToBuffer(payload.updateStalePriceThreshold.threshold, 8),
Expand Down

0 comments on commit 7f8a9eb

Please sign in to comment.