From 5f5153820f83944d2eda0b4aabcc803d7280ca4c Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 12 Jan 2021 11:48:26 +0100 Subject: [PATCH] Add absolutely minimal implementation of chunking --- dagger/chunking.nim | 10 ++++++++++ dagger/merkledag.nim | 3 +++ tests/testChunking.nim | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 dagger/chunking.nim create mode 100644 dagger/merkledag.nim create mode 100644 tests/testChunking.nim diff --git a/dagger/chunking.nim b/dagger/chunking.nim new file mode 100644 index 00000000..4b15a48f --- /dev/null +++ b/dagger/chunking.nim @@ -0,0 +1,10 @@ +import ./merkledag + +export merkledag + +proc createChunks*(file: File): MerkleDag = + let contents = file.readAll() + MerkleDag(data: cast[seq[byte]](contents)) + +proc assembleChunks*(dag: MerkleDag, output: File) = + output.write(cast[string](dag.data)) diff --git a/dagger/merkledag.nim b/dagger/merkledag.nim new file mode 100644 index 00000000..4466aba7 --- /dev/null +++ b/dagger/merkledag.nim @@ -0,0 +1,3 @@ +type + MerkleDag* = object + data*: seq[byte] diff --git a/tests/testChunking.nim b/tests/testChunking.nim new file mode 100644 index 00000000..5442f21b --- /dev/null +++ b/tests/testChunking.nim @@ -0,0 +1,31 @@ +import std/unittest +import std/os +import pkg/dagger/chunking + +suite "chunking": + + var input, output: File + + setup: + input = open("tests/input.txt", fmReadWrite) + output = open("tests/output.txt", fmReadWrite) + input.write("foo") + input.setFilePos(0) + + teardown: + input.close() + output.close() + removeFile("tests/input.txt") + removeFile("tests/output.txt") + + test "creates a Merkle DAG from a file": + check createChunks(input) != MerkleDag.default + + test "creates a file from a Merkle DAG": + let dag = createChunks(input) + assembleChunks(dag, output) + + input.setFilePos(0) + output.setFilePos(0) + check output.readAll() == input.readAll() +