-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
104 lines (78 loc) · 2.28 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from datetime import datetime
import hashlib
import pandas as pd
import random
from requests import get
class user:
def __init__(self):
#self.ip=get('https://api.ipify.org').text
self.peers=[]
self.coin=0
self.choices = ['R','S','P']
self.combination = ''
def addPeer(self,port):
if port in range(1024,65500):
self.peers.append(port)
self.peers.sort()
return True
return False
def calcCoin(self,CPU,RAM):
Rfile=pd.read_csv("ram.csv",index_col=0)
Cfile=pd.read_csv("cpu.csv",index_col=0)
self.coin=Rfile.loc[RAM][0]+Cfile.loc[CPU][0]
return self.coin
def genComb(self,co):
for _ in range(co):
self.combination+=random.choice(self.choices)
return self.combination
class block:
def __init__(self,timestamp,data,bhash,lastHash):
self.timestamp=timestamp
self.data=data
self.bhash=bhash
self.lastHash=lastHash
@staticmethod
def hashfunc(dataInput):
return hashlib.sha256(str.encode(dataInput)).hexdigest()
@classmethod
def genGenesis(cls):
return cls('1','gendata',cls.hashfunc('1'+'0000'+'gendata'),'0000')
@property
def display(self):
return {
"timestamp": self.timestamp,
"data": self.data,
"bhash": self.bhash,
"lastHash": self.lastHash
}
class blockchain:
def __init__(self):
self.chain = [block.genGenesis()]
self.user=user()
def addBlock(self,data):
timestamp = datetime.now()
lastHash = self.chain[len(self.chain)-1].bhash
bhash = block.hashfunc(str(timestamp)+lastHash+data)
nblock = block(timestamp,data,bhash,lastHash)
self.chain.append(nblock)
def displayChain(self):
return { i: self.chain[i].display for i in range(len(self.chain)) }
def RSP(self,rCom,combination):
wins=0
for (i,j) in zip(rCom,combination):
if i==j:
wins+=1
return wins
@staticmethod
def isValid(chain):
test = block.genGenesis()
if chain[0].bhash!=test.bhash:
return False
for i in range(len(chain)-1):
if chain[i].bhash!=chain[i+1].lastHash:
return False
if chain[i].bhash!=block.hashfunc(str(chain[i].timestamp)+chain[i].lastHash+chain[i].data):
return False
if chain[-1].bhash!=block.hashfunc(str(chain[-1].timestamp)+chain[-1].lastHash+chain[-1].data):
return False
return True