-
Notifications
You must be signed in to change notification settings - Fork 51
/
Main.py
92 lines (77 loc) · 4.02 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
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
from InputsConfig import InputsConfig as p
from Event import Event, Queue
from Scheduler import Scheduler
from Statistics import Statistics
if p.model == 3:
from Models.AppendableBlock.BlockCommit import BlockCommit
from Models.Consensus import Consensus
from Models.AppendableBlock.Transaction import FullTransaction as FT
from Models.AppendableBlock.Node import Node
from Models.Incentives import Incentives
from Models.AppendableBlock.Statistics import Statistics
from Models.AppendableBlock.Verification import Verification
elif p.model == 2:
from Models.Ethereum.BlockCommit import BlockCommit
from Models.Ethereum.Consensus import Consensus
from Models.Ethereum.Transaction import LightTransaction as LT, FullTransaction as FT
from Models.Ethereum.Node import Node
from Models.Ethereum.Incentives import Incentives
elif p.model == 1:
from Models.Bitcoin.BlockCommit import BlockCommit
from Models.Bitcoin.Consensus import Consensus
from Models.Transaction import LightTransaction as LT, FullTransaction as FT
from Models.Bitcoin.Node import Node
from Models.Incentives import Incentives
elif p.model == 0:
from Models.BlockCommit import BlockCommit
from Models.Consensus import Consensus
from Models.Transaction import LightTransaction as LT, FullTransaction as FT
from Models.Node import Node
from Models.Incentives import Incentives
########################################################## Start Simulation ##############################################################
def main():
for i in range(p.Runs):
clock = 0 # set clock to 0 at the start of the simulation
if p.hasTrans:
if p.Ttechnique == "Light":
LT.create_transactions() # generate pending transactions
elif p.Ttechnique == "Full":
FT.create_transactions() # generate pending transactions
Node.generate_gensis_block() # generate the gensis block for all miners
# initiate initial events >= 1 to start with
BlockCommit.generate_initial_events()
while not Queue.isEmpty() and clock <= p.simTime:
next_event = Queue.get_next_event()
clock = next_event.time # move clock to the time of the event
BlockCommit.handle_event(next_event)
Queue.remove_event(next_event)
# for the AppendableBlock process transactions and
# optionally verify the model implementation
if p.model == 3:
BlockCommit.process_gateway_transaction_pools()
if i == 0 and p.VerifyImplemetation:
Verification.perform_checks()
Consensus.fork_resolution() # apply the longest chain to resolve the forks
# distribute the rewards between the particiapting nodes
Incentives.distribute_rewards()
# calculate the simulation results (e.g., block statstics and miners' rewards)
Statistics.calculate()
if p.model == 3:
Statistics.print_to_excel(i, True)
Statistics.reset()
else:
########## reset all global variable before the next run #############
Statistics.reset() # reset all variables used to calculate the results
Node.resetState() # reset all the states (blockchains) for all nodes in the network
fname = "(Allverify)1day_{0}M_{1}K.xlsx".format(
p.Bsize/1000000, p.Tn/1000)
# print all the simulation results in an excel file
Statistics.print_to_excel(fname)
fname = "(Allverify)1day_{0}M_{1}K.xlsx".format(
p.Bsize/1000000, p.Tn/1000)
# print all the simulation results in an excel file
Statistics.print_to_excel(fname)
Statistics.reset2() # reset profit results
######################################################## Run Main method #####################################################################
if __name__ == '__main__':
main()