From: pukkamustard <pukkamustard@posteo.net>
To: 52555@debbugs.gnu.org
Cc: pukkamustard <pukkamustard@posteo.net>
Subject: [bug#52555] [PATCH v3 8/8] eris: Use IPFS to get ERIS blocks.
Date: Thu, 29 Dec 2022 18:13:27 +0000 [thread overview]
Message-ID: <20221229181327.758-9-pukkamustard@posteo.net> (raw)
In-Reply-To: <20221229181327.758-1-pukkamustard@posteo.net>
* guix/eris/ipfs.scm: New files.
* Makefile.am (MODULES): Add it.
* guix/eris.scm (%eris-peers): Add IPFS.
(peer->block-ref): Handle IPFS peer.
---
Makefile.am | 1 +
guix/eris.scm | 32 ++++---
guix/eris/ipfs.scm | 214 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 237 insertions(+), 10 deletions(-)
create mode 100644 guix/eris/ipfs.scm
diff --git a/Makefile.am b/Makefile.am
index 373f6b7c27..6f648a40a3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -135,6 +135,7 @@ MODULES = \
guix/eris.scm \
guix/eris/fs-store.scm \
guix/eris/http.scm \
+ guix/eris/ipfs.scm \
guix/platform.scm \
guix/platforms/arm.scm \
guix/platforms/mips.scm \
diff --git a/guix/eris.scm b/guix/eris.scm
index d56643bec4..5b0c1ee36b 100644
--- a/guix/eris.scm
+++ b/guix/eris.scm
@@ -22,6 +22,7 @@ (define-module (guix 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)
@@ -42,8 +43,10 @@ (define (guix-eris-block-reducer)
(define %eris-peers
(make-parameter
- ;; TODO
- (list (string->uri "http://localhost:8081"))))
+ ;; TODO: make ERIS peers configurable somewhere
+ (list
+ (string->uri "http://localhost:8081")
+ 'ipfs)))
(define* (try-in-order ref #:key block-refs)
(match block-refs
@@ -55,18 +58,27 @@ (define* (try-in-order ref #:key block-refs)
(() #f)))
(define* (peer->block-ref peer #:key open-connection)
- (case (uri-scheme peer)
+ (cond
+ ((uri? peer) (case (uri-scheme peer)
- ((http https)
- (lambda (ref)
- (eris-http-block-ref ref
- #:host peer
- #:open-connection open-connection)))
+ ((http https)
+ (lambda (ref)
+ (eris-http-block-ref ref
+ #:host peer
+ #:open-connection open-connection)))
- ;; unsupported ERIS peer URL
- (else (lambda (_) #f))))
+ ;; 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
diff --git a/guix/eris/ipfs.scm b/guix/eris/ipfs.scm
new file mode 100644
index 0000000000..9771414e7b
--- /dev/null
+++ b/guix/eris/ipfs.scm
@@ -0,0 +1,214 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2022 pukkamustard <pukkamustard@posteo.net>
+;;;
+;;; 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 <http://www.gnu.org/licenses/>.
+
+
+;;; Commentary:
+;;;
+;;; This module provides an interface to the IPFS daemons HTTP API for storing
+;;; and retrieving blocks. This can be used to store blocks of ERIS encoded
+;;; content.
+;;;
+;;; See also the IPFS API documentation:
+;;; https://docs.ipfs.io/reference/http/api/#api-v0-block-put
+
+(define-module (guix eris ipfs)
+ #:use-module (eris utils base32)
+ #:use-module (sodium generichash)
+ #:use-module (json)
+ #:use-module (web uri)
+ #:use-module (web client)
+ #:use-module (web response)
+ #:use-module (srfi srfi-71)
+ #:use-module (rnrs io ports)
+ #:use-module (rnrs bytevectors)
+
+ #:use-module ((guix build download)
+ #:select ((open-connection-for-uri
+ . guix:open-connection-for-uri)))
+ #:export (%ipfs-base-url
+
+ eris-ipfs-reducer
+ eris-ipfs-ref))
+
+
+;; CID encoding
+
+;; Multicodec codes
+;; (https://github.com/multiformats/multicodec/blob/master/table.csv)
+(define multicodec-raw-code #x55)
+(define multicodec-blake2b-256-code #xb220)
+
+(define (blake2b-256->binary-cid hash)
+ "Encode a Blake2b-256 hash as binary CID"
+ (call-with-values
+ (lambda () (open-bytevector-output-port))
+ (lambda (port get-bytevector)
+ ;; CID version
+ (put-u8 port 1)
+ ;; multicoded content-type
+ (put-u8 port multicodec-raw-code)
+ ;; set multihash to blake2b-256. This is the manually encoded varint of
+ ;; 0xb220
+ (put-u8 port 160) (put-u8 port 228) (put-u8 port 2)
+ ;; set hash lenght
+ (put-u8 port 32)
+ ;; and finally the hash itself
+ (put-bytevector port hash)
+
+ ;; finalize and get the bytevector
+ (get-bytevector))))
+
+(define (binary-cid->cid bcid)
+ "Encode a binary CID as Base32 encoded CID"
+ ;; 'b' is the multibsae code for base32
+ (string-append "b"
+ ;; the IPFS daemon uses lower-case, so to be consistent we
+ ;; also.
+ (string-downcase
+ ;; base32 encode the binary cid
+ (base32-encode bcid))))
+
+(define blake2b-256->cid
+ (compose binary-cid->cid blake2b-256->binary-cid))
+
+
+;; IPFS API
+
+(define %ipfs-base-url
+ ;; URL of the IPFS gateway.
+ (make-parameter "http://localhost:5001"))
+
+(define* (call url decode
+ #:optional
+ (method http-post)
+ #:key port body (false-if-404? #t) (headers '())
+ (keep-alive #t)
+ (open-connection guix:open-connection-for-uri)
+ (timeout 10))
+ "Invoke the endpoint at URL using METHOD. Decode the resulting JSON body
+using DECODE, a one-argument procedure that takes an input port; when DECODE
+is false, return the input port. When FALSE-IF-404? is true, return #f upon
+404 responses."
+ (let* ((url (if (string? url) (string->uri url) url))
+ (port (or port (open-connection url #:timeout timeout)))
+ (response response-port
+ (if keep-alive
+ (method url #:streaming? #t
+ #:body body
+ #:port port
+ #:keep-alive? #t)
+ (method url #:streaming? #t
+ #:body body
+ #:port port
+ ;; IPFS daemon seems to responds with bad
+ ;; request if PUT requests are kept alive and
+ ;; do not have "Connection: close" header.
+ #:keep-alive? #f
+ #:headers `((connection close)
+ ,@headers)))))
+ (cond ((= 200 (response-code response))
+ (if decode
+ (let ((result (decode response-port)))
+ (close-port response-port)
+ result)
+ response-port))
+ ((and false-if-404?
+ (= 404 (response-code response)))
+ (close-port response-port)
+ #f)
+ (else
+ (close-port response-port)
+ (format #t "~a\n" response)
+ (throw 'ipfs-error url response)))))
+
+(define-syntax-rule (false-if-ipfs-error exp)
+ "Return $f if EXP triggers a network related or IPFS related exception."
+ (with-exception-handler
+ (lambda (exn)
+ (let ((kind (exception-kind exn))
+ (errno (system-error-errno
+ (cons 'system-error (exception-args exn)))))
+ (cond
+ ((= errno ECONNREFUSED) #f)
+ (else (raise-exception exp)))))
+ (lambda () exp)
+ #:unwind? #t))
+
+(define %multipart-boundary
+ ;; XXX: We might want to find a more reliable boundary.
+ (string-append (make-string 24 #\-) "2698127afd7425a6"))
+
+(define (bytevector->form-data bv port)
+ "Write to PORT a 'multipart/form-data' representation of BV."
+ (display (string-append "--" %multipart-boundary "\r\n"
+ "Content-Disposition: form-data\r\n"
+ "Content-Type: application/octet-stream\r\n\r\n")
+ port)
+ (put-bytevector port bv)
+ (display (string-append "\r\n--" %multipart-boundary "--\r\n")
+ port))
+
+(define (ipfs-block-put bv)
+ "Store a block on IPFS and return the CID of the block"
+ (call (string-append (%ipfs-base-url)
+ "/api/v0/block/put"
+ "?format=raw&mhtype=blake2b-256")
+ (lambda (port) (assoc-ref (json->scm port) "Key"))
+ #:headers `((content-type
+ . (multipart/form-data
+ (boundary . ,%multipart-boundary))))
+ #:body (call-with-bytevector-output-port
+ (lambda (port) (bytevector->form-data bv port)))
+ ;; IPFS daemon does not seem to accept connection re-use when putting
+ ;; blocks.
+ #:keep-alive #f))
+
+(define* (ipfs-block-get cid #:key
+ (open-connection guix:open-connection-for-uri))
+ "Get a block from IPFS via the HTTP API"
+ (false-if-ipfs-error
+ (call (string-append (%ipfs-base-url)
+ "/api/v0/block/get"
+ "?arg=" cid)
+ get-bytevector-all
+ #:timeout 5
+ #:open-connection open-connection)))
+
+;; ERIS block reducer
+
+(define eris-ipfs-reducer
+ (case-lambda
+ ;; initialization. Nothing to do here. In an improved implementation we
+ ;; might create a single HTTP connection and reuse it for all blocks.
+ (() '())
+
+ ;; Completion. Again, nothing to do.
+ ((_) 'done)
+
+ ;; store a block
+ ((_ ref-block)
+ ;; ref-block is a pair consisting of the reference to the block and the
+ ;; block itself.
+ (ipfs-block-put (cdr ref-block)))))
+
+(define* (eris-ipfs-ref ref #:key
+ (open-connection guix:open-connection-for-uri))
+ "Dereference a block from IPFS"
+ (ipfs-block-get (blake2b-256->cid ref)
+ #:open-connection open-connection))
--
2.38.1
next prev parent reply other threads:[~2022-12-29 18:24 UTC|newest]
Thread overview: 65+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-16 16:17 [bug#52555] [RFC PATCH 0/3] Decentralized substitute distribution with ERIS pukkamustard
2021-12-16 16:20 ` [bug#52555] [RFC PATCH 1/3] publish: Add ERIS URN to narinfo pukkamustard
2021-12-16 16:20 ` [bug#52555] [RFC PATCH 2/3] WIP: gnu: guile-eris: Update to unreleased git version pukkamustard
2021-12-16 16:20 ` [bug#52555] [RFC PATCH 3/3] publish: Add IPFS support pukkamustard
2021-12-20 16:25 ` [bug#52555] [RFC PATCH 0/3] Decentralized substitute distribution with ERIS Ludovic Courtès
2021-12-23 11:42 ` pukkamustard
2021-12-24 14:48 ` Ludovic Courtès
2022-01-25 19:21 ` [bug#52555] [RFC PATCH v2 0/5] " pukkamustard
2022-01-25 19:21 ` [bug#52555] [RFC PATCH v2 1/5] WIP: gnu: guile-eris: Update to unreleased git version pukkamustard
2022-01-25 19:21 ` [bug#52555] [RFC PATCH v2 2/5] publish: Add ERIS URN to narinfo pukkamustard
2022-01-29 21:09 ` Maxime Devos
2022-01-29 21:15 ` Maxime Devos
2022-02-02 10:16 ` pukkamustard
2022-01-25 19:21 ` [bug#52555] [RFC PATCH v2 3/5] Add (guix eris) pukkamustard
2022-01-29 21:23 ` Maxime Devos
2022-02-02 10:28 ` pukkamustard
2022-02-02 15:36 ` Maxime Devos
2022-01-29 21:24 ` Maxime Devos
2022-01-25 19:22 ` [bug#52555] [RFC PATCH v2 4/5] publish: Add support for storing ERIS encoded blocks to IPFS pukkamustard
2022-01-29 21:28 ` Maxime Devos
2022-02-02 10:24 ` pukkamustard
2022-01-25 19:22 ` [bug#52555] [RFC PATCH v2 5/5] substitute: Fetch substitutes using ERIS pukkamustard
2022-01-29 21:29 ` Maxime Devos
2022-02-02 10:11 ` pukkamustard
2022-01-29 21:33 ` Maxime Devos
2022-01-29 21:38 ` Maxime Devos
2022-01-29 21:40 ` Maxime Devos
2022-01-29 21:40 ` Maxime Devos
2022-02-02 10:38 ` pukkamustard
2022-01-29 21:00 ` [bug#52555] [RFC PATCH v2 0/5] Decentralized substitute distribution with ERIS Maxime Devos
2022-02-02 9:50 ` pukkamustard
2022-01-29 21:08 ` Maxime Devos
2022-02-02 9:56 ` pukkamustard
2022-02-02 11:09 ` Maxime Devos
2022-01-29 21:52 ` Maxime Devos
2022-02-02 11:10 ` pukkamustard
2022-02-03 20:36 ` Maxime Devos
2022-02-04 10:20 ` pukkamustard
2022-01-30 11:46 ` Maxime Devos
2022-02-02 10:51 ` pukkamustard
2022-02-02 11:27 ` Maxime Devos
2022-02-02 12:42 ` pukkamustard
2022-02-02 15:07 ` Maxime Devos
2022-02-02 15:27 ` Maxime Devos
2022-02-04 16:16 ` Maxime Devos
2022-12-29 18:13 ` [bug#52555] [PATCH v3 0/8] " pukkamustard
2022-12-29 18:13 ` [bug#52555] [PATCH v3 1/8] publish: Add ERIS URN to narinfo pukkamustard
2023-01-14 18:34 ` [bug#52555] [RFC PATCH 0/3] Decentralized substitute distribution with ERIS Ludovic Courtès
2022-12-29 18:13 ` [bug#52555] [PATCH v3 2/8] publish: Store ERIS encoded blocks to a local block store pukkamustard
2023-01-14 18:42 ` [bug#52555] [RFC PATCH 0/3] Decentralized substitute distribution with ERIS Ludovic Courtès
2022-12-29 18:13 ` [bug#52555] [PATCH v3 3/8] publish: Add HTTP endpoint for resolving ERIS blocks pukkamustard
2022-12-29 18:13 ` [bug#52555] [PATCH v3 4/8] WIP: substitute: Fetch substitutes using ERIS pukkamustard
2022-12-29 18:13 ` [bug#52555] [PATCH v3 5/8] eris/http: Add HTTP block de-referencer pukkamustard
2022-12-29 18:13 ` [bug#52555] [PATCH v3 6/8] WIP: eris: Use HTTP to get ERIS blocks pukkamustard
2022-12-29 18:13 ` [bug#52555] [PATCH v3 7/8] eris: Use parameterized %eris-peers when getting blocks pukkamustard
2022-12-29 18:13 ` pukkamustard [this message]
2023-01-14 18:25 ` [bug#52555] [RFC PATCH 0/3] Decentralized substitute distribution with ERIS Ludovic Courtès
2023-12-28 9:40 ` [bug#52555] [PATCH v4 0/7] " pukkamustard
2023-12-28 9:40 ` [bug#52555] [PATCH v4 1/7] narinfo: Add ERIS field pukkamustard
2023-12-28 9:40 ` [bug#52555] [PATCH v4 2/7] gnu: Add guile-coap pukkamustard
2023-12-28 9:40 ` [bug#52555] [PATCH v4 3/7] gnu: guile-eris: Update to 1.2.0-dev pukkamustard
2023-12-28 9:40 ` [bug#52555] [PATCH v4 4/7] publish: Add ERIS URN to narinfo pukkamustard
2023-12-28 9:40 ` [bug#52555] [PATCH v4 5/7] eris: Connect with an ERIS Store over CoAP+Unix pukkamustard
2023-12-28 9:40 ` [bug#52555] [PATCH v4 6/7] substitute: Decode substitutes using ERIS pukkamustard
2023-12-28 9:40 ` [bug#52555] [PATCH v4 7/7] gnu: Add kapla pukkamustard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221229181327.758-9-pukkamustard@posteo.net \
--to=pukkamustard@posteo.net \
--cc=52555@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).