def test_scenario_b(self): state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone(state.n2i("//BANK-trader")) self.assertIsNotNone(state.n2i("//BANK-trader/USD")) self.assertIsNotNone(state.n2i("//BANK-trader/paper")) self.assertIsNotNone(state.n2i("//BANK-dealer")) self.assertIsNotNone(state.n2i("//BANK-dealer/USD")) self.assertIsNotNone(state.n2i("//BANK-dealer/paper")) self.assertIn("count", state.State[state.n2i("//BANK-trader/USD")]) self.assertIn("count", state.State[state.n2i("//BANK-trader/paper")]) self.assertIn("count", state.State[state.n2i("//BANK-dealer/USD")]) self.assertIn("count", state.State[state.n2i("//BANK-dealer/paper")]) trader_usd = state.State[state.n2i("//BANK-trader/USD")]["count"] trader_paper = state.State[state.n2i("//BANK-trader/paper")]["count"] dealer_usd = state.State[state.n2i("//BANK-dealer/USD")]["count"] dealer_paper = state.State[state.n2i("//BANK-dealer/paper")]["count"] client_cli.main( args=["--name", "BANK-trader", "--script", os.path.join(self.scenarios_path, "scenario_b_1_trader"), "--echo", "--url", self.url]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone(state.n2i("//BANK-trader/offer-b")) client_cli.main( args=["--name", "BANK-dealer", "--script", os.path.join(self.scenarios_path, "scenario_b_2_dealer"), "--echo", "--url", self.url]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertEquals(state.State[state.n2i("//BANK-trader/USD")]["count"], trader_usd - 99388) self.assertEquals(state.State[state.n2i("//BANK-dealer/USD")]["count"], dealer_usd + 99388) self.assertEquals( state.State[state.n2i("//BANK-trader/paper")]["count"], trader_paper + 1) self.assertEquals( state.State[state.n2i("//BANK-dealer/paper")]["count"], dealer_paper - 1)
def test_scenario_a(self): state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone(state.n2i("//BANK-trader", 'Participant')) self.assertIsNotNone(state.n2i("//BANK-trader/USD", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-trader/paper", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-agent", 'Participant')) self.assertIsNotNone(state.n2i("//BANK-agent/USD", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-agent/paper", 'Holding')) self.assertEquals( state.State[state.n2i("//BANK-trader/USD", 'Holding')]["count"], 1000000) self.assertEquals( state.State[state.n2i("//BANK-agent/USD", 'Holding')]["count"], 1000000) self.assertEquals( state.State[state.n2i("//BANK-trader/paper", 'Holding')]["count"], 10) self.assertEquals( state.State[state.n2i("//BANK-agent/paper", 'Holding')]["count"], 10) client_cli.main(args=[ "--name", "BANK-trader", "--script", os.path.join(os.path.dirname(__file__), "cp_scenarios", "scenario_a_1_trader"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone( state.n2i("//BANK-trader/offer-a", 'ExchangeOffer')) client_cli.main(args=[ "--name", "BANK-agent", "--script", os.path.join(self.scenarios_path, "scenario_a_2_agent"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertEquals( state.State[state.n2i("//BANK-trader/USD", 'Holding')]["count"], 900612) self.assertEquals( state.State[state.n2i("//BANK-agent/USD", 'Holding')]["count"], 1099388) self.assertEquals( state.State[state.n2i("//BANK-trader/paper", 'Holding')]["count"], 11) self.assertEquals( state.State[state.n2i("//BANK-agent/paper", "Holding")]["count"], 9)
def transactions_exchange(self): client_cli.main(args=[ "--name", "alice", "--script", os.path.join(self.script_path, "ex_alice_transactions"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertEqual( state.State[state.n2i("//bob/USD", "Holding")]['count'], 1024) self.assertEqual( state.State[state.n2i("//bob/batches/choc_chip001", "Holding")]['count'], 12) self.assertEqual( state.State[state.n2i("//bob/holding/token", "Holding")]['count'], 1) self.assertEqual( state.State[state.n2i("//bob/jars/choc_chip", "Holding")]['count'], 0) self.assertEqual( state.State[state.n2i("//alice/USD", "Holding")]['count'], 976) self.assertEqual( state.State[state.n2i("//alice/holding/token", "Holding")]['count'], 1) self.assertEqual( state.State[state.n2i("//alice/jars/choc_chip", "Holding")]['count'], 12)
def test_scenario_setup(self): state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone(state.n2i("//marketplace/asset/token", 'Asset')) self.assertIsNotNone(state.n2i("//mkt", 'Participant')) self.assertIsNotNone(state.n2i("//mkt/market/account", 'Account')) self.assertIsNotNone( state.n2i("//mkt/asset-type/currency", 'AssetType')) self.assertIsNotNone( state.n2i("//mkt/asset-type/commercialpaper", 'AssetType')) self.assertIsNotNone(state.n2i("//mkt/asset/currency/USD", 'Asset')) self.assertIsNotNone( state.n2i("//mkt/asset/commercialpaper/note", 'Asset')) self.assertIsNotNone( state.n2i("//mkt/market/holding/currency/USD", 'Holding')) self.assertIsNotNone(state.n2i("//mkt/market/holding/token", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-trader", 'Participant')) self.assertIsNotNone(state.n2i("//BANK-trader/USD", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-trader/paper", 'Holding')) self.assertIsNotNone( state.n2i("//BANK-trader/holding/token", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-agent", 'Participant')) self.assertIsNotNone(state.n2i("//BANK-agent/USD", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-agent/paper", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-agent/holding/token", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-dealer", 'Participant')) self.assertIsNotNone(state.n2i("//BANK-dealer/USD", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-dealer/paper", 'Holding')) self.assertIsNotNone( state.n2i("//BANK-dealer/holding/token", 'Holding'))
def test_transactions_reg(self): client_cli.main(args=[ "--name", "user", "--script", os.path.join(self.script_path, "reg_transactions"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone(state.n2i("//user", 'Participant')) self.assertIsNotNone(state.n2i("//user/user/account", 'Account')) self.assertIsNotNone( state.n2i("//user/asset-type/currency", 'AssetType')) self.assertIsNotNone(state.n2i("//user/asset-type/good", 'AssetType')) self.assertIsNotNone(state.n2i("//user/asset/currency/USD", 'Asset')) self.assertIsNotNone(state.n2i("//user/asset/good/paper", 'Asset')) self.assertIsNotNone( state.n2i("//user/user/holding/currency/USD", 'Holding')) self.assertIsNotNone( state.n2i("//user/user/holding/good/paper", 'Holding')) self.assertIsNotNone(state.n2i("//user/user/holding/token", 'Holding')) self.assertIsNotNone( state.n2i("//user/user/holding/good/paper", 'Holding')) self.assertIsNotNone( state.n2i("//user/user/holding/good/paper", 'Holding'))
def transactions_reg(self): client_cli.main(args=[ '--name', 'mkt', '--script', os.path.join(self.script_path, 'reg_mkt_transactions'), '--echo', '--url', self.url ]) client_cli.main(args=[ "--name", "alice", "--script", os.path.join(self.script_path, "reg_alice_transactions"), "--echo", "--url", self.url ]) client_cli.main(args=[ "--name", "bob", "--script", os.path.join(self.script_path, "reg_bob_transactions"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone(state.n2i("//alice", 'Participant')) self.assertIsNotNone(state.n2i("//mkt", 'Participant')) self.assertIsNotNone(state.n2i("//bob", 'Participant')) self.assertIsNotNone(state.n2i("//alice/account", 'Account')) self.assertIsNotNone( state.n2i("//mkt/asset-type/currency", 'AssetType')) self.assertIsNotNone(state.n2i("//mkt/asset-type/cookie", 'AssetType')) self.assertIsNotNone(state.n2i("//mkt/asset/currency/USD", 'Asset')) self.assertIsNotNone(state.n2i("//alice/USD", 'Holding')) self.assertIsNotNone(state.n2i("//alice/jars/choc_chip", 'Holding')) self.assertIsNotNone(state.n2i("//alice/holding/token", 'Holding')) self.assertIsNotNone(state.n2i("//bob/USD", "Holding")) self.assertIsNotNone(state.n2i("//bob/jars/choc_chip", "Holding"))
def setUpClass(cls): cls.vnm = None try: if 'TEST_VALIDATOR_URLS' in os.environ: urls = (os.environ['TEST_VALIDATOR_URLS']).split(",") cls.url = urls[0] else: vnm_config = defaultValidatorConfig.copy() if 'mktplace.transactions.market_place' not in \ vnm_config['TransactionFamilies']: vnm_config['TransactionFamilies'].append( 'mktplace.transactions.market_place') vnm_config['InitialWaitTime'] = 1 vnm_config['TargetWaitTime'] = 1 cls.vnm = ValidatorNetworkManager(http_port=9500, udp_port=9600, cfg=vnm_config) cls.vnm.launch_network(1) # the url of the initial validator cls.url = cls.vnm.urls()[0] + '/' os.environ['CURRENCYHOME'] = os.path.join( os.path.dirname(__file__), "all_transactions") cls.script_path = os.path.join(os.path.dirname(__file__), 'all_transactions') state = mktplace_state.MarketPlaceState(cls.url) state.fetch() except: if cls.vnm is not None: cls.vnm.shutdown() raise
def setUpClass(cls): cls.vnm = None try: if 'TEST_VALIDATOR_URLS' in os.environ: urls = (os.environ['TEST_VALIDATOR_URLS']).split(",") cls.url = urls[0] else: families = ['mktplace.transactions.market_place'] overrides = { 'InitialWaitTime': 1, 'TargetWaitTime': 1, "TransactionFamilies": families, } cls.vnm = get_default_vnm(1, overrides=overrides) cls.vnm.do_genesis() cls.vnm.launch() # the url of the initial validator cls.url = cls.vnm.urls()[0] + '/' os.environ['CURRENCYHOME'] = os.path.join( os.path.dirname(__file__), "all_transactions") cls.script_path = os.path.join(os.path.dirname(__file__), 'all_transactions') state = mktplace_state.MarketPlaceState(cls.url) state.fetch() except: if cls.vnm is not None: cls.vnm.shutdown() raise
def setUp(self): self.save_environ = os.environ.copy() os.environ['CURRENCYHOME'] = os.path.join(os.path.dirname(__file__), "all_transactions") self.script_path = os.path.join(os.path.dirname(__file__), 'all_transactions') state = mktplace_state.MarketPlaceState(self.url) state.fetch()
def setUpClass(cls): cls.vnm = None try: if 'TEST_VALIDATOR_URLS' in os.environ: urls = (os.environ['TEST_VALIDATOR_URLS']).split(",") cls.url = urls[0] else: vnm_config = defaultValidatorConfig.copy() if 'mktplace.transactions.market_place' not in \ vnm_config['TransactionFamilies']: vnm_config['TransactionFamilies'].append( 'mktplace.transactions.market_place') cls.vnm = ValidatorNetworkManager(http_port=9500, udp_port=9600, cfg=vnm_config) cls.vnm.launch_network(5) # the url of the initial validator cls.url = cls.vnm.urls()[0] + '/' os.environ['CURRENCYHOME'] = os.path.join( os.path.dirname(__file__), "cp_scenarios") cls.scenarios_path = os.path.join(os.path.dirname(__file__), 'cp_scenarios') client_cli.main(args=[ "--name", "mkt", "--script", os.path.join(cls.scenarios_path, "scenario_setup_1_mkt"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-trader", "--script", os.path.join(os.path.dirname(__file__), "cp_scenarios", "scenario_setup_2_trader"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-agent", "--script", os.path.join(cls.scenarios_path, "scenario_setup_3_agent"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-dealer", "--script", os.path.join(cls.scenarios_path, "scenario_setup_4_dealer"), "--echo", "--url", cls.url ]) state = mktplace_state.MarketPlaceState(cls.url) state.fetch() except: if cls.vnm is not None: cls.vnm.shutdown() raise
def setUpClass(cls): cls.vnm = None try: if 'TEST_VALIDATOR_URLS' in os.environ: urls = (os.environ['TEST_VALIDATOR_URLS']).split(",") cls.url = urls[0] else: families = ['mktplace.transactions.market_place'] overrides = { "TransactionFamilies": families, } cls.vnm = get_default_vnm(5, overrides=overrides) cls.vnm.do_genesis() cls.vnm.launch() # the url of the initial validator cls.url = cls.vnm.urls()[0] + '/' os.environ['CURRENCYHOME'] = os.path.join( os.path.dirname(__file__), "cp_scenarios") cls.scenarios_path = os.path.join(os.path.dirname(__file__), 'cp_scenarios') client_cli.main(args=[ "--name", "mkt", "--script", os.path.join(cls.scenarios_path, "scenario_setup_1_mkt"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-trader", "--script", os.path.join(os.path.dirname(__file__), "cp_scenarios", "scenario_setup_2_trader"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-agent", "--script", os.path.join(cls.scenarios_path, "scenario_setup_3_agent"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-dealer", "--script", os.path.join(cls.scenarios_path, "scenario_setup_4_dealer"), "--echo", "--url", cls.url ]) state = mktplace_state.MarketPlaceState(cls.url) state.fetch() except: if cls.vnm is not None: cls.vnm.shutdown() raise
def transactions_unr(self): client_cli.main(args=[ "--name", "alice", "--script", os.path.join(self.script_path, "unr_alice_transactions"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNone(state.n2i("//alice", 'Participant')) self.assertIsNone(state.n2i("//alice/jars/choc_chip", "Holding")) self.assertIsNone(state.n2i("//alice/USD", "Holding")) self.assertIsNone(state.n2i("//alice/holding/token", "Holding")) self.assertIsNone(state.n2i("//alice/account", "Account"))
def setup(self): self.state = mktplace_state.MarketPlaceState(self.urls[0]) with Progress("Creating participants") as p: for i in range(0, self.count): name = "actor-{}".format(i) keyfile = os.path.join(self.testDir, "{}.wif".format(name)) if os.path.exists(keyfile): key = read_key_file(keyfile) else: key = signing.encode_privkey(signing.generate_privkey(), 'wif') write_key_file(keyfile, key) url = self.urls[random.randint(0, len(self.urls) - 1)] a = MktActor(name, url, key) self.Actors.append(a) p.step() with Progress("Registering actors assets") as p: for a in self.Actors: # create assets a.register_asset(a.Name + "-asset") p.step() self.wait_for_transaction_commits() with Progress("Registering holdings") as p: for a in self.Actors: a.update() a.offers = [] for a2 in self.Actors: count = 0 if a is a2: # for each iteration we need 1 to pay with and 1 to # give count = 2 * self.count * self.iterations for ast in a2.assets.keys(): a.register_holding(ast, count) p.step() self.wait_for_transaction_commits()
def test_transactions_unr(self): client_cli.main(args=[ "--name", "user", "--script", os.path.join(self.script_path, "unr_transactions"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNone(state.n2i("//user")) self.assertIsNone(state.n2i("//user/user/account")) self.assertIsNone(state.n2i("//user/asset-type/currency")) self.assertIsNone(state.n2i("//user/asset-type/good")) self.assertIsNone(state.n2i("//user/asset/currency/USD")) self.assertIsNone(state.n2i("//user/asset/good/paper")) self.assertIsNone(state.n2i("//user/user/holding/currency/USD")) self.assertIsNone(state.n2i("//user/user/holding/good/paper")) self.assertIsNone(state.n2i("//user/user/holding/token")) self.assertIsNone(state.n2i("//user/user/holding/good/paper")) self.assertIsNone(state.n2i("//user/user/holding/good/paper"))
def setUpClass(cls): cls.vnm = None try: cls.url = "http://localhost:8800" os.environ['CURRENCYHOME'] = os.path.join( os.path.dirname(__file__), "cp_scenarios") cls.scenarios_path = os.path.join(os.path.dirname(__file__), 'cp_scenarios') client_cli.main(args=[ "--name", "mkt", "--script", os.path.join(cls.scenarios_path, "scenario_setup_1_mkt"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-trader", "--script", os.path.join(os.path.dirname(__file__), "cp_scenarios", "scenario_setup_2_trader"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-agent", "--script", os.path.join(cls.scenarios_path, "scenario_setup_3_agent"), "--echo", "--url", cls.url ]) client_cli.main(args=[ "--name", "BANK-dealer", "--script", os.path.join(cls.scenarios_path, "scenario_setup_4_dealer"), "--echo", "--url", cls.url ]) state = mktplace_state.MarketPlaceState(cls.url) state.fetch() except: if cls.vnm is not None: cls.vnm.shutdown() raise
def __init__(self, name, ledger_url, key=None, state=None): # record of all transactions this actor has committed self.transactions = [] self.accounts = None self.assetType = None self.assets = {} self.holdings = {} # map, fully qualified names self.holdingsByAsset = {} # map, fqn asset to fqn holding id. self.Name = name self.state = state if self.state is None: self.state = mktplace_state.MarketPlaceState(ledger_url) # create keys # Generate key for validator self.Key = key if not self.Key: self.Key = generate_private_key() self.creator = self.state.n2i('//{0}'.format(name)) if self.creator: addr = get_address_from_private_key_wif(self.Key) partxn = self.state.State[self.creator] if addr != partxn["address"]: raise Exception("Participant key mismatch") self.client = mktplace_client.MarketPlaceClient(ledger_url, creator=self.creator, name=name, keystring=self.Key, state=self.state) if not self.creator: self.creator = self.client.register_participant(name) self.client.CreatorID = self.creator self.transactions.append(self.creator) if not self.creator: raise self.state.CreatorID = self.creator self._register_account() self._register_asset_type() # LOAD objects from state for no in self.state.list(creator=self.creator, fields="*"): n = no['name'] if n.startswith(self.get_qualified_name("/assets/")): o = self.get_state_object(n) if self.creator != o["creator"]: raise Exception("Transaction namespace violation.") self.assets[n] = no elif n.startswith(self.get_qualified_name("/assets")): o = self.get_state_object(n) if self.creator != o["creator"]: raise Exception("Transaction namespace violation.") self.assetType = no elif n.startswith(self.get_qualified_name("/account/")): o = self.get_state_object(n) if self.creator != o["creator"]: raise Exception("Transaction namespace violation.") self.holdings[n] = self.state.NameMap[n] asset_name = self.state.i2n(o["asset"]) self.holdingsByAsset[asset_name] = no
def test_scenario_d(self): state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone(state.n2i("//BANK-trader", 'Participant')) self.assertIsNotNone(state.n2i("//BANK-trader/USD", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-trader/paper", 'Holding')) self.assertIsNotNone(state.n2i("//BANK-agent", 'Participant')) self.assertIsNotNone(state.n2i("//BANK-agent/USD", "Holding")) self.assertIsNotNone(state.n2i("//BANK-agent/paper", 'Holding')) self.assertIn("count", state.State[state.n2i("//BANK-trader/USD", 'Holding')]) self.assertIn("count", state.State[state.n2i("//BANK-trader/paper", "Holding")]) self.assertIn("count", state.State[state.n2i("//BANK-agent/USD", 'Holding')]) self.assertIn("count", state.State[state.n2i("//BANK-agent/paper", "Holding")]) trader_usd = state.State[state.n2i("//BANK-trader/USD", 'Holding')]["count"] trader_paper = state.State[state.n2i("//BANK-trader/paper", 'Holding')]["count"] agent_usd = state.State[state.n2i("//BANK-agent/USD", 'Holding')]["count"] agent_paper = state.State[state.n2i("//BANK-agent/paper", 'Holding')]["count"] client_cli.main(args=[ "--name", "BANK-trader", "--script", os.path.join(self.scenarios_path, "scenario_d_1_trader"), "--echo", "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertIsNotNone( state.n2i("//BANK-trader/offer-d", 'ExchangeOffer')) client_cli.main(args=[ "--name", "BANK-agent", "--script", os.path.join(self.scenarios_path, "scenario_d_2_agent"), "--url", self.url ]) state = mktplace_state.MarketPlaceState(self.url) state.fetch() self.assertEquals( state.State[state.n2i("//BANK-trader/USD", 'Holding')]["count"], trader_usd + 100000) self.assertEquals( state.State[state.n2i("//BANK-agent/USD", 'Holding')]["count"], agent_usd - 100000) self.assertEquals( state.State[state.n2i("//BANK-trader/paper", 'Holding')]["count"], trader_paper - 1) self.assertEquals( state.State[state.n2i("//BANK-agent/paper", "Holding")]["count"], agent_paper + 1)