def _create_node(self, port): signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", port)) node.is_peer = True return node
def _create_node(self): signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) node.is_peer = True return node
def test_generate_coupons_coupon(self): self._set_clock(2015, 4, 1, 1) signingkey = signed_object.generate_signing_key() ident = signed_object.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10021)) node.is_peer = True path = tempfile.mkdtemp() gossip = Gossip(node) journal = Journal(node, gossip, gossip.dispatcher, consensus=DevModeConsensus(), data_directory=path) journal.global_store.TransactionStores['/BondTransaction'] = \ self.store # creates a redemption updates = Family._generate_coupons(journal) self.assertNotEquals(updates, []) transaction = BondTransaction(updates[0]) transaction.sign_object(self.key) transaction.check_valid(self.store) transaction.apply(self.store) org_usd_holding = self.store["34d813716009ca1786222a44347ccff" "258a4ab6029d936664fde0d13f23992b5"] self.assertEquals(org_usd_holding["amount"], 25000.0)
def _setup(self, port): signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) firstNode = Node(identifier=ident, signingkey=signingkey, address=("localhost", port)) core = Gossip(firstNode) return core
def _create_node(self): signingkey = signed_object.generate_signing_key() ident = signed_object.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", self._next_port)) self.__class__._next_port = self._next_port + 1 return node
def _create_node(self, port): signingkey = sign_obj.generate_signing_key() ident = sign_obj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", port)) return node
def test_journal_transaction_block_missing_transactions(self): # Test missing transactions, should return list of missing transactions minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10002)) path = tempfile.mkdtemp() # Takes a journal, create a temporary directory to use with the journal journal = Journal(node, DataDirectory=path) transBlock.sign_from_node(node) missing = transBlock.missing_transactions(journal) # No missing transactions self.assertEquals(missing, []) minfo = { '__SIGNATURE__': 'Test', '__NONCE__': time.time(), 'Dependencies': [] } transaction = Transaction(minfo) transaction.sign_from_node(node) transBlock.TransactionIDs += [transaction.Identifier] missing = transBlock.missing_transactions(journal) # One missing transactions self.assertEquals(missing, [transaction.Identifier]) journal.TransactionStore[transaction.Identifier] = transaction missing = transBlock.missing_transactions(journal) # Back to no missing transactions self.assertEquals(missing, [])
def _create_node(self): signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) return node
def do_set(self, args): """ set -- Command to set properties of the interpreter set url --url <url> set nodeid --name <name> --keyfile <file> """ pargs = args.split() if len(pargs) == 0: print 'missing subcommand url|nodeid' return try: if pargs[0] == 'url': parser = argparse.ArgumentParser() parser.add_argument('--url', help='url used to connect to a validator', required=True) options = parser.parse_args(pargs) self.BaseURL = options.url print "server URL set to {0}".format(self.BaseURL) return elif pargs[0] == 'nodeid': pargs = args.split() parser = argparse.ArgumentParser() parser.add_argument('--name', help='name to use for the client', default='txnclient') parser.add_argument('--keyfile', help='name of the file that contains ' 'the wif format private key') options = parser.parse_args(pargs[1:]) addr = (socket.gethostbyname("localhost"), 0) name = options.name if options.keyfile: signingkey = generate_signing_key( wifstr=read_key_file(options.keyfile)) else: signingkey = generate_signing_key() identifier = generate_identifier(signingkey) self.LocalNode = Node(address=addr, identifier=identifier, signingkey=signingkey, name=name) print "local id set to {0}".format(self.LocalNode) return else: print "unknown subcommand; {0}".format(pargs[0]) return except Exception as e: print 'an error occured processing {0}: {1}'.format(args, str(e)) return
def test_matching_nothing_to_match(self): signingkey = signed_object.generate_signing_key() ident = signed_object.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10002)) node.is_peer = True path = tempfile.mkdtemp() gossip = Gossip(node) journal = Journal(node, gossip, gossip.dispatcher, consensus=DevModeConsensus(), data_directory=path) journal.global_store.TransactionStores['/BondTransaction'] = \ self.store matched_orders = _generate_match_orders(journal) self.assertEquals(matched_orders, [])
def test_gossip_core_init(self): # Test correct init signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) firstNode = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) core = Gossip(firstNode) self.assertIsNotNone(core)
def _create_node(self, port=None): if port is None: port = self._next_port self.__class__._next_port = self._next_port + 1 signingkey = sign_obj.generate_signing_key() ident = sign_obj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", port)) return node
def test_gossip_core_bad_node_address(self): # Make sure it fails if given a node without an address # Should always throw an error signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey) try: core = Gossip(node) self.fail("Should raise an error") except GossipException, e: self.assertIsInstance(e, GossipException)
def test_add_message(self): # Add a message to a packet, along with source node and destination # Resets the packet attributes with that of the message pak = Packet() # Need signingkey, otherwise throws errors srcNode = Node(identifier="source", signingkey="source") desNode = Node(identifier="destination", signingkey="destination") msg = Message({'__SIGNATURE__': "MsgTestS"}) pak.add_message(msg, srcNode, desNode, 1) # Check that msg and pak have the same attributes self.assertEqual([msg.Identifier, msg.TimeToLive, msg.IsReliable], [pak.Identifier, pak.TimeToLive, pak.IsReliable]) # Check correct SenderID self.assertEqual(srcNode.Identifier, pak.SenderID) # Check correct destinationID and destination RTE self.assertEqual(desNode.Identifier, pak.DestinationID) self.assertEqual(desNode.Estimator.RTO, pak.RoundTripEstimate) # Check correct data and msg self.assertEqual(pak.Message, msg) self.assertEqual(pak.Data, repr(msg))
def __init__(self, baseurl, keystring=None): cmd.Cmd.__init__(self) self.prompt = 'client> ' self.CurrentState = {} self.LedgerWebClient = ledger_web_client.LedgerWebClient(baseurl) signingkey = generate_signing_key( wifstr=keystring) if keystring else generate_signing_key() identifier = generate_identifier(signingkey) self.LocalNode = Node(identifier=identifier, signingkey=signingkey, name="txnclient")
def __init__(self, baseurl, keystring=None): cmd.Cmd.__init__(self) self.prompt = 'client> ' self._current_state = {} self._client = SawtoothClient(baseurl) signingkey = generate_signing_key( wifstr=keystring) if keystring else generate_signing_key() identifier = generate_identifier(signingkey) self._local_node = Node(identifier=identifier, signingkey=signingkey, name="txnclient")
def test_matching_no_quotes(self): signingkey = signed_object.generate_signing_key() ident = signed_object.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10003)) node.is_peer = True path = tempfile.mkdtemp() gossip = Gossip(node) journal = Journal(node, gossip, gossip.dispatcher, consensus=DevModeConsensus(), data_directory=path) org2 = self.store.lookup("organization:name", "Second Bank") bond = self.store.lookup("bond:cusip", "912828R77") transaction = BondTransaction({ "UpdateType": "CreateOrder", 'Updates': [{ "UpdateType": "CreateOrder", "Action": "Buy", "OrderType": "Market", "FirmId": org2["object-id"], "Isin": bond["isin"], "Quantity": 100000, "object_id": "123453716009ca1786222a44347ccff258a4ab6029" + "d936664fde0d13f23992b7" }] }) transaction.sign_object(self.key) try: transaction.check_valid(self.store) transaction.apply(self.store) except InvalidTransactionError: self.fail("This should be valid") journal.global_store.TransactionStores['/BondTransaction'] = \ self.store matched_orders = _generate_match_orders(journal) self.assertEquals(matched_orders, [])
def test_matching_no_order(self): signingkey = signed_object.generate_signing_key() ident = signed_object.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10004)) node.is_peer = True path = tempfile.mkdtemp() gossip = Gossip(node) journal = Journal(node, gossip, gossip.dispatcher, consensus=DevModeConsensus(), data_directory=path) transaction = BondTransaction({ "UpdateType": "CreateQuote", 'Updates': [{ "UpdateType": "CreateQuote", "Firm": "ABCD", "Isin": "US912828R770", "BidPrice": "101", "BidQty": 250000, "AskPrice": "101", "AskQty": 250000, "object_id": "555553716009ca1786222a44347ccff258a4ab6029" + "d936664fde0d13f23992b7" }] }) transaction.sign_object(self.key) try: transaction.check_valid(self.store) transaction.apply(self.store) except InvalidTransactionError: self.fail("This should be valid") journal.global_store.TransactionStores['/BondTransaction'] = \ self.store matched_orders = _generate_match_orders(journal) self.assertEquals(matched_orders, [])
def test_sign_node_assertion(self): # Test that an assertion error is thrown when a node is passed # that does not have a Signingkey # create SignedObject temp = SignedObject({"TestSignatureDictKey": "test"}, "TestSignatureDictKey") # create a Node that does not have a signingKey testNode = Node(name="badNode") try: # should throw an an assertion error, Otherwise fail test temp.sign_from_node(testNode) self.fail("Should have raised an Assertion Error") except AssertionError, e: self.assertIsInstance(e, AssertionError)
def test_journal_transaction_block_build_message(self): # Test build_message, returns a TransactionBlockMessage minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) transBlock.sign_from_node(node) transBlock.Status = tbStatus.valid msg = transBlock.build_message() self.assertEquals(msg.MessageType, "/journal.messages.TransactionBlockMessage" + "/TransactionBlock") self.assertEquals(msg.TransactionBlock, transBlock)
def test_journal_transaction_block_str(self): # Test str function for a signed transaction block minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) # Need to sign TransactionBlock, use sign_from_node form signed object transBlock.sign_from_node(node) self.assertEquals(str(transBlock), "{0}, {1}, {2}, {3:0.2f}" .format(transBlock.BlockNum, transBlock.Identifier[:8], len(transBlock.TransactionIDs), transBlock.CommitTime))
def test_journal_transaction_block_is_valid(self): # Test whether or not a transblock is valid minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10000)) # Takes a journal, create a temporary directory to use with the journal path = tempfile.mkdtemp() journal = Journal(node, DataDirectory=path) # Need to sign TransactionBlock, use sign_from_node form signed object transBlock.sign_from_node(node) self.assertTrue(transBlock.is_valid(journal))
def test_journal_transaction_block_dump(self): # Test that transactions dump the correct info minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) transBlock.sign_from_node(node) transBlock.Status = tbStatus.valid tbDic = transBlock.dump() self.assertEquals(tbDic["TransactionIDs"], []) self.assertEquals(tbDic["TransactionBlockType"], "/TransactionBlock") self.assertEquals(tbDic["BlockNum"], 0) self.assertIsNotNone(tbDic["Signature"]) self.assertNotEquals(tbDic["Signature"], "")
def test_node_init(self): # Test normal Init for a single node signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) self.assertEqual(node.NetHost, "localhost") self.assertEqual(node.NetPort, 8800) self.assertEqual(node.Identifier, ident) self.assertEqual(node.SigningKey, signingkey) self.assertEqual(node.Name, ident[:8]) self.assertFalse(node.is_peer) self.assertIsInstance(node.Estimator, RoundTripEstimator) self.assertIsInstance(node.MessageQ, TransmissionQueue) self.assertEqual(node.Delay, node._fixeddelay)
def test_journal_transaction_block_not_is_valid(self): # Test that an invalid Transblock does not get verified as valid minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10001)) # Takes a journal, create a temporary directory to use with the journal path = tempfile.mkdtemp() journal = Journal(node, DataDirectory=path) # Need to sign TransactionBlock, use sign_from_node form signed object try: transBlock.is_valid(journal) except AssertionError, e: self.assertIsInstance(e, AssertionError)
def test_journal_transaction_block_cmp_nonvalid_blocks(self): # Test that a ValueError is raised when a transBlock is not valid minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock1 = TransactionBlock(minfo) transBlock2 = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) # Need to sign TransactionBlock, use sign_from_node form signed object transBlock1.sign_from_node(node) transBlock2.sign_from_node(node) try: cmp(transBlock2, transBlock1) self.fail("This should cause a ValueError") except ValueError, e1: self.assertIsInstance(e1, ValueError)
def test_journal_transaction_block_update_block_weight(self): # Test block update weight minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock = TransactionBlock(minfo) transBlock.Status = tbStatus.valid signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 10003)) # Takes a journal, create a temporary directory to use with the journal path = tempfile.mkdtemp() journal = Journal(node, DataDirectory=path) transBlock.sign_from_node(node) transBlock.update_block_weight(journal) # No transactions self.assertEquals(transBlock.TransactionDepth, 0) minfo = { '__SIGNATURE__': 'Test', '__NONCE__': time.time(), 'Dependencies': [] } transaction = Transaction(minfo) transaction.sign_from_node(node) transBlock.TransactionIDs += [transaction.Identifier] transBlock.update_block_weight(journal) # One transaction self.assertEquals(transBlock.TransactionDepth, 1) minfo = { '__SIGNATURE__': 'Test', "BlockNum": 1, 'PreviousBlockID': transBlock.Identifier } newTransBlock = TransactionBlock(minfo) newTransBlock.Status = tbStatus.valid journal.BlockStore[transBlock.Identifier] = transBlock newTransBlock.update_block_weight(journal) # Get depth from previous block self.assertEquals(newTransBlock.TransactionDepth, 1)
def test_signed_node(self): # Verify that signed_node and sign_object does not invalidate the # signed object and can be returned to original # create initial signed object temp = SignedObject({"TestSignatureDictKey": "test"}, "TestSignatureDictKey") # save origanl OriginatorID before creating node idBeforeNode = temp.OriginatorID # create a node instance key = generate_private_key() sigkey = SigObj.generate_signing_key(wifstr=key) nodeid = SigObj.generate_identifier(sigkey) testNode = Node(name="testNode", signingkey=sigkey, identifier=nodeid) temp.sign_from_node(testNode) # save new OriginatorID after the idAfterNode = temp.OriginatorID self.assertNotEqual(idAfterNode, idBeforeNode) # check that the signed_object signature is still valid and reset # OrignatorId back to original self.assertTrue(temp.is_valid("unused parameter")) self.assertNotEqual(temp.OriginatorID, idBeforeNode)
def test_journal_transaction_block_cmp_valid_blocks(self): # Test the overridden cmp function # Needs the Blocks to be signed and valid minfo = {'__SIGNATURE__': 'Test', "BlockNum": 0} transBlock1 = TransactionBlock(minfo) transBlock2 = TransactionBlock(minfo) signingkey = SigObj.generate_signing_key() ident = SigObj.generate_identifier(signingkey) node = Node(identifier=ident, signingkey=signingkey, address=("localhost", 8800)) # Need to sign TransactionBlock, use sign_from_node form signed object transBlock1.sign_from_node(node) transBlock2.sign_from_node(node) transBlock1.Status = tbStatus.valid transBlock2.Status = tbStatus.valid # Test Equal Transaction Blocks self.assertEquals(cmp(transBlock2, transBlock1), 0) # Test a Transaction Block with greater Transaction Depth transBlock2.TransactionDepth = 10 self.assertEquals(cmp(transBlock2, transBlock1), 1) # Test a Transaction Block with lesser Transaction Depth transBlock1.TransactionDepth = 20 self.assertEquals(cmp(transBlock2, transBlock1), -1)