Start implementing the Epichord DHT: NodeID type

This commit is contained in:
Trolli Schmittlauch 2020-02-24 21:53:47 +01:00
commit e83710f10b
2 changed files with 45 additions and 0 deletions

34
src/EpiChord.hs Normal file
View file

@ -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

11
src/shell.nix Normal file
View file

@ -0,0 +1,11 @@
let
pkgs = import <nixpkgs> {};
in
pkgs.mkShell {
buildInputs = [
(pkgs.haskellPackages.ghcWithPackages (pkgs: with pkgs;
[
mtl
]))
];
}