Spaces:
Sleeping
Sleeping
Create BlockChain_base.py
Browse files- BlockChain_base.py +101 -0
BlockChain_base.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
import json
|
| 3 |
+
from time import time
|
| 4 |
+
|
| 5 |
+
class Blockchain:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.chain = []
|
| 8 |
+
self.current_transactions = []
|
| 9 |
+
self.user_wallets = {}
|
| 10 |
+
self.user_gpus = {}
|
| 11 |
+
|
| 12 |
+
# Genesis ๋ธ๋ก ์์ฑ
|
| 13 |
+
self.new_block(previous_hash="1")
|
| 14 |
+
|
| 15 |
+
def new_block(self, previous_hash=None):
|
| 16 |
+
"""
|
| 17 |
+
๋ธ๋ก์ฒด์ธ์ ์๋ก์ด ๋ธ๋ก ์ถ๊ฐ
|
| 18 |
+
:param previous_hash: ์ด์ ๋ธ๋ก์ ํด์ ๊ฐ
|
| 19 |
+
:return: ์๋ก ์์ฑ๋ ๋ธ๋ก
|
| 20 |
+
"""
|
| 21 |
+
block = {
|
| 22 |
+
'index': len(self.chain) + 1,
|
| 23 |
+
'timestamp': time(),
|
| 24 |
+
'transactions': self.current_transactions,
|
| 25 |
+
'previous_hash': previous_hash or self.hash(self.chain[-1]),
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# ํ์ฌ ํธ๋์ญ์
์ด๊ธฐํ
|
| 29 |
+
self.current_transactions = []
|
| 30 |
+
|
| 31 |
+
# ๋ธ๋ก์ ์ฒด์ธ์ ์ถ๊ฐ
|
| 32 |
+
self.chain.append(block)
|
| 33 |
+
return block
|
| 34 |
+
|
| 35 |
+
def new_transaction(self, id, kind, data):
|
| 36 |
+
"""
|
| 37 |
+
์๋ก์ด ํธ๋์ญ์
์์ฑ ๋ฐ ์ถ๊ฐ
|
| 38 |
+
id: ์์ฒญ์
|
| 39 |
+
kind: ์์ฒญ ์ข
๋ฅ (inference, add, out)
|
| 40 |
+
data: inference ์ [์
๋ ฅ prompt, output], peer add ์ gpu mem, out ์ ๋ํ gpu mem
|
| 41 |
+
|
| 42 |
+
return: ํด๋น ํธ๋์ญ์
์ ํฌํจํ ๋ธ๋ก์ ์ธ๋ฑ์ค
|
| 43 |
+
"""
|
| 44 |
+
transaction = {
|
| 45 |
+
'id': id,
|
| 46 |
+
'kind': kind,
|
| 47 |
+
'data': data,
|
| 48 |
+
}
|
| 49 |
+
self.current_transactions.append(transaction)
|
| 50 |
+
|
| 51 |
+
# ๊ฐ ์ ์ ์ ํ๋๋ ฅ ์
๋ฐ์ดํธ
|
| 52 |
+
if kind == "inference":
|
| 53 |
+
if id not in self.user_wallets:
|
| 54 |
+
# ์๋ก์ด ์ ์ ์ธ ๊ฒฝ์ฐ ์ด๊ธฐ ํ๋๋ ฅ ์ค์
|
| 55 |
+
self.user_wallets[id] = 10 # ์ด๊ธฐ ํ๋๋ ฅ์ 10์ผ๋ก ์ค์
|
| 56 |
+
else:
|
| 57 |
+
# inference ์์ฒญ ์ ์ฐจ๊ฐ
|
| 58 |
+
self.user_wallets[id] -= 1
|
| 59 |
+
elif kind == "add":
|
| 60 |
+
if id not in self.user_gpus:
|
| 61 |
+
self.user_gpus[id] = int(data)
|
| 62 |
+
else:
|
| 63 |
+
self.user_gpus[id] += int(data)
|
| 64 |
+
elif kind == "out":
|
| 65 |
+
if id in self.user_gpus:
|
| 66 |
+
self.user_gpus[id] -= int(data)
|
| 67 |
+
if self.user_gpus[id] < 0:
|
| 68 |
+
del(self.user_gpus[id])
|
| 69 |
+
|
| 70 |
+
return self.last_block['index'] + 1
|
| 71 |
+
|
| 72 |
+
def get_user_balance(self, id):
|
| 73 |
+
# ํน์ ์ ์ ์ ํ๋๋ ฅ์ ์กฐํ
|
| 74 |
+
return self.user_wallets.get(id, 0)
|
| 75 |
+
|
| 76 |
+
def get_user_gpu_mem(self, id):
|
| 77 |
+
# ํน์ ์ ์ ์ ๊ธฐ์ฌ gpu๋ฅผ ์กฐํ
|
| 78 |
+
return self.user_gpus.get(id, 0)
|
| 79 |
+
|
| 80 |
+
def get_total_gpu_mem(self):
|
| 81 |
+
# ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ ๋ฐํ
|
| 82 |
+
result = 0
|
| 83 |
+
for mem in self.user_gpus.values():
|
| 84 |
+
result += int(mem)
|
| 85 |
+
return result
|
| 86 |
+
@property
|
| 87 |
+
def last_block(self):
|
| 88 |
+
"""
|
| 89 |
+
์ฒด์ธ์ ๋ง์ง๋ง ๋ธ๋ก ๋ฐํ
|
| 90 |
+
"""
|
| 91 |
+
return self.chain[-1]
|
| 92 |
+
|
| 93 |
+
@staticmethod
|
| 94 |
+
def hash(block):
|
| 95 |
+
"""
|
| 96 |
+
๋ธ๋ก์ SHA-256์ผ๋ก ํด์ฑ
|
| 97 |
+
:param block: ๋ธ๋ก
|
| 98 |
+
:return: ํด์ ๊ฐ
|
| 99 |
+
"""
|
| 100 |
+
block_string = json.dumps(block, sort_keys=True).encode()
|
| 101 |
+
return hashlib.sha256(block_string).hexdigest()
|