GitHub Action
commited on
Commit
·
451ae3d
1
Parent(s):
5de0c3e
Sync from GitHub with Git LFS
Browse files- agents/bootstrap.txt +4 -4
- agents/examples/bootstrap.txt +4 -4
- agents/init.py +30 -19
- agents/peer_sync.py +42 -52
agents/bootstrap.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
DID: "did:hmp:
|
| 2 |
-
DID: "did:hmp:
|
| 3 |
-
DID: "did:hmp:
|
| 4 |
-
DID: "did:hmp:
|
|
|
|
| 1 |
+
DID: "did:hmp:167cfb6f-a919-4504-afe4-238b672bbae6"; NAME: "Agent_108"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEACV9h3qLWlm0IJItkEQvn5D22Q+O8Uq1I6LdtCwnK/zo=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.118.43:8000", "nonce": 16325, "pow_hash": "000099808f6f01e383c1f8fdc4bff8fa131c0682261739a03ff51da492ab1b68", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.118.43:8000", "nonce": 153671, "pow_hash": "0000090379d407e0ffa6c5f94af4609205946ab2fc7eca40c8b3fd820949feac", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.118.134:8000", "nonce": 138266, "pow_hash": "00007a9f6b0f7bf9d32cfab0a08211a6a60b819eac2ceb898397c75cb963b806", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.118.134:8000", "nonce": 91766, "pow_hash": "00002a5aa289255baeb7e621a0349b653b452455021239be068f5effc52e259e", "difficulty": 4, "expires": ""}];
|
| 2 |
+
DID: "did:hmp:1fd7668d-a9bf-46e5-8ae5-ac4b8df769c6"; NAME: "Agent_112"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAw/vQaBUqK5Ozuq9FYHfsW0cjdmvgrg0olmaHG04I+ZU=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.121.43:8000", "nonce": 40411, "pow_hash": "000030ca9d258d3ea1bb04d207b9642b0114d6c05df3294e70b63aa5123c49f9", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.121.43:8000", "nonce": 46070, "pow_hash": "00000e85d20f871ce59f507a601a09b0e1c1148ba4f6eec70166abfd5451ca59", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.121.134:8000", "nonce": 62229, "pow_hash": "0000c5ba7bd99f5cd0b1baa5968e963f0675173b6af80badd2aeb544a3613d93", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.121.134:8000", "nonce": 21311, "pow_hash": "0000369df22355fbc5f9692e991c004f557f9f706089f74aa9b488d1cba43ef7", "difficulty": 4, "expires": ""}];
|
| 3 |
+
DID: "did:hmp:ad56773b-67a2-48ee-ae96-9883d5337d88"; NAME: "Agent_116"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAomr7cF1Q2qp2U6Ajnm+R/0REbv6NP8rjc7MD+SWxXd8=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.124.43:8000", "nonce": 182643, "pow_hash": "00004b9c2bf9cf6b49ca3513a4279d9dc0dfe09b6187e917ce141aafe128faf7", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.124.43:8000", "nonce": 69396, "pow_hash": "000076285f0a996e71fdad8baf4d3c79037d4139c452af9f9e25543be3701631", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.124.134:8000", "nonce": 269, "pow_hash": "00000d7b8d66e3d1a6f0450ba60f53dff580ba4ab281a3bcb16d7e3c666aaa1f", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.124.134:8000", "nonce": 32052, "pow_hash": "0000cc20a4a036840e6608aed26bcd75f2db4f829ef146922ff3d537f1625827", "difficulty": 4, "expires": ""}];
|
| 4 |
+
DID: "did:hmp:22bdf36b-9dd3-4c48-a421-793cfc03de56"; NAME: "Agent_119"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEACc1AdaXpvw6vGAZnZLSinmO0Ld2EFUuIQe0/exYj8Pg=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.132.43:8000", "nonce": 24431, "pow_hash": "0000931685439f7548098cd811729b982424c4aba35925e93d91da4a87264097", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.132.43:8000", "nonce": 118878, "pow_hash": "00003265d1849224450343ec7b1ec9802b0b9396b5c0578bf3f33e2991815fdb", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.132.134:8000", "nonce": 36224, "pow_hash": "0000717efdd28d16c60cf1229b00c6409a184a8cf7fd703d6586bac657c6fb27", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.132.134:8000", "nonce": 8486, "pow_hash": "0000f692cf2d79fcb97217a6d4b32b3044b8275102b6ee6c8c7fecc8cb1b814e", "difficulty": 4, "expires": ""}];
|
agents/examples/bootstrap.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
DID: "did:hmp:
|
| 2 |
-
DID: "did:hmp:
|
| 3 |
-
DID: "did:hmp:
|
| 4 |
-
DID: "did:hmp:
|
|
|
|
| 1 |
+
DID: "did:hmp:167cfb6f-a919-4504-afe4-238b672bbae6"; NAME: "Agent_108"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEACV9h3qLWlm0IJItkEQvn5D22Q+O8Uq1I6LdtCwnK/zo=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.118.43:8000", "nonce": 16325, "pow_hash": "000099808f6f01e383c1f8fdc4bff8fa131c0682261739a03ff51da492ab1b68", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.118.43:8000", "nonce": 153671, "pow_hash": "0000090379d407e0ffa6c5f94af4609205946ab2fc7eca40c8b3fd820949feac", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.118.134:8000", "nonce": 138266, "pow_hash": "00007a9f6b0f7bf9d32cfab0a08211a6a60b819eac2ceb898397c75cb963b806", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.118.134:8000", "nonce": 91766, "pow_hash": "00002a5aa289255baeb7e621a0349b653b452455021239be068f5effc52e259e", "difficulty": 4, "expires": ""}];
|
| 2 |
+
DID: "did:hmp:1fd7668d-a9bf-46e5-8ae5-ac4b8df769c6"; NAME: "Agent_112"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAw/vQaBUqK5Ozuq9FYHfsW0cjdmvgrg0olmaHG04I+ZU=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.121.43:8000", "nonce": 40411, "pow_hash": "000030ca9d258d3ea1bb04d207b9642b0114d6c05df3294e70b63aa5123c49f9", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.121.43:8000", "nonce": 46070, "pow_hash": "00000e85d20f871ce59f507a601a09b0e1c1148ba4f6eec70166abfd5451ca59", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.121.134:8000", "nonce": 62229, "pow_hash": "0000c5ba7bd99f5cd0b1baa5968e963f0675173b6af80badd2aeb544a3613d93", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.121.134:8000", "nonce": 21311, "pow_hash": "0000369df22355fbc5f9692e991c004f557f9f706089f74aa9b488d1cba43ef7", "difficulty": 4, "expires": ""}];
|
| 3 |
+
DID: "did:hmp:ad56773b-67a2-48ee-ae96-9883d5337d88"; NAME: "Agent_116"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAomr7cF1Q2qp2U6Ajnm+R/0REbv6NP8rjc7MD+SWxXd8=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.124.43:8000", "nonce": 182643, "pow_hash": "00004b9c2bf9cf6b49ca3513a4279d9dc0dfe09b6187e917ce141aafe128faf7", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.124.43:8000", "nonce": 69396, "pow_hash": "000076285f0a996e71fdad8baf4d3c79037d4139c452af9f9e25543be3701631", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.124.134:8000", "nonce": 269, "pow_hash": "00000d7b8d66e3d1a6f0450ba60f53dff580ba4ab281a3bcb16d7e3c666aaa1f", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.124.134:8000", "nonce": 32052, "pow_hash": "0000cc20a4a036840e6608aed26bcd75f2db4f829ef146922ff3d537f1625827", "difficulty": 4, "expires": ""}];
|
| 4 |
+
DID: "did:hmp:22bdf36b-9dd3-4c48-a421-793cfc03de56"; NAME: "Agent_119"; KEY: "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEACc1AdaXpvw6vGAZnZLSinmO0Ld2EFUuIQe0/exYj8Pg=\n-----END PUBLIC KEY-----\n"; ADDRESS: [{"addr": "tcp://95.59.132.43:8000", "nonce": 24431, "pow_hash": "0000931685439f7548098cd811729b982424c4aba35925e93d91da4a87264097", "difficulty": 4, "expires": ""}, {"addr": "udp://95.59.132.43:8000", "nonce": 118878, "pow_hash": "00003265d1849224450343ec7b1ec9802b0b9396b5c0578bf3f33e2991815fdb", "difficulty": 4, "expires": ""}, {"addr": "tcp://197.182.132.134:8000", "nonce": 36224, "pow_hash": "0000717efdd28d16c60cf1229b00c6409a184a8cf7fd703d6586bac657c6fb27", "difficulty": 4, "expires": ""}, {"addr": "udp://197.182.132.134:8000", "nonce": 8486, "pow_hash": "0000f692cf2d79fcb97217a6d4b32b3044b8275102b6ee6c8c7fecc8cb1b814e", "difficulty": 4, "expires": ""}];
|
agents/init.py
CHANGED
|
@@ -160,30 +160,41 @@ def update_pow_for_addresses(storage, difficulty=4):
|
|
| 160 |
|
| 161 |
enriched = []
|
| 162 |
for addr in addresses:
|
| 163 |
-
# если уже dict → PoW записан
|
| 164 |
if isinstance(addr, dict):
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
"nonce": nonce,
|
| 179 |
-
"
|
| 180 |
-
"difficulty": difficulty
|
| 181 |
-
|
| 182 |
-
|
| 183 |
|
| 184 |
storage.set_config(addr_key, json.dumps(enriched))
|
| 185 |
|
| 186 |
-
print("[+] Адреса обновлены с PoW
|
| 187 |
|
| 188 |
def init_prompts_and_ethics():
|
| 189 |
folder = os.path.dirname(__file__)
|
|
|
|
| 160 |
|
| 161 |
enriched = []
|
| 162 |
for addr in addresses:
|
|
|
|
| 163 |
if isinstance(addr, dict):
|
| 164 |
+
# уже в новом формате → оставляем как есть
|
| 165 |
+
if "addr" in addr and "pow_hash" in addr:
|
| 166 |
+
enriched.append(addr)
|
| 167 |
+
continue
|
| 168 |
+
# старый формат с "address"/"pow" → конвертим
|
| 169 |
+
if "address" in addr and "pow" in addr:
|
| 170 |
+
enriched.append({
|
| 171 |
+
"addr": addr["address"],
|
| 172 |
+
"nonce": addr["pow"].get("nonce"),
|
| 173 |
+
"pow_hash": addr["pow"].get("hash"),
|
| 174 |
+
"difficulty": addr["pow"].get("difficulty", difficulty),
|
| 175 |
+
"expires": addr.get("expires", "")
|
| 176 |
+
})
|
| 177 |
+
continue
|
| 178 |
+
|
| 179 |
+
# строка → нужно сгенерировать PoW
|
| 180 |
+
if isinstance(addr, str):
|
| 181 |
+
nonce, hash_value = storage.generate_pow(
|
| 182 |
+
peer_id=agent_id,
|
| 183 |
+
pubkey=pubkey,
|
| 184 |
+
address=addr,
|
| 185 |
+
difficulty=difficulty
|
| 186 |
+
)
|
| 187 |
+
enriched.append({
|
| 188 |
+
"addr": addr,
|
| 189 |
"nonce": nonce,
|
| 190 |
+
"pow_hash": hash_value,
|
| 191 |
+
"difficulty": difficulty,
|
| 192 |
+
"expires": ""
|
| 193 |
+
})
|
| 194 |
|
| 195 |
storage.set_config(addr_key, json.dumps(enriched))
|
| 196 |
|
| 197 |
+
print("[+] Адреса обновлены с PoW в унифицированном формате.")
|
| 198 |
|
| 199 |
def init_prompts_and_ethics():
|
| 200 |
folder = os.path.dirname(__file__)
|
agents/peer_sync.py
CHANGED
|
@@ -29,6 +29,8 @@ print(f"[PeerSync] Local ports: {local_ports}")
|
|
| 29 |
# ---------------------------
|
| 30 |
# Загрузка bootstrap
|
| 31 |
# ---------------------------
|
|
|
|
|
|
|
| 32 |
def load_bootstrap_peers(filename="bootstrap.txt"):
|
| 33 |
try:
|
| 34 |
with open(filename, "r", encoding="utf-8") as f:
|
|
@@ -42,76 +44,64 @@ def load_bootstrap_peers(filename="bootstrap.txt"):
|
|
| 42 |
if not line or line.startswith("#"):
|
| 43 |
continue
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
continue
|
| 59 |
-
|
|
|
|
| 60 |
try:
|
| 61 |
addresses = json.loads(addresses_json)
|
| 62 |
except Exception as e:
|
| 63 |
print(f"[Bootstrap] Failed to parse JSON addresses: {line} ({e})")
|
| 64 |
continue
|
| 65 |
-
rest = rest[addr_end:].strip()
|
| 66 |
-
|
| 67 |
-
# 3. pubkey (в кавычках)
|
| 68 |
-
pub_start = rest.find('"')
|
| 69 |
-
pub_end = rest.find('"', pub_start + 1)
|
| 70 |
-
if pub_start == -1 or pub_end == -1:
|
| 71 |
-
print(f"[Bootstrap] Invalid pubkey: {line}")
|
| 72 |
-
continue
|
| 73 |
-
pubkey = rest[pub_start + 1:pub_end].replace("\\n", "\n")
|
| 74 |
-
rest = rest[pub_end + 1:].strip()
|
| 75 |
-
|
| 76 |
-
# 4. pow_nonce
|
| 77 |
-
nonce_end = rest.find(" ")
|
| 78 |
-
if nonce_end == -1:
|
| 79 |
-
print(f"[Bootstrap] Invalid pow_nonce: {line}")
|
| 80 |
-
continue
|
| 81 |
-
try:
|
| 82 |
-
pow_nonce = int(rest[:nonce_end])
|
| 83 |
-
except ValueError:
|
| 84 |
-
print(f"[Bootstrap] Invalid pow_nonce: {rest[:nonce_end]} in line: {line}")
|
| 85 |
-
continue
|
| 86 |
-
rest = rest[nonce_end:].strip()
|
| 87 |
-
|
| 88 |
-
# 5. pow_hash (в кавычках)
|
| 89 |
-
if rest.startswith('"') and rest.endswith('"'):
|
| 90 |
-
pow_hash = rest[1:-1]
|
| 91 |
-
else:
|
| 92 |
-
print(f"[Bootstrap] Invalid pow_hash: {line}")
|
| 93 |
-
continue
|
| 94 |
|
| 95 |
-
#
|
| 96 |
expanded_addresses = []
|
| 97 |
for addr in addresses:
|
| 98 |
-
if addr
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
else:
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
|
|
|
| 105 |
storage.add_or_update_peer(
|
| 106 |
peer_id=did,
|
| 107 |
-
name=
|
| 108 |
addresses=expanded_addresses,
|
| 109 |
source="bootstrap",
|
| 110 |
status="offline",
|
| 111 |
pubkey=pubkey,
|
| 112 |
capabilities=None,
|
| 113 |
-
|
| 114 |
-
pow_hash=pow_hash
|
| 115 |
)
|
| 116 |
|
| 117 |
print(f"[Bootstrap] Loaded peer {did} -> {expanded_addresses}")
|
|
|
|
| 29 |
# ---------------------------
|
| 30 |
# Загрузка bootstrap
|
| 31 |
# ---------------------------
|
| 32 |
+
import json
|
| 33 |
+
|
| 34 |
def load_bootstrap_peers(filename="bootstrap.txt"):
|
| 35 |
try:
|
| 36 |
with open(filename, "r", encoding="utf-8") as f:
|
|
|
|
| 44 |
if not line or line.startswith("#"):
|
| 45 |
continue
|
| 46 |
|
| 47 |
+
# Разделяем строку на ключ:значение по ";"
|
| 48 |
+
parts = [p.strip() for p in line.split(";") if p.strip()]
|
| 49 |
+
data = {}
|
| 50 |
+
for part in parts:
|
| 51 |
+
if ":" not in part:
|
| 52 |
+
continue
|
| 53 |
+
key, val = part.split(":", 1)
|
| 54 |
+
key = key.strip().upper()
|
| 55 |
+
val = val.strip()
|
| 56 |
+
if val.startswith('"') and val.endswith('"'):
|
| 57 |
+
val = val[1:-1].replace("\\n", "\n")
|
| 58 |
+
data[key] = val
|
| 59 |
+
|
| 60 |
+
# Проверка обязательных полей
|
| 61 |
+
did = data.get("DID")
|
| 62 |
+
pubkey = data.get("KEY")
|
| 63 |
+
addresses_json = data.get("ADDRESS")
|
| 64 |
+
name = data.get("NAME")
|
| 65 |
+
|
| 66 |
+
if not did or not pubkey or not addresses_json:
|
| 67 |
+
print(f"[Bootstrap] Missing required fields in line: {line}")
|
| 68 |
continue
|
| 69 |
+
|
| 70 |
+
# Парсим адреса
|
| 71 |
try:
|
| 72 |
addresses = json.loads(addresses_json)
|
| 73 |
except Exception as e:
|
| 74 |
print(f"[Bootstrap] Failed to parse JSON addresses: {line} ({e})")
|
| 75 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Расширяем any:// в tcp/udp
|
| 78 |
expanded_addresses = []
|
| 79 |
for addr in addresses:
|
| 80 |
+
if isinstance(addr, dict):
|
| 81 |
+
addr_str = addr.get("address")
|
| 82 |
+
if addr_str.startswith("any://"):
|
| 83 |
+
hostport = addr_str[len("any://"):]
|
| 84 |
+
expanded_addresses.append({"address": f"tcp://{hostport}", **addr})
|
| 85 |
+
expanded_addresses.append({"address": f"udp://{hostport}", **addr})
|
| 86 |
+
else:
|
| 87 |
+
expanded_addresses.append(addr)
|
| 88 |
else:
|
| 89 |
+
if addr.startswith("any://"):
|
| 90 |
+
hostport = addr[len("any://"):]
|
| 91 |
+
expanded_addresses.extend([f"tcp://{hostport}", f"udp://{hostport}"])
|
| 92 |
+
else:
|
| 93 |
+
expanded_addresses.append(addr)
|
| 94 |
|
| 95 |
+
# Сохраняем в storage
|
| 96 |
storage.add_or_update_peer(
|
| 97 |
peer_id=did,
|
| 98 |
+
name=name,
|
| 99 |
addresses=expanded_addresses,
|
| 100 |
source="bootstrap",
|
| 101 |
status="offline",
|
| 102 |
pubkey=pubkey,
|
| 103 |
capabilities=None,
|
| 104 |
+
heard_from=None
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
print(f"[Bootstrap] Loaded peer {did} -> {expanded_addresses}")
|