def initTxSim(): addMiner() # Create wallets # print "Create wallets..." nodeList = btcnet.getNodeList() for node in nodeList: if "Spy" in node: continue # print "Initiating "+node genBTCAddress(node) execWS(node, "settxfee 0.0") # Generate initial blocks genBlocks(110) time.sleep(10) # Send funds to all nodes # print "Funding nodes..." for node in nodeList: if "Spy" in node or "Miner" in node: continue try: fundNode(node, 3) except: #try 2 times time.sleep(0.5) fundNode(node, 3) time.sleep(0.2) time.sleep(10) #Create block to confirm txs genBlocks(110) time.sleep(10)
def generateTransactions(arg, stop_event): db = "" nodeList = btcnet.getNodeList() global lastTx global mutex txfreq = datetime.timedelta(seconds=3) for n in nodeList: lastTx[n] = datetime.datetime.now() - txfreq while not stop_event.is_set(): nodes = btcnet.getRandList("node", 2, "Spy") #Only use node if no other thread is working on it if nLocks[nodes[0]].acquire(False): # only generate 1 tx per second for each node if datetime.datetime.now() < lastTx[nodes[0]] + txfreq: continue mutex.acquire() try: txhash = sendTx(nodes[0], nodes[1], 0.00000001) #Send 1 satoshi except: print "ERROR: could not send tx" mutex.release() if (txhash != None): db = db + txhash + " " + nodes[0] + " " + nodes[1] + "\n" lastTx[nodes[0]] = datetime.datetime.now() nLocks[nodes[0]].release() time.sleep(0.0001) txdb.write(db) txdb.flush()
def runTxSim(duration): global txdb txdb = open("db/txs.db", "a+") #Generate blocks t_stop = threading.Event() thrBlocks = threading.Thread(target=generateBlocks, args=(2, t_stop)) thrBlocks.start() #randomly generate transactions numThreads = len(btcnet.getNodeList()) / 10 print "Starting " + str(numThreads) + " tx threads" for i in range(numThreads): thrTx = threading.Thread(target=generateTransactions, args=(1, t_stop)) thrTx.start() #Stop simulation after 'duration' time.sleep(duration) t_stop.set() time.sleep(5) txdb.flush() txdb.close() numtxs = len(open("db/txs.db", "r").readlines()) print "Generated " + str(numtxs) + " transactions"
def addSpy(num, connect): node = btcnet.getRandNode("nodeR", "Miner") spyName = spy + str(num) btcnet.renameNode(node, spyName) if connect: #Connect to all nodes nodeList = btcnet.getNodeList("nodeR") for node in nodeList: btcnet.connectNode(spyName, node)
def stop(): if not os.path.exists('log'): os.makedirs('log') spyList = btcnet.getNodeList(spy) for s in spyList: nLog = os.popen('docker exec -t ' + s + ' cat /root/.bitcoin/regtest/debug.log').read() f = open("log/" + s + ".log", "w") f.write(nLog) f.close() os.system("docker stop $(docker ps -a --filter=\"name=" + spy + "\" -q) > /dev/null")
def initTxSim(): addMiner() # Create wallets # nodeList = btcnet.getNodeList() for node in nodeList: genBTCAddress(node) execWS(node, "settxfee 0.0") # Generate initial blocks genBlocks(110) time.sleep(30) # Send funds to all nodes # for node in nodeList: fundNode(node, 1) time.sleep(1) # #Create block to confirm txs genBlocks(100) time.sleep(30)
def runTxSim(duration, threads): global txdb txdb = open("db/txs.db", "a") #Generate blocks t_stop = threading.Event() # thrBlocks = threading.Thread(target=generateBlocks, args=(2,t_stop)) # thrBlocks.start() global nLocks nLocks = {} nodeList = btcnet.getNodeList(exclude="Spy") for n in nodeList: nLocks[n] = threading.RLock() #randomly generate transactions numThreads = threads print "Starting " + str(numThreads) + " tx threads" threads = [] for i in range(numThreads): thrTx = threading.Thread(target=generateTransactions, args=(1, t_stop)) thrTx.start() threads.append(thrTx) #Stop simulation after 'duration' time.sleep(duration) t_stop.set() for t in threads: t.join() txdb.close() txdb = open("db/txs.db", "r") numtxs = len(txdb.readlines()) print "Generated " + str(numtxs) + " transactions" txdb.close() time.sleep(30)
def printBalances(): nodeList = btcnet.getNodeList() for node in nodeList: balance = getBalance(node) print "balance(" + node + ")=" + str(balance) + " unconfirmed=" + str( getUnconfirmedBalance(node))