;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2022 pukkamustard ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see . (define-module (guix eris) #:use-module (eris) #:use-module (guix config) #:use-module (guix eris fs-store) #:use-module (guix eris http) #:use-module (guix eris ipfs) #:use-module (web uri) #:use-module (ice-9 match) #:export (guix-eris-block-reducer guix-eris-block-ref %eris-block-store-directory)) (define %eris-block-store-directory (make-parameter (or (getenv "GUIX_ERIS_BLOCK_STORE_DIRECTORY") (string-append %state-directory "/eris")))) (define (guix-eris-block-reducer) "Returns a block reducer that stores blocks of ERIS encoded content." (eris-fs-store-reducer (%eris-block-store-directory))) (define %eris-peers (make-parameter ;; TODO: make ERIS peers configurable somewhere (list (string->uri "http://localhost:8081") 'ipfs))) (define* (try-in-order ref #:key block-refs) (match block-refs ((block-ref . rest) (let ((block (block-ref ref))) (if block block (try-in-order ref #:block-refs rest)))) (() #f))) (define* (peer->block-ref peer #:key open-connection) (cond ((uri? peer) (case (uri-scheme peer) ((http https) (lambda (ref) (eris-http-block-ref ref #:host peer #:open-connection open-connection))) ;; unsupported ERIS peer URL (else (lambda (_) #f)))) ((eqv? 'ipfs peer) (lambda (ref) (eris-ipfs-ref ref #:open-connection open-connection))))) (define* (guix-eris-block-ref ref #:key open-connection) "Attempts to dereference a block of some ERIS encoded content with reference REF. First the local block store is checked, followed by remote peers as configured in the parameter %eris-peers (in order). Returns #f if the block could not be de-referenced." (try-in-order ref #:block-refs (cons ;; first try and get block from local block store (lambda (ref) (eris-fs-store-ref ref #:store-directory (%eris-block-store-directory))) ;; then try peers (map (lambda (peer) (peer->block-ref peer #:open-connection open-connection)) (%eris-peers)))))