Spaces:
Runtime error
Runtime error
Delete Blockchain.py
Browse files- Blockchain.py +0 -113
Blockchain.py
DELETED
@@ -1,113 +0,0 @@
|
|
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 |
-
for id, mem in self.user_gpus.items():
|
22 |
-
self.user_wallets[id] += mem//2
|
23 |
-
|
24 |
-
block = {
|
25 |
-
'index': len(self.chain) + 1,
|
26 |
-
'timestamp': time(),
|
27 |
-
'transactions': self.current_transactions,
|
28 |
-
'previous_hash': previous_hash or self.hash(self.chain[-1]),
|
29 |
-
}
|
30 |
-
|
31 |
-
# ํ์ฌ ํธ๋์ญ์
์ด๊ธฐํ
|
32 |
-
self.current_transactions = []
|
33 |
-
|
34 |
-
# ๋ธ๋ก์ ์ฒด์ธ์ ์ถ๊ฐ
|
35 |
-
self.chain.append(block)
|
36 |
-
return block
|
37 |
-
|
38 |
-
def new_transaction(self, id, kind, data):
|
39 |
-
"""
|
40 |
-
์๋ก์ด ํธ๋์ญ์
์์ฑ ๋ฐ ์ถ๊ฐ
|
41 |
-
id: ์์ฒญ์
|
42 |
-
kind: ์์ฒญ ์ข
๋ฅ (inference, add, out)
|
43 |
-
data: inference ์ [์
๋ ฅ prompt, output], peer add ์ gpu mem, out ์ ๋ํ gpu mem
|
44 |
-
|
45 |
-
return: ํด๋น ํธ๋์ญ์
์ ํฌํจํ ๋ธ๋ก์ ์ธ๋ฑ์ค
|
46 |
-
"""
|
47 |
-
|
48 |
-
transaction = {
|
49 |
-
'id': id,
|
50 |
-
'kind': kind,
|
51 |
-
'data': data,
|
52 |
-
}
|
53 |
-
self.current_transactions.append(transaction)
|
54 |
-
|
55 |
-
# ๊ฐ ์ ์ ์ ํ๋๋ ฅ ์
๋ฐ์ดํธ
|
56 |
-
if kind == "inference":
|
57 |
-
if id not in self.user_wallets:
|
58 |
-
# ์๋ก์ด ์ ์ ์ธ ๊ฒฝ์ฐ ์ด๊ธฐ ํ๋๋ ฅ ์ค์
|
59 |
-
self.user_wallets[id] = 10 # ์ด๊ธฐ ํ๋๋ ฅ์ 10์ผ๋ก ์ค์
|
60 |
-
self.user_wallets[id] -= 1
|
61 |
-
else:
|
62 |
-
# inference ์์ฒญ ์ ์ฐจ๊ฐ
|
63 |
-
self.user_wallets[id] -= 1
|
64 |
-
if self.user_wallets[id]<0:
|
65 |
-
self.user_wallets[id] = 0
|
66 |
-
elif kind == "add":
|
67 |
-
if id not in self.user_gpus:
|
68 |
-
self.user_gpus[id] = int(data)
|
69 |
-
else:
|
70 |
-
self.user_gpus[id] += int(data)
|
71 |
-
elif kind == "out":
|
72 |
-
if id in self.user_gpus:
|
73 |
-
del(self.user_gpus[id])
|
74 |
-
|
75 |
-
return self.last_block['index'] + 1
|
76 |
-
|
77 |
-
def get_user_balance(self, id):
|
78 |
-
# ํน์ ์ ์ ์ ํ๋๋ ฅ์ ์กฐํ
|
79 |
-
return self.user_wallets.get(id, 10)
|
80 |
-
|
81 |
-
def get_user_gpu_mem(self, id):
|
82 |
-
# ํน์ ์ ์ ์ ๊ธฐ์ฌ gpu๋ฅผ ์กฐํ
|
83 |
-
return self.user_gpus.get(id, 0)
|
84 |
-
|
85 |
-
def get_total_gpu_mem(self):
|
86 |
-
# ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ ๋ฐํ
|
87 |
-
result = 0
|
88 |
-
for mem in self.user_gpus.values():
|
89 |
-
result += int(mem)
|
90 |
-
return result
|
91 |
-
|
92 |
-
def get_total_coin(self):
|
93 |
-
# ์ ์ฒด ์ฝ์ธ(ํ๋๋ ฅ) ์ ๋ฐํ
|
94 |
-
result = 0
|
95 |
-
for coin in self.user_wallets.values():
|
96 |
-
result += int(coin)
|
97 |
-
return result
|
98 |
-
@property
|
99 |
-
def last_block(self):
|
100 |
-
"""
|
101 |
-
์ฒด์ธ์ ๋ง์ง๋ง ๋ธ๋ก ๋ฐํ
|
102 |
-
"""
|
103 |
-
return self.chain[-1]
|
104 |
-
|
105 |
-
@staticmethod
|
106 |
-
def hash(block):
|
107 |
-
"""
|
108 |
-
๋ธ๋ก์ SHA-256์ผ๋ก ํด์ฑ
|
109 |
-
:param block: ๋ธ๋ก
|
110 |
-
:return: ํด์ ๊ฐ
|
111 |
-
"""
|
112 |
-
block_string = json.dumps(block, sort_keys=True).encode()
|
113 |
-
return hashlib.sha256(block_string).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|