Source Code

;; Bitcoin Block Propagation

(define-map blocks {height: uint} {
    hash: (buff 32),
    prev-hash: (buff 32),
    merkle-root: (buff 32),
    timestamp: uint,
    broadcaster: principal
})

(define-data-var chain-tip uint u0)

(define-constant ERR-INVALID-BLOCK (err u200))
(define-constant ERR-BLOCK-EXISTS (err u201))

;; Broadcast new block
(define-public (broadcast-block 
    (hash (buff 32))
    (prev-hash (buff 32))
    (merkle-root (buff 32))
    (timestamp uint))
    (let ((height (+ (var-get chain-tip) u1)))
        (if (is-some (map-get? blocks {height: height}))
            ERR-BLOCK-EXISTS
            (begin
                (map-set blocks {height: height} {
                    hash: hash,
                    prev-hash: prev-hash,
                    merkle-root: merkle-root,
                    timestamp: timestamp,
                    broadcaster: tx-sender
                })
                (var-set chain-tip height)
                (ok height)
            )
        )
    )
)

;; Get block by height
(define-read-only (get-block (height uint))
    (map-get? blocks {height: height})
)

;; Get chain tip
(define-read-only (get-chain-tip)
    (ok (var-get chain-tip))
)

Functions (3)

FunctionAccessArgs
broadcast-blockpublichash: (buff 32
get-blockread-onlyheight: uint
get-chain-tipread-only