OMG IT WORKED run Graph.py, transcript in output.csv

This commit is contained in:
Brandon Goodell 2018-02-08 17:39:55 -07:00
parent 956c59ad24
commit 5372a2f609
4 changed files with 65 additions and 45 deletions

View file

@ -43,9 +43,11 @@ class Blockchain(object):
for ident in self.leaves: for ident in self.leaves:
tempCumDiff = 0.0 tempCumDiff = 0.0
thisBlockIdent = ident thisBlockIdent = ident
if self.blocks[thisBlockIdent].diff is not None:
tempCumDiff += self.blocks[thisBlockIdent].diff tempCumDiff += self.blocks[thisBlockIdent].diff
while self.blocks[thisBlockIdent].parent is not None: while self.blocks[thisBlockIdent].parent is not None:
thisBlockIdent = self.blocks[thisBlockIdent].parent thisBlockIdent = self.blocks[thisBlockIdent].parent
if self.blocks[thisBLockIdent].diff is not None:
tempCumDiff += self.blocks[thisBlockIdent].diff tempCumDiff += self.blocks[thisBlockIdent].diff
if tempCumDiff > maxCumDiff: if tempCumDiff > maxCumDiff:
# If more than one leaf ties for maxCumDiff, each node in the # If more than one leaf ties for maxCumDiff, each node in the

View file

@ -29,6 +29,7 @@ class Edge(object):
class Test_Edge(unittest.TestCase): class Test_Edge(unittest.TestCase):
def test_e(self): def test_e(self):
params = None
nelly = Node(params) nelly = Node(params)
milly = Node(params) milly = Node(params)
ed = Edge(params) ed = Edge(params)
@ -36,5 +37,5 @@ class Test_Edge(unittest.TestCase):
self.assertEqual(len(self.nodes),2) self.assertEqual(len(self.nodes),2)
suite = unittest.TestLoader().loadTestsFromTestCase(Test_Edge) #suite = unittest.TestLoader().loadTestsFromTestCase(Test_Edge)
unittest.TextTestRunner(verbosity=1).run(suite) #unittest.TextTestRunner(verbosity=1).run(suite)

View file

@ -29,11 +29,13 @@ class Graph(object):
self.globalTime = deepcopy(self.startTime) self.globalTime = deepcopy(self.startTime)
self.birthRate = params[7] self.birthRate = params[7]
self.deathRate = params[8] self.deathRate = params[8]
self.data = params[9] self.filename = params[9]
self.data = params[10]
self.blankBlockchain = Blockchain() self.blankBlockchain = Blockchain()
self.blankBlockchain.targetRate = self.data["targetRate"] self.blankBlockchain.targetRate = self.targetRate
self.blankBlockchain.mode = self.data["mode"] self.blankBlockchain.mode = self.mode
self.blankBlockchain.diff = 1.0
self._createInit() self._createInit()
@ -41,28 +43,28 @@ class Graph(object):
def _createInit(self): def _createInit(self):
# For simplicity, all nodes will have a genesis block with t=0.0 and no offset # For simplicity, all nodes will have a genesis block with t=0.0 and no offset
for i in range(self.numInitNodes): for i in range(self.numInitNodes):
offset = newOffset() offset = newOffset(None)
intens = newIntensity() intens = newIntensity(None)
name = newIdent(len(self.nodes)) name = newIdent(len(self.nodes))
dat = {"offset":offset, "intensity":intens, "blockchain":deepcopy(self.blankBlockchain)} dat = {"offset":offset, "intensity":intens, "blockchain":deepcopy(self.blankBlockchain)}
params = {"ident":name, "data":dat, "verbose":self.verbosity, "mode":self.mode, "targetRate":self.targetRate} params = {"ident":name, "data":dat, "verbose":self.verbosity, "mode":self.mode, "targetRate":self.targetRate}
nelly = Node(params) nelly = Node(params)
self.nodes.update({nelly.ident:nelly}) self.nodes.update({nelly.ident:nelly})
t = self.startTime t = self.startTime
self.nodes[nelly.ident].generateBlock(t, i) self.nodes[nelly.ident].generateBlock(t)
touched = {} touched = {}
for xNode in self.nodes: for xNode in self.nodes:
for yNode in self.nodes: for yNode in self.nodes:
notSameNode = (xNode != yNode) notSameNode = (xNode != yNode)
xNodeHasRoom = (len(self.nodes[xNode].edges) < self.maxNeighbors) xNodeHasRoom = (len(self.nodes[xNode].edges) < self.maxNeighbors)
yNodeHasRoom = (len(self.ndoes[yNode].edges) < self.maxNeighbors) yNodeHasRoom = (len(self.nodes[yNode].edges) < self.maxNeighbors)
xyNotTouched = ((xNode, yNode) not in touched) xyNotTouched = ((xNode, yNode) not in touched)
yxNotTouched = ((yNode, xNode) not in touched) yxNotTouched = ((yNode, xNode) not in touched)
if notSameNode and xNodeHasRoom and yNodeHasRoom and xyNotTouched and yxNotTouched: if notSameNode and xNodeHasRoom and yNodeHasRoom and xyNotTouched and yxNotTouched:
touched.update({(xNode,yNode):True, (yNode,xNode):True}) touched.update({(xNode,yNode):True, (yNode,xNode):True})
if random.random() < self.probEdge: if random.random() < self.probEdge:
params = [newIdent(len(self.edges)), {}, self.verbosity] params = [newIdent(len(self.edges)), {"pendingBlocks":{}, "length":random.random()}, self.verbosity]
ed = Edge(params) ed = Edge(params)
ed.nodes.update({xNode:self.nodes[xNode], yNode:self.nodes[yNode]}) ed.nodes.update({xNode:self.nodes[xNode], yNode:self.nodes[yNode]})
self.edges.update({ed.ident:ed}) self.edges.update({ed.ident:ed})
@ -76,13 +78,13 @@ class Graph(object):
for xNode in self.nodes: for xNode in self.nodes:
xNodeHasRoom = (len(self.nodes[xNode].edges) < self.maxNeighbors) xNodeHasRoom = (len(self.nodes[xNode].edges) < self.maxNeighbors)
iStillHasRoom = (len(neighbors) < self.maxNeighbors) iStillHasRoom = (len(neighbors) < self.maxNeighbors)
if xNodeHasRoom and and iStillHasRoom and random.random() < self.probEdge: if xNodeHasRoom and iStillHasRoom and random.random() < self.probEdge:
neighbors.append(xNode) neighbors.append(xNode)
newNodeName = newIdent(len(self.nodes)) newNodeName = newIdent(len(self.nodes))
offset = newOffset() offset = newOffset(None)
intens = newIntensity() intens = newIntensity(None)
dat = {"offset":offset, "intensity":intens, "blockchain":deepcopy(self.blankBlockchain)} dat = {"offset":offset, "intensity":intens, "blockchain":deepcopy(self.blankBlockchain)}
params = {"ident":newNodeName, "data":dat, "verbose":self.verbosity, "mode":self.mode, "targetRate":self.targetRate} params = {"ident":newNodeName, "data":dat, "verbose":self.verbosity, "mode":self.mode, "targetRate":self.targetRate}
newNode = Node(params) newNode = Node(params)
@ -108,7 +110,8 @@ class Graph(object):
leaver = self.nodes[leaverIdent] leaver = self.nodes[leaverIdent]
neighbors = [] neighbors = []
for ed in leaver.edges: for ed in leaver.edges:
neighbors.append((ed.Ident, ed.getNeighbor(leaverIdent))) edge = leaver.edges[ed]
neighbors.append((edge.ident, edge.getNeighbor(leaverIdent)))
for neighbor in neighbors: for neighbor in neighbors:
edIdent = neighbor[0] edIdent = neighbor[0]
neiIdent = neighbor[1] neiIdent = neighbor[1]
@ -125,19 +128,24 @@ class Graph(object):
self.nodes[discoIdent].propagate(t, blockIdent) self.nodes[discoIdent].propagate(t, blockIdent)
return out return out
def eventBlockArrival(self, destNodeIdent, edgeIdent, blockIdent, t): def eventBlockArrival(self, pendingIdent, edgeIdent, t):
out = str(t) + ",blockArriv," + str(destNodeIdent) + "," + str(edgeIdent) + "," + str(blockIdent) + "," out = str(t) + ",blockArriv,"
destNode = self.nodes[destNodeIdent]
edge = self.edges[edgeIdent] edge = self.edges[edgeIdent]
block = deepcopy(edge.data["pendingBlocks"][blockIdent]) pendingData = edge.data["pendingBlocks"][pendingIdent] # pendingDat = {"timeOfArrival":timeOfArrival, "destIdent":otherIdent, "block":blockToProp}
block.arrivTimestamp = t + self.nodes[destNodeIdent].data["offset"] out += str(pendingData["destIdent"]) + "," + str(edgeIdent) + "," + str(pendingData["block"].ident)
self.nodes[destNodeIdent].updateBlockchain({blockIdent:block}) destNode = self.nodes[pendingData["destIdent"]]
edge = self.edges[edgeIdent]
block = deepcopy(pendingData["block"])
block.arrivTimestamp = t + destNode.data["offset"]
destNode.updateBlockchain({block.ident:block})
del edge.data["pendingBlocks"][pendingIdent]
return out return out
def go(self): def go(self):
with open(self.filename,"w") as writeFile: with open(self.filename,"w") as writeFile:
writeFile.write("timestamp,eventId,eventData\n") writeFile.write("timestamp,eventId,eventData\n")
eventType = None
while self.globalTime - self.startTime< self.runTime: while self.globalTime - self.startTime< self.runTime:
u = -1.0*math.log(1.0-random.random())/self.birthRate u = -1.0*math.log(1.0-random.random())/self.birthRate
eventType = ("nodeJoins", None) eventType = ("nodeJoins", None)
@ -158,30 +166,39 @@ class Graph(object):
edge = self.edges[edgeIdent] edge = self.edges[edgeIdent]
pB = edge.data["pendingBlocks"] pB = edge.data["pendingBlocks"]
for pendingIdent in pB: for pendingIdent in pB:
pendingData = pB[pendingIdent] # pendingDat = {"timeOfArrival":timeOfArrival, "destIdent":otherIdent, "block":blockToProp} pendingData = pB[pendingIdent]
if pendingData["timeOfArrival"] - self.globalTime < u: if pendingData["timeOfArrival"] - self.globalTime < u:
eventTime = ("blockArriv", (pendingData["destIdent"], edgeIdent, pendingData["block"])) eventType = ("blockArriv", pendingIdent, edgeIdent)
u = v u = v
assert eventType is not None
self.globalTime += u self.globalTime += u
out = "" out = ""
if eventTime[0] == "nodeJoins": if eventType[0] == "nodeJoins":
out = self.eventNodeJoins(self.globalTime) out = self.eventNodeJoins(self.globalTime)
elif eventTime[0] == "nodeLeaves": elif eventType[0] == "nodeLeaves":
out = self.eventNodeLeaves(self.globalTime) out = self.eventNodeLeaves(self.globalTime)
elif eventTime[0] == "blockDisco": elif eventType[0] == "blockDisco":
out = self.eventBlockDiscovery(eventTime[1], self.globalTime) out = self.eventBlockDiscovery(eventType[1], self.globalTime)
elif eventTime[0] == "blockArriv": elif eventType[0] == "blockArriv":
out = self.eventBlockArrival(eventTime[1], eventTime[2], eventTime[3], self.globalTime) out = self.eventBlockArrival(eventType[1], eventType[2], self.globalTime)
else: else:
print("WHAAAA") print("WHAAAA")
with open(self.filename, "a") as writeFile: with open(self.filename, "a") as writeFile:
writeFile.write(out + "\n") writeFile.write(out + "\n")
mode = "Nakamoto"
targetRate = 1.0/600000.0
numInitNodes = 10
maxNeighbors = 8
probEdge = 0.1
verbosity = True
startTime = time.time()
runTime = 10.0
globalTime = startTime
birthRate = 1.0/10.0
deathRate = 0.99*1.0/10.0
filename = "output.csv"
greg = Graph([mode, targetRate, numInitNodes, maxNeighbors, probEdge, verbosity, runTime, birthRate, deathRate, filename, []])
greg.go()

View file

@ -29,9 +29,9 @@ class Node(object):
t = discoTime t = discoTime
s = t+self.data["offset"] s = t+self.data["offset"]
diff = self.data["blockchain"].diff diff = self.data["blockchain"].diff
params = {"ident":name, "disco":t, "arriv":s, "parent":None, "diff":diff} params = {"ident":newName, "disco":t, "arriv":s, "parent":None, "diff":diff}
newBlock = Block(params) newBlock = Block(params)
self.data["blockchain"].addBlock(newBlock, mode, tr) self.data["blockchain"].addBlock(newBlock)
return newName return newName
def updateBlockchain(self, incBlocks): def updateBlockchain(self, incBlocks):
@ -43,7 +43,7 @@ class Node(object):
if key in self.data["blockchain"].blocks: if key in self.data["blockchain"].blocks:
del tempData[key] del tempData[key]
elif incBlocks[key].parent in self.data["blockchain"].blocks or incBlocks[key].parent is None: elif incBlocks[key].parent in self.data["blockchain"].blocks or incBlocks[key].parent is None:
self.data["blockchain"].addBlock(incBlocks[key], self.mode, self.targetRate) self.data["blockchain"].addBlock(incBlocks[key])
del tempData[key] del tempData[key]
incBlocks = deepcopy(tempData) incBlocks = deepcopy(tempData)
while len(incBlocks)>0: while len(incBlocks)>0:
@ -58,13 +58,13 @@ class Node(object):
def propagate(self, timeOfProp, blockIdent): def propagate(self, timeOfProp, blockIdent):
for edgeIdent in self.edges: for edgeIdent in self.edges:
edge = self.edges[edgeIdent] edge = self.edges[edgeIdent]
length = e.data["length"] length = edge.data["length"]
timeOfArrival = timeOfProp + length timeOfArrival = timeOfProp + length
otherIdent = e.getNeighbor(self.ident) otherIdent = edge.getNeighbor(self.ident)
other = e.nodes[otherIdent] other = edge.nodes[otherIdent]
bc = other.data["blockchain"] bc = other.data["blockchain"]
if blockIdent not in bc.blocks: if blockIdent not in bc.blocks:
pB = e.data["pendingBlocks"] pB = edge.data["pendingBlocks"]
pendingIdent = newIdent(len(pB)) pendingIdent = newIdent(len(pB))
mybc = self.data["blockchain"] mybc = self.data["blockchain"]
blockToProp = mybc.blocks[blockIdent] blockToProp = mybc.blocks[blockIdent]