Exemplo n.º 1
0
 async def decorate_demand(self, demand: DemandBuilder):
     """Add properties and constraints to a Demand."""
     for constraint in self.market_decoration.constraints:
         demand.ensure(constraint)
     demand.properties.update(
         {p.key: p.value
          for p in self.market_decoration.properties})
Exemplo n.º 2
0
    async def vm(
            self,
            image_hash: str,
            min_mem_gib: float = 0.5,
            min_storage_gib: float = 2.0,
            timeout=timedelta(minutes=5),
    ):
        from yapapi.package import vm

        console = Console()
        now = datetime.now(timezone.utc)
        if isinstance(timeout, int):
            timeout = timedelta(minutes=timeout)
        assert isinstance(timeout, timedelta)
        expires = now + timeout
        pacakge = await vm.repo(image_hash=image_hash,
                                min_mem_gib=min_mem_gib,
                                min_storage_gib=min_storage_gib)
        package_url = await pacakge.resolve_url()

        console.print(f"resolved vm package: {package_url}")
        console.print(f"demand expires: {expires}")
        demand = DemandBuilder()
        demand.add(Activity(expiration=expires))
        await pacakge.decorate_demand(demand)
        console.print(render_demand(demand.properties, demand.constraints))
Exemplo n.º 3
0
async def test_respond_to_provider_offer(offer_props, expiration_secs, expected_props):
    strategy = GoodStrategy()
    demand = DemandBuilder()
    expiration = datetime.now() + timedelta(seconds=expiration_secs)
    demand.add(Activity(expiration=expiration))
    offer_kwargs = {"proposal__proposal__properties": offer_props}
    offer = OfferProposalFactory(**offer_kwargs)

    updated_demand = await strategy.respond_to_provider_offer(demand, offer)
    del updated_demand.properties["golem.srv.comp.expiration"]

    assert updated_demand.properties == expected_props
Exemplo n.º 4
0
async def test_payload():
    builder = DemandBuilder()
    await builder.decorate(_FooPayload(port=1234, min_mem_gib=32))
    assert builder.properties == {"golem.srv.app.foo.port": 1234}
    assert (
        builder.constraints
        == "(&(golem.runtime.name=foo)\n\t(golem.inf.mem.gib>=32)\n\t(golem.inf.storage.gib>=1024))"
    )
Exemplo n.º 5
0
 async def create_demand_builder(self, expiration_time: datetime,
                                 payload: Payload) -> DemandBuilder:
     """Create a `DemandBuilder` for given `payload` and `expiration_time`."""
     builder = DemandBuilder()
     builder.add(
         props.Activity(expiration=expiration_time, multi_activity=True))
     builder.add(props.NodeInfo(subnet_tag=self._subnet))
     if self._subnet:
         builder.ensure(f"({props.NodeInfoKeys.subnet_tag}={self._subnet})")
     await builder.decorate(self.payment_decorator, self.strategy, payload)
     return builder
Exemplo n.º 6
0
async def test_autodecorating_model():
    @dataclass
    class Foo(AutodecoratingModel):
        bar: str = prop("some.bar")
        max_baz: int = constraint("baz", "<=", 100)

    foo = Foo(bar="a nice one", max_baz=50)
    demand = DemandBuilder()
    await foo.decorate_demand(demand)
    assert demand.properties == {"some.bar": "a nice one"}
    assert demand.constraints == "(baz<=50)"
Exemplo n.º 7
0
def test_builder():
    print("inf.cores=", vm.InfVmKeys.names())
    b = DemandBuilder()
    e = datetime.now(timezone.utc) + timedelta(days=1)
    b.add(props.Activity(expiration=e))
    b.add(vm.VmRequest(package_url="", package_format=vm.VmPackageFormat.GVMKIT_SQUASH))
    print(b)
Exemplo n.º 8
0
def test_add_properties():
    demand = DemandBuilder()
    assert demand.properties == {}
    demand.add_properties({"golem.foo": 667})
    demand.add_properties({"golem.bar": "blah"})
    assert demand.properties == {
        "golem.foo": 667,
        "golem.bar": "blah",
    }
Exemplo n.º 9
0
async def list_offers(conf: Configuration):
    async with conf.market() as client:
        market_api = Market(client)
        dbuild = DemandBuilder()
        dbuild.add(yp.Identification(name="some scannig node", subnet_tag="testnet"))
        dbuild.add(yp.Activity(expiration=datetime.now(timezone.utc)))

        async with market_api.subscribe(dbuild.props, dbuild.cons) as subscription:
            async for event in subscription.events():
                print(f"Offer: {event.id}")
                print(f"from {event.issuer}")
                print(f"props {json.dumps(event.props, indent=4)}")
                print("\n\n")
        print("done")
Exemplo n.º 10
0
async def list_offers(conf: Configuration, subnet_tag: str):
    async with conf.market() as client:
        market_api = Market(client)
        dbuild = DemandBuilder()
        dbuild.add(yp.NodeInfo(
            name="Golem Stats Indexer", subnet_tag=subnet_tag))
        dbuild.add(yp.Activity(expiration=datetime.now(timezone.utc)))
        async with market_api.subscribe(dbuild.properties, dbuild.constraints) as subscription:
            async for event in subscription.events():
                if event.props['golem.runtime.name'] != "wasmtime":

                    if event.issuer in str(test):
                        continue
                    else:
                        data = event.props
                        if event.props['golem.runtime.name'] == "gminer":
                            try:
                                data["wallet"] = event.props['golem.com.payment.platform.polygon-polygon-glm.address']
                            except:
                                data['wallet'] = event.props["golem.com.payment.platform.erc20-polygon-glm.address"]

                            data['golem.node.debug.subnet'] = "Thorg"
                            data['id'] = event.issuer
                            test.append(json.dumps(data))
                        if event.props['golem.runtime.name'] == "hminer":
                            data['wallet'] = event.props['golem.com.payment.platform.polygon-polygon-glm.address']
                            data['golem.node.debug.subnet'] = "Thorg"
                            data['id'] = event.issuer
                            test.append(json.dumps(data))
                        if "golem.com.payment.platform.zksync-mainnet-glm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.zksync-mainnet-glm.address']
                        elif "golem.com.payment.platform.zksync-rinkeby-tglm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.zksync-rinkeby-tglm.address']
                        elif "golem.com.payment.platform.erc20-mainnet-glm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.erc20-mainnet-glm.address']
                        elif "golem.com.payment.platform.erc20-polygon-glm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.erc20-polygon-glm.address']
                        elif "golem.com.payment.platform.erc20-rinkeby-tglm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.erc20-rinkeby-tglm.address']
                        data['id'] = event.issuer
                        test.append(json.dumps(data))
Exemplo n.º 11
0
    async def decorate_demand(self, demand: DemandBuilder) -> None:
        await super().decorate_demand(demand)

        # Ensure that the offer uses `PriceModel.LINEAR` price model.
        demand.ensure(f"({com.PRICE_MODEL}={com.PriceModel.LINEAR.value})")
Exemplo n.º 12
0
 async def decorate_demand(self, demand: DemandBuilder):
     image_url = await self.resolve_url()
     demand.ensure(str(self.constraints))
     demand.add(
         VmRequest(package_url=image_url,
                   package_format=VmPackageFormat.GVMKIT_SQUASH))
Exemplo n.º 13
0
async def renegotiate_offers(conf: Configuration, subnet_tag: str):
    """Rejects every proposal & then renegotiates it"""
    async with conf.market() as client:
        market_api = Market(client)
        dbuild = DemandBuilder()
        dbuild.add(
            yp.NodeInfo(name="some renegotiating node", subnet_tag=subnet_tag))
        dbuild.add(
            yp.Activity(
                expiration=datetime.datetime.now(datetime.timezone.utc) +
                datetime.timedelta(minutes=30)))

        async with market_api.subscribe(dbuild.properties,
                                        dbuild.constraints) as subscription:
            issuers = set()
            proposals = 0
            rejected_proposals = set()  # Already rejected, don't reject again
            async for event in subscription.events():
                node_name = event.props.get("golem.node.id.name")
                proposal_id = event._proposal.proposal.proposal_id
                print(f"\n[{node_name}] {'*'*15} {proposal_id}")
                prev_proposal_id = event._proposal.proposal.prev_proposal_id
                print(f"[{node_name}] prev_proposal_id: {prev_proposal_id}")
                if not event.is_draft:
                    if proposals > PROPOSALS_LIMIT:
                        print(f"[node_name] Skipping additional proposal")
                        break
                    await _respond(event, dbuild)
                    proposals += 1
                    issuers.add(event.issuer)
                    print(
                        f"[{node_name}] Responded. proposals={proposals}, issuers={len(issuers)}"
                    )
                    continue

                print(
                    f"[{node_name}] Offer: {proposal_id} from {event.issuer} is_draft: {event.is_draft}"
                )
                if prev_proposal_id not in rejected_proposals:
                    await event.reject()
                    print(
                        f"[{node_name}] Rejected {len(rejected_proposals)}. id: {proposal_id}"
                    )
                    await asyncio.sleep(1)
                    print(f"[{node_name}] Renegotiating. id: {proposal_id}")
                    new_offer_id = await _respond(event, dbuild)
                    print(f"[{node_name}] new_offer_id: {new_offer_id}")
                    rejected_proposals.add(new_offer_id)
                    continue
                print(".create_agreement()")
                agreement = await event.create_agreement()
                print(".confirm()")
                confirm_result = await agreement.confirm()
                print(f"[{node_name}] agreement.confirm(): {confirm_result}")
                if confirm_result:
                    terminate_reason = {
                        "message": "Work cancelled",
                        "golem.requestor.code": "Cancelled",
                    }
                    terminate_result = await agreement.terminate(
                        terminate_reason)
                    print(f"agreement.terminate(): {terminate_result}")
        print("All done")