From e83710f10bedd428488492233e353caf043b9669 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Mon, 24 Feb 2020 21:53:47 +0100 Subject: [PATCH] Start implementing the Epichord DHT: NodeID type --- src/EpiChord.hs | 34 ++++++++++++++++++++++++++++++++++ src/shell.nix | 11 +++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/EpiChord.hs create mode 100644 src/shell.nix diff --git a/src/EpiChord.hs b/src/EpiChord.hs new file mode 100644 index 0000000..d5669ec --- /dev/null +++ b/src/EpiChord.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{- | +Module : EpiChord +Description : An opinionated implementation of the EpiChord DHT by Leong et al. +Copyright : (c) schmittlauch, 2019 +License : AGPL-3 +Stability : experimental + +Here be dragons +-} + +module EpiChord where + +newtype NodeID = NodeID { getNodeID :: Integer } deriving (Eq, Show, Enum) + +instance Bounded NodeID where + minBound = NodeID 0 + maxBound = NodeID (2^idBits - 1) + +instance Num NodeID where + a + b = NodeID $ (getNodeID a + getNodeID b) `mod` (getNodeID maxBound + 1) + a * b = NodeID $ (getNodeID a * getNodeID b) `mod` (getNodeID maxBound + 1) + a - b = NodeID $ (getNodeID a - getNodeID b) `mod` (getNodeID maxBound + 1) + -- Todo: decide whether throwing exceptions isn't better + fromInteger i = NodeID $ i `mod` (getNodeID maxBound + 1) + signum = NodeID . signum . getNodeID + abs = NodeID . abs . getNodeID -- ToDo: make sure that at creation time only IDs within the range are used + +-- Todo: Num Instanz selbst definieren +-- Todo: Ist es sinnvoll, das hier Teil von Ord zu machen? + +-- define protocol constants +idBits :: Integer +idBits = 256 diff --git a/src/shell.nix b/src/shell.nix new file mode 100644 index 0000000..8758a58 --- /dev/null +++ b/src/shell.nix @@ -0,0 +1,11 @@ +let + pkgs = import {}; +in +pkgs.mkShell { + buildInputs = [ + (pkgs.haskellPackages.ghcWithPackages (pkgs: with pkgs; + [ + mtl + ])) + ]; +}