ldhldh commited on
Commit
140dac2
โ€ข
1 Parent(s): dd149cf

Create BlockChain_base.py

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