From 0b3e995206e162a5573d419bb342697b14f310fe Mon Sep 17 00:00:00 2001 From: Shashwat Jolly Date: Sun, 28 Jun 2020 22:49:02 +0530 Subject: [PATCH 1/2] Implement block header and body division --- block.py | 4 +++- fullNode.py | 5 +++++ miner.py | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/block.py b/block.py index 576cc2c..13828dc 100644 --- a/block.py +++ b/block.py @@ -7,8 +7,10 @@ def getHash(k): class Block: """docstring for Block""" - def __init__(self, identifier, transactionList, params): + def __init__(self, identifier, transactionList, params, size, type): self.params = params self.identifier = identifier self.transactionList = transactionList self.hash = self.identifier+"_"+getHash(10) + self.size = size + self.type = type diff --git a/fullNode.py b/fullNode.py index 57dd248..6cae132 100644 --- a/fullNode.py +++ b/fullNode.py @@ -38,6 +38,11 @@ def receiveBlock(self): """Receive newly mined block from neighbour""" while True: b = yield self.pipes[self.identifier].get() + + yield self.env.timeout(b.size * 10) + if(b.type == "Body"): + continue + if len(self.blockchain) > 0: currID = int(self.blockchain[-1].identifier[1:]) else: diff --git a/miner.py b/miner.py index 9734119..432f299 100644 --- a/miner.py +++ b/miner.py @@ -32,7 +32,9 @@ def blockGenerator(self, params): """Simulating Tx validation time""" self.env.timeout(0.1) l.append(transaction.identifier) - b = Block("B"+str(self.currentBlockID), transactionList, params) + b = Block("B"+str(self.currentBlockID), transactionList, params, params['blockHeaderSize'], "Header") + if(self.params['blockDivision']): + b_body = Block("b"+str(self.currentBlockID), transactionList, params, params['blockBodySize'], "Body") """Collection of data""" """If block has been mined earlier, inrease count of fork""" @@ -57,6 +59,11 @@ def blockGenerator(self, params): broadcast(self.env, b, "Block", self.identifier, self.neighbourList, \ self.params, pipes=self.pipes, nodes=self.nodes) + if(self.params["blockDivision"]): + """Broadcast block body to all neighbours""" + broadcast(self.env, b_body, "Block", self.identifier, self.neighbourList, \ + self.params, pipes=self.pipes, nodes=self.nodes) + """Remove transactions from local pool""" self.transactionPool.popTransaction(transactionCount) self.currentBlockID += 1 @@ -70,6 +77,12 @@ def receiveBlock(self): """Receive newly mined block from neighbour""" while True: b = yield self.pipes[self.identifier].get() + + if(self.params["blockDivision"]): + yield self.env.timeout(b.size * 10) + if(b.type == "Body"): + continue + if len(self.blockchain) > 0: currID = int(self.blockchain[-1].identifier[1:]) else: From 1892ba0fd176aa369f82261e51b185eb216b1a96 Mon Sep 17 00:00:00 2001 From: Shashwat Jolly Date: Sun, 28 Jun 2020 23:36:10 +0530 Subject: [PATCH 2/2] Modify implementation --- block.py | 5 ++--- fullNode.py | 4 ---- miner.py | 16 ++-------------- pipe.py | 3 +++ 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/block.py b/block.py index 13828dc..36b900e 100644 --- a/block.py +++ b/block.py @@ -7,10 +7,9 @@ def getHash(k): class Block: """docstring for Block""" - def __init__(self, identifier, transactionList, params, size, type): + def __init__(self, identifier, transactionList, params): self.params = params self.identifier = identifier self.transactionList = transactionList self.hash = self.identifier+"_"+getHash(10) - self.size = size - self.type = type + self.size = params['blockSize'] diff --git a/fullNode.py b/fullNode.py index 6cae132..0a82245 100644 --- a/fullNode.py +++ b/fullNode.py @@ -38,11 +38,7 @@ def receiveBlock(self): """Receive newly mined block from neighbour""" while True: b = yield self.pipes[self.identifier].get() - yield self.env.timeout(b.size * 10) - if(b.type == "Body"): - continue - if len(self.blockchain) > 0: currID = int(self.blockchain[-1].identifier[1:]) else: diff --git a/miner.py b/miner.py index 432f299..892768e 100644 --- a/miner.py +++ b/miner.py @@ -32,9 +32,7 @@ def blockGenerator(self, params): """Simulating Tx validation time""" self.env.timeout(0.1) l.append(transaction.identifier) - b = Block("B"+str(self.currentBlockID), transactionList, params, params['blockHeaderSize'], "Header") - if(self.params['blockDivision']): - b_body = Block("b"+str(self.currentBlockID), transactionList, params, params['blockBodySize'], "Body") + b = Block("B"+str(self.currentBlockID), transactionList, params) """Collection of data""" """If block has been mined earlier, inrease count of fork""" @@ -59,11 +57,6 @@ def blockGenerator(self, params): broadcast(self.env, b, "Block", self.identifier, self.neighbourList, \ self.params, pipes=self.pipes, nodes=self.nodes) - if(self.params["blockDivision"]): - """Broadcast block body to all neighbours""" - broadcast(self.env, b_body, "Block", self.identifier, self.neighbourList, \ - self.params, pipes=self.pipes, nodes=self.nodes) - """Remove transactions from local pool""" self.transactionPool.popTransaction(transactionCount) self.currentBlockID += 1 @@ -77,12 +70,7 @@ def receiveBlock(self): """Receive newly mined block from neighbour""" while True: b = yield self.pipes[self.identifier].get() - - if(self.params["blockDivision"]): - yield self.env.timeout(b.size * 10) - if(b.type == "Body"): - continue - + yield self.env.timeout(b.size * 10) if len(self.blockchain) > 0: currID = int(self.blockchain[-1].identifier[1:]) else: diff --git a/pipe.py b/pipe.py index fd5bf35..2607f88 100644 --- a/pipe.py +++ b/pipe.py @@ -15,8 +15,11 @@ def latencyPut(self, value, delay): return self.store.put(value) def put(self, value, sourceLocation): + # Simulate transmission delay + # yield self.env.timeout(value.size * 1000) destLocation = self.allNodes[self.identifier].location delay = getTransmissionDelay(sourceLocation, destLocation) + delay = delay + value.size * 10 return self.env.process(self.latencyPut(value, delay)) def get(self):