From 8e9cf1636e4ccd2c42c23e44efc49d79e9bab5d2 Mon Sep 17 00:00:00 2001 From: AtHeartEngineer <1675654+AtHeartEngineer@users.noreply.github.com> Date: Tue, 13 Sep 2022 20:27:48 +0000 Subject: [PATCH] init --- dandelion_utils/__init__.py | 0 dandelion_utils/constants.py | 17 +++++++++++++++ dandelion_utils/generateStemLength.py | 20 ++++++++++++++++++ dandelion_utils/reviseStemLength.py | 27 ++++++++++++++++++++++++ requirements.txt | 2 ++ stemMeasurements.py | 30 +++++++++++++++++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 dandelion_utils/__init__.py create mode 100644 dandelion_utils/constants.py create mode 100644 dandelion_utils/generateStemLength.py create mode 100644 dandelion_utils/reviseStemLength.py create mode 100644 requirements.txt create mode 100644 stemMeasurements.py diff --git a/dandelion_utils/__init__.py b/dandelion_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dandelion_utils/constants.py b/dandelion_utils/constants.py new file mode 100644 index 0000000..5844fbc --- /dev/null +++ b/dandelion_utils/constants.py @@ -0,0 +1,17 @@ +# Dandelion Constants + +# GossipsubStemMin should be at minimum 1, but recommend setting it to 2 or 3 +GossipsubStemMin = 3 + +# GossipsubStemMax should be set to at least GossipsubStemMin + 1, and would highly discourage setting this to above 10 or 12. +GossipsubStemMax = 8 + +# Determines how many peers to send a dandelion message to. +# In an ideal case, this should be set to 1, but for redundancy we recommend setting it to 2 in case a peer is malicious or malfunctioning. +numberOfStemPeers = 2 + +# Highly recommend stemReductionMin be 1, but it could be set to 0 +StemReductionMin = 1 + +# stemReductionMax could be the same value as stemReductionMin if you want a more deterministic, but recommend it be set to stemReductionMin + 1 +StemReductionMax = 2 \ No newline at end of file diff --git a/dandelion_utils/generateStemLength.py b/dandelion_utils/generateStemLength.py new file mode 100644 index 0000000..73edd50 --- /dev/null +++ b/dandelion_utils/generateStemLength.py @@ -0,0 +1,20 @@ +# Generates a random int between stemMin and stemMax, with fallbacks: +# returns zero if dandelion isn't enable +# If stemMin is undefined or invalid it is set to 2 +# if stemMax is undefined or invalid it is set to the minimum value between stemMin + 3 and 10 + +import math +from random import random + + +def generateStemLength(stemMin: int, stemMax: int) -> int: + # StemMin should be at minimum 1, but recommend setting it to 2 + if (stemMin is None or stemMin < 1): + stemMin = 2 + + # This is set to a <= and not an < because there should be some randomness in the stem length + if (stemMax is None or stemMax <= stemMin): + stemMax = min(stemMin + 3, 10) + + # Returns a random whole number between stemMin and stemMax + return math.floor(random() * (stemMax - stemMin + 1) + stemMin) diff --git a/dandelion_utils/reviseStemLength.py b/dandelion_utils/reviseStemLength.py new file mode 100644 index 0000000..3c8af63 --- /dev/null +++ b/dandelion_utils/reviseStemLength.py @@ -0,0 +1,27 @@ +# This function is used when receiving a message that has a stem length, and is used to reduce the stem length for the next hop. +# Reduces the stemLength by a random whole number between stemReductionMin and stemReductionMax. The randomness helps obfuscate the sending source and helps propogate the message via floodsub/classic gossipsub to the network sooner. + +import math +from random import random + + +def reviseStemLength(stemLength: int, stemReductionMin: int, stemReductionMax: int) -> int: + if (stemLength > 0): + # Highly recommend stemReductionMin be 1, but it could be set to 0 + if (stemReductionMin is None or stemReductionMin < 0): + stemReductionMin = 1 + + # stemReductionMax could be the same value as stemReductionMin if you want a more deterministic, but recommend it be set to stemReductionMin + 1 + if (stemReductionMax is None or stemReductionMax < stemReductionMin): + stemReductionMax = stemReductionMin + 1 + + delta = math.floor(random() * (stemReductionMax - stemReductionMin + 1) + stemReductionMin) + + newStemLength = stemLength - delta + print(f'REVISING STEM LENGTH: {stemLength} (stemlength) - {delta} (delta) = {newStemLength} (newStemLength)') + + if (newStemLength < 0): + newStemLength = 0 + return newStemLength + else: + return 0 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..64d4bfd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +numpy +pandas \ No newline at end of file diff --git a/stemMeasurements.py b/stemMeasurements.py new file mode 100644 index 0000000..2685e73 --- /dev/null +++ b/stemMeasurements.py @@ -0,0 +1,30 @@ +from dandelion_utils.constants import GossipsubStemMin, GossipsubStemMax, numberOfStemPeers, StemReductionMin, StemReductionMax + +from dandelion_utils.generateStemLength import generateStemLength + +from dandelion_utils.reviseStemLength import reviseStemLength + +import numpy as np +import pandas as pd + + +def printConstants(): + print("CONSTANTS:") + print(" - GossipsubStemMin: {}".format(GossipsubStemMin)) + print(" - GossipsubStemMax: {}".format(GossipsubStemMax)) + print(" - numberOfStemPeers: {}".format(numberOfStemPeers)) + print(" - StemReductionMin: {}".format(StemReductionMin)) + print(" - StemReductionMax: {}".format(StemReductionMax)) + + +def main(): + # Generate a random initial stem length + stemLength = generateStemLength(GossipsubStemMin, GossipsubStemMax) + print("stemLength: {}".format(stemLength)) + newStemLength = reviseStemLength(stemLength, StemReductionMin, StemReductionMax) + print("newStemLength: {}".format(newStemLength)) + + +if __name__ == "__main__": + printConstants() + main()