-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (55 loc) · 2.24 KB
/
main.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
import hashlib
import math
import random
import json
TRANSACTIONS_PER_BLOCK = 10
BLOCK_DIFFICULTY = 2 ** 6 # block difficulty is expressed linearly
BLOCK_REWARD = 100
thisWallet= random.randint(0,2**31)
print("this wallet: ", thisWallet)
def get_ledger_data():
with open('ledger.json') as json_file:
return json.load(json_file)
ledger = get_ledger_data()
blocks = [] if not len(ledger) else ledger
def solve_hash(blockData):
n = random.randint(0, 1000000000) # each miner randomizes start point, making expected value linear to hashing power regardless of computational power per client
zeros = math.log(BLOCK_DIFFICULTY, 2)
wholeZeros = math.floor(zeros - zeros % 1)
extraRequired = hex(round(0xF * (zeros % 1)))
start_n = n
res = hashlib.sha384((blockData + str(n)).encode("utf-8")).hexdigest()
remainder = 0xF
while res[:wholeZeros] != "0" * wholeZeros or remainder < extraRequired:
res = hashlib.sha384((blockData + str(n)).encode("utf-8")).hexdigest()
n += 1
remainder = hex(int(res[wholeZeros : wholeZeros + 1], 16))
if not (n-start_n) % 100000:
current_public_ledger = get_ledger_data()
if len(blocks) != len(current_public_ledger): # Another client found hash before, abondon
return -1, -1
print(f"found solution after {n-start_n} iterations")
return n, res
key = "0" if not len(ledger) else str(ledger[len(ledger)-1]["blockNumber"])
blockdata = "{ blockNumber: " + str(int(key) + 1) + ", prevBlock: " + str(key) + "}"
ledger = open("ledger.json")
# blocks
for i in range(10):
nonce, solution = solve_hash(str(blockdata))
if nonce == -1:
print("ANOTHER CLINET FOUND BLOCK, SEARCHING FOR NEXT BLOCK")
blocks = get_ledger_data()
continue
key = solution
transactions = [{"from": 0, "to": thisWallet, "amount": BLOCK_REWARD}]
blockdata = {
"blockNumber": len(blocks),
"hash": key,
"prevBlockHash": blocks[i - 1]["hash"] if len(blocks) else "0",
"nonce": nonce,
"transactions": transactions,
}
blocks.append(blockdata)
json_object = json.dumps(blocks, indent=4)
with open("ledger.json", "w") as outfile:
outfile.write(json_object)