From 5372a2f60931b40e502df7b458ffe26a8c3a91fe Mon Sep 17 00:00:00 2001 From: Brandon Goodell Date: Thu, 8 Feb 2018 17:39:55 -0700 Subject: [PATCH] OMG IT WORKED run Graph.py, transcript in output.csv --- source-code/Poisson-Graphs/Blockchain.py | 6 +- source-code/Poisson-Graphs/Edge.py | 5 +- source-code/Poisson-Graphs/Graph.py | 85 ++++++++++++++---------- source-code/Poisson-Graphs/Node.py | 14 ++-- 4 files changed, 65 insertions(+), 45 deletions(-) diff --git a/source-code/Poisson-Graphs/Blockchain.py b/source-code/Poisson-Graphs/Blockchain.py index 17808e3..ceacc94 100644 --- a/source-code/Poisson-Graphs/Blockchain.py +++ b/source-code/Poisson-Graphs/Blockchain.py @@ -43,10 +43,12 @@ class Blockchain(object): for ident in self.leaves: tempCumDiff = 0.0 thisBlockIdent = ident - tempCumDiff += self.blocks[thisBlockIdent].diff + if self.blocks[thisBlockIdent].diff is not None: + tempCumDiff += self.blocks[thisBlockIdent].diff while self.blocks[thisBlockIdent].parent is not None: thisBlockIdent = self.blocks[thisBlockIdent].parent - tempCumDiff += self.blocks[thisBlockIdent].diff + if self.blocks[thisBLockIdent].diff is not None: + tempCumDiff += self.blocks[thisBlockIdent].diff if tempCumDiff > maxCumDiff: # If more than one leaf ties for maxCumDiff, each node in the # network should pick one of these two arbitrarily. Since we diff --git a/source-code/Poisson-Graphs/Edge.py b/source-code/Poisson-Graphs/Edge.py index b3634f2..2861ab9 100644 --- a/source-code/Poisson-Graphs/Edge.py +++ b/source-code/Poisson-Graphs/Edge.py @@ -29,6 +29,7 @@ class Edge(object): class Test_Edge(unittest.TestCase): def test_e(self): + params = None nelly = Node(params) milly = Node(params) ed = Edge(params) @@ -36,5 +37,5 @@ class Test_Edge(unittest.TestCase): self.assertEqual(len(self.nodes),2) -suite = unittest.TestLoader().loadTestsFromTestCase(Test_Edge) -unittest.TextTestRunner(verbosity=1).run(suite) +#suite = unittest.TestLoader().loadTestsFromTestCase(Test_Edge) +#unittest.TextTestRunner(verbosity=1).run(suite) diff --git a/source-code/Poisson-Graphs/Graph.py b/source-code/Poisson-Graphs/Graph.py index 54e5645..efbbb15 100644 --- a/source-code/Poisson-Graphs/Graph.py +++ b/source-code/Poisson-Graphs/Graph.py @@ -29,11 +29,13 @@ class Graph(object): self.globalTime = deepcopy(self.startTime) self.birthRate = params[7] self.deathRate = params[8] - self.data = params[9] + self.filename = params[9] + self.data = params[10] self.blankBlockchain = Blockchain() - self.blankBlockchain.targetRate = self.data["targetRate"] - self.blankBlockchain.mode = self.data["mode"] + self.blankBlockchain.targetRate = self.targetRate + self.blankBlockchain.mode = self.mode + self.blankBlockchain.diff = 1.0 self._createInit() @@ -41,28 +43,28 @@ class Graph(object): def _createInit(self): # For simplicity, all nodes will have a genesis block with t=0.0 and no offset for i in range(self.numInitNodes): - offset = newOffset() - intens = newIntensity() + offset = newOffset(None) + intens = newIntensity(None) name = newIdent(len(self.nodes)) dat = {"offset":offset, "intensity":intens, "blockchain":deepcopy(self.blankBlockchain)} params = {"ident":name, "data":dat, "verbose":self.verbosity, "mode":self.mode, "targetRate":self.targetRate} nelly = Node(params) self.nodes.update({nelly.ident:nelly}) t = self.startTime - self.nodes[nelly.ident].generateBlock(t, i) + self.nodes[nelly.ident].generateBlock(t) touched = {} for xNode in self.nodes: for yNode in self.nodes: notSameNode = (xNode != yNode) 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) yxNotTouched = ((yNode, xNode) not in touched) if notSameNode and xNodeHasRoom and yNodeHasRoom and xyNotTouched and yxNotTouched: touched.update({(xNode,yNode):True, (yNode,xNode):True}) 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.nodes.update({xNode:self.nodes[xNode], yNode:self.nodes[yNode]}) self.edges.update({ed.ident:ed}) @@ -76,13 +78,13 @@ class Graph(object): for xNode in self.nodes: xNodeHasRoom = (len(self.nodes[xNode].edges) < 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) newNodeName = newIdent(len(self.nodes)) - offset = newOffset() - intens = newIntensity() + offset = newOffset(None) + intens = newIntensity(None) dat = {"offset":offset, "intensity":intens, "blockchain":deepcopy(self.blankBlockchain)} params = {"ident":newNodeName, "data":dat, "verbose":self.verbosity, "mode":self.mode, "targetRate":self.targetRate} newNode = Node(params) @@ -108,7 +110,8 @@ class Graph(object): leaver = self.nodes[leaverIdent] neighbors = [] 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: edIdent = neighbor[0] neiIdent = neighbor[1] @@ -125,19 +128,24 @@ class Graph(object): self.nodes[discoIdent].propagate(t, blockIdent) return out - def eventBlockArrival(self, destNodeIdent, edgeIdent, blockIdent, t): - out = str(t) + ",blockArriv," + str(destNodeIdent) + "," + str(edgeIdent) + "," + str(blockIdent) + "," - destNode = self.nodes[destNodeIdent] + def eventBlockArrival(self, pendingIdent, edgeIdent, t): + out = str(t) + ",blockArriv," edge = self.edges[edgeIdent] - block = deepcopy(edge.data["pendingBlocks"][blockIdent]) - block.arrivTimestamp = t + self.nodes[destNodeIdent].data["offset"] - self.nodes[destNodeIdent].updateBlockchain({blockIdent:block}) + pendingData = edge.data["pendingBlocks"][pendingIdent] # pendingDat = {"timeOfArrival":timeOfArrival, "destIdent":otherIdent, "block":blockToProp} + out += str(pendingData["destIdent"]) + "," + str(edgeIdent) + "," + str(pendingData["block"].ident) + 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 def go(self): with open(self.filename,"w") as writeFile: writeFile.write("timestamp,eventId,eventData\n") - + eventType = None while self.globalTime - self.startTime< self.runTime: u = -1.0*math.log(1.0-random.random())/self.birthRate eventType = ("nodeJoins", None) @@ -158,30 +166,39 @@ class Graph(object): edge = self.edges[edgeIdent] pB = edge.data["pendingBlocks"] for pendingIdent in pB: - pendingData = pB[pendingIdent] # pendingDat = {"timeOfArrival":timeOfArrival, "destIdent":otherIdent, "block":blockToProp} + pendingData = pB[pendingIdent] if pendingData["timeOfArrival"] - self.globalTime < u: - eventTime = ("blockArriv", (pendingData["destIdent"], edgeIdent, pendingData["block"])) + eventType = ("blockArriv", pendingIdent, edgeIdent) u = v - + assert eventType is not None self.globalTime += u out = "" - if eventTime[0] == "nodeJoins": + if eventType[0] == "nodeJoins": out = self.eventNodeJoins(self.globalTime) - elif eventTime[0] == "nodeLeaves": + elif eventType[0] == "nodeLeaves": out = self.eventNodeLeaves(self.globalTime) - elif eventTime[0] == "blockDisco": - out = self.eventBlockDiscovery(eventTime[1], self.globalTime) - elif eventTime[0] == "blockArriv": - out = self.eventBlockArrival(eventTime[1], eventTime[2], eventTime[3], self.globalTime) + elif eventType[0] == "blockDisco": + out = self.eventBlockDiscovery(eventType[1], self.globalTime) + elif eventType[0] == "blockArriv": + out = self.eventBlockArrival(eventType[1], eventType[2], self.globalTime) else: print("WHAAAA") with open(self.filename, "a") as writeFile: 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() diff --git a/source-code/Poisson-Graphs/Node.py b/source-code/Poisson-Graphs/Node.py index be9e9d9..fac0f71 100644 --- a/source-code/Poisson-Graphs/Node.py +++ b/source-code/Poisson-Graphs/Node.py @@ -29,9 +29,9 @@ class Node(object): t = discoTime s = t+self.data["offset"] 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) - self.data["blockchain"].addBlock(newBlock, mode, tr) + self.data["blockchain"].addBlock(newBlock) return newName def updateBlockchain(self, incBlocks): @@ -43,7 +43,7 @@ class Node(object): if key in self.data["blockchain"].blocks: del tempData[key] 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] incBlocks = deepcopy(tempData) while len(incBlocks)>0: @@ -58,13 +58,13 @@ class Node(object): def propagate(self, timeOfProp, blockIdent): for edgeIdent in self.edges: edge = self.edges[edgeIdent] - length = e.data["length"] + length = edge.data["length"] timeOfArrival = timeOfProp + length - otherIdent = e.getNeighbor(self.ident) - other = e.nodes[otherIdent] + otherIdent = edge.getNeighbor(self.ident) + other = edge.nodes[otherIdent] bc = other.data["blockchain"] if blockIdent not in bc.blocks: - pB = e.data["pendingBlocks"] + pB = edge.data["pendingBlocks"] pendingIdent = newIdent(len(pB)) mybc = self.data["blockchain"] blockToProp = mybc.blocks[blockIdent]