2022-11-30 14:28:27 +00:00
|
|
|
#!/bin/python
|
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
import networkx as nx
|
2023-01-23 17:04:54 +00:00
|
|
|
import logging, random
|
2023-03-14 10:16:43 +00:00
|
|
|
from functools import partial, partialmethod
|
2022-11-30 14:28:27 +00:00
|
|
|
from datetime import datetime
|
2023-02-24 09:21:28 +00:00
|
|
|
from statistics import mean
|
2022-11-30 14:28:27 +00:00
|
|
|
from DAS.tools import *
|
2023-01-16 21:43:52 +00:00
|
|
|
from DAS.results import *
|
2022-11-30 14:28:27 +00:00
|
|
|
from DAS.observer import *
|
|
|
|
from DAS.validator import *
|
|
|
|
|
|
|
|
class Simulator:
|
2023-02-15 14:06:42 +00:00
|
|
|
"""This class implements the main DAS simulator."""
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-02-23 11:16:43 +00:00
|
|
|
def __init__(self, shape, config):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes the simulation with a set of parameters (shape)."""
|
2023-01-23 17:04:54 +00:00
|
|
|
self.shape = shape
|
2023-03-06 14:15:57 +00:00
|
|
|
self.config = config
|
2022-11-30 14:28:27 +00:00
|
|
|
self.format = {"entity": "Simulator"}
|
2023-01-23 17:04:54 +00:00
|
|
|
self.result = Result(self.shape)
|
2023-02-08 19:10:26 +00:00
|
|
|
self.validators = []
|
|
|
|
self.logger = []
|
2023-02-23 11:16:43 +00:00
|
|
|
self.logLevel = config.logLevel
|
2023-02-08 19:10:26 +00:00
|
|
|
self.proposerID = 0
|
|
|
|
self.glob = []
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-03-20 10:29:06 +00:00
|
|
|
# In GossipSub the initiator might push messages without participating in the mesh.
|
|
|
|
# proposerPublishOnly regulates this behavior. If set to true, the proposer is not
|
|
|
|
# part of the p2p distribution graph, only pushes segments to it. If false, the proposer
|
|
|
|
# might get back segments from other peers since links are symmetric.
|
|
|
|
self.proposerPublishOnly = True
|
|
|
|
|
|
|
|
# If proposerPublishOnly == True, this regulates how many copies of each segment are
|
|
|
|
# pushed out by the proposer.
|
|
|
|
# 1: the data is sent out exactly once on rows and once on columns (2 copies in total)
|
|
|
|
# self.shape.netDegree: default behavior similar (but not same) to previous code
|
|
|
|
self.proposerPublishTo = self.shape.netDegree
|
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
def initValidators(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes all the validators in the network."""
|
2023-01-23 17:04:54 +00:00
|
|
|
self.glob = Observer(self.logger, self.shape)
|
2022-11-30 14:28:27 +00:00
|
|
|
self.validators = []
|
2023-03-06 14:15:57 +00:00
|
|
|
if self.config.evenLineDistribution:
|
2023-03-21 14:16:19 +00:00
|
|
|
|
|
|
|
lightVal = int(self.shape.numberNodes * self.shape.class1ratio * self.shape.vpn1)
|
|
|
|
heavyVal = int(self.shape.numberNodes * (1-self.shape.class1ratio) * self.shape.vpn2)
|
|
|
|
totalValidators = lightVal + heavyVal
|
|
|
|
rows = list(range(self.shape.blockSize)) * (int(totalValidators/self.shape.blockSize)+1)
|
|
|
|
columns = list(range(self.shape.blockSize)) * (int(totalValidators/self.shape.blockSize)+1)
|
|
|
|
offset = heavyVal*self.shape.chi
|
2023-03-06 14:15:57 +00:00
|
|
|
random.shuffle(rows)
|
|
|
|
random.shuffle(columns)
|
2023-03-13 14:03:55 +00:00
|
|
|
for i in range(self.shape.numberNodes):
|
2023-03-06 14:15:57 +00:00
|
|
|
if self.config.evenLineDistribution:
|
2023-03-21 14:16:19 +00:00
|
|
|
if i < int(heavyVal/self.shape.vpn2): # First start with the heavy nodes
|
|
|
|
start = i *self.shape.chi*self.shape.vpn2
|
|
|
|
end = (i+1)*self.shape.chi*self.shape.vpn2
|
|
|
|
else: # Then the solo stakers
|
|
|
|
j = i - int(heavyVal/self.shape.vpn2)
|
|
|
|
start = offset+( j *self.shape.chi)
|
|
|
|
end = offset+((j+1)*self.shape.chi)
|
|
|
|
r = rows[start:end]
|
|
|
|
c = columns[start:end]
|
2023-03-23 19:10:27 +00:00
|
|
|
val = Validator(i, int(not i!=0), self.logger, self.shape, r, c)
|
2023-03-06 14:15:57 +00:00
|
|
|
else:
|
|
|
|
val = Validator(i, int(not i!=0), self.logger, self.shape)
|
2022-11-30 14:28:27 +00:00
|
|
|
if i == self.proposerID:
|
|
|
|
val.initBlock()
|
|
|
|
else:
|
|
|
|
val.logIDs()
|
|
|
|
self.validators.append(val)
|
2023-03-21 14:16:19 +00:00
|
|
|
self.logger.debug("Validators initialized.", extra=self.format)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-23 17:04:54 +00:00
|
|
|
def initNetwork(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes the simulated network."""
|
2023-01-23 17:04:54 +00:00
|
|
|
rowChannels = [[] for i in range(self.shape.blockSize)]
|
|
|
|
columnChannels = [[] for i in range(self.shape.blockSize)]
|
2022-12-20 10:13:54 +00:00
|
|
|
for v in self.validators:
|
2023-02-23 19:58:16 +00:00
|
|
|
if not (self.proposerPublishOnly and v.amIproposer):
|
|
|
|
for id in v.rowIDs:
|
|
|
|
rowChannels[id].append(v)
|
|
|
|
for id in v.columnIDs:
|
|
|
|
columnChannels[id].append(v)
|
2022-12-20 10:13:54 +00:00
|
|
|
|
2023-03-21 14:16:19 +00:00
|
|
|
# Check rows/columns distribution
|
|
|
|
#totalR = 0
|
|
|
|
#totalC = 0
|
|
|
|
#for r in rowChannels:
|
|
|
|
# totalR += len(r)
|
|
|
|
#for c in columnChannels:
|
|
|
|
# totalC += len(c)
|
|
|
|
|
2023-01-23 17:04:54 +00:00
|
|
|
for id in range(self.shape.blockSize):
|
|
|
|
|
2023-02-15 01:50:03 +00:00
|
|
|
# If the number of nodes in a channel is smaller or equal to the
|
|
|
|
# requested degree, a fully connected graph is used. For n>d, a random
|
|
|
|
# d-regular graph is set up. (For n=d+1, the two are the same.)
|
2023-03-20 16:09:17 +00:00
|
|
|
if not rowChannels[id]:
|
|
|
|
self.logger.error("No nodes for row %d !" % id, extra=self.format)
|
|
|
|
continue
|
|
|
|
elif (len(rowChannels[id]) <= self.shape.netDegree):
|
2023-02-15 01:50:03 +00:00
|
|
|
self.logger.debug("Graph fully connected with degree %d !" % (len(rowChannels[id]) - 1), extra=self.format)
|
|
|
|
G = nx.complete_graph(len(rowChannels[id]))
|
|
|
|
else:
|
|
|
|
G = nx.random_regular_graph(self.shape.netDegree, len(rowChannels[id]))
|
2022-12-20 10:13:54 +00:00
|
|
|
if not nx.is_connected(G):
|
2023-01-16 21:43:52 +00:00
|
|
|
self.logger.error("Graph not connected for row %d !" % id, extra=self.format)
|
2022-12-20 10:13:54 +00:00
|
|
|
for u, v in G.edges:
|
|
|
|
val1=rowChannels[id][u]
|
|
|
|
val2=rowChannels[id][v]
|
2023-02-15 02:10:55 +00:00
|
|
|
val1.rowNeighbors[id].update({val2.ID : Neighbor(val2, 0, self.shape.blockSize)})
|
|
|
|
val2.rowNeighbors[id].update({val1.ID : Neighbor(val1, 0, self.shape.blockSize)})
|
2023-01-23 17:04:54 +00:00
|
|
|
|
2023-03-20 16:09:17 +00:00
|
|
|
if not columnChannels[id]:
|
|
|
|
self.logger.error("No nodes for column %d !" % id, extra=self.format)
|
|
|
|
continue
|
|
|
|
elif (len(columnChannels[id]) <= self.shape.netDegree):
|
2023-02-15 01:50:03 +00:00
|
|
|
self.logger.debug("Graph fully connected with degree %d !" % (len(columnChannels[id]) - 1), extra=self.format)
|
|
|
|
G = nx.complete_graph(len(columnChannels[id]))
|
|
|
|
else:
|
|
|
|
G = nx.random_regular_graph(self.shape.netDegree, len(columnChannels[id]))
|
2022-12-20 10:13:54 +00:00
|
|
|
if not nx.is_connected(G):
|
2023-01-16 21:43:52 +00:00
|
|
|
self.logger.error("Graph not connected for column %d !" % id, extra=self.format)
|
2022-12-20 10:13:54 +00:00
|
|
|
for u, v in G.edges:
|
|
|
|
val1=columnChannels[id][u]
|
|
|
|
val2=columnChannels[id][v]
|
2023-02-15 02:10:55 +00:00
|
|
|
val1.columnNeighbors[id].update({val2.ID : Neighbor(val2, 1, self.shape.blockSize)})
|
|
|
|
val2.columnNeighbors[id].update({val1.ID : Neighbor(val1, 1, self.shape.blockSize)})
|
2022-12-20 10:13:54 +00:00
|
|
|
|
2023-02-23 19:58:16 +00:00
|
|
|
for v in self.validators:
|
|
|
|
if (self.proposerPublishOnly and v.amIproposer):
|
|
|
|
for id in v.rowIDs:
|
|
|
|
count = min(self.proposerPublishTo, len(rowChannels[id]))
|
|
|
|
publishTo = random.sample(rowChannels[id], count)
|
|
|
|
for vi in publishTo:
|
|
|
|
v.rowNeighbors[id].update({vi.ID : Neighbor(vi, 0, self.shape.blockSize)})
|
|
|
|
for id in v.columnIDs:
|
|
|
|
count = min(self.proposerPublishTo, len(columnChannels[id]))
|
|
|
|
publishTo = random.sample(columnChannels[id], count)
|
|
|
|
for vi in publishTo:
|
|
|
|
v.columnNeighbors[id].update({vi.ID : Neighbor(vi, 1, self.shape.blockSize)})
|
2022-12-20 10:13:54 +00:00
|
|
|
|
2023-02-15 01:51:47 +00:00
|
|
|
if self.logger.isEnabledFor(logging.DEBUG):
|
2023-03-13 14:03:55 +00:00
|
|
|
for i in range(0, self.shape.numberNodes):
|
2023-02-15 01:51:47 +00:00
|
|
|
self.logger.debug("Val %d : rowN %s", i, self.validators[i].rowNeighbors, extra=self.format)
|
|
|
|
self.logger.debug("Val %d : colN %s", i, self.validators[i].columnNeighbors, extra=self.format)
|
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
def initLogger(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes the logger."""
|
2023-03-14 10:16:43 +00:00
|
|
|
logging.TRACE = 5
|
|
|
|
logging.addLevelName(logging.TRACE, 'TRACE')
|
|
|
|
logging.Logger.trace = partialmethod(logging.Logger.log, logging.TRACE)
|
|
|
|
logging.trace = partial(logging.log, logging.TRACE)
|
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
logger = logging.getLogger("DAS")
|
2023-03-03 10:26:00 +00:00
|
|
|
if len(logger.handlers) == 0:
|
|
|
|
logger.setLevel(self.logLevel)
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(self.logLevel)
|
|
|
|
ch.setFormatter(CustomFormatter())
|
|
|
|
logger.addHandler(ch)
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger = logger
|
|
|
|
|
|
|
|
def run(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It runs the main simulation until the block is available or it gets stucked."""
|
2022-11-30 14:28:27 +00:00
|
|
|
self.glob.checkRowsColumns(self.validators)
|
2022-12-20 10:13:54 +00:00
|
|
|
self.validators[self.proposerID].broadcastBlock()
|
|
|
|
arrived, expected = self.glob.checkStatus(self.validators)
|
|
|
|
missingSamples = expected - arrived
|
2023-01-16 21:43:52 +00:00
|
|
|
missingVector = []
|
2023-01-13 15:51:27 +00:00
|
|
|
steps = 0
|
2023-01-26 00:12:13 +00:00
|
|
|
while(True):
|
2023-01-16 21:43:52 +00:00
|
|
|
missingVector.append(missingSamples)
|
2022-11-30 14:28:27 +00:00
|
|
|
oldMissingSamples = missingSamples
|
2023-02-15 01:55:06 +00:00
|
|
|
self.logger.debug("PHASE SEND %d" % steps, extra=self.format)
|
2023-03-13 14:03:55 +00:00
|
|
|
for i in range(0,self.shape.numberNodes):
|
2023-01-26 13:29:12 +00:00
|
|
|
self.validators[i].send()
|
2023-02-15 01:55:06 +00:00
|
|
|
self.logger.debug("PHASE RECEIVE %d" % steps, extra=self.format)
|
2023-03-13 14:03:55 +00:00
|
|
|
for i in range(1,self.shape.numberNodes):
|
2022-12-20 10:13:54 +00:00
|
|
|
self.validators[i].receiveRowsColumns()
|
2023-02-15 01:55:06 +00:00
|
|
|
self.logger.debug("PHASE RESTORE %d" % steps, extra=self.format)
|
2023-03-13 14:03:55 +00:00
|
|
|
for i in range(1,self.shape.numberNodes):
|
2022-12-07 14:46:45 +00:00
|
|
|
self.validators[i].restoreRows()
|
|
|
|
self.validators[i].restoreColumns()
|
2023-02-15 01:55:06 +00:00
|
|
|
self.logger.debug("PHASE LOG %d" % steps, extra=self.format)
|
2023-03-13 14:03:55 +00:00
|
|
|
for i in range(0,self.shape.numberNodes):
|
2022-11-30 14:28:27 +00:00
|
|
|
self.validators[i].logRows()
|
|
|
|
self.validators[i].logColumns()
|
2023-02-24 09:21:28 +00:00
|
|
|
|
|
|
|
# log TX and RX statistics
|
|
|
|
statsTxInSlot = [v.statsTxInSlot for v in self.validators]
|
|
|
|
statsRxInSlot = [v.statsRxInSlot for v in self.validators]
|
2023-03-21 14:16:19 +00:00
|
|
|
self.logger.debug("step %d: TX_prod=%.1f, RX_prod=%.1f, TX_avg=%.1f, TX_max=%.1f, Rx_avg=%.1f, Rx_max=%.1f" %
|
2023-02-24 09:21:28 +00:00
|
|
|
(steps, statsTxInSlot[0], statsRxInSlot[0],
|
|
|
|
mean(statsTxInSlot[1:]), max(statsTxInSlot[1:]),
|
|
|
|
mean(statsRxInSlot[1:]), max(statsRxInSlot[1:])), extra=self.format)
|
2023-03-13 14:03:55 +00:00
|
|
|
for i in range(0,self.shape.numberNodes):
|
2023-01-25 23:34:21 +00:00
|
|
|
self.validators[i].updateStats()
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
arrived, expected = self.glob.checkStatus(self.validators)
|
|
|
|
missingSamples = expected - arrived
|
2023-01-11 16:20:19 +00:00
|
|
|
missingRate = missingSamples*100/expected
|
2023-01-16 21:43:52 +00:00
|
|
|
self.logger.debug("step %d, missing %d of %d (%0.02f %%)" % (steps, missingSamples, expected, missingRate), extra=self.format)
|
2022-11-30 14:28:27 +00:00
|
|
|
if missingSamples == oldMissingSamples:
|
2023-02-15 14:06:42 +00:00
|
|
|
self.logger.debug("The block cannot be recovered, failure rate %d!" % self.shape.failureRate, extra=self.format)
|
2023-01-25 20:51:59 +00:00
|
|
|
missingVector.append(missingSamples)
|
2022-11-30 14:28:27 +00:00
|
|
|
break
|
|
|
|
elif missingSamples == 0:
|
2023-01-25 20:51:59 +00:00
|
|
|
#self.logger.info("The entire block is available at step %d, with failure rate %d !" % (steps, self.shape.failureRate), extra=self.format)
|
|
|
|
missingVector.append(missingSamples)
|
2022-11-30 14:28:27 +00:00
|
|
|
break
|
|
|
|
else:
|
2023-01-13 15:51:27 +00:00
|
|
|
steps += 1
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-25 20:51:59 +00:00
|
|
|
self.result.populate(self.shape, missingVector)
|
|
|
|
return self.result
|
2022-11-30 14:28:27 +00:00
|
|
|
|