forked from schmittlauch/Hash2Pub
Start implementing the Epichord DHT: NodeID type
This commit is contained in:
commit
e83710f10b
34
src/EpiChord.hs
Normal file
34
src/EpiChord.hs
Normal 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
11
src/shell.nix
Normal file
|
@ -0,0 +1,11 @@
|
|||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
in
|
||||
pkgs.mkShell {
|
||||
buildInputs = [
|
||||
(pkgs.haskellPackages.ghcWithPackages (pkgs: with pkgs;
|
||||
[
|
||||
mtl
|
||||
]))
|
||||
];
|
||||
}
|
Loading…
Reference in a new issue