Exemplo n.º 1
0
    def test_real(self):
        ewctrl = EWalletController(self.pwallet.get_model(), self.wctrl)
        config = {"offer_expiry_interval": 30,
                  "ep_expiry_interval": 30}
        comm1 = MockComm()
        comm2 = MockComm()
        comm1.add_peer(comm2)
        comm2.add_peer(comm1)
        agent1 = EAgent(ewctrl, config, comm1)
        agent2 = EAgent(ewctrl, config, comm2)

        self.cd0 = "obc:cc8e64cef1a880f5132e73b5a1f52a72565c92afa8ec36c445c635fe37b372fd:0:263370"
        self.cd1 = "obc:caff27b3fe0a826b776906aceafecac7bb34af16971b8bd790170329309391ac:0:265577"

        cv0 = { 'color_spec' : self.cd0, 'value' : 100 }
        cv1 = { 'color_spec' : self.cd1, 'value' : 200 }

        ag1_offer = MyEOffer(None, cv0, cv1)
        ag2_offer = MyEOffer(None, cv0, cv1)
        ag2_offer.auto_post = False

        agent1.register_my_offer(ag1_offer)
        agent2.register_my_offer(ag2_offer)
        for _ in xrange(3):
            agent1._update_state()
            agent2._update_state()
Exemplo n.º 2
0
    def test_real(self):
        ewctrl = EWalletController(self.pwallet.get_model(), self.wctrl)
        config = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        comm1 = MockComm()
        comm2 = MockComm()
        comm1.add_peer(comm2)
        comm2.add_peer(comm1)
        agent1 = EAgent(ewctrl, config, comm1)
        agent2 = EAgent(ewctrl, config, comm2)

        self.cd0 = "obc:cc8e64cef1a880f5132e73b5a1f52a72565c92afa8ec36c445c635fe37b372fd:0:263370"
        self.cd1 = "obc:caff27b3fe0a826b776906aceafecac7bb34af16971b8bd790170329309391ac:0:265577"

        cv0 = {'color_spec': self.cd0, 'value': 100}
        cv1 = {'color_spec': self.cd1, 'value': 200}

        ag1_offer = MyEOffer(None, cv0, cv1)
        ag2_offer = MyEOffer(None, cv0, cv1)
        ag2_offer.auto_post = False

        agent1.register_my_offer(ag1_offer)
        agent2.register_my_offer(ag2_offer)
        for _ in xrange(3):
            agent1._update_state()
            agent2._update_state()
Exemplo n.º 3
0
    def setUp(self):
        self.colordef0 = POBColorDefinition(1, {
            'txhash': 'genesis',
            'outindex': 0
        })
        self.colordef1 = OBColorDefinition(2, {
            'txhash': 'genesis',
            'outindex': 0
        })
        self.colorvalue0 = SimpleColorValue(colordef=self.colordef0,
                                            value=1,
                                            label='test')
        self.colorvalue1 = SimpleColorValue(colordef=self.colordef0,
                                            value=2,
                                            label='test2')
        self.colorvalue2 = SimpleColorValue(colordef=self.colordef1, value=1)

        self.e0 = MyEOffer.from_data({
            'oid': 1,
            'A': self.colorvalue0,
            'B': self.colorvalue1
        })
        self.e1 = MyEOffer.from_data({
            'oid': 2,
            'A': self.colorvalue0,
            'B': self.colorvalue1
        })
Exemplo n.º 4
0
    def test_real(self):
        ewctrl = EWalletController(self.pwallet.get_model(), self.wctrl)
        config = {"offer_expiry_interval": 30,
                  "ep_expiry_interval": 30}
        comm1 = MockComm()
        comm2 = MockComm()
        comm1.add_peer(comm2)
        comm2.add_peer(comm1)
        agent1 = EAgent(ewctrl, config, comm1)
        agent2 = EAgent(ewctrl, config, comm2)

        frobla_color_desc = "obc:cc8e64cef1a880f5132e73b5a1f52a72565c92afa8ec36c445c635fe37b372fd:0:263370"
        foo_color_desc = "obc:caff27b3fe0a826b776906aceafecac7bb34af16971b8bd790170329309391ac:0:265577"

        self.cd0 = OBColorDefinition(1, {'txhash': 'cc8e64cef1a880f5132e73b5a1f52a72565c92afa8ec36c445c635fe37b372fd',
                                         'outindex': 0, 'height':263370})

        self.cd1 = OBColorDefinition(1, {'txhash': 'caff27b3fe0a826b776906aceafecac7bb34af16971b8bd790170329309391ac',
                                         'outindex': 0, 'height':265577})

        cv0 = SimpleColorValue(colordef=self.cd0, value=100)
        cv1 = SimpleColorValue(colordef=self.cd1, value=200)

        ag1_offer = MyEOffer(None, cv0, cv1)
        ag2_offer = MyEOffer(None, cv0, cv1, False)

        agent1.register_my_offer(ag1_offer)
        agent2.register_my_offer(ag2_offer)
        for _ in xrange(3):
            agent1._update_state()
            agent2._update_state()
Exemplo n.º 5
0
    def test_tx_spec(self):
        alice_cv = {'color_spec': self.color_spec, 'value': 10}
        bob_cv = {'color_spec': "", 'value': 500}
        alice_offer = MyEOffer(None, alice_cv, bob_cv)
        bob_offer = MyEOffer(None, bob_cv, alice_cv)
        bob_etx = self.ewc.make_etx_spec(bob_cv, alice_cv)
        self.assertTrue(isinstance(bob_etx, ETxSpec))
        for target in bob_etx.targets:
            # check address
            address = target[0]
            self.assertTrue(isinstance(address, type(u"unicode")))
            # TODO check address is correct format

            # check color_spec
            color_spec = target[1]
            self.assertTrue(isinstance(color_spec, type("str")))
            color_spec_parts = len(color_spec.split(":"))
            self.assertTrue(color_spec_parts == 4 or color_spec_parts == 1)

            # check value
            value = target[2]
            self.assertTrue(isinstance(value, type(10)))
        signed = self.ewc.make_reply_tx(bob_etx, alice_cv, bob_cv)
        self.assertTrue(isinstance(signed, RawTxSpec))

        self.ewc.publish_tx(signed, alice_offer)

        alice_etx = self.ewc.make_etx_spec(alice_cv, bob_cv)
        self.assertTrue(isinstance(alice_etx, ETxSpec))
        for target in alice_etx.targets:
            # check address
            address = target[0]
            self.assertTrue(isinstance(address, type(u"unicode")))
            # TODO check address is correct format

            # check color_spec
            color_spec = target[1]
            self.assertTrue(isinstance(color_spec, type("str")))
            color_spec_parts = len(color_spec.split(":"))
            self.assertTrue(color_spec_parts == 4 or color_spec_parts == 1)

            # check value
            value = target[2]
            self.assertTrue(isinstance(value, type(10)))
        signed = self.ewc.make_reply_tx(alice_etx, bob_cv, alice_cv)
        self.assertTrue(isinstance(signed, RawTxSpec))

        oets = OperationalETxSpec(self.model, self.ewc)
        oets.set_our_value_limit(bob_cv)
        oets.prepare_inputs(alice_etx)
        zero = SimpleColorValue(colordef=UNCOLORED_MARKER, value=0)
        self.assertRaises(ZeroSelectError, oets.select_coins, zero)
        toomuch = SimpleColorValue(colordef=UNCOLORED_MARKER,
                                   value=10000000000000)
        self.assertRaises(InsufficientFundsError, oets.select_coins, toomuch)
Exemplo n.º 6
0
    def test_real(self):
        self.add_coins()
        self.assertFalse(self.agent0.has_active_ep())
        self.agent0.register_my_offer(self.offer0)
        self.agent1.register_my_offer(self.offer1)
        for i in range(3):
            self.agent0._update_state()
            self.agent1._update_state()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        m0 = self.comm0.get_messages()
        m1 = self.comm1.get_messages()
        self.assertEquals(len(m0), 2)
        self.assertEquals(len(m1), 2)

        self.assertEquals(self.offer0.get_data(), m0[1]['offer'])
        self.assertEquals(self.offer0.get_data(), m1[1]['offer'])
        # expire offers
        offer2 = MyEOffer(None, self.cv0, self.cv1)

        self.agent0.register_my_offer(offer2)
        self.agent0.update()
        self.agent0.update()
        self.agent1.update()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.update()
        self.agent1.update()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        offer3 = MyEOffer(None, self.cv1, self.cv0)
        self.agent0.make_exchange_proposal(offer3, offer2)
        self.assertRaises(Exception, self.agent0.make_exchange_proposal,
                          offer3, offer2)
        ep = MyEProposal(self.ewc, offer3, offer2)

        def raiseException(x):
            raise Exception()

        self.agent0.set_event_handler('accept_ep', raiseException)

        # methods that should do nothing now that we have an active ep
        self.agent0.accept_exchange_proposal(ep)
        self.agent0.match_offers()

        self.agent0.register_their_offer(offer3)
        self.agent0.accept_exchange_proposal(ep)
        ep2 = MyEProposal(self.ewc, offer3, offer2)
        self.agent0.dispatch_exchange_proposal(ep2.get_data())

        # test a few corner cases
        self.agent0.register_my_offer(offer2)
        offer2.refresh(-1000)
        self.assertTrue(offer2.expired())
        self.agent0.service_my_offers()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.register_their_offer(offer3)
        offer3.refresh(-1000)
        self.agent0.service_their_offers()
Exemplo n.º 7
0
 def p2ptrade_make_offer(self, we_sell, params):
     asset = self.get_asset_definition(params['moniker'])
     value = asset.parse_value(params['value'])
     bitcoin = self.get_asset_definition('bitcoin')
     price = bitcoin.parse_value(params['price'])
     total = int(float(value)/float(asset.unit)*float(price))
     color_desc = asset.get_color_set().color_desc_list[0]
     sell_side = {"color_spec": color_desc, "value": value}
     buy_side = {"color_spec": "", "value": total}
     if we_sell:
         return MyEOffer(None, sell_side, buy_side)
     else:
         return MyEOffer(None, buy_side, sell_side)
Exemplo n.º 8
0
    def test_basic(self):
        model = MockModel()
        ewctrl = EWalletController(model, None)
        config = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        comm = MockComm()
        agent = EAgent(ewctrl, config, comm)

        # At this point the agent should not have an active proposal
        self.assertFalse(agent.has_active_ep())
        # no messages should have been sent to the network
        self.assertEqual(len(comm.get_messages()), 0)

        color_spec = "obc:03524a4d6492e8d43cb6f3906a99be5a1bcd93916241f759812828b301f25a6c:0:153267"

        cv0 = {'color_spec': "", 'value': 100}
        cv1 = {'color_spec': color_spec, 'value': 200}

        my_offer = MyEOffer(None, cv0, cv1)

        their_offer = EOffer('abcdef', cv1, cv0)

        agent.register_my_offer(my_offer)
        agent.register_their_offer(their_offer)
        agent.update()

        # Agent should have an active exchange proposal
        self.assertTrue(agent.has_active_ep())
        # Exchange proposal should have been sent over comm
        # it should be the only message, as we should not resend our offer
        # if their is an active proposal to match it
        self.assertTrue(len(comm.get_messages()), 1)
        [proposal] = comm.get_messages()
        # The offer data should be in the proposal
        their_offer_data = their_offer.get_data()
        self.assertEquals(their_offer_data, proposal["offer"])
Exemplo n.º 9
0
    def setUp(self):
        self.colordef0 = POBColorDefinition(
            1, {'txhash': 'genesis', 'outindex': 0})
        self.colordef1 = OBColorDefinition(
            2, {'txhash': 'genesis', 'outindex': 0})
        self.colorvalue0 = SimpleColorValue(colordef=self.colordef0,
                                    value=1, label='test')
        self.colorvalue1 = SimpleColorValue(colordef=self.colordef0,
                                    value=2, label='test2')
        self.colorvalue2 = SimpleColorValue(colordef=self.colordef1,
                                    value=1)

        self.e0 = MyEOffer.from_data({'oid':1,
                                      'A': self.colorvalue0,
                                      'B': self.colorvalue1 })
        self.e1 = MyEOffer.from_data({'oid':2,
                                      'A': self.colorvalue0,
                                      'B': self.colorvalue1 })
Exemplo n.º 10
0
    def test_real(self):
        self.add_coins()
        self.assertFalse(self.agent0.has_active_ep())
        self.agent0.register_my_offer(self.offer0)
        self.agent1.register_my_offer(self.offer1)
        for i in range(3):
            self.agent0._update_state()
            self.agent1._update_state()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        m0 = self.comm0.get_messages()
        m1 = self.comm1.get_messages()
        self.assertEquals(len(m0), 2)
        self.assertEquals(len(m1), 2)

        self.assertEquals(self.offer0.get_data(), m0[1]["offer"])
        self.assertEquals(self.offer0.get_data(), m1[1]["offer"])
        # expire offers
        offer2 = MyEOffer(None, self.cv0, self.cv1)

        self.agent0.register_my_offer(offer2)
        self.agent0.update()
        self.agent0.update()
        self.agent1.update()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.update()
        self.agent1.update()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        offer3 = MyEOffer(None, self.cv1, self.cv0)
        self.agent0.make_exchange_proposal(offer3, offer2)
        self.assertRaises(Exception, self.agent0.make_exchange_proposal, offer3, offer2)
        ep = MyEProposal(self.ewc, offer3, offer2)

        def raiseException(x):
            raise Exception()

        self.agent0.set_event_handler("accept_ep", raiseException)

        # methods that should do nothing now that we have an active ep
        self.agent0.accept_exchange_proposal(ep)
        self.agent0.match_offers()

        self.agent0.register_their_offer(offer3)
        self.agent0.accept_exchange_proposal(ep)
        ep2 = MyEProposal(self.ewc, offer3, offer2)
        self.agent0.dispatch_exchange_proposal(ep2.get_data())

        # test a few corner cases
        self.agent0.register_my_offer(offer2)
        offer2.refresh(-1000)
        self.assertTrue(offer2.expired())
        self.agent0.service_my_offers()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.register_their_offer(offer3)
        offer3.refresh(-1000)
        self.agent0.service_their_offers()
Exemplo n.º 11
0
    def _p2ptrade_make_offer(self, we_sell, moniker, value, price, wait):

        # sanitize inputs
        asset = sanitize.asset(self.model, moniker)
        bitcoin = sanitize.asset(self.model, 'bitcoin')
        value = sanitize.assetamount(asset, value)
        price = sanitize.assetamount(bitcoin, price)
        wait = sanitize.integer(wait)

        total = int(Decimal(value) / Decimal(asset.unit) * Decimal(price))
        color_desc = asset.get_color_set().color_desc_list[0]
        sell_side = {"color_spec": color_desc, "value": value}
        buy_side = {"color_spec": "", "value": total}
        agent = self._init_p2ptrade()
        if we_sell:
            agent.register_my_offer(MyEOffer(None, sell_side, buy_side))
        else:
            agent.register_my_offer(MyEOffer(None, buy_side, sell_side))
        self._p2ptrade_wait(agent, wait)
Exemplo n.º 12
0
    def setUp(self):
        self.pwallet = PersistentWallet(None, True)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        adm = self.model.get_asset_definition_manager()

        # make sure you have the asset 'testobc' in your testnet.wallet !!
        self.asset = adm.get_asset_by_moniker('testobc')
        self.color_spec = self.asset.get_color_set().get_data()[0]

        self.comm0 = MockComm()
        self.comm1 = MockComm()
        self.comm0.add_peer(self.comm1)
        self.comm1.add_peer(self.comm0)
        self.wc = WalletController(self.model)
        self.ewc = EWalletController(self.model, self.wc)
        self.econfig = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.agent0 = EAgent(self.ewc, self.econfig, self.comm0)
        self.agent1 = EAgent(self.ewc, self.econfig, self.comm1)

        self.cv0 = {'color_spec': "", 'value': 100}
        self.cv1 = {'color_spec': self.color_spec, 'value': 200}
        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)
Exemplo n.º 13
0
    def add_coins(self):
        self.config['asset_definitions'] = [
            {"color_set": [""], "monikers": ["bitcoin"], "unit": 100000000},  
            {"color_set": [self.cspec], "monikers": ['test'], "unit": 1},]
        self.config['hdwam'] = {
            "genesis_color_sets": [ 
                [self.cspec],
                ],
            "color_set_states": [
                {"color_set": [""], "max_index": 1},
                {"color_set": [self.cspec], "max_index": 7},
                ]
            }
        self.config['bip0032'] = True
        self.pwallet = PersistentWallet(self.path, self.config)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        self.ewc.model = self.model
        self.wc.model = self.model
        def null(a):
            pass
        self.wc.publish_tx = null
        # modify model colored coin context, so test runs faster
        ccc = self.model.ccc
        cdbuilder = ColorDataBuilderManager(
            ccc.colormap, ccc.blockchain_state, ccc.cdstore,
            ccc.metastore, AidedColorDataBuilder)

        ccc.colordata = ThinColorData(
            cdbuilder, ccc.blockchain_state, ccc.cdstore, ccc.colormap)

        # need to query the blockchain
        self.model.utxo_man.update_all()

        adm = self.model.get_asset_definition_manager()
        asset = adm.get_asset_by_moniker('test')
        cq = self.model.make_coin_query({"asset": asset})
        utxo_list = cq.get_result()

        self.cd = ColorDefinition.from_color_desc(1, self.cspec)

        self.cv0 = SimpleColorValue(colordef=UNCOLORED_MARKER, value=100)
        self.cv1 = SimpleColorValue(colordef=self.cd, value=200)

        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)
Exemplo n.º 14
0
    def setUp(self):
        self.pwallet = PersistentWallet(None, True)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        adm = self.model.get_asset_definition_manager()

        # make sure you have the asset 'testobc' in your testnet.wallet !!
        self.asset = adm.get_asset_by_moniker('testobc')
        self.color_spec = self.asset.get_color_set().get_data()[0]

        self.comm0 = MockComm()
        self.comm1 = MockComm()
        self.comm0.add_peer(self.comm1)
        self.comm1.add_peer(self.comm0)
        self.wc = WalletController(self.model)
        self.ewc = EWalletController(self.model, self.wc)
        self.econfig = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.agent0 = EAgent(self.ewc, self.econfig, self.comm0)
        self.agent1 = EAgent(self.ewc, self.econfig, self.comm1)

        self.cv0 = { 'color_spec' : "", 'value' : 100 }
        self.cv1 = { 'color_spec' : self.color_spec, 'value' : 200 }
        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)
Exemplo n.º 15
0
class TestAgent(unittest.TestCase):

    def setUp(self):
        self.pwallet = PersistentWallet(None, True)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        adm = self.model.get_asset_definition_manager()

        # make sure you have the asset 'testobc' in your testnet.wallet !!
        self.asset = adm.get_asset_by_moniker('testobc')
        self.color_spec = self.asset.get_color_set().get_data()[0]

        self.comm0 = MockComm()
        self.comm1 = MockComm()
        self.comm0.add_peer(self.comm1)
        self.comm1.add_peer(self.comm0)
        self.wc = WalletController(self.model)
        self.ewc = EWalletController(self.model, self.wc)
        self.econfig = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.agent0 = EAgent(self.ewc, self.econfig, self.comm0)
        self.agent1 = EAgent(self.ewc, self.econfig, self.comm1)

        self.cv0 = { 'color_spec' : "", 'value' : 100 }
        self.cv1 = { 'color_spec' : self.color_spec, 'value' : 200 }
        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)

    def test_set_event_handler(self):
        tmp = { 'test': 0}
        def handler(val):
            tmp['test'] = val
        self.agent0.set_event_handler('click', handler)
        self.agent0.fire_event('click', 7)
        self.assertEqual(tmp['test'], 7)
        self.agent0.ep_timeout = time.time() - 100
        self.assertFalse(self.agent0.has_active_ep())

    def test_real(self):
        self.assertFalse(self.agent0.has_active_ep())
        self.agent0.register_my_offer(self.offer0)
        self.agent1.register_my_offer(self.offer1)
        for i in range(3):
            self.agent0._update_state()
            self.agent1._update_state()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        m0 = self.comm0.get_messages()
        m1 = self.comm1.get_messages()
        self.assertEquals(len(m0), 2)
        self.assertEquals(len(m1), 2)

        self.assertEquals(self.offer0.get_data(), m0[1]['offer'])
        self.assertEquals(self.offer0.get_data(), m1[1]['offer'])
        # expire offers
        offer2 = MyEOffer(None, self.cv0, self.cv1)

        self.agent0.register_my_offer(offer2)
        self.agent0.update()
        self.agent0.update()
        self.agent1.update()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.update()
        self.agent1.update()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        offer3 = MyEOffer(None, self.cv1, self.cv0)
        self.agent0.make_exchange_proposal(offer3, offer2)
        self.assertRaises(Exception, self.agent0.make_exchange_proposal,
                          offer3, offer2)
        ep = MyEProposal(self.ewc, offer3, offer2)
        def raiseException(x):
            raise Exception()
        self.agent0.set_event_handler('accept_ep', raiseException)

        # methods that should do nothing now that we have an active ep
        self.agent0.accept_exchange_proposal(ep)
        self.agent0.match_offers()

        self.agent0.register_their_offer(offer3)
        self.agent0.accept_exchange_proposal(ep)
        ep2 = MyEProposal(self.ewc, offer3, offer2)
        self.agent0.dispatch_exchange_proposal(ep2.get_data())

        # test a few corner cases
        self.agent0.register_my_offer(offer2)
        offer2.refresh(-1000)
        self.assertTrue(offer2.expired())
        self.agent0.service_my_offers()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.register_their_offer(offer3)
        offer3.refresh(-1000)
        self.agent0.service_their_offers()
Exemplo n.º 16
0
class TestAgent(unittest.TestCase):
    def setUp(self):
        self.path = ":memory:"
        self.config = {
            "hdw_master_key": "91813223e97697c42f05e54b3a85bae601f04526c5c053ff0811747db77cfdf5f1accb50b3765377c379379cd5aa512c38bf24a57e4173ef592305d16314a0f4",
            "testnet": True,
            "ccc": {"colordb_path": self.path},
        }
        self.pwallet = PersistentWallet(self.path, self.config)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        self.wc = WalletController(self.model)
        self.ewc = EWalletController(self.model, self.wc)
        self.econfig = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.comm0 = MockComm()
        self.comm1 = MockComm()
        self.comm0.add_peer(self.comm1)
        self.comm1.add_peer(self.comm0)
        self.agent0 = EAgent(self.ewc, self.econfig, self.comm0)
        self.agent1 = EAgent(self.ewc, self.econfig, self.comm1)
        self.cspec = "obc:03524a4d6492e8d43cb6f3906a99be5a1bcd93916241f759812828b301f25a6c:0:153267"

    def add_coins(self):
        self.config["asset_definitions"] = [
            {"color_set": [""], "monikers": ["bitcoin"], "unit": 100000000},
            {"color_set": [self.cspec], "monikers": ["test"], "unit": 1},
        ]
        self.config["hdwam"] = {
            "genesis_color_sets": [[self.cspec]],
            "color_set_states": [{"color_set": [""], "max_index": 1}, {"color_set": [self.cspec], "max_index": 7}],
        }
        self.config["bip0032"] = True
        self.pwallet = PersistentWallet(self.path, self.config)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        self.ewc.model = self.model
        self.wc.model = self.model

        def null(a):
            pass

        self.wc.publish_tx = null
        # modify model colored coin context, so test runs faster
        ccc = self.model.ccc
        cdbuilder = ColorDataBuilderManager(
            ccc.colormap, ccc.blockchain_state, ccc.cdstore, ccc.metastore, AidedColorDataBuilder
        )

        ccc.colordata = ThinColorData(cdbuilder, ccc.blockchain_state, ccc.cdstore, ccc.colormap)

        # need to query the blockchain
        self.model.utxo_man.update_all()

        adm = self.model.get_asset_definition_manager()
        asset = adm.get_asset_by_moniker("test")
        cq = self.model.make_coin_query({"asset": asset})
        utxo_list = cq.get_result()

        self.cd = ColorDefinition.from_color_desc(1, self.cspec)

        self.cv0 = SimpleColorValue(colordef=UNCOLORED_MARKER, value=100)
        self.cv1 = SimpleColorValue(colordef=self.cd, value=200)

        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)

    def test_set_event_handler(self):
        tmp = {"test": 0}

        def handler(val):
            tmp["test"] = val

        self.agent0.set_event_handler("click", handler)
        self.agent0.fire_event("click", 7)
        self.assertEqual(tmp["test"], 7)
        self.agent0.ep_timeout = time.time() - 100
        self.assertFalse(self.agent0.has_active_ep())

    def test_real(self):
        self.add_coins()
        self.assertFalse(self.agent0.has_active_ep())
        self.agent0.register_my_offer(self.offer0)
        self.agent1.register_my_offer(self.offer1)
        for i in range(3):
            self.agent0._update_state()
            self.agent1._update_state()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        m0 = self.comm0.get_messages()
        m1 = self.comm1.get_messages()
        self.assertEquals(len(m0), 2)
        self.assertEquals(len(m1), 2)

        self.assertEquals(self.offer0.get_data(), m0[1]["offer"])
        self.assertEquals(self.offer0.get_data(), m1[1]["offer"])
        # expire offers
        offer2 = MyEOffer(None, self.cv0, self.cv1)

        self.agent0.register_my_offer(offer2)
        self.agent0.update()
        self.agent0.update()
        self.agent1.update()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.update()
        self.agent1.update()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        offer3 = MyEOffer(None, self.cv1, self.cv0)
        self.agent0.make_exchange_proposal(offer3, offer2)
        self.assertRaises(Exception, self.agent0.make_exchange_proposal, offer3, offer2)
        ep = MyEProposal(self.ewc, offer3, offer2)

        def raiseException(x):
            raise Exception()

        self.agent0.set_event_handler("accept_ep", raiseException)

        # methods that should do nothing now that we have an active ep
        self.agent0.accept_exchange_proposal(ep)
        self.agent0.match_offers()

        self.agent0.register_their_offer(offer3)
        self.agent0.accept_exchange_proposal(ep)
        ep2 = MyEProposal(self.ewc, offer3, offer2)
        self.agent0.dispatch_exchange_proposal(ep2.get_data())

        # test a few corner cases
        self.agent0.register_my_offer(offer2)
        offer2.refresh(-1000)
        self.assertTrue(offer2.expired())
        self.agent0.service_my_offers()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.register_their_offer(offer3)
        offer3.refresh(-1000)
        self.agent0.service_their_offers()
Exemplo n.º 17
0
class TestAgent(unittest.TestCase):
    def setUp(self):
        self.path = ":memory:"
        self.config = {
            'hdw_master_key':
            '91813223e97697c42f05e54b3a85bae601f04526c5c053ff0811747db77cfdf5f1accb50b3765377c379379cd5aa512c38bf24a57e4173ef592305d16314a0f4',
            'testnet': True,
            'ccc': {
                'colordb_path': self.path
            },
        }
        self.pwallet = PersistentWallet(self.path, self.config)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        self.wc = WalletController(self.model)
        self.ewc = EWalletController(self.model, self.wc)
        self.econfig = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.comm0 = MockComm()
        self.comm1 = MockComm()
        self.comm0.add_peer(self.comm1)
        self.comm1.add_peer(self.comm0)
        self.agent0 = EAgent(self.ewc, self.econfig, self.comm0)
        self.agent1 = EAgent(self.ewc, self.econfig, self.comm1)
        self.cspec = "obc:03524a4d6492e8d43cb6f3906a99be5a1bcd93916241f759812828b301f25a6c:0:153267"

    def add_coins(self):
        self.config['asset_definitions'] = [
            {
                "color_set": [""],
                "monikers": ["bitcoin"],
                "unit": 100000000
            },
            {
                "color_set": [self.cspec],
                "monikers": ['test'],
                "unit": 1
            },
        ]
        self.config['hdwam'] = {
            "genesis_color_sets": [
                [self.cspec],
            ],
            "color_set_states": [
                {
                    "color_set": [""],
                    "max_index": 1
                },
                {
                    "color_set": [self.cspec],
                    "max_index": 7
                },
            ]
        }
        self.config['bip0032'] = True
        self.pwallet = PersistentWallet(self.path, self.config)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        self.ewc.model = self.model
        self.wc.model = self.model

        def null(a):
            pass

        self.wc.publish_tx = null
        # modify model colored coin context, so test runs faster
        ccc = self.model.ccc
        cdbuilder = ColorDataBuilderManager(ccc.colormap, ccc.blockchain_state,
                                            ccc.cdstore, ccc.metastore,
                                            AidedColorDataBuilder)

        ccc.colordata = ThinColorData(cdbuilder, ccc.blockchain_state,
                                      ccc.cdstore, ccc.colormap)

        # need to query the blockchain
        self.model.utxo_man.update_all()

        adm = self.model.get_asset_definition_manager()
        asset = adm.get_asset_by_moniker('test')
        cq = self.model.make_coin_query({"asset": asset})
        utxo_list = cq.get_result()

        self.cd = ColorDefinition.from_color_desc(1, self.cspec)

        self.cv0 = SimpleColorValue(colordef=UNCOLORED_MARKER, value=100)
        self.cv1 = SimpleColorValue(colordef=self.cd, value=200)

        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)

    def test_set_event_handler(self):
        tmp = {'test': 0}

        def handler(val):
            tmp['test'] = val

        self.agent0.set_event_handler('click', handler)
        self.agent0.fire_event('click', 7)
        self.assertEqual(tmp['test'], 7)
        self.agent0.ep_timeout = time.time() - 100
        self.assertFalse(self.agent0.has_active_ep())

    def test_real(self):
        self.add_coins()
        self.assertFalse(self.agent0.has_active_ep())
        self.agent0.register_my_offer(self.offer0)
        self.agent1.register_my_offer(self.offer1)
        for i in range(3):
            self.agent0._update_state()
            self.agent1._update_state()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        m0 = self.comm0.get_messages()
        m1 = self.comm1.get_messages()
        self.assertEquals(len(m0), 2)
        self.assertEquals(len(m1), 2)

        self.assertEquals(self.offer0.get_data(), m0[1]['offer'])
        self.assertEquals(self.offer0.get_data(), m1[1]['offer'])
        # expire offers
        offer2 = MyEOffer(None, self.cv0, self.cv1)

        self.agent0.register_my_offer(offer2)
        self.agent0.update()
        self.agent0.update()
        self.agent1.update()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.update()
        self.agent1.update()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        offer3 = MyEOffer(None, self.cv1, self.cv0)
        self.agent0.make_exchange_proposal(offer3, offer2)
        self.assertRaises(Exception, self.agent0.make_exchange_proposal,
                          offer3, offer2)
        ep = MyEProposal(self.ewc, offer3, offer2)

        def raiseException(x):
            raise Exception()

        self.agent0.set_event_handler('accept_ep', raiseException)

        # methods that should do nothing now that we have an active ep
        self.agent0.accept_exchange_proposal(ep)
        self.agent0.match_offers()

        self.agent0.register_their_offer(offer3)
        self.agent0.accept_exchange_proposal(ep)
        ep2 = MyEProposal(self.ewc, offer3, offer2)
        self.agent0.dispatch_exchange_proposal(ep2.get_data())

        # test a few corner cases
        self.agent0.register_my_offer(offer2)
        offer2.refresh(-1000)
        self.assertTrue(offer2.expired())
        self.agent0.service_my_offers()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.register_their_offer(offer3)
        offer3.refresh(-1000)
        self.agent0.service_their_offers()
Exemplo n.º 18
0
    def add_coins(self):
        self.config['asset_definitions'] = [
            {
                "color_set": [""],
                "monikers": ["bitcoin"],
                "unit": 100000000
            },
            {
                "color_set": [self.cspec],
                "monikers": ['test'],
                "unit": 1
            },
        ]
        self.config['hdwam'] = {
            "genesis_color_sets": [
                [self.cspec],
            ],
            "color_set_states": [
                {
                    "color_set": [""],
                    "max_index": 1
                },
                {
                    "color_set": [self.cspec],
                    "max_index": 7
                },
            ]
        }
        self.config['bip0032'] = True
        self.pwallet = PersistentWallet(self.path, self.config)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        self.ewc.model = self.model
        self.wc.model = self.model

        def null(a):
            pass

        self.wc.publish_tx = null
        # modify model colored coin context, so test runs faster
        ccc = self.model.ccc
        cdbuilder = ColorDataBuilderManager(ccc.colormap, ccc.blockchain_state,
                                            ccc.cdstore, ccc.metastore,
                                            AidedColorDataBuilder)

        ccc.colordata = ThinColorData(cdbuilder, ccc.blockchain_state,
                                      ccc.cdstore, ccc.colormap)

        # need to query the blockchain
        self.model.utxo_man.update_all()

        adm = self.model.get_asset_definition_manager()
        asset = adm.get_asset_by_moniker('test')
        cq = self.model.make_coin_query({"asset": asset})
        utxo_list = cq.get_result()

        self.cd = ColorDefinition.from_color_desc(1, self.cspec)

        self.cv0 = SimpleColorValue(colordef=UNCOLORED_MARKER, value=100)
        self.cv1 = SimpleColorValue(colordef=self.cd, value=200)

        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)
Exemplo n.º 19
0
 def test_compatibility(self):
   meo = MyEOffer(1, "A", "B")
   eo = EOffer(1, "A", "B")
   self.assertTrue(meo.is_same_as_mine(eo))
Exemplo n.º 20
0
 def test_convert_from_data(self):
   data = {'oid': 1, 'A':"A", 'B':"B"}
   result = MyEOffer.from_data(data)
   expected = MyEOffer(1, "A", "B")
   self.assertEqual(result, expected)
Exemplo n.º 21
0
 def test_auto_post_default(self):
     meo = MyEOffer(1, "A", "B")
     self.assertTrue(meo.auto_post)
Exemplo n.º 22
0
 def test_convert_from_data(self):
     data = {'oid': 1, 'A': "A", 'B': "B"}
     result = MyEOffer.from_data(data)
     expected = MyEOffer(1, "A", "B")
     self.assertEqual(result, expected)
Exemplo n.º 23
0
 def test_compatibility(self):
     meo = MyEOffer(1, "A", "B")
     eo = EOffer(1, "A", "B")
     self.assertTrue(meo.is_same_as_mine(eo))
Exemplo n.º 24
0
 def p2ptrade_make_mirror_offer(self, offer):
     data = offer.get_data()
     return MyEOffer(None, data['B'], data['A'], False)
Exemplo n.º 25
0
class TestAgent(unittest.TestCase):
    def setUp(self):
        self.pwallet = PersistentWallet(None, True)
        self.pwallet.init_model()
        self.model = self.pwallet.get_model()
        adm = self.model.get_asset_definition_manager()

        # make sure you have the asset 'testobc' in your testnet.wallet !!
        self.asset = adm.get_asset_by_moniker('testobc')
        self.color_spec = self.asset.get_color_set().get_data()[0]

        self.comm0 = MockComm()
        self.comm1 = MockComm()
        self.comm0.add_peer(self.comm1)
        self.comm1.add_peer(self.comm0)
        self.wc = WalletController(self.model)
        self.ewc = EWalletController(self.model, self.wc)
        self.econfig = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.agent0 = EAgent(self.ewc, self.econfig, self.comm0)
        self.agent1 = EAgent(self.ewc, self.econfig, self.comm1)

        self.cv0 = {'color_spec': "", 'value': 100}
        self.cv1 = {'color_spec': self.color_spec, 'value': 200}
        self.offer0 = MyEOffer(None, self.cv0, self.cv1)
        self.offer1 = MyEOffer(None, self.cv1, self.cv0)

    def test_set_event_handler(self):
        tmp = {'test': 0}

        def handler(val):
            tmp['test'] = val

        self.agent0.set_event_handler('click', handler)
        self.agent0.fire_event('click', 7)
        self.assertEqual(tmp['test'], 7)
        self.agent0.ep_timeout = time.time() - 100
        self.assertFalse(self.agent0.has_active_ep())

    def test_real(self):
        self.assertFalse(self.agent0.has_active_ep())
        self.agent0.register_my_offer(self.offer0)
        self.agent1.register_my_offer(self.offer1)
        for i in range(3):
            self.agent0._update_state()
            self.agent1._update_state()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        m0 = self.comm0.get_messages()
        m1 = self.comm1.get_messages()
        self.assertEquals(len(m0), 2)
        self.assertEquals(len(m1), 2)

        self.assertEquals(self.offer0.get_data(), m0[1]['offer'])
        self.assertEquals(self.offer0.get_data(), m1[1]['offer'])
        # expire offers
        offer2 = MyEOffer(None, self.cv0, self.cv1)

        self.agent0.register_my_offer(offer2)
        self.agent0.update()
        self.agent0.update()
        self.agent1.update()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.update()
        self.agent1.update()
        self.assertFalse(self.agent0.has_active_ep())
        self.assertFalse(self.agent1.has_active_ep())

        offer3 = MyEOffer(None, self.cv1, self.cv0)
        self.agent0.make_exchange_proposal(offer3, offer2)
        self.assertRaises(Exception, self.agent0.make_exchange_proposal,
                          offer3, offer2)
        ep = MyEProposal(self.ewc, offer3, offer2)

        def raiseException(x):
            raise Exception()

        self.agent0.set_event_handler('accept_ep', raiseException)

        # methods that should do nothing now that we have an active ep
        self.agent0.accept_exchange_proposal(ep)
        self.agent0.match_offers()

        self.agent0.register_their_offer(offer3)
        self.agent0.accept_exchange_proposal(ep)
        ep2 = MyEProposal(self.ewc, offer3, offer2)
        self.agent0.dispatch_exchange_proposal(ep2.get_data())

        # test a few corner cases
        self.agent0.register_my_offer(offer2)
        offer2.refresh(-1000)
        self.assertTrue(offer2.expired())
        self.agent0.service_my_offers()
        self.agent0.cancel_my_offer(offer2)
        self.agent0.register_their_offer(offer3)
        offer3.refresh(-1000)
        self.agent0.service_their_offers()