Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions packages/polywrap-client/polywrap_client/client.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from __future__ import annotations

import json
from dataclasses import dataclass
from textwrap import dedent
from typing import Any, Dict, List, Optional, Union, cast

from polywrap_core import (
Client,
ClientConfig,
Env,
GetFileOptions,
GetManifestOptions,
InvokerOptions,
IUriResolutionContext,
IUriResolver,
IWrapPackage,
TryResolveUriOptions,
Env,
Uri,
UriPackage,
UriPackageOrWrapper,
UriResolutionContext,
Wrapper,
build_clean_uri_history,
)
from polywrap_manifest import AnyWrapManifest
from polywrap_msgpack import msgpack_decode, msgpack_encode
Expand Down Expand Up @@ -61,9 +63,7 @@ def get_implementations(self, uri: Uri) -> Result[Union[List[Uri], None]]:
else:
return Err.from_str(f"Unable to find implementations for uri: {uri}")

def get_env_by_uri(
self, uri: Uri
) -> Union[Env, None]:
def get_env_by_uri(self, uri: Uri) -> Union[Env, None]:
return self._config.envs.get(uri)

async def get_file(
Expand Down Expand Up @@ -98,34 +98,32 @@ async def load_wrapper(
if result.is_err():
return cast(Err, result)
if result.is_ok() and result.ok is None:
# FIXME: add resolution stack
return Err.from_str(
dedent(
f"""
Error resolving URI "{uri.uri}"
Resolution Stack: NotImplemented
Resolution Stack: {json.dumps(build_clean_uri_history(resolution_context.get_history()), indent=2)}
"""
)
)

uri_package_or_wrapper = result.unwrap()

if isinstance(uri_package_or_wrapper, Uri):
# FIXME: add resolution stack
return Err.from_str(
dedent(
f"""
Error resolving URI "{uri.uri}"
URI not found
Resolution Stack: NotImplemented
Resolution Stack: {json.dumps(build_clean_uri_history(resolution_context.get_history()), indent=2)}
"""
)
)

if isinstance(uri_package_or_wrapper, UriPackage):
return await uri_package_or_wrapper.package.create_wrapper()
if isinstance(uri_package_or_wrapper, IWrapPackage):
return await uri_package_or_wrapper.create_wrapper()

return Ok(uri_package_or_wrapper.wrapper)
return Ok(uri_package_or_wrapper)

async def invoke(self, options: InvokerOptions) -> Result[Any]:
resolution_context = options.resolution_context or UriResolutionContext()
Expand Down
3 changes: 2 additions & 1 deletion packages/polywrap-client/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ exclude_dirs = ["tests"]
target-version = ["py310"]

[tool.pyright]
# default
typeCheckingMode = "strict"
reportShadowedImports = false

[tool.pytest.ini_options]
asyncio_mode = "auto"
Expand Down
54 changes: 17 additions & 37 deletions packages/polywrap-client/tests/test_bignumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,52 @@
from polywrap_client import PolywrapClient
from polywrap_core import Uri, InvokerOptions


async def test_invoke_bignumber_1arg_and_1prop():
client = PolywrapClient()
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
args = { "arg1": "123", # The base number
args = {
"arg1": "123", # The base number
"obj": {
"prop1": "1000", # multiply the base number by this factor
}
"prop1": "1000", # multiply the base number by this factor
},
}
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
result = await client.invoke(options)
assert result.unwrap() == "123000"


async def test_invoke_bignumber_with_1arg_and_2props():
client = PolywrapClient()
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
args = {
"arg1": "123123",
"obj": {
"prop1": "1000",
"prop2": "4"
}
}
args = {"arg1": "123123", "obj": {"prop1": "1000", "prop2": "4"}}
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
result = await client.invoke(options)
assert result.unwrap() == str(123123*1000*4)
assert result.unwrap() == str(123123 * 1000 * 4)


async def test_invoke_bignumber_with_2args_and_1prop():
client = PolywrapClient()
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
args = {
"arg1": "123123",
"obj": {
"prop1": "1000",
"prop2": "444"
}
}
args = {"arg1": "123123", "obj": {"prop1": "1000", "prop2": "444"}}
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
result = await client.invoke(options)
assert result.unwrap() == str(123123*1000*444)
assert result.unwrap() == str(123123 * 1000 * 444)


async def test_invoke_bignumber_with_2args_and_2props():
client = PolywrapClient()
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
args = {
"arg1": "123123",
"arg2": "555",
"obj": {
"prop1": "1000",
"prop2": "4"
}
}
args = {"arg1": "123123", "arg2": "555", "obj": {"prop1": "1000", "prop2": "4"}}
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
result = await client.invoke(options)
assert result.unwrap() == str(123123*555*1000*4)
assert result.unwrap() == str(123123 * 555 * 1000 * 4)


async def test_invoke_bignumber_with_2args_and_2props_floats():
client = PolywrapClient()
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
args = {
"arg1": "123.123",
"arg2": "55.5",
"obj": {
"prop1": "10.001",
"prop2": "4"
}
}
args = {"arg1": "123.123", "arg2": "55.5", "obj": {"prop1": "10.001", "prop2": "4"}}
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
result = await client.invoke(options)
assert result.unwrap() == str(123.123*55.5*10.001*4)
assert result.unwrap() == str(123.123 * 55.5 * 10.001 * 4)
8 changes: 3 additions & 5 deletions packages/polywrap-client/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import pytest
from pathlib import Path
import pytest
from polywrap_client import PolywrapClient, PolywrapClientConfig
from polywrap_manifest import deserialize_wrap_manifest
from polywrap_core import Uri, InvokerOptions, UriWrapper
from polywrap_core import Uri, InvokerOptions, IFileReader
from polywrap_uri_resolvers import BaseUriResolver, SimpleFileReader, StaticResolver
from polywrap_result import Result, Ok, Err
from polywrap_wasm import WRAP_MANIFEST_PATH, WRAP_MODULE_PATH, IFileReader, WasmWrapper
from polywrap_wasm import WRAP_MANIFEST_PATH, WRAP_MODULE_PATH, WasmWrapper

@pytest.fixture
def simple_wrap_module():
Expand Down Expand Up @@ -58,8 +57,7 @@ async def test_invoke(
wasm_module=simple_wrap_module,
manifest=manifest
)
uri_wrapper = UriWrapper(uri=Uri("ens/wrapper.eth"), wrapper=wrapper)
resolver = StaticResolver.from_list([uri_wrapper]).unwrap()
resolver = StaticResolver({Uri("ens/wrapper.eth"): wrapper})

config = PolywrapClientConfig(resolver=resolver)
client = PolywrapClient(config=config)
Expand Down
24 changes: 12 additions & 12 deletions packages/polywrap-client/tests/test_sha3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,78 +19,78 @@ async def test_invoke_sha3_512():
result = await client.invoke(options)
s = hashlib.sha512()
s.update(b"hello polywrap!")
assert result.result == s.digest()
assert result.unwrap() == s.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_sha3_384():
options = InvokerOptions(uri=uri, method="sha3_384", args=args, encode_result=False)
result = await client.invoke(options)
s = hashlib.sha384()
s.update(b"hello polywrap!")
assert result.result == s.digest()
assert result.unwrap() == s.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_sha3_256():
options = InvokerOptions(uri=uri, method="sha3_256", args=args, encode_result=False)
result = await client.invoke(options)
s = hashlib.sha256()
s.update(b"hello polywrap!")
assert result.result == s.digest()
assert result.unwrap() == s.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_sha3_224():
options = InvokerOptions(uri=uri, method="sha3_224", args=args, encode_result=False)
result = await client.invoke(options)
s = hashlib.sha224()
s.update(b"hello polywrap!")
assert result.result == s.digest()
assert result.unwrap() == s.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_keccak_512():
options = InvokerOptions(uri=uri, method="keccak_512", args=args, encode_result=False)
result = await client.invoke(options)
k = keccak.new(digest_bits=512)
k.update(b'hello polywrap!')
assert result.result == k.digest()
assert result.unwrap() == k.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_keccak_384():
options = InvokerOptions(uri=uri, method="keccak_384", args=args, encode_result=False)
result = await client.invoke(options)
k = keccak.new(digest_bits=384)
k.update(b'hello polywrap!')
assert result.result == k.digest()
assert result.unwrap() == k.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_keccak_256():
options = InvokerOptions(uri=uri, method="keccak_256", args=args, encode_result=False)
result = await client.invoke(options)
k = keccak.new(digest_bits=256)
k.update(b'hello polywrap!')
assert result.result == k.digest()
assert result.unwrap() == k.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_keccak_224():
options = InvokerOptions(uri=uri, method="keccak_224", args=args, encode_result=False)
result = await client.invoke(options)
k = keccak.new(digest_bits=224)
k.update(b'hello polywrap!')
assert result.result == k.digest()
assert result.unwrap() == k.digest()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_hex_keccak_256():
options = InvokerOptions(uri=uri, method="hex_keccak_256", args=args, encode_result=False)
result = await client.invoke(options)
k = keccak.new(digest_bits=256)
k.update(b'hello polywrap!')
assert result.result == k.hexdigest()
assert result.unwrap() == k.hexdigest()

@pytest.mark.skip(reason="buffer keccak must be implemented in python in order to assert")
async def test_invoke_buffer_keccak_256():
options = InvokerOptions(uri=uri, method="buffer_keccak_256", args=args, encode_result=False)
result = await client.invoke(options)
# TODO: Not sure exactly what this function `buffer_keccak_256` is doing in order to assert it properly
assert result.result == False
assert result.unwrap() == False

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_shake_256():
Expand All @@ -99,7 +99,7 @@ async def test_invoke_shake_256():
result = await client.invoke(options)
s = SHAKE256.new()
s.update(b"hello polywrap!")
assert result.result == s.read(8).hex()
assert result.unwrap() == s.read(8).hex()

@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
async def test_invoke_shake_128():
Expand All @@ -108,4 +108,4 @@ async def test_invoke_shake_128():
result = await client.invoke(options)
s = SHAKE128.new()
s.update(b"hello polywrap!")
assert result.result == s.read(8).hex()
assert result.unwrap() == s.read(8).hex()
Loading