Esempio n. 1
0
async def test_select_template_retrieves_contracts(sandbox):
    number_of_contracts = 10

    async with async_network(url=sandbox, dars=Pending) as network:
        client = network.aio_new_party()
        client.add_ledger_ready(lambda _: [
            CreateCommand(Counter, {
                "owner": client.party,
                "value": 0
            }),
            *[
                CreateCommand(AccountRequest, {"owner": client.party})
                for _ in range(number_of_contracts)
            ],
        ])

        @client.ledger_created(AccountRequest)
        async def on_account_request(event):
            counter_cid, counter_cdata = await event.acs_find_one(Counter)
            return [
                ExerciseCommand(event.cid, "CreateAccount",
                                dict(accountId=counter_cdata["value"])),
                ExerciseCommand(counter_cid, "Increment"),
            ]

        await network.aio_run(keep_open=False)

        data = client.find_active(Account)

    assert len(data) == number_of_contracts
Esempio n. 2
0
    def test_object_atomic_default_true(self):
        with pytest.warns(DeprecationWarning):
            builder = CommandBuilder(atomic_default=True)
        builder.create("Sample:Untyped", {"arg": 1})
        builder.exercise(SOME_CONTRACT_ID, "SomeChoice",
                         {"choiceArg": "value"})

        expected = [
            CommandPayload(
                party=SOME_PARTY,
                ledger_id=DEFAULTS.default_ledger_id,
                workflow_id=DEFAULTS.default_workflow_id,
                application_id=DEFAULTS.default_application_id,
                command_id=DEFAULTS.default_command_id,
                commands=[
                    CreateCommand(SOME_TEMPLATE_NAME, dict(arg=1)),
                    ExerciseCommand(SOME_CONTRACT_ID, "SomeChoice",
                                    {"choiceArg": "value"}),
                ],
            )
        ]

        actual = builder.build(DEFAULTS)

        assert expected == actual
Esempio n. 3
0
def create_initial_state(event: "ReadyEvent"):
    try:
        LOG.info("Uploading our DAR...")
        event.client.ensure_dar(PostOffice)

        LOG.info("DAR uploaded. Creating the initial postman role contract...")
        return CreateCommand("Main:PostmanRole", dict(postman=event.party))
    except:
        LOG.exception("Failed to set up our initial state!")
Esempio n. 4
0
def test_serialize_create(dar_fixture):
    sut = ProtobufSerializer(dar_fixture.lookup)

    command = CreateCommand("Pending:AccountRequest", dict(owner="SomeParty"))

    expected = G_Command()
    expected.create.template_id.MergeFrom(
        dar_fixture.get_identifier("Pending:AccountRequest"))
    expected.create.create_arguments.fields.append(
        G_RecordField(label="owner", value=G_Value(party=Party("SomeParty"))))
    actual = sut.serialize_command(command)

    assert expected == actual
Esempio n. 5
0
async def test_template_filtering(sandbox):
    # First, create a few contracts stretching across two DARs and validate that all of those
    # contracts show up in the active contract set. async_network will supply the list of DARs to
    # dazl.
    async with async_network(url=sandbox, dars=[AllParty,
                                                PostOffice]) as network:
        client = network.aio_new_party()

        # Remember the party, because we're going to reconnect as this party, but with a restricted
        # set of DARs.
        party = client.party

        network.start()
        await client.submit([
            CreateCommand("AllParty:PrivateContract", {"someParty": party}),
            CreateCommand("AllParty:PrivateContract", {"someParty": party}),
            CreateCommand("AllParty:PrivateContract", {"someParty": party}),
            CreateCommand("Main:PostmanRole", {"postman": party}),
            CreateCommand("Main:PostmanRole", {"postman": party}),
        ])

        # The ACS should only contain the five contracts we created: two from Post Office, and three
        # from AllKindsOf.
        contracts = client.find_active("*")
        assert len(contracts) == 5

    # Now create a new client to the same sandbox, but with less DARs
    async with async_network(url=sandbox, dars=[PostOffice]) as network:
        client = network.aio_party(party)
        network.start()

        await client.ready()

        # The ACS should only contain the two contracts we created that were part of the Post Office
        # model.
        contracts = client.find_active("*")
        assert len(contracts) == 2
Esempio n. 6
0
    def test_single_create_untyped(self):
        expr = create("Sample:Untyped", {"arg": 1})

        expected = [
            CommandPayload(
                party=SOME_PARTY,
                ledger_id=DEFAULTS.default_ledger_id,
                workflow_id=DEFAULTS.default_workflow_id,
                application_id=DEFAULTS.default_application_id,
                command_id=DEFAULTS.default_command_id,
                commands=[CreateCommand(SOME_TEMPLATE_NAME, dict(arg=1))],
            )
        ]
        actual = CommandBuilder.coerce(expr).build(DEFAULTS)

        assert expected == actual
Esempio n. 7
0
    def test_object_create_untyped(self):
        with pytest.warns(DeprecationWarning):
            builder = CommandBuilder()
        builder.create("Sample:Untyped", {"arg": 1})

        expected = [
            CommandPayload(
                party=SOME_PARTY,
                ledger_id=DEFAULTS.default_ledger_id,
                workflow_id=DEFAULTS.default_workflow_id,
                application_id=DEFAULTS.default_application_id,
                command_id=DEFAULTS.default_command_id,
                commands=[CreateCommand(SOME_TEMPLATE_NAME, dict(arg=1))],
            )
        ]
        actual = builder.build(DEFAULTS)

        assert expected == actual
 def create_one_of_everything(self, _):
     return CreateCommand(TEMPLATE, {
         **SOME_ARGS, "operator": self.operator
     })
    async def on_ready(self, event):
        await self.network.aio_global().ensure_dar(SimpleDar)

        return CreateCommand(Simple.OperatorRole, {"operator": event.party})