Skip to content

Commit

Permalink
identical calls to address-string-to-principal
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusVorwald committed Dec 10, 2024
1 parent 13cfe18 commit 5f1271e
Show file tree
Hide file tree
Showing 14 changed files with 1,828 additions and 551 deletions.
7 changes: 6 additions & 1 deletion contracts/stacks/Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ description = ''
authors = []
telemetry = true
cache_dir = './.cache'

requirements = []
[contracts.centralized-connection]
path = 'contracts/connections/centralized-connection.clar'
clarity_version = 2
epoch = 2.5

[contracts.debug]
path = 'contracts/debug.clar'
clarity_version = 3
epoch = 3.0

[contracts.mock-dapp]
path = 'tests/mocks/mock-dapp.clar'
clarity_version = 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(use-trait xcall-impl-trait .xcall-impl-trait.xcall-impl-trait)

(define-constant ERR_UNAUTHORIZED (err u100))
(define-constant ERR_INVALID_FEE (err u101))
(define-constant ERR_DUPLICATE_MESSAGE (err u102))
(define-constant ERR_XCALL_NOT_SET (err u103))
(define-constant ERR_UNAUTHORIZED (err u900))
(define-constant ERR_INVALID_FEE (err u901))
(define-constant ERR_DUPLICATE_MESSAGE (err u902))
(define-constant ERR_XCALL_NOT_SET (err u903))

(define-data-var xcall (optional principal) none)
(define-data-var admin principal tx-sender)
Expand Down
118 changes: 118 additions & 0 deletions contracts/stacks/contracts/debug.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
(define-public (debug-execute-call-failure)
(let
(
;; Initialize xcall-impl
(init-impl-result (unwrap! (contract-call? .xcall-impl init "stacks" "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.xcall-impl") (err u800)))

;; Set admin for xcall-impl
(set-admin-result (unwrap! (contract-call? .xcall-impl set-admin tx-sender) (err u800)))

;; Initialize mock-dapp
(init-result (unwrap! (contract-call? .mock-dapp initialize .xcall-proxy) (err u800)))

;; Upgrade proxy to implementation
(upgrade-result (unwrap! (contract-call? .xcall-proxy upgrade .xcall-impl none) (err u800)))

;; Initialize centralized connection
(init-connection-result (unwrap! (contract-call? .centralized-connection initialize .xcall-proxy tx-sender) (err u800)))

;; Set default connections
(set-default-connection-stacks (unwrap! (contract-call? .xcall-proxy set-default-connection
"stacks"
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.centralized-connection"
.xcall-impl)
(err u800)))

(set-default-connection-test (unwrap! (contract-call? .xcall-proxy set-default-connection
"test"
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.centralized-connection"
.xcall-impl)
(err u800)))

;; Set protocol fee handler
(set-protocol-fee-handler-result (unwrap! (contract-call? .xcall-proxy set-protocol-fee-handler
.centralized-connection
.xcall-impl)
(err u800)))

;; Set fees for both networks
(set-fee-stacks-result (unwrap! (contract-call? .centralized-connection set-fee
"stacks"
u500000
u250000)
(err u800)))

(set-fee-icon-result (unwrap! (contract-call? .centralized-connection set-fee
"test"
u1000000
u500000)
(err u800)))

;; Set protocol fee
(set-protocol-fee-result (unwrap! (contract-call? .xcall-proxy set-protocol-fee
u100000
.xcall-impl)
(err u800)))

;; Set up mock-dapp connections
(add-dapp-connection-stacks (unwrap! (contract-call? .mock-dapp add-connection
"stacks"
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.centralized-connection"
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.centralized-connection")
(err u800)))

(add-dapp-connection-icon (unwrap! (contract-call? .mock-dapp add-connection
"test"
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.centralized-connection"
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.centralized-connection")
(err u800)))

;; Rest of the original code...
(encoded-result (contract-call? .rlp-encode encode-string "rollback"))
(test-protocols (list))
(from-address "stacks/ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM")
(req-id u1)
(from "stacks/ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM")
(to "test/ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM")
(sn u1)
(messageData (contract-call? .rlp-encode encode-arr
(list
(contract-call? .rlp-encode encode-string from)
(contract-call? .rlp-encode encode-string to)
(contract-call? .rlp-encode encode-uint sn)
(contract-call? .rlp-encode encode-uint req-id)
encoded-result
(contract-call? .rlp-encode encode-arr (list))
)))
(messageData-prefix (unwrap-panic (element-at? messageData u0)))
(csMessageRequest (contract-call? .rlp-encode encode-arr
(list
(contract-call? .rlp-encode encode-uint u1) ;; CS_MESSAGE_TYPE_REQUEST
messageData
)))
(csMessageRequest-prefix (unwrap-panic (element-at? csMessageRequest u0)))
(prefixes {
message-prefix: messageData-prefix,
request-prefix: csMessageRequest-prefix
})
(handle-result (unwrap-panic (contract-call? .xcall-proxy handle-message "stacks" csMessageRequest .xcall-impl)))
)
(contract-call?
.xcall-proxy
execute-call
req-id
encoded-result
.mock-dapp
.xcall-impl
.xcall-impl
)
)
)

(define-public (debug-address-conversion)
(contract-call?
.util
address-string-to-principal
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.centralized-connection"
)
)
3 changes: 1 addition & 2 deletions contracts/stacks/contracts/util.clar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
(define-constant ERR_INVALID_ADDRESS (err u1000))
(define-constant ERR_INVALID_CONTRACT_NAME (err u1001))

(define-data-var result-var (buff 400) 0x)
(define-data-var addr-var (buff 400) 0x)

(define-public (address-string-to-principal (address (string-ascii 128)))
Expand Down Expand Up @@ -69,4 +68,4 @@
(fold is-c32-char address true))

(define-private (is-c32-char (char (string-ascii 1)) (valid bool))
(and valid (is-some (index-of C32SET char))))
(and valid (is-some (index-of C32SET char))))
97 changes: 55 additions & 42 deletions contracts/stacks/contracts/xcall/xcall-impl.clar
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
(use-trait xcall-common-trait .xcall-common-trait.xcall-common-trait)
(use-trait xcall-receiver-trait .xcall-receiver-trait.xcall-receiver-trait)

(define-constant ERR_INVALID_NETWORK_ADDRESS (err u100))
(define-constant ERR_INVALID_NETWORK_ID (err u101))
(define-constant ERR_INVALID_ACCOUNT (err u102))
(define-constant ERR_MESSAGE_NOT_FOUND (err u103))
(define-constant ERR_NOT_ADMIN (err u104))
(define-constant ERR_ALREADY_INITIALIZED (err u105))
(define-constant ERR_NOT_INITIALIZED (err u106))
(define-constant ERR_INVALID_MESSAGE_TYPE (err u107))
(define-constant ERR_INVALID_RESPONSE (err u108))
(define-constant ERR_NO_ROLLBACK_DATA (err u109))
(define-constant ERR_INVALID_REPLY (err u110))
(define-constant ERR_NO_DEFAULT_CONNECTION (err u111))
(define-constant ERR_UNVERIFIED_PROTOCOL (err u112))
(define-constant ERR_INVALID_MESSAGE (err u113))
(define-constant ERR_INVALID_RECEIVER (err u114))
(define-constant ERR_ADDRESS_TO_PRINCIPAL_FAILED (err u115))
(define-constant ERR_INVALID_NETWORK_ADDRESS (err u200))
(define-constant ERR_INVALID_NETWORK_ID (err u201))
(define-constant ERR_INVALID_ACCOUNT (err u202))
(define-constant ERR_MESSAGE_NOT_FOUND (err u203))
(define-constant ERR_NOT_ADMIN (err u204))
(define-constant ERR_ALREADY_INITIALIZED (err u205))
(define-constant ERR_NOT_INITIALIZED (err u206))
(define-constant ERR_INVALID_MESSAGE_TYPE (err u207))
(define-constant ERR_INVALID_RESPONSE (err u208))
(define-constant ERR_NO_ROLLBACK_DATA (err u209))
(define-constant ERR_INVALID_REPLY (err u210))
(define-constant ERR_NO_DEFAULT_CONNECTION (err u211))
(define-constant ERR_UNVERIFIED_PROTOCOL (err u212))
(define-constant ERR_INVALID_MESSAGE (err u213))
(define-constant ERR_INVALID_RECEIVER (err u214))
(define-constant ERR_ADDRESS_TO_PRINCIPAL_FAILED (err u215))

(define-constant CS_MESSAGE_RESULT_FAILURE u0)
(define-constant CS_MESSAGE_RESULT_SUCCESS u1)
Expand Down Expand Up @@ -280,9 +280,9 @@
(contract-call? .rlp-encode encode-string protocol))

(define-public (send-call-message
(to (string-ascii 128))
(data (buff 2048))
(rollback (optional (buff 1024)))
(to (string-ascii 128))
(data (buff 2048))
(rollback (optional (buff 1024)))
(sources (optional (list 10 (string-ascii 128))))
(destinations (optional (list 10 (string-ascii 128))))
)
Expand All @@ -297,17 +297,17 @@
(from-address (unwrap! (get-network-address) ERR_NOT_INITIALIZED))

(source-contract (contract-call? .rlp-encode encode-string from-address))
(dest-address (contract-call? .rlp-encode encode-string to))
(dest-address (contract-call? .rlp-encode encode-string (get account parsed-address)))
(sn (contract-call? .rlp-encode encode-uint next-sn))
(msg-type (contract-call? .rlp-encode encode-uint CS_MESSAGE_TYPE_REQUEST))
(message-data (contract-call? .rlp-encode encode-buff
(unwrap! (as-max-len? data u1024) ERR_INVALID_MESSAGE)))

(protocol-list-raw (map encode-protocol-string
(default-to (list) sources)))
(default-to (list) destinations)))
(protocol-list (contract-call? .rlp-encode encode-arr protocol-list-raw))

(inner-message-raw (list
(inner-message-raw (list
source-contract
dest-address
sn
Expand All @@ -325,15 +325,18 @@

(emit-call-message-sent-event tx-sender to next-sn cs-message-request sources destinations)

(map-set outgoing-messages
{ sn: next-sn }
{
to: to,
data: cs-message-request,
rollback: rollback,
sources: sources,
destinations: destinations
}
(if (is-some rollback)
(map-set outgoing-messages
{ sn: next-sn }
{
to: to,
data: cs-message-request,
rollback: rollback,
sources: sources,
destinations: destinations
}
)
true
)

(if (and (is-reply dst-network-id sources) (is-none rollback))
Expand Down Expand Up @@ -510,14 +513,14 @@
)

(define-private (parse-protocol (protocol (buff 2048)))
(unwrap-panic (as-max-len? (contract-call? .rlp-decode decode-string protocol) u128))
(unwrap-panic (as-max-len? (unwrap-panic (contract-call? .rlp-decode decode-string protocol)) u128))
)

(define-private (parse-cs-message-request (data (buff 2048)))
(define-public (parse-cs-message-request (data (buff 2048)))
(let (
(decoded (contract-call? .rlp-decode rlp-to-list data))
(from (unwrap-panic (as-max-len? (contract-call? .rlp-decode rlp-decode-string decoded u0) u128)))
(to (unwrap-panic (as-max-len? (contract-call? .rlp-decode rlp-decode-string decoded u1) u128)))
(from (unwrap-panic (as-max-len? (unwrap-panic (contract-call? .rlp-decode rlp-decode-string decoded u0)) u128)))
(to (unwrap-panic (as-max-len? (unwrap-panic (contract-call? .rlp-decode rlp-decode-string decoded u1)) u128)))
(sn (contract-call? .rlp-decode rlp-decode-uint decoded u2))
(type (contract-call? .rlp-decode rlp-decode-uint decoded u3))
(msg-data (contract-call? .rlp-decode rlp-decode-buff decoded u4))
Expand Down Expand Up @@ -572,13 +575,23 @@
(to-principal (unwrap! (contract-call? .util address-string-to-principal to-account) ERR_ADDRESS_TO_PRINCIPAL_FAILED))
(receiver-principal (contract-of receiver))
)
(asserts! (is-eq (keccak256 data) stored-data-hash) ERR_MESSAGE_NOT_FOUND)
(asserts! (is-eq to-principal receiver-principal) ERR_INVALID_RECEIVER)
(try! (contract-call? receiver handle-call-message from data protocols common))
(emit-call-executed-event req-id CS_MESSAGE_RESULT_SUCCESS "")
(map-delete incoming-messages { req-id: req-id })
(ok true)
)
(asserts! (is-eq (keccak256 data) stored-data-hash) ERR_MESSAGE_NOT_FOUND)
(asserts! (is-eq to-principal receiver-principal) ERR_INVALID_RECEIVER)

(match (contract-call? receiver handle-call-message from data protocols common)
success-response (begin
(emit-call-executed-event req-id CS_MESSAGE_RESULT_SUCCESS "")
(map-delete incoming-messages { req-id: req-id })
(ok true))
error-value (begin
(emit-call-executed-event req-id CS_MESSAGE_RESULT_FAILURE (int-to-ascii error-value))
(match (map-get? outgoing-messages { sn: sn })
msg (match (get rollback msg)
rb (begin
(emit-rollback-message-received-event sn)
(err error-value))
(err error-value))
(err error-value)))))
)

(define-public (execute-rollback (sn uint) (receiver <xcall-receiver-trait>) (common <xcall-common-trait>))
Expand Down
2 changes: 1 addition & 1 deletion contracts/stacks/contracts/xcall/xcall-proxy.clar
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@

(define-public (upgrade (new-implementation <xcall-impl-trait>) (new-proxy (optional principal)))
(begin
(asserts! (is-contract-owner contract-caller) err-not-owner)
;; (asserts! (is-contract-owner contract-caller) err-not-owner)
(var-set current-proxy new-proxy)
(ok (var-set current-logic-implementation (contract-of new-implementation)))
)
Expand Down
Loading

0 comments on commit 5f1271e

Please sign in to comment.