Skip to content

Commit

Permalink
Merge pull request #7 from ggleszczynski/features/implementation
Browse files Browse the repository at this point in the history
Implementing logic
  • Loading branch information
grzegorz-leszczynski-reef authored Jan 3, 2025
2 parents c8e6b96 + 4af7d75 commit a50bff9
Show file tree
Hide file tree
Showing 26 changed files with 3,532 additions and 440 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ The goal of this project is to implement a distributed and decentralized system
- Validators can request the connection information of miners from the subtensor network. This information is validated and
decrypted locally using the validator's private key.

## Communication Flow
## Basic Communication Flow

<!--
@startuml ./assets/diagrams/CommunicationFlow
participant Validator
participant Miner
participant GitHub
participant Storage
participant Bittensor
participant AddressManager
database Storage
database Bittensor
Validator -> Validator: Generate Validator key-pair
Validator -> GitHub: Publish public key along with HotKey
GitHub -> Miner: Fetch Validator info
Miner -> Miner: Encrypt Miner IP/Domain with Validator public key
Miner -> Storage: Add/update file with encrypted IPs/Domains for Validators
Validator -> Bittensor: Publish public key along with HotKey
Bittensor -> Miner: Fetch new Validator info
Miner -> AddressManager: Generate new address
Miner -> Miner: Encrypt generated address with Validator public key
Miner -> Storage: Update file with encrypted addresses for Validators
Miner -> Bittensor: Publish file location
Bittensor -> Validator: Fetch file location
Storage -> Validator: Fetch Miner file
Validator -> Validator: Decrypt Miner file entry encrypted for given Validator
Validator -> Miner: Send request using decrypted Miner IP/Domain
Validator -> Miner: Send request using decrypted Miner address
@enduml
-->

Expand Down
19 changes: 10 additions & 9 deletions assets/diagrams/CommunicationFlow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 73 additions & 29 deletions bt_ddos_shield/address.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,98 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum


class AddressType(Enum):
"""
Possible types of address.
"""
IP = "ip" # IPv4 address
IPV6 = "ipv6" # IPv6 address
DOMAIN = "domain" # domain name
IP = "ip" # IPv4 address
IPV6 = "ipv6" # IPv6 address
DOMAIN = "domain" # domain name
S3 = "s3" # address identifies S3 object (id is object name)
EC2 = "ec2" # address identifies EC2 instance (id is instance id)


class Address(ABC):
@dataclass
class Address:
"""
Class describing address, which redirects to original miner's server.
Class describing some address - domain or IP.
"""

def __init__(self, address_id, address_type: AddressType, address: str, port: int):
"""
Args:
address_id: Identifier (used by AddressManager) of the address. Type depends on the implementation.
address_type: Type of the address.
address: Address.
port: Port of the address.
"""
self.address_id = address_id
self.address_type = address_type
self.address = address
self.port = port
address_id: str
""" identifier (used by AbstractAddressManager implementation) of the address """
address_type: AddressType
address: str
port: int

def __repr__(self):
return f"Address(id={self.address_id}, type={self.address_type}, address={self.address}:{self.port})"


class AddressSerializerException(Exception):
pass


class AddressDeserializationException(AddressSerializerException):
"""
Exception thrown when deserialization of address data fails.
"""
pass


class AbstractAddressSerializer(ABC):
"""
Class used to serialize and deserialize addresses.
"""

@abstractmethod
def encrypt(self) -> bytes:
def serialize(self, address: Address) -> bytes:
"""
Encrypts address data.
Returns:
bytes: Encrypted address data.
Serialize address data. Output format depends on the implementation.
"""
pass

@classmethod
@abstractmethod
def decrypt(cls, encrypted_data: bytes) -> Address:
def deserialize(self, serialized_data: bytes) -> Address:
"""
Create address from encrypted address data.
Deserialize address data. Throws AddressDeserializationException if data format is not recognized.
Args:
encrypted_data: Encrypted address data.
Returns:
Address: Created address.
serialized_data: Data serialized before by serialize method.
"""
pass


class DefaultAddressSerializer(AbstractAddressSerializer):
"""
Address serializer implementation which serialize address to string.
"""

encoding: str

def __init__(self, encoding: str = "utf-8"):
"""
Args:
encoding: Encoding used for transforming generated address string to bytes.
"""
self.encoding = encoding

def serialize(self, address: Address) -> bytes:
assert address.address.find(":") == -1, "Address should not contain colon character"
address_str: str = f"{address.address_type.value}:{address.address}:{address.port}:{address.address_id}"
return address_str.encode(self.encoding)

def deserialize(self, serialized_data: bytes) -> Address:
try:
address_str: str = serialized_data.decode(self.encoding)
parts = address_str.split(":")
if len(parts) < 4:
raise AddressDeserializationException(f"Invalid number of parts, address_str='{address_str}'")
address_type: AddressType = AddressType(parts[0])
address: str = parts[1]
port: int = int(parts[2])
address_id: str = ":".join(parts[3:]) # there can possibly be some colons in address_id
return Address(address_id=address_id, address_type=address_type, address=address, port=port)
except Exception as e:
raise AddressDeserializationException(f"Failed to deserialize address, error='{e}'") from e
Loading

0 comments on commit a50bff9

Please sign in to comment.