前のブロックのiter,hashoutをベースに iter%(2**delay) == 0 になるまで、ブロックごとに262144回sha256を計算して、hash値を求める。hashoutが決まっているので、先回りしてブロックを作れそうだが、SolanaはPoSのため、不正がバレるとstakingが没収されてしまう。なるほどねー
import datetime
import hashlib
import time
import os
import pathlib
import random
import string
from random import randint
iter=1
hashin = ''.join(random.choices(string.ascii_uppercase + string.digits, k = 10))
hashout = hashlib.sha256(hashin.encode()).hexdigest()
print(hashin)
print(hashout)
def datastream():
v1 = int(randint(1200, 1500))
v2 = int(randint(1300, 1700))
v3 = int(randint(1100, 1500))
v4 = int(randint(4000, 5600))
v5 = int(randint(4000, 5600))
v6 = int(randint(1900, 2400))
v7 = int(randint(1920, 2300))
v8 = int(randint(1850, 2200))
v9 = int(randint(1900, 2300))
v10 = int(randint(1800, 2200))
return [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10];
class Block:
blockNo = 0
count = iter
data = None
next = None
hash = "None"
previous_hash = "None"
timestamp = datetime.datetime.now()
def __init__(self, data):
self.data = datastream()
def hash(self):
file = pathlib.Path("TToken")
if file.exists():
tier=open("TToken").readline().rstrip()
else:
with open("benchmark.py") as infile:
exec(infile.read())
tier=open("TToken").readline().rstrip()
if tier=="T1":
h = hashlib.md5()
elif tier=="T2":
h = hashlib.sha1()
elif tier=="T3":
h = hashlib.blake2s()
elif tier=="T4":
h = hashlib.sha3_256()
h.update(
str(self.nonce).encode('utf-8') +
str(self.data).encode('utf-8') +
str(self.previous_hash).encode('utf-8') +
str(self.timestamp).encode('utf-8') +
str(self.blockNo).encode('utf-8')
)
return h.hexdigest()
block.blockNo = self.block.blockNo + 1
def __str__(self):
return "Block Number: " + str(self.blockNo) + "\nHistory Count: " + str(self.count) + "\nBlock Data: " + str(self.data) + "\nBlock Hash: " + str(self.hash) + "\nPrevious Hash: " + str(self.previous_hash) + "\n--------------"
class Blockchain:
block = Block("Genesis")
dummy = head = block
def add(self, block):
if (self.block.blockNo ==0):
block.previous_hash = "Origin"
else:
block.previous_hash = self.block.hash
block.blockNo = self.block.blockNo + 1
self.block.next = block
self.block = self.block.next
def mine(self, block):
global iter
global hashin
global hashout
delay = 18
cont = 1
while(cont == 1):
if int(iter%(2**delay) == 0):
block.count = iter
block.hash = hashout
self.add(block)
print(block)
iter += 1
hashin=hashout
hashout = hashlib.sha256(hashin.encode()).hexdigest()
cont = 0
break
else:
iter += 1
hashin=hashout
hashout = hashlib.sha256(hashin.encode()).hexdigest()
t_initial = time.perf_counter()
blockchain = Blockchain()
b=int(input('Enter the number of Blocks for this simulation:'))
for n in range(b):
blockchain.mine(Block(n+1))
t_final = time.perf_counter()
delta_t = t_final - t_initial
delta_unit = delta_t*1000/b
print("comutation Time per Block(ms):"+str(delta_unit))
input('Press ENTER to exit')
Enter the number of Blocks for this simulation:3
Block Number: 1
History Count: 262144
Block Data: [1297, 1642, 1372, 5138, 5301, 2188, 1998, 2150, 1914, 1862]
Block Hash: a53e39cef8be27ba38e73fe216fbfc2efc63bca056fa2a6a18380e9d93c98ea3
Previous Hash: Origin
————–
Block Number: 2
History Count: 524288
Block Data: [1211, 1633, 1307, 4757, 5133, 2206, 2032, 1891, 2257, 2139]
Block Hash: 79561ebd2627b432d1e619dee9db7ac85593a4357925827754b1faefd42c1b72
Previous Hash: a53e39cef8be27ba38e73fe216fbfc2efc63bca056fa2a6a18380e9d93c98ea3
————–
Block Number: 3
History Count: 786432
Block Data: [1459, 1682, 1131, 5339, 4983, 2057, 1948, 2192, 2017, 2076]
Block Hash: d33e10fa10273b5d64ccdad34ffcbaae7673cb785807c49f199b204a148e6cd9
Previous Hash: 79561ebd2627b432d1e619dee9db7ac85593a4357925827754b1faefd42c1b72
————–
comutation Time per Block(ms):795.6611973543962
Press ENTER to exit