def init(): global nodeState #tracemalloc.start() for nodeId in nodeState: REPEATED_BLOCK_COUNT.append({}) sim.schedulleExecution(CYCLE, nodeId)
def init(): global nodeState for nodeId in nodeState: REPEATED_BLOCK_COUNT.append({}) # init k-buckets for i in range(0, bucketsNumber): nodeState[nodeId][TABLE][i] = deque([ ]) sim.schedulleExecution(CYCLE, nodeId)
def CYCLE(myself): global nodeState # with churn the node might be gone if myself not in nodeState: return # show progress for one node if myself == 0 and nodeState[myself][CURRENT_CYCLE] % 43200 == 0: improve_performance(nodeState[myself][CURRENT_CYCLE]) value = datetime.datetime.fromtimestamp(time.time()) #output.write('{} id: {} cycle: {}\n'.format(value.strftime('%Y-%m-%d %H:%M:%S'), runId, nodeState[myself][CURRENT_CYCLE])) #output.flush() print('{} id: {} cycle: {}'.format(value.strftime('%Y-%m-%d %H:%M:%S'), runId, nodeState[myself][CURRENT_CYCLE])) # Cheat to avoid using a deterministic function to know which nodes will we send addr's if nodeState[myself][CURRENT_CYCLE] % (24 * 60 * 60) == 0: update_relays(myself) # Check if we need to make a dns request dns_address_seed(myself) open_connections(myself) # Main cycle to dispatch messages on nodes which we are connected to for node in nodeState[myself][NODES_CONNECTED]: cnode = nodeState[myself][NODE_NEIGHBOURHOOD][node] send_messages(myself, node, cnode) # TODO implement feeler connections nodeState[myself][CURRENT_CYCLE] += 1 nodeState[myself][CURRENT_TIME] += 1 # schedule next execution if nodeState[myself][CURRENT_CYCLE] < nb_cycles: sim.schedulleExecution(CYCLE, myself)
def CYCLE(self): global nodeState, TX_NUMBER, BLOCK_NUMBER if self == 0 and nodeState[self][CURRENT_CYCLE] % 500 == 0: value = datetime.datetime.fromtimestamp(time.time()) logger.info('time: {} node: {} cycle: {}'.format(value.strftime('%Y-%m-%d %H:%M:%S'), self, nodeState[self][CURRENT_CYCLE])) print("Time:", value.strftime('%Y-%m-%d %H:%M:%S')) print("Cycle: ", nodeState[self][CURRENT_CYCLE], "/", nbCycles-1) print("Queued events: ", sim.getNumberEvents()) improve_performance() if self not in nodeState: return if self not in NETWORK_NODES: # join the network if random.random() <= probJoin: join(self) else: # lookup for new neighbors if connsCount(self) < p: lookup(self) # Advertise self # 24h = 8 640 000ms if nodeState[self][CURRENT_TIME] % 875000 == 0: for n in nodeState[self][CONNS]: sim.send(ADDR, n, self, ADDR_MSG, [self]) nodeState[self][MEMB_MSGS_SENT] += 1 if nodeState[self][RELAY_NODES]: for n in random.sample(list(nodeState[self][CONNS].keys()), min(connsCount(self), 2)): sim.send(ADDR, n, self, ADDR_MSG, nodeState[self][RELAY_NODES]) nodeState[self][RELAY_NODES].clear() # lifecheck and cycle ping lifeCheckDBNeighbs(self) for n in nodeState[self][CONNS]: sim.send(PING, n, self, PING_MSG) #nodeState[self][MEMB_MSGS_SENT] += 1 # Announcement if len(nodeState[self][CONNS][n][QUEUED_INVS]) > 0: sim.send(INV, n, self, INV_MSG, nodeState[self][CONNS][n][QUEUED_INVS].copy()) nodeState[self][DISS_MSGS_SENT] += 1 nodeState[self][CONNS][n][QUEUED_INVS].clear() for block in nodeState[self][QUEUED_BLOCKS]: if addBlockToBlockchain(nodeState[self][BLOCKCHAIN], block): # Remove known txs that are already in blocks txs = list(block.getBody()) for t in txs: if t in nodeState[self][QUEUED_TXS]: nodeState[self][QUEUED_TXS].remove(t) addBlockToBlockchain(REAL_BLOCKCHAIN, block) # Stop creating blocks/txs at 90% of cycles if nodeState[self][CURRENT_CYCLE] < 0.9 * nbCycles: # Create transactions if self in TX_NODES[nodeState[self][CURRENT_CYCLE]]: t = generateTx(TX_NUMBER) TX_NUMBER += 1 nodeState[self][KNOWN_TXS][t.getHash()] = t nodeState[self][QUEUED_TXS].append(t) addInvConns(self, self, [MSG_TX, t.getHash()]) # Create block if self in MINER_NODES and len(nodeState[self][QUEUED_TXS]) > minTxPerBlock: b = generateBlock(self, nodeState[self][QUEUED_TXS]) REPEATED_BLOCK_COUNT[self].update({b.getHash():0}) BLOCK_NUMBER += 1 nodeState[self][QUEUED_TXS].clear() nodeState[self][KNOWN_BLOCKS][b.getHash()] = b nodeState[self][QUEUED_BLOCKS].append(b) for n in nodeState[self][CONNS].keys(): sim.send(BLOCK, n, self, BLOCK_MSG, b) nodeState[self][DISS_MSGS_SENT] += 1 del b nodeState[self][CURRENT_CYCLE] += 1 nodeState[self][CURRENT_TIME] += nodeCycle if nodeState[self][CURRENT_CYCLE] < nbCycles: sim.schedulleExecution(CYCLE, self)
def init(): # schedule execution for all nodes for nodeId in nodeState: sim.schedulleExecution(CYCLE, nodeId)
def CYCLE(self): global nodeState, TX_NUMBER, BLOCK_NUMBER if self == 0 and nodeState[self][CURRENT_CYCLE] % 500 == 0: value = datetime.datetime.fromtimestamp(time.time()) logger.info('time: {} node: {} cycle: {}'.format( value.strftime('%Y-%m-%d %H:%M:%S'), self, nodeState[self][CURRENT_CYCLE])) print("Time:", value.strftime('%Y-%m-%d %H:%M:%S')) print("Cycle: ", nodeState[self][CURRENT_CYCLE], "/", nbCycles - 1) print("Queued events: ", sim.getNumberEvents()) improve_performance() if self not in nodeState: return if self not in NETWORK_NODES: # join the network if random.random() <= probJoin: join(self) else: ''' if neighbsSize(self) < tableSize and len(nodeState[self][DB]) > 2: tmp = random.choices(nodeState[self][DB], k=2) for n in tmp: addEntryNeighbs(self, n) sim.send(FINDNODE, n, self, FINDNODE_MSG) ''' # Advertise self # 24h = 8 640 000ms if nodeState[self][CURRENT_TIME] % 875000 == 0: for n in nodeState[self][NEIGHBS]: sim.send(NEIGHBORS, n, self, NEIGHBORS_MSG, [self]) nodeState[self][MEMB_MSGS_SENT] += 1 if nodeState[self][CURRENT_TIME] % 360000 == 0: improve(self) # lifecheck and cycle ping lifeCheckDBNeighbs(self) for n in nodeState[self][DB]: sim.send(PING, n, self, PING_MSG) #nodeState[self][MEMB_MSGS_SENT] += 1 # lookup for new neighbors if neighbsSize(self) < neighbThreshold: lookup(self) # Message dissemination # Blockchain maintenance # Proccess queued blocks for b in nodeState[self][QUEUED_BLOCKS]: # Add to blockchain if b.getHash() not in nodeState[self][BLOCKCHAIN_HASHES]: if addBlockToBlockchain(nodeState[self][BLOCKCHAIN], b): # Remove known txs that are already in blocks txs = list(b.getBody()) for t in txs: if t in nodeState[self][QUEUED_TXS]: nodeState[self][QUEUED_TXS].remove(t) nodeState[self][BLOCKCHAIN_HASHES][b.getHash()] = b addBlockToBlockchain(REAL_BLOCKCHAIN, b) nodeState[self][QUEUED_BLOCKS].clear() # Lazy push if nodeState[self][CURRENT_TIME] % 12500 == 0 and nodeState[self][ QUEUED_ANN]: for n in nodeState[self][BTREE][LAZY]: sim.send(ANNOUNCEMENT, n, self, ANNOUNCEMENT_MSG, nodeState[self][QUEUED_ANN]) nodeState[self][DISS_MSGS_SENT] += 1 nodeState[self][QUEUED_ANN].clear() # Handle missing messages timeouts for a in nodeState[self][MISSING_ANN].copy().keys(): if a not in nodeState[self][ASKED_ANN]: ask = True elif nodeState[self][CURRENT_TIME] >= nodeState[self][ASKED_ANN][a]: ask = True else: ask = False if ask: if len(nodeState[self][MISSING_ANN][a]) == 0: del nodeState[self][MISSING_ANN][a] continue t = a[0] h = a[1] source = nodeState[self][MISSING_ANN][a].pop(0) if t == ANN_BLOCK and h not in nodeState[self][KNOWN_BLOCKS]: nodeState[self][ASKED_ANN][ a] = nodeState[self][CURRENT_TIME] + 1250 graftBTree(self, source) sim.send(GRAFT, source, self, GRAFT_MSG) sim.send(GETDATA, source, self, GETDATA_MSG, ANN_BLOCK, [h]) nodeState[self][DISS_MSGS_SENT] += 2 elif t == ANN_TX and h not in nodeState[self][KNOWN_TXS]: nodeState[self][ASKED_ANN][ a] = nodeState[self][CURRENT_TIME] + 1250 graftBTree(self, source) sim.send(GRAFT, source, self, GRAFT_MSG) sim.send(GETDATA, source, self, GETDATA_MSG, ANN_TX, [h]) nodeState[self][DISS_MSGS_SENT] += 2 # Create block & Txs # Stop creating blocks and txs at 90% of cycles if nodeState[self][CURRENT_CYCLE] < 0.9 * nbCycles: if self in TX_NODES[nodeState[self][CURRENT_CYCLE]]: t = generateTx(TX_NUMBER) TX_NUMBER += 1 nodeState[self][KNOWN_TXS][t.getHash()] = t nodeState[self][QUEUED_TXS].append(t) for n in nodeState[self][BTREE][EAGER]: sim.send(TXS, n, self, TXS_MSG, t) nodeState[self][DISS_MSGS_SENT] += 1 nodeState[self][QUEUED_ANN].append((ANN_TX, t.getHash())) if len(nodeState[self] [QUEUED_TXS]) > minTxPerBlock and self in MINER_NODES: b = generateBlock(self, nodeState[self][QUEUED_TXS]) REPEATED_BLOCK_COUNT[self].update({b.getHash(): 0}) BLOCK_NUMBER += 1 nodeState[self][KNOWN_BLOCKS][b.getHash()] = b nodeState[self][QUEUED_BLOCKS].append(b) for n in nodeState[self][BTREE][EAGER]: sim.send(BLOCK, n, self, BLOCK_MSG, b) nodeState[self][DISS_MSGS_SENT] += 1 nodeState[self][QUEUED_ANN].append((ANN_BLOCK, b.getHash())) nodeState[self][CURRENT_CYCLE] += 1 nodeState[self][CURRENT_TIME] += nodeCycle if nodeState[self][CURRENT_CYCLE] < nbCycles: sim.schedulleExecution(CYCLE, self)
def CYCLE(self): global nodeState, TX_NUMBER, BLOCK_NUMBER if self == 0 and nodeState[self][CURRENT_CYCLE] % 500 == 0: value = datetime.datetime.fromtimestamp(time.time()) logger.info('node: {} cycle: {}'.format(self, nodeState[self][CURRENT_CYCLE])) print("Time:", value.strftime('%Y-%m-%d %H:%M:%S')) print("Cycle: ", nodeState[self][CURRENT_CYCLE], "/", nbCycles-1) print("Queued events: ", sim.getNumberEvents()) improve_performance() if self not in nodeState: return if self not in NETWORK_NODES: # join the network if random.random() <= probJoin: join(self) else: if len(getNeighbors(self)) == 0: join(self) lifeCheckDbTable(self) # Optimize #if nodeState[self][CURRENT_CYCLE]%5 == 0: for n in nodeState[self][DB]: sim.send(PING, n, self, PING_MSG) #nodeState[self][MEMB_MSGS_SENT] += 1 # 24h = 8 640 000ms if nodeState[self][CURRENT_TIME] % 875000 == 0: lookup(self, self) # Message dissemination neighbors = getNeighbors(self) # Blockchain maintenance # Announcements for n in neighbors: tmp = [] for b in nodeState[self][QUEUED_BLOCKS]: tmp.append(b.getHash()) if tmp: sim.send(NEWBLOCKHASHES, n, self, NEWBLOCKHASHES_MSG, tmp) nodeState[self][DISS_MSGS_SENT] += 1 del tmp sendTxs = [] for h in nodeState[self][ANN_TXS]: if n not in nodeState[self][ANN_TXS][h] and h in nodeState[self][KNOWN_TXS]: t = nodeState[self][KNOWN_TXS][h] if t in nodeState[self][QUEUED_TXS]: sendTxs.append(t) if sendTxs: sim.send(TRANSACTIONS, n, self, TRANSACTIONS_MSG, sendTxs) nodeState[self][DISS_MSGS_SENT] += 1 # Proccess known blocks for b in nodeState[self][QUEUED_BLOCKS]: # Add to blockchain if b.getHash() not in nodeState[self][BLOCKCHAIN_HASHES]: if addBlockToBlockchain(nodeState[self][BLOCKCHAIN], b): nodeState[self][BLOCKCHAIN_HASHES][b.getHash()] = b cleanupTxsMsgs(self, b) addBlockToBlockchain(REAL_BLOCKCHAIN, b) nodeState[self][QUEUED_BLOCKS].clear() # Create block # Stop creating blocks at 90% of cycles if nodeState[self][CURRENT_CYCLE] < 0.9 * nbCycles: # Create transactions if self in TX_NODES[nodeState[self][CURRENT_CYCLE]]: t = generateTx(TX_NUMBER) TX_NUMBER += 1 nodeState[self][KNOWN_TXS][t.getHash()] = t nodeState[self][QUEUED_TXS].append(t) if not nodeState[self][ANN_TXS].get(t.getHash()): nodeState[self][ANN_TXS][t.getHash()] = [] if len(nodeState[self][QUEUED_TXS]) > minTxPerBlock and self in MINER_NODES: b = generateBlock(self, nodeState[self][QUEUED_TXS]) REPEATED_BLOCK_COUNT[self].update({b.getHash():0}) BLOCK_NUMBER += 1 nodeState[self][KNOWN_BLOCKS][b.getHash()] = b nodeState[self][QUEUED_BLOCKS].append(b) # Send NewBlock to math.sqrt(neighbors) # Send NewBlockHashes to remaining rootSample = random.sample(neighbors, int(math.sqrt(len(neighbors)))) for n in neighbors: if n in rootSample: sim.send(NEWBLOCK, n, self, NEWBLOCK_MSG, b) else: sim.send(NEWBLOCKHASHES, n, self, NEWBLOCKHASHES_MSG, [b.getHash()]) nodeState[self][DISS_MSGS_SENT] += 1 nodeState[self][CURRENT_CYCLE] += 1 nodeState[self][CURRENT_TIME] += nodeCycle if nodeState[self][CURRENT_CYCLE] < nbCycles: sim.schedulleExecution(CYCLE, self)