diff --git a/exchange.py b/exchange.py new file mode 100644 index 0000000000000000000000000000000000000000..7847e92cd637ed194b26b6bb2b4f5303751d344d --- /dev/null +++ b/exchange.py @@ -0,0 +1,12 @@ +from main import ureg,Q_ +import locale +import time + +from trade import Trade +class Exchange: + def __init__(self, p: float, ppw: float): + self.__timestamp: time.time_ns() + self.__power = Q_(p, ureg.watt) + self.__pricePerWatt = locale.currency(ppw) + self.__trades: set[Trade] = set() + self.__sig: bytes #signed exchange hash when becoming active \ No newline at end of file diff --git a/main.py b/main.py index 4e47fdc3ddca5a3a8c8b955d5059802cef75d4cd..6915656ccf3d8bc588e4e2c16bc78895df0cb540 100644 --- a/main.py +++ b/main.py @@ -1,22 +1,27 @@ import sys import random +import fastapi import dilithium.dilithium from dilithium import Dilithium from pint import UnitRegistry + ureg = UnitRegistry() Q_ = ureg.Quantity +v1 = Q_(5, ureg.volt) +a1 = Q_(4, ureg.amp) +p1 = v1 * a1 +p1.ito(ureg.watt) +print(p1) -dis1 = 10 * ureg.watt -print(repr(dis1)) -print(dis1) d1 = Dilithium(dilithium.dilithium.DEFAULT_PARAMETERS["dilithium3"]) -pk, sk = d1.keygen([random.randint(0,sys.maxsize),random.randint(0,sys.maxsize),random.randint(0,sys.maxsize),random.randint(0,sys.maxsize)]) +pk, sk = d1.keygen([random.randint(0, sys.maxsize), random.randint(0, sys.maxsize), random.randint(0, sys.maxsize), + random.randint(0, sys.maxsize)]) msg = b"this is a message" -sig = d1.sign_with_input(sk,msg) +sig = d1.sign_with_input(sk, msg) result = d1.verify(pk, msg, sig) print(result) diff --git a/offer.py b/offer.py index fb4adba3b530e15f2602384d1b710f42a1487422..2f33c059c7cddcd5d07fc68cd309622f79fa7345 100644 --- a/offer.py +++ b/offer.py @@ -1,4 +1,4 @@ -from main import ureg,Q_ -class Offer: - def __init__(self): - self.__power = 0 \ No newline at end of file +from exchange import Exchange +class Offer(Exchange): + def __init__(self, p: float, ppw: float): + Exchange.__init__(self, p, ppw) diff --git a/participant.py b/participant.py index 22e35b73d5ee0bb489c9fac4b23257fdebdbd0a5..79e67fe4ff611e68eb32a4b0b53ca06215c197bb 100644 --- a/participant.py +++ b/participant.py @@ -1,6 +1,8 @@ -from typing import Optional +from main import ureg,Q_ +import locale from random import randint +import ipaddress import sys from dilithium import Dilithium @@ -9,13 +11,37 @@ import dilithium.dilithium from offer import Offer from request import Request +from exchange import Exchange from trade import Trade +import socket +def get_pub_ip(): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.settimeout(0) + try: + # doesn't even have to be reachable + s.connect(('10.254.254.254', 1)) + ip = s.getsockname()[0] + except Exception: + ip = '127.0.0.1' + finally: + s.close() + return ip + + class Participant: def __init__(self): d1 = Dilithium(dilithium.dilithium.DEFAULT_PARAMETERS["dilithium3"]) self.__secretKey, self.__publicKey = d1.keygen([randint(0,sys.maxsize),randint(0,sys.maxsize),randint(0,sys.maxsize),randint(0,sys.maxsize)]) - self.next_offer: Optional[Offer] = None - self.next_request: Optional[Request] = None - self.active_trades: list[Trade] = [] + self.public_ip: ipaddress.IPv4Address = get_pub_ip() #set to current ipv4, confirm before each new round + self.known_participants: set[Participant] = set() + self.available_exchanges: set[Exchange] = set() #known available exchanges from other participants for next turns + self.next_exchanges: set[Exchange] = set() #own exchanges for next turn + self.active_trades: set[Trade] = set() #own active trades for this turn + self.__trade_history: list[Trade] = [] #every own past trade + self.__current_power = Q_(0, ureg.watt) #real time power exchange with the grid + self.__current_inhouse_demand = Q_(0, ureg.watt) #real time demand from household appliances and storage + self.__projected_inhouse_demand = Q_(0, ureg.watt) #expected demand for next round + self.__current_inhouse_supply = Q_(0, ureg.watt) #real time inhouse production from solar, wind, storage, ... + self.__projected_inhouse_supply = Q_(0, ureg.watt) #expected supply for next round diff --git a/request.py b/request.py index e0be93ccdbc3cc0c1019b761098615ade4029fed..05cb08f87ebe68d25fb141747a38c2ef4dbe5c6f 100644 --- a/request.py +++ b/request.py @@ -1,3 +1,7 @@ -class Request: - def __init__(self): - self.power = 0 \ No newline at end of file +from exchange import Exchange + +class Request(Exchange): + def __init__(self, p: float, ppw: float): + Exchange.__init__(self, p,ppw) + + diff --git a/shell.nix b/shell.nix index 6b17bf2f10f537e2c02ed86905eb52bb0b0437e1..4f35d8c1e6bf8142f37544f9d9e673196a7875a3 100644 --- a/shell.nix +++ b/shell.nix @@ -5,6 +5,7 @@ let mypython = pkgs.python3.withPackages (python-pkgs: [ python-pkgs.numpy python-pkgs.pint + python-pkgs.fastapi (python-pkgs.callPackage ./dilithium.nix { }) ]); in diff --git a/trade.py b/trade.py index 4a19f8de20ff222c10a5700d75b9ea43523ab8c3..b822381a9dbe44a6bc427cbc2414736cc667868b 100644 --- a/trade.py +++ b/trade.py @@ -1,7 +1,15 @@ +from main import ureg,Q_ +import locale +import time + from offer import Offer from request import Request class Trade: - def __init__(self, o: Offer, r: Request): + def __init__(self, o: Offer, r: Request, p: float, ppw: float): + self.__timestamp = time.time_ns() self.__offer: Offer = o - self.__request: Request = r \ No newline at end of file + self.__request: Request = r + self.__power = Q_(p, ureg.watt) + self.__pricePerWatt = locale.currency(ppw) + self.__sig: bytes \ No newline at end of file