Skip to content

Commit

Permalink
14 oct update part-1. SubDomainRegistrar, PublicResolver, keyValueTre…
Browse files Browse the repository at this point in the history
…e, Multibox added & all tests added for them
  • Loading branch information
Aviksaikat committed Oct 15, 2023
1 parent ba847f6 commit 958836d
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 7 deletions.
77 changes: 77 additions & 0 deletions src/fds/contracts/KeyValueTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Copyright 2023 The FairDataSociety Authors
This file is part of the FairDataSociety library.
The FairDataSociety library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The FairDataSociety library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the FairDataSociety library. If not, see <http:www.gnu.org/licenses/>.
handles KeyValueTree contract
"""

from typing import List, Tuple, Union

from ape import networks, project
from ape.api.accounts import AccountAPI
from ape.api.transactions import ReceiptAPI
from ape.types import AddressType

from fds.utils.convert import int_to_bytes32
from fds.utils.Exceptions import AccountNotSetUp


class KeyValueTree:
def __init__(self, account: AccountAPI, contract_address: Union[AddressType, None] = None):
self.account = account
self.contract_address = contract_address
self.contract = None
self.web3 = networks.provider._web3

if not self.account:
raise AccountNotSetUp("Set up user account first")

if contract_address:
self.contract = project.KeyValueTree.at(self.contract_address)

def getSharedId(self) -> ReceiptAPI:
return self.contract.getSharedId() # type: ignore

def getRootId(self) -> ReceiptAPI:
return self.contract.getRootId() # type: ignore

def setKeyValue(self, nodeId: int, key: int, value: int) -> bool:
self.nodeId = int_to_bytes32(nodeId)
self.key = key.to_bytes(32, byteorder="big")
self.value = value.to_bytes(32, byteorder="big")

return self.contract.setKeyValue(self.nodeId, self.key, self.value, sender=self.account) # type: ignore # noqa: 501

def getKeyValue(self, nodeId: int, key: int) -> ReceiptAPI:
self.nodeId = int_to_bytes32(nodeId)
self.key = key.to_bytes(32, byteorder="big")

return self.contract.getValue(self.nodeId, self.key) # type: ignore

def getKeyValues(self, nodeId: int) -> Tuple:
self.nodeId = int_to_bytes32(nodeId)
return self.contract.getKeyValues(self.nodeId) # type: ignore

def addChildNode(self, parent_node_id: str, sub_node_id: str) -> ReceiptAPI:
self.parent_node_id = parent_node_id
self.sub_node_id = sub_node_id

return self.contract.addChildNode(self.parent_node_id, self.sub_node_id, sender=self.account) # type: ignore # noqa: 501

def getChildren(self, parent_node_id: str) -> List:
self.parent_node_id = parent_node_id

return self.contract.getChildren(self.parent_node_id) # type: ignore
28 changes: 26 additions & 2 deletions src/fds/contracts/Multibox.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,35 @@
handles Multibox contract
"""

from typing import Tuple, Union
from typing import Union

from ape import convert, networks, project
from ape import networks, project
from ape.api.accounts import AccountAPI
from ape.api.transactions import ReceiptAPI
from ape.types import AddressType

from fds.utils.Exceptions import AccountNotSetUp


class MultiBox:
def __init__(self, account: AccountAPI, contract_address: Union[AddressType, None] = None):
self.account = account
self.contract_address = contract_address
self.contract = None
self.web3 = networks.provider._web3

if not self.account:
raise AccountNotSetUp("Set up user account first")

if contract_address:
self.contract = project.Multibox.at(self.contract_address)
else:
return self.deploy()

def deploy(self):
self.contract = project.Multibox.deploy(sender=self.account)
# * call the createRoot method and initialise the contract
self.contract.init(sender=self.account)

def getRoots(self) -> ReceiptAPI:
return self.contract.getRoots() # type: ignore
7 changes: 5 additions & 2 deletions src/fds/contracts/PublicResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def setAll(
If this is desired behavior, use f"{x!r}" or "{!r}".format(x).
Otherwise, decode the bytes
"""
if not self.account:
raise AccountNotSetUp("Set up user account first")

self.node = f"0x{node.to_bytes(32, byteorder='big').hex()}"
self.address = address
self.content = content.encode("utf-8")
Expand Down Expand Up @@ -127,7 +130,7 @@ def setMultiHash(self, node: int, _hash: str) -> ReceiptAPI:
raise AccountNotSetUp("Set up user account first")

self.node = f"0x{node.to_bytes(32, byteorder='big').hex()}"
self.hash = _hash.encode("utf-8")
self.hash = f"0x{_hash.encode().hex()}"

return self.contract.setMultihash(self.node, self.hash, sender=self.account) # type: ignore

Expand All @@ -139,4 +142,4 @@ def getMultiHash(self, node: int) -> str:
"""
self.node = f"0x{node.to_bytes(32, byteorder='big').hex()}"

return convert(self.contract.multihash(self.node), str) # type: ignore
return bytes.fromhex(convert(self.contract.multihash(self.node).hex(), str)[2:]).decode("utf-8") # type: ignore # noqa: 501
Empty file.
23 changes: 20 additions & 3 deletions src/fds/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ethpm_types import ContractType

from fds.contracts.ENSRegistry import EnsRegistry
from fds.contracts.KeyValueTree import KeyValueTree
from fds.fds_contract import FDSContract
from fds.fds_crypto import Crypto
from fds.fds_wallet import Wallet
Expand Down Expand Up @@ -247,11 +248,27 @@ def ENS(owner, ensContract):
def subDomainRegistrarContract(owner, ensContract):
node = 1
node = node.to_bytes(32, byteorder="big")
contract = project.SubdomainRegistrar.deploy(ensContract.address, node, sender=owner)

return contract
return project.SubdomainRegistrar.deploy(ensContract.address, node, sender=owner)


@pytest.fixture
def publicResolverContract(owner, ensContract):
return project.PublicResolver.deploy(ensContract.address, sender=owner)


@pytest.fixture
def multibox(owner):
mb = project.Multibox.deploy(sender=owner)
mb.init(sender=owner)

return mb


@pytest.fixture
def key_value_tree(owner):
return project.KeyValueTree.deploy(owner.address, sender=owner)


@pytest.fixture
def kvt(owner, key_value_tree):
return KeyValueTree(owner, key_value_tree.address)
176 changes: 176 additions & 0 deletions src/fds/tests/data/contracts/abi/Multibox.json

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/fds/tests/unit_tests/test_key_value_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ape import convert

from fds.contracts.KeyValueTree import KeyValueTree


def test_load_key_value_tree(owner, key_value_tree):
kvt = KeyValueTree(owner, key_value_tree.address)

assert kvt


def test_get_shared_id(kvt):
assert (
kvt.getSharedId().hex()
== "0x23e642b7242469a5e3184a6566020c815689149967703a98c0affc14b9ca9b28"
)


def test_get_root_id(kvt):
assert (
kvt.getRootId().hex()
== "0xc7f5bbf5fe95923f0691c94f666ac3dfed12456cd33bd018e7620c3d93edd5a6"
)


def test_set_n_get_key_value(kvt):
node = 1
key = 1
value = 420

kvt.setKeyValue(node, key, value)

assert convert(kvt.getKeyValue(node, key).hex(), int) == value


def test_add_child_node(kvt):
parent_node_id = "0xc7f5bbf5fe95923f0691c94f666ac3dfed12456cd33bd018e7620c3d93edd5a6"
sub_node_id = "0x23e642b7242469a5e3184a6566020c815689149967703a98c0affc14b9ca9b28"
kvt.addChildNode(parent_node_id, sub_node_id)

assert kvt.getChildren(parent_node_id)[-1].hex() == sub_node_id
13 changes: 13 additions & 0 deletions src/fds/tests/unit_tests/test_multibox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fds.contracts.Multibox import MultiBox


def test_multibox_deploy(owner):
mb = MultiBox(owner)

assert mb


def test_get_root(owner, multibox):
mb = MultiBox(owner, multibox.address)

assert mb.getRoots()[-1] != "0x000000000000000000000000000000000000000"
2 changes: 2 additions & 0 deletions src/fds/utils/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def int_to_bytes32(input: int) -> str:
return f"0x{input.to_bytes(32, byteorder='big').hex()}"

0 comments on commit 958836d

Please sign in to comment.