def test_full_client(msg_mass): ## Run a single client on single CPU and test-stress it. (sometx, mesages_q) = msg_mass (factory, instance, tr) = sometx responses = [] t0 = timer() for (tx, data, core) in mesages_q: tr.clear() instance.lineReceived(data) response = tr.value() responses += [(tx, data, core, response)] t1 = timer() print "\nQuery message rate: %2.2f / sec" % (1.0 / ((t1-t0)/(len(mesages_q)))) ## Now we test the Commit t0 = timer() for (tx, data, core, response) in responses: resp = response.split(" ") pub, sig, hashhead, seqStr = map(b64decode, resp[1:]) assert resp[0] == "OK" tr.clear() data = package_commit(core, [(pub, sig, hashhead, seqStr)]) instance.lineReceived(data) response = tr.value() flag, pub, sig, hashhead, seqStr = unpackage_commit_response(response) assert flag == "OK" t1 = timer() print "\nCommit message rate: %2.2f / sec" % (1.0 / ((t1-t0)/(len(responses))))
def get_commit_responses(resp): try: assert len(resp) <= 3 for r in resp: res = unpackage_commit_response(r) if res[0] != "OK": print resp d_end.errback(Exception("Commit failed.")) return t1 = default_timer() # print print "Commit OK", t1 - t0, t0, t1 d_end.callback(t1 - t0) except Exception as e: d_end.errback(e) return
def get_commit_responses(resp): try: assert len(resp) <= 3 for r in resp: res = unpackage_commit_response(r) if res[0] != "OK": print resp d_end.errback(Exception("Commit failed.")) return t1 = default_timer() # print print "Commit OK", t1 - t0, t0, t1 d_end.callback(t1 - t0) except Exception as e: d_end.errback(e) return
def test_TxCommit(sometx): (factory, instance, tr), (k1, k2, tx1, tx2, tx3) = sometx # Check the list is up to date for ik in tx3.get_utxo_in_keys(): assert ik in factory.db #dataCore = map(b64encode, [tx3.serialize(), tx1.serialize(), tx2.serialize(), # k1.export()[0], k2.export()[0], k1.sign(tx3.id()), k2.sign(tx3.id())]) #H = sha256(" ".join(data1)).digest() #data = " ".join(["Query", str(len(data1))] + data1) H, dataString, dataCoreList = package_query(tx3, [tx1, tx2], [k1, k2]) instance.lineReceived(dataString) response = tr.value() _, k, s, hashhead, seqStr = unpackage_query_response(response) new_H = sha256(" ".join( dataCoreList + [hashhead] + [seqStr]) ).digest() k2 = rscoin.Key(k) assert factory.key.verify(new_H, s) assert k2.verify(new_H, s) ## Now we test the Commit tr.clear() # data = " ".join(["Commit", str(len(dataCore))] + dataCore + map(b64encode, [k, s])) dataString2 = package_commit(dataCoreList, [(k, s, hashhead, seqStr)]) instance.lineReceived(dataString2) response = tr.value() flag, pub, sig, hashhead, seqStr = unpackage_commit_response(response) new_h = sha256(" ".join(dataCoreList + [hashhead] +[seqStr])).digest() assert factory.key.verify(new_h, sig) k3 = rscoin.Key(pub) assert k3.verify(new_h, sig)
def r_process(results): for msg in results: parsed = unpackage_commit_response(msg) if parsed[0] != "OK": raise Exception("Response not OK.") pub, sig = parsed[1:] kx = rscoin.Key(pub) if not (kx.verify(tx.id(), sig) and kx.id() in auths): raise Exception("Invalid Signature.") auths.remove(kx.id()) active = ActiveTx("activetx.log", keys) active.add(tx_ser) active.save(reactor) print " ".join(core)
def r_process(results): for msg in results: parsed = unpackage_commit_response(msg) if parsed[0] != "OK": raise Exception("Response not OK.") pub, sig = parsed[1:] kx = rscoin.Key(pub) if not (kx.verify(tx.id(), sig) and kx.id() in auths): raise Exception("Invalid Signature.") auths.remove( kx.id() ) active = ActiveTx("activetx.log", keys) active.add(tx_ser) active.save(reactor) print " ".join(core)
def test_TxCommit(sometx): # query phase (factory, instance, tr), (k1, k2, tx1, tx2, tx3) = sometx # Check the list is up to date for ik in tx3.get_utxo_in_keys(): assert ik in factory.db H, dataString, dataCoreString = package_query(tx3, [tx1, tx2], [k1, k2]) instance.lineReceived(dataString) response = tr.value() _, k, s, hashhead, seqStr = unpackage_query_response(response) new_H = sha256(" ".join( dataCoreString + [hashhead] + [seqStr]) ).digest() #... notice in sometx, there is only one factory playing mintette #... factory.key.pub.export( EcPt.POINT_CONVERSION_UNCOMPRESSED ) == k assert factory.key.verify(new_H, s) # commit phase tr.clear() # items here is the same with dataCore dataString2 = package_commit(dataCoreString, [(k, s, hashhead, seqStr)]) instance.lineReceived(dataString2) response = tr.value() flag, pub, sig, hashhead, seqStr = unpackage_commit_response(response) new_h = sha256(" ".join(dataCoreString + [hashhead] +[seqStr])).digest() assert factory.key.verify(new_h, sig) k3 = rscoin.Key(pub) assert k3.verify(new_h, sig)
def test_TxCommit_Issued(sometx): (factory, instance, tr), (k1, k2, tx1, tx2, tx3) = sometx kIssue = rscoin.Key(urandom(32), public=False) pubIssue = kIssue.export()[0] factory.special_key = kIssue.id() # Asssign this as the special key tx3 = rscoin.Tx([], [rscoin.OutputTx(k1.id(), 250)]) sig1 = kIssue.sign(tx3.id()) assert tx3.check_transaction_utxo([], [pubIssue], [sig1], kIssue.id()) assert tx3.check_transaction([], [pubIssue], [sig1], kIssue.id()) ## Now we test the Commit # Ensure the entries are not in before sending message for k, v in tx3.get_utxo_out_entries(): assert k not in factory.db # Send message tr.clear() data, dataCoreList= package_issue(tx3, [kIssue, sig1]) instance.lineReceived(data) response = tr.value() # Ensure the returned signatures check flag, pub, sig, hashhead, seqStr = unpackage_commit_response(response) assert flag == "OK" kx = rscoin.Key(pub) new_h = sha256(" ".join(dataCoreList + [hashhead] +[seqStr])).digest() assert kx.verify(new_h, sig) # Ensure the entries are now in for k, v in tx3.get_utxo_out_entries(): assert factory.db[k] == v
def test_multiple(): import os try: os.mkdir("testscratch") except: pass # Make special keys for making coins secret_special = "KEYSPECIAL" public_special = rscoin.Key(secret_special, public=False).id() # Define a number of keys all_keys = [] for x in range(100): secret = "KEY%s" % x public = rscoin.Key(secret, public=False).id() all_keys += [(public, secret)] # Make up the directory directory = [] for x, (pub, _) in enumerate(all_keys): directory += [(pub, "127.0.0.1", 8080 + x)] # Build the factories factories = {} for pub, sec in all_keys: factory = RSCFactory(sec, directory, public_special, conf_dir="testscratch", N=5) factories[pub] = factory # Make a mass of transactions k1 = rscoin.Key(urandom(32), public=False) k2 = rscoin.Key(urandom(32), public=False) all_tx_in = [] all_tx_out = [] for _ in range(10): tx1 = rscoin.Tx([], [rscoin.OutputTx(k1.id(), 100)]) tx2 = rscoin.Tx([], [rscoin.OutputTx(k2.id(), 150)]) tx3 = rscoin.Tx( [rscoin.InputTx(tx1.id(), 0), rscoin.InputTx(tx2.id(), 0)], [rscoin.OutputTx(k1.id(), 250)] ) all_tx_in += [ tx1, tx2 ] all_tx_out += [ tx3 ] print "Lens: all_tx_in: %s all_tx_out: %s" % (len(all_tx_in), len(all_tx_out)) for tx in all_tx_in: for kv, vv in tx.get_utxo_out_entries(): for f in factories.values(): f.db[kv] = vv data = (tx3, [tx1.serialize(), tx2.serialize()], [k1.export()[0], k2.export()[0]], [k1.sign(tx3.id()), k2.sign(tx3.id())]) # Put the transaction through total = 0 [ kid1, kid2 ] = tx3.get_utxo_in_keys() au1 = get_authorities(directory, kid1, N = 5) au2 = get_authorities(directory, kid2, N = 5) auxes = set(au1 + au2) assert len(auxes) == 10 for aid in auxes: assert isinstance(aid, str) and len(aid) == 32 assert aid in factories H, msg, dataCore = package_query(tx3, [tx1, tx2], [k1, k2]) xset = [] rss = [] for kid, f in factories.iteritems(): # resp = f.process_TxQuery(data) instance = f.buildProtocol(None) tr = StringTransport() instance.makeConnection(tr) instance.lineReceived(msg) resp_msg = unpackage_query_response(tr.value().strip()) assert kid == f.key.id() if resp_msg[0] == "OK": [r, s] = resp_msg[1:] total += 1 xset += [ f.key.id() ] rss += [(r,s)] else: pass assert 5 <= total <= 10 assert set(auxes) == set(xset) ## Now test the commit phase assert 5 <= len(rss) <= 10 msg_commit = package_commit(dataCore, rss) #from twisted.python import log #import sys #log.startLogging(sys.stdout) total = 0 for kid, f in factories.iteritems(): instance = f.buildProtocol(None) tr = StringTransport() instance.makeConnection(tr) instance.lineReceived(msg_commit) resp_commit = tr.value().strip() resp_l = unpackage_commit_response(resp_commit) if resp_l[0] == "OK": total += 1 assert total == 5
def test_multiple(): import os try: os.mkdir("testscratch") except: pass # Make special keys for making coins secret_special = "KEYSPECIAL" public_special = rscoin.Key(secret_special, public=False).id() # Define a number of keys all_keys = [] for x in range(100): secret = "KEY%s" % x public = rscoin.Key(secret, public=False).id() all_keys += [(public, secret)] # Make up the directory directory = [] for x, (pub, _) in enumerate(all_keys): directory += [(pub, "127.0.0.1", 8080 + x)] # Build the factories factories = {} for pub, sec in all_keys: factory = RSCFactory(sec, directory, public_special, conf_dir="testscratch", N=5) factories[pub] = factory # Make a mass of transactions k1 = rscoin.Key(urandom(32), public=False) k2 = rscoin.Key(urandom(32), public=False) all_tx_in = [] all_tx_out = [] for _ in range(10): tx1 = rscoin.Tx([], [rscoin.OutputTx(k1.id(), 100)]) tx2 = rscoin.Tx([], [rscoin.OutputTx(k2.id(), 150)]) tx3 = rscoin.Tx( [rscoin.InputTx(tx1.id(), 0), rscoin.InputTx(tx2.id(), 0)], [rscoin.OutputTx(k1.id(), 250)]) all_tx_in += [tx1, tx2] all_tx_out += [tx3] print "Lens: all_tx_in: %s all_tx_out: %s" % (len(all_tx_in), len(all_tx_out)) for tx in all_tx_in: for kv, vv in tx.get_utxo_out_entries(): for f in factories.values(): f.db[kv] = vv data = (tx3, [tx1.serialize(), tx2.serialize()], [k1.export()[0], k2.export()[0]], [k1.sign(tx3.id()), k2.sign(tx3.id())]) # Put the transaction through total = 0 [kid1, kid2] = tx3.get_utxo_in_keys() au1 = get_authorities(directory, kid1, N=5) au2 = get_authorities(directory, kid2, N=5) auxes = set(au1 + au2) assert len(auxes) == 10 for aid in auxes: assert isinstance(aid, str) and len(aid) == 32 assert aid in factories H, msg, dataCore = package_query(tx3, [tx1, tx2], [k1, k2]) xset = [] rss = [] for kid, f in factories.iteritems(): # resp = f.process_TxQuery(data) instance = f.buildProtocol(None) tr = StringTransport() instance.makeConnection(tr) instance.lineReceived(msg) resp_msg = unpackage_query_response(tr.value().strip()) assert kid == f.key.id() if resp_msg[0] == "OK": [r, s] = resp_msg[1:] total += 1 xset += [f.key.id()] rss += [(r, s)] else: pass assert 5 <= total <= 10 assert set(auxes) == set(xset) ## Now test the commit phase assert 5 <= len(rss) <= 10 msg_commit = package_commit(dataCore, rss) #from twisted.python import log #import sys #log.startLogging(sys.stdout) total = 0 for kid, f in factories.iteritems(): instance = f.buildProtocol(None) tr = StringTransport() instance.makeConnection(tr) instance.lineReceived(msg_commit) resp_commit = tr.value().strip() resp_l = unpackage_commit_response(resp_commit) if resp_l[0] == "OK": total += 1 assert total == 5
def test_multiple(): import os try: os.mkdir("testscratch") except: pass # Make special keys for making coins secret_special = "KEYSPECIAL" public_special = rscoin.Key(secret_special, public=False).id() # Define a number of keys all_keys = [] for x in range(100): secret = "KEY%s" % x public = rscoin.Key(secret, public=False).id() all_keys += [(public, secret)] # Make up the directory directory = [] for x, (pub, _) in enumerate(all_keys): directory += [(pub, "127.0.0.1", 8080 + x)] # Build the factories factories = {} for pub, sec in all_keys: factory = RSCFactory(sec, directory, public_special, conf_dir="testscratch", N=5) factories[pub] = factory # Make a mass of transactions k1 = rscoin.Key(urandom(32), public=False) k2 = rscoin.Key(urandom(32), public=False) all_tx_in = [] all_tx_out = [] for _ in range(10): #...Notice the tx will have different id, because of R inside the Tx tx1 = rscoin.Tx([], [rscoin.OutputTx(k1.id(), 100)]) tx2 = rscoin.Tx([], [rscoin.OutputTx(k2.id(), 150)]) tx3 = rscoin.Tx( [rscoin.InputTx(tx1.id(), 0), rscoin.InputTx(tx2.id(), 0)], [rscoin.OutputTx(k1.id(), 250)] ) all_tx_in += [ tx1, tx2 ] all_tx_out += [ tx3 ] print "Lens: all_tx_in: %s all_tx_out: %s" % (len(all_tx_in), len(all_tx_out)) for tx in all_tx_in: for kv, vv in tx.get_utxo_out_entries(): for f in factories.values(): f.db[kv] = vv #... here the each tx1 and tx2 are added into utxo of every minetette #... notice here the tx1, tx2, tx3 come from the last time loop of transactions initilization at line 293 data = (tx3, [tx1.serialize(), tx2.serialize()], [k1.export()[0], k2.export()[0]], [k1.sign(tx3.id()), k2.sign(tx3.id())]) # Put the transaction through total = 0 [ kid1, kid2 ] = tx3.get_utxo_in_keys() #... N is the number of mintettes in one shard #... Notice inkey = pack("32sI", intx.tx_id, intx.pos) #... Kid1 and kid2 can be used as transaction id to pass into get_authorities because the first 32 byes are transaction id #... The last 4 byte for 'pos' are ignored in the get_authroties() au1 = get_authorities(directory, kid1, N = 5) au2 = get_authorities(directory, kid2, N = 5) auxes = set(au1 + au2) #??? Does this happen by chance: no overlapping betten au1 and au2 assert len(auxes) == 10 for aid in auxes: assert isinstance(aid, str) and len(aid) == 32 assert aid in factories H, msg, dataCore = package_query(tx3, [tx1, tx2], [k1, k2]) xset = [] rss = [] for kid, f in factories.iteritems(): # resp = f.process_TxQuery(data) instance = f.buildProtocol(None) tr = StringTransport() instance.makeConnection(tr) instance.lineReceived(msg) resp_msg = unpackage_query_response(tr.value().strip()) assert kid == f.key.id() if resp_msg[0] == "OK": [pub, sig, hashhead, seqStr] = resp_msg[1:] total += 1 xset += [ f.key.id() ] rss += [(pub, sig, hashhead, seqStr)] else: pass assert 5 <= total <= 10 assert set(auxes) == set(xset) ## Now test the commit phase assert 5 <= len(rss) <= 10 msg_commit = package_commit(dataCore, rss) #from twisted.python import log #import sys #log.startLogging(sys.stdout) total = 0 for kid, f in factories.iteritems(): instance = f.buildProtocol(None) tr = StringTransport() instance.makeConnection(tr) instance.lineReceived(msg_commit) resp_commit = tr.value().strip() resp_l = unpackage_commit_response(resp_commit) if resp_l[0] == "OK": total += 1 assert total == 5
def test_audit_speed(msg_mass): (sometx, mesages_q) = msg_mass (factory, instance, tr) = sometx responses = [] t0 = timer() for (tx, data, core) in mesages_q: tr.clear() instance.lineReceived(data) response = tr.value() responses += [(tx, data, core, response)] t1 = timer() #print "\nQuery message rate: %2.2f / sec" % (1.0 / ((t1-t0)/(len(mesages_q)))) ## Now we test the Commit t0 = timer() for (tx, data, core, response) in responses: resp = response.split(" ") pub, sig, hashhead, seqStr = map(b64decode, resp[1:]) assert resp[0] == "OK" tr.clear() data = package_commit(core, [(pub, sig, hashhead, seqStr)]) instance.lineReceived(data) response = tr.value() flag, pub, sig, hashhead, seqStr = unpackage_commit_response(response) assert flag == "OK" t1 = timer() #print "\nCommit message rate: %2.2f / sec" % (1.0 / ((t1-t0)/(len(responses)))) ## log verification test t0 = timer() logger = RSCLogger() last_queried_tx, data, core = mesages_q[-1] authPub = factory.key.pub.export(EcPt.POINT_CONVERSION_UNCOMPRESSED) seq = len(mesages_q) quired_hashhead, sig, dataCore = logger.query_hashhead(last_queried_tx.id(), authPub, seq) assert quired_hashhead !=None new_h = sha256(" ".join( dataCore + [quired_hashhead] +[str(seq)])).digest() assert factory.key.verify(new_h, sig) assert logger.verify_log(len(mesages_q), quired_hashhead) == True assert logger.verify_log(int(seqStr), hashhead) == True t1 = timer() ## test hashhead query protocol data = package_hashQuery(last_queried_tx, authPub, str(seq)) tr.clear() instance.lineReceived(data) response = tr.value() quired_hashhead, sig, dataCore = unpackage_hash_response(response) assert quired_hashhead !=None new_h = sha256(" ".join( dataCore + [quired_hashhead] +[str(seq)])).digest() assert factory.key.verify(new_h, sig) auditor = Auditor(factory.directory, factory.special_key) t0 = timer() assert auditor.audit_log(factory.key.id(), "localhost", 27017, factory.get_hash_head(), 2000) t1 = timer() # the seq == the number of entries because there is only one mintette print "\nLog auditing rate: %2.2f / sec" % (1.0 / ((t1-t0)/(seq)))
def test_log_verify(msg_mass): (sometx, mesages_q) = msg_mass (factory, instance, tr) = sometx responses = [] t0 = timer() for (tx, data, core) in mesages_q: tr.clear() instance.lineReceived(data) response = tr.value() responses += [(tx, data, core, response)] t1 = timer() print "\nQuery message rate: %2.2f / sec" % (1.0 / ((t1-t0)/(len(mesages_q)))) ## Now we test the Commit t0 = timer() for (tx, data, core, response) in responses: resp = response.split(" ") pub, sig, hashhead, seqStr = map(b64decode, resp[1:]) assert resp[0] == "OK" tr.clear() data = package_commit(core, [(pub, sig, hashhead, seqStr)]) instance.lineReceived(data) response = tr.value() flag, pub, sig, hashhead, seqStr = unpackage_commit_response(response) assert flag == "OK" t1 = timer() print "\nCommit message rate: %2.2f / sec" % (1.0 / ((t1-t0)/(len(responses)))) ## log verification test t0 = timer() logger = RSCLogger() last_queried_tx, data, core = mesages_q[-1] authPub = factory.key.pub.export(EcPt.POINT_CONVERSION_UNCOMPRESSED) seq = len(mesages_q) quired_hashhead, sig, dataCore = logger.query_hashhead(last_queried_tx.id(), authPub, seq) assert quired_hashhead !=None new_h = sha256(" ".join( dataCore + [quired_hashhead] +[str(seq)])).digest() assert factory.key.verify(new_h, sig) assert logger.verify_log(len(mesages_q), quired_hashhead) == True assert logger.verify_log(int(seqStr), hashhead) == True t1 = timer() ## test hashhead query protocol data = package_hashQuery(last_queried_tx, authPub, str(seq)) tr.clear() instance.lineReceived(data) response = tr.value() quired_hashhead, sig, dataCore = unpackage_hash_response(response) assert quired_hashhead !=None new_h = sha256(" ".join( dataCore + [quired_hashhead] +[str(seq)])).digest() assert factory.key.verify(new_h, sig) assert logger.verify_log(len(mesages_q), quired_hashhead) == True assert logger.verify_log(int(seqStr), hashhead) == True print "\nLog verification rate: %2.2f / sec" % (1.0 / ((t1-t0)/(len(responses)))) ## test query by tx_id,pos inTx = last_queried_tx.inTx[0] tx_id = inTx.tx_id pos = inTx.pos logEntries = logger.query_log_by_inputAddrId(tx_id, pos) assert logEntries != None assert len(logEntries) == 2 ## test the auditor auditor = Auditor(factory.directory, factory.special_key) auditor.connect_to_log_db("localhost", 27017) assert auditor.audit_logEntry(logEntries[0], factory.directory[0][0]) assert auditor.audit_logEntry(logEntries[1], factory.directory[0][0])