ldhldh commited on
Commit
751c006
ยท
1 Parent(s): 6331f0b

Delete Blockchain.py

Browse files
Files changed (1) hide show
  1. 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()