Skip to content

Commit

Permalink
Merge branch 'dev' into release/v1.7.5
Browse files Browse the repository at this point in the history
  • Loading branch information
nauttiilus authored Jan 29, 2025
2 parents 84aaea2 + 1053809 commit c94fefc
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 132 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ target/
profile_default/
ipython_config.py

# wandb
wandb/

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
Expand Down Expand Up @@ -253,3 +256,6 @@ cert/

# wandb
wandb/

# neural internet register api
neurons/register-api/
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.20.0
hooks:
- id: commitlint
stages: [commit-msg]
additional_dependencies: ['@commitlint/config-angular']
23 changes: 15 additions & 8 deletions cert/gen_ca.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ echo "2.2 Use the server private key to generate a certificate generation reques
openssl req -new -key server.key -out server.req -sha256 -subj "/C=US/ST=NY/CN=server.neuralinternet.ai/O=NI"

echo "2.3 Use the certificate generation request and the CA cert to generate the server cert."
openssl x509 -req -in server.req -CA ca.cer -CAkey ca.key -CAcreateserial -set_serial 100 -days "$ca_cert_expire_days" -outform PEM -passin pass:"$pem_password" -out server.cer -sha256 -extensions v3_req -extfile <(
cat << EOF
# Create a temporary extensions file
cat << EOF > extfile.cnf
[ v3_req ]
subjectAltName = @alt_names
Expand All @@ -35,13 +35,16 @@ IP.1 = 127.0.0.1
IP.2 = 0.0.0.0
IP.3 = "$local_ip"
EOF
)

openssl x509 -req -in server.req -CA ca.cer -CAkey ca.key -CAcreateserial -set_serial 100 -days "$ca_cert_expire_days" -outform PEM -passin pass:"$pem_password" -out server.cer -sha256 -extensions v3_req -extfile extfile.cnf

# Remove the temporary extensions file
rm extfile.cnf

echo "2.4 Convert the cer to PEM CRT format"
openssl x509 -inform PEM -in server.cer -out server.crt

echo "2.5 Clean up now that the cert has been created, we no longer need the request"
echo "2.5 Clean up - now that the cert has been created, we no longer need the request"
rm server.req

#for frontend server
Expand All @@ -52,8 +55,8 @@ echo "3.2 Use the client private key to generate a certificate generation reques
openssl req -new -key client.key -out client.req -subj "/C=US/ST=NY/CN=client.neuralinternet.ai/O=NI"

echo "3.3 Use the certificate generation request and the CA cert to generate the client cert."
openssl x509 -req -in client.req -CA ca.cer -CAkey ca.key -CAcreateserial -set_serial 101 -days "$ca_cert_expire_days" -outform PEM -out client.cer -passin pass:"$pem_password" -extensions v3_req -extfile <(
cat << EOF
# Create a temporary extensions file
cat << EOF > extfile.cnf
[ v3_req ]
subjectAltName = @alt_names
Expand All @@ -62,13 +65,17 @@ IP.1 = 127.0.0.1
IP.2 = 0.0.0.0
IP.3 = "$local_ip"
EOF
)

openssl x509 -req -in client.req -CA ca.cer -CAkey ca.key -CAcreateserial -set_serial 101 -days "$ca_cert_expire_days" -outform PEM -out client.cer -passin pass:"$pem_password" -extensions v3_req -extfile extfile.cnf

# Remove the temporary extensions file
rm extfile.cnf

echo "3.4 Convert the client certificate and private key to pkcs#12 format for use by browsers."
openssl pkcs12 -export -inkey client.key -in client.cer -out client.p12 -passout pass:"$pem_password"

echo "3.5. Convert the cer to PEM CRT format"
openssl x509 -inform PEM -in client.cer -out client.crt

echo "3.6. Clean up now that the cert has been created, we no longer need the request."
echo "3.6. Clean up - now that the cert has been created, we no longer need the request."
rm client.req
1 change: 1 addition & 0 deletions commitlint-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {extends: ['@commitlint/config-conventional']}
6 changes: 3 additions & 3 deletions compute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import string

# Define the version of the template module.
__version__ = "1.7.5"
__minimal_miner_version__ = "1.6.0"
__minimal_validator_version__ = "1.7.5"
__version__ = "1.8.0"
__minimal_miner_version__ = "1.8.0"
__minimal_validator_version__ = "1.8.0"

version_split = __version__.split(".")
__version_as_int__ = (100 * int(version_split[0])) + (10 * int(version_split[1])) + (1 * int(version_split[2]))
Expand Down
18 changes: 10 additions & 8 deletions compute/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import bittensor.utils.networking as net
import time
import uvicorn
from bittensor import axon, subtensor
from bittensor.axon import FastAPIThreadedServer, AxonMiddleware
from bittensor import axon
from bittensor.core.subtensor import Subtensor as subtensor

from bittensor.core.axon import FastAPIThreadedServer, AxonMiddleware
from fastapi import FastAPI, APIRouter
from rich.prompt import Confirm
from starlette.requests import Request
Expand All @@ -39,7 +41,7 @@


def serve_extrinsic(
subtensor: "bittensor.subtensor",
subtensor: "bittensor.core.subtensor",
wallet: "bittensor.wallet",
ip: str,
port: int,
Expand Down Expand Up @@ -250,16 +252,16 @@ def __init__(
if config is None:
config = axon.config()
config = copy.deepcopy(config)
config.axon.ip = ip or config.axon.get("ip", bittensor.defaults.axon.ip)
config.axon.port = port or config.axon.get("port", bittensor.defaults.axon.port)
config.axon.ip = ip or config.axon.get("ip", bittensor.core.settings.DEFAULTS.axon.ip)
config.axon.port = port or config.axon.get("port", bittensor.core.settings.DEFAULTS.axon.port)
config.axon.external_ip = external_ip or config.axon.get(
"external_ip", bittensor.defaults.axon.external_ip
"external_ip", bittensor.core.settings.DEFAULTS.axon.external_ip
)
config.axon.external_port = external_port or config.axon.get(
"external_port", bittensor.defaults.axon.external_port
"external_port", bittensor.core.settings.DEFAULTS.axon.external_port
)
config.axon.max_workers = max_workers or config.axon.get(
"max_workers", bittensor.defaults.axon.max_workers
"max_workers", bittensor.core.settings.DEFAULTS.axon.max_workers
)
axon.check_config(config)
self.config = config
Expand Down
6 changes: 3 additions & 3 deletions compute/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import bittensor.utils.networking as net

import compute
import inspect


def prometheus_extrinsic(
subtensor: "bittensor.subtensor",
subtensor: "bittensor.core.subtensor.Subtensor.MockSubtensor",
wallet: "bittensor.wallet",
port: int,
netuid: int,
Expand Down Expand Up @@ -99,9 +100,8 @@ def prometheus_extrinsic(

# Add netuid, not in prometheus_info
call_params["netuid"] = netuid

bittensor.logging.info("Serving prometheus on: {}:{} ...".format(subtensor.network, netuid))
success, err = subtensor._do_serve_prometheus(
success, err = subtensor.do_serve_prometheus(
wallet=wallet,
call_params=call_params,
wait_for_finalization=wait_for_finalization,
Expand Down
1 change: 1 addition & 0 deletions compute/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Allocate(bt.Synapse):
docker_action: dict = {
"action": "",
"ssh_key": "",
"key_type": "",
}

def deserialize(self) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion compute/utils/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from compute.utils.cache import ttl_cache

bt_blocktime = bt.__blocktime__
bt_blocktime = bt.BLOCKTIME


@ttl_cache(maxsize=1, ttl=bt_blocktime)
Expand Down
174 changes: 119 additions & 55 deletions neurons/Miner/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,83 +373,147 @@ def build_sample_container():
bt.logging.info(f"Error build sample container {e}")
return {"status": False}

def retrieve_allocation_key():
try:
file_path = 'allocation_key'
# Open the file in read mode ('r') and read the data
with open(file_path, 'r') as file:
allocation_key_encoded = file.read()

# Decode the base64-encoded public key from the file
allocation_key = base64.b64decode(allocation_key_encoded).decode('utf-8')
return allocation_key
except Exception as e:
bt.logging.info(f"Error retrieving allocation key.")
return None

def restart_container():
def restart_container(public_key:str):
try:
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
# stop and remove the container by using the SIGTERM signal to PID 1 (init) process in the container
if running_container.status == "running":
running_container.exec_run(cmd="kill -15 1")
running_container.wait()
running_container.restart()
return {"status": True}
else:
bt.logging.info("No running container.")
allocation_key = retrieve_allocation_key()
if allocation_key is None:
bt.logging.info("Failed to retrieve allocation key.")
return {"status": False}
# compare public_key to the local saved allocation key for security
if allocation_key.strip() == public_key.strip():
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
# stop and remove the container by using the SIGTERM signal to PID 1 (init) process in the container
if running_container.status == "running":
running_container.exec_run(cmd="kill -15 1")
running_container.wait()
running_container.restart()
return {"status": True}
else:
bt.logging.info("No running container.")
return {"status": False}
else:
bt.logging.info(f"Permission denied.")
return {"status":False}
except Exception as e:
bt.logging.info(f"Error restart container: {e}")
return {"status": False}

def pause_container():
def pause_container(public_key:str):
try:
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
running_container.pause()
return {"status": True}
else:
bt.logging.info("Unable to find container")
allocation_key = retrieve_allocation_key()
if allocation_key is None:
bt.logging.info("Failed to retrieve allocation key.")
return {"status": False}
# compare public_key to the local saved allocation key for security
if allocation_key.strip() == public_key.strip():
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
running_container.pause()
return {"status": True}
else:
bt.logging.info("Unable to find container")
return {"status": False}
else:
bt.logging.info(f"Permission denied.")
return {"status:": False}
except Exception as e:
bt.logging.info(f"Error pausing container {e}")
return {"status": False}

def unpause_container():
def unpause_container(public_key:str):
try:
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
running_container.unpause()
return {"status": True}
allocation_key = retrieve_allocation_key()
if allocation_key is None:
bt.logging.info("Failed to retrieve allocation key.")
return {"status": False}
# compare public_key to the local saved allocation key for security
if allocation_key.strip() == public_key.strip():
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
running_container.unpause()
return {"status": True}
else:
bt.logging.info("Unable to find container")
return {"status": False}
else:
bt.logging.info("Unable to find container")
bt.logging.info(f"Permission denied.")
return {"status": False}
except Exception as e:
bt.logging.info(f"Error unpausing container {e}")
return {"status": False}

def exchange_key_container(new_ssh_key: str):
def exchange_key_container(new_ssh_key: str, public_key: str, key_type: str = "user" ):
try:
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
# stop and remove the container by using the SIGTERM signal to PID 1 (init) process in the container
if running_container.status == "running":
running_container.exec_run(cmd=f"bash -c \"echo '{new_ssh_key}' > /root/.ssh/authorized_keys & sync & sleep 1\"")
running_container.exec_run(cmd="kill -15 1")
running_container.wait()
running_container.restart()
return {"status": True}
allocation_key = retrieve_allocation_key()
if allocation_key is None:
bt.logging.info("Failed to retrieve allocation key.")
return {"status": False}
# compare public_key to the local saved allocation key for security
if allocation_key.strip() == public_key.strip():
client, containers = get_docker()
running_container = None
for container in containers:
if container_name in container.name:
running_container = container
break
if running_container:
# stop and remove the container by using the SIGTERM signal to PID 1 (init) process in the container
if running_container.status == "running":
exist_key = running_container.exec_run(cmd="cat /root/.ssh/authorized_keys")
exist_key = exist_key.output.decode("utf-8").split("\n")
user_key = exist_key[0]
terminal_key = ""
if len(exist_key) > 1:
terminal_key = exist_key[1]
if key_type == "terminal":
terminal_key = new_ssh_key
elif key_type == "user":
user_key = new_ssh_key
else:
bt.logging.debug("Invalid key type to swap the SSH key")
return {"status": False}
key_list = user_key + "\n" + terminal_key
# bt.logging.debug(f"New SSH key: {key_list}")
running_container.exec_run(cmd=f"bash -c \"echo '{key_list}' > /root/.ssh/authorized_keys & sync & sleep 1\"")
running_container.exec_run(cmd="kill -15 1")
running_container.wait()
running_container.restart()
return {"status": True}
else:
bt.logging.info("Unable to find container")
return {"status": False}
else:
bt.logging.info("Unable to find container")
bt.logging.info(f"Permission denied.")
return {"status": False}
except Exception as e:
bt.logging.info(f"Error changing SSH key on container {e}")
Expand Down
Loading

0 comments on commit c94fefc

Please sign in to comment.