diff --git a/dkg/asset.py b/dkg/asset.py index ad002ec..3ccc1f5 100644 --- a/dkg/asset.py +++ b/dkg/asset.py @@ -34,6 +34,7 @@ PRIVATE_HISTORICAL_REPOSITORY, ) from dkg.dataclasses import ( + BidSuggestionRange, KnowledgeAssetContentVisibility, KnowledgeAssetEnumStates, NodeResponseDict, @@ -230,6 +231,7 @@ def create( content_asset_storage_address, public_assertion_id, DEFAULT_HASH_FUNCTION_ID, + token_amount or BidSuggestionRange.LOW, )["bidSuggestion"] ) @@ -396,6 +398,7 @@ def update( content_asset_storage_address, public_assertion_id, DEFAULT_HASH_FUNCTION_ID, + token_amount or BidSuggestionRange.LOW, )["bidSuggestion"] ) @@ -499,8 +502,8 @@ def burn(self, ual: UAL) -> dict[str, UAL | TxReceipt]: def get( self, ual: UAL, - state: str | HexStr | int = KnowledgeAssetEnumStates.LATEST.value, - content_visibility: str = KnowledgeAssetContentVisibility.ALL.value, + state: str | HexStr | int = KnowledgeAssetEnumStates.LATEST, + content_visibility: str = KnowledgeAssetContentVisibility.ALL, output_format: Literal["JSON-LD", "N-Quads"] = "JSON-LD", validate: bool = True, ) -> dict[str, UAL | HexStr | list[JSONLD] | dict[str, str]]: @@ -528,10 +531,10 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]: is_state_finalized = False match state: - case KnowledgeAssetEnumStates.LATEST.value: + case KnowledgeAssetEnumStates.LATEST: public_assertion_id, is_state_finalized = handle_latest_state(token_id) - case KnowledgeAssetEnumStates.LATEST_FINALIZED.value: + case KnowledgeAssetEnumStates.LATEST_FINALIZED: public_assertion_id, is_state_finalized = handle_latest_finalized_state( token_id ) @@ -592,7 +595,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]: ) result = {"operation": {}} - if content_visibility != KnowledgeAssetContentVisibility.PRIVATE.value: + if content_visibility != KnowledgeAssetContentVisibility.PRIVATE: formatted_public_assertion = public_assertion match output_format: @@ -609,7 +612,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]: f"{output_format} isn't supported!" ) - if content_visibility == KnowledgeAssetContentVisibility.PUBLIC.value: + if content_visibility == KnowledgeAssetContentVisibility.PUBLIC: result = { **result, "asertion": formatted_public_assertion, @@ -626,7 +629,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]: "status": get_public_operation_result["status"], } - if content_visibility != KnowledgeAssetContentVisibility.PUBLIC.value: + if content_visibility != KnowledgeAssetContentVisibility.PUBLIC: private_assertion_link_triples = list( filter( lambda element: PRIVATE_ASSERTION_PREDICATE in element, @@ -702,7 +705,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]: f"{output_format} isn't supported!" ) - if content_visibility == KnowledgeAssetContentVisibility.PRIVATE: + if content_visibility == KnowledgeAssetContentVisibility: result = { **result, "assertion": formatted_private_assertion, @@ -751,6 +754,7 @@ def extend_storing_period( content_asset_storage_address, latest_finalized_state, DEFAULT_HASH_FUNCTION_ID, + token_amount or BidSuggestionRange.LOW, )["bidSuggestion"] ) @@ -806,6 +810,7 @@ def add_tokens( content_asset_storage_address, latest_finalized_state, DEFAULT_HASH_FUNCTION_ID, + token_amount or BidSuggestionRange.LOW, )["bidSuggestion"] ) - sum(agreement_data.tokensInfo) @@ -863,6 +868,7 @@ def add_update_tokens( content_asset_storage_address, unfinalized_state, DEFAULT_HASH_FUNCTION_ID, + token_amount or BidSuggestionRange.LOW, )["bidSuggestion"] ) - sum(agreement_data.tokensInfo) diff --git a/dkg/dataclasses.py b/dkg/dataclasses.py index 9fc0c1b..526b094 100644 --- a/dkg/dataclasses.py +++ b/dkg/dataclasses.py @@ -15,10 +15,12 @@ # specific language governing permissions and limitations # under the License. -from enum import Enum +from enum import auto, Enum import pandas as pd +from dkg.types import AutoStrEnum, AutoStrEnumUpperCase + class BlockchainResponseDict(dict): pass @@ -34,12 +36,18 @@ def to_dataframe(self) -> pd.DataFrame: return pd.DataFrame(self) -class KnowledgeAssetEnumStates(Enum): - LATEST = "LATEST" - LATEST_FINALIZED = "LATEST_FINALIZED" +class BidSuggestionRange(AutoStrEnum): + LOW = auto() + MEDIUM = auto() + HIGH = auto() + ALL = auto() + +class KnowledgeAssetEnumStates(AutoStrEnumUpperCase): + LATEST = auto() + LATEST_FINALIZED = auto() -class KnowledgeAssetContentVisibility(Enum): - ALL = "ALL" - PUBLIC = "PUBLIC" - PRIVATE = "PRIVATE" +class KnowledgeAssetContentVisibility(AutoStrEnumUpperCase): + ALL = auto() + PUBLIC = auto() + PRIVATE = auto() diff --git a/dkg/network.py b/dkg/network.py index 25da8ae..3be3fda 100644 --- a/dkg/network.py +++ b/dkg/network.py @@ -16,6 +16,7 @@ # under the License. from dkg.constants import DEFAULT_HASH_FUNCTION_ID +from dkg.dataclasses import BidSuggestionRange from dkg.manager import DefaultRequestManager from dkg.method import Method from dkg.module import Module @@ -33,19 +34,28 @@ def __init__(self, manager: DefaultRequestManager): _get_bid_suggestion = Method(NodeRequest.bid_suggestion) def get_bid_suggestion( - self, public_assertion_id: DataHexStr, size_in_bytes: int, epochs_number: int, + self, + public_assertion_id: DataHexStr, + size_in_bytes: int, + epochs_number: int, + range: BidSuggestionRange = BidSuggestionRange.LOW.value, ) -> int: content_asset_storage_address = self._get_asset_storage_address( "ContentAssetStorage" ) - return int( - self._get_bid_suggestion( - self.manager.blockchain_provider.blockchain_id, - epochs_number, - size_in_bytes, - content_asset_storage_address, - public_assertion_id, - DEFAULT_HASH_FUNCTION_ID, - )["bidSuggestion"] + response = self._get_bid_suggestion( + self.manager.blockchain_provider.blockchain_id, + epochs_number, + size_in_bytes, + content_asset_storage_address, + public_assertion_id, + DEFAULT_HASH_FUNCTION_ID, + range, + ) + + return ( + int(response["bidSuggestion"]) + if range != BidSuggestionRange.ALL + else response ) diff --git a/dkg/providers/blockchain.py b/dkg/providers/blockchain.py index 8bcc683..7478b7b 100644 --- a/dkg/providers/blockchain.py +++ b/dkg/providers/blockchain.py @@ -190,6 +190,9 @@ def set_account(self, private_key: DataHexStr): self.w3.eth.default_account = self.account.address def _get_network_gas_price(self) -> Wei | None: + if self.environment == "development": + return None + blockchain_name, _ = self.blockchain_id.split(":") default_gas_price = self.w3.to_wei( diff --git a/dkg/types/__init__.py b/dkg/types/__init__.py index 25e79fb..352396d 100644 --- a/dkg/types/__init__.py +++ b/dkg/types/__init__.py @@ -1,3 +1,4 @@ +from .general import AutoStrEnum, AutoStrEnumUpperCase # NOQA: F401 from .blockchain import (ABI, ABIElement, ABIError, ABIEvent, # NOQA: F401 ABIFunction, ABIParameter, AgreementData, Environment) from .dkg_node import UAL # NOQA: F401 diff --git a/dkg/types/general.py b/dkg/types/general.py new file mode 100644 index 0000000..0c19370 --- /dev/null +++ b/dkg/types/general.py @@ -0,0 +1,29 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from enum import Enum + +class AutoStrEnum(str, Enum): + @staticmethod + def _generate_next_value_(name: str, start: int, count: int, last_values: list) -> str: + return name.lower() + + +class AutoStrEnumUpperCase(str, Enum): + @staticmethod + def _generate_next_value_(name: str, start: int, count: int, last_values: list) -> str: + return name \ No newline at end of file diff --git a/dkg/utils/node_request.py b/dkg/utils/node_request.py index cdc6f44..0b07991 100644 --- a/dkg/utils/node_request.py +++ b/dkg/utils/node_request.py @@ -16,12 +16,12 @@ # under the License. from dataclasses import dataclass, field -from enum import Enum +from enum import auto, Enum from typing import Any, Type -from dkg.dataclasses import HTTPRequestMethod +from dkg.dataclasses import BidSuggestionRange, HTTPRequestMethod from dkg.exceptions import OperationFailed, OperationNotFinished -from dkg.types import UAL, Address, DataHexStr, NQuads +from dkg.types import AutoStrEnumUpperCase, UAL, Address, DataHexStr, NQuads @dataclass @@ -44,6 +44,7 @@ class NodeRequest: "contentAssetStorageAddress": Address, "firstAssertionId": DataHexStr, "hashFunctionId": int, + "bidSuggestionRange": BidSuggestionRange, }, ) get_operation_result = NodeCall( @@ -92,86 +93,82 @@ class NodeRequest: ) -class LocalStoreOperationStatus(Enum): - LOCAL_STORE_INIT_START = "LOCAL_STORE_INIT_START" - LOCAL_STORE_INIT_END = "LOCAL_STORE_INIT_END" - LOCAL_STORE_START = "LOCAL_STORE_START" - LOCAL_STORE_END = "LOCAL_STORE_END" +class LocalStoreOperationStatus(AutoStrEnumUpperCase): + LOCAL_STORE_INIT_START = auto() + LOCAL_STORE_INIT_END = auto() + LOCAL_STORE_START = auto() + LOCAL_STORE_END = auto() class PublishOperationStatus(Enum): - VALIDATING_PUBLISH_ASSERTION_REMOTE_START = ( - "VALIDATING_PUBLISH_ASSERTION_REMOTE_START" - ) - VALIDATING_PUBLISH_ASSERTION_REMOTE_END = "VALIDATING_PUBLISH_ASSERTION_REMOTE_END" - INSERTING_ASSERTION = "INSERTING_ASSERTION" - PUBLISHING_ASSERTION = "PUBLISHING_ASSERTION" - PUBLISH_START = "PUBLISH_START" - PUBLISH_INIT_START = "PUBLISH_INIT_START" - PUBLISH_INIT_END = "PUBLISH_INIT_END" - PUBLISH_LOCAL_STORE_START = "PUBLISH_LOCAL_STORE_START" - PUBLISH_LOCAL_STORE_END = "PUBLISH_LOCAL_STORE_END" - PUBLISH_REPLICATE_START = "PUBLISH_REPLICATE_START" - PUBLISH_REPLICATE_END = "PUBLISH_REPLICATE_END" - PUBLISH_END = "PUBLISH_END" - - -class UpdateOperationStatus(Enum): - UPDATE_START = "UPDATE_START" - UPDATE_INIT_START = "UPDATE_INIT_START" - UPDATE_INIT_END = "UPDATE_INIT_END" - UPDATE_REPLICATE_START = "UPDATE_REPLICATE_START" - UPDATE_REPLICATE_END = "UPDATE_REPLICATE_END" - VALIDATING_UPDATE_ASSERTION_REMOTE_START = ( - "VALIDATING_UPDATE_ASSERTION_REMOTE_START" - ) - VALIDATING_UPDATE_ASSERTION_REMOTE_END = "VALIDATING_UPDATE_ASSERTION_REMOTE_END" - UPDATE_END = "UPDATE_END" - - -class StoreTypes(Enum): - TRIPLE = "TRIPLE" - PENDING = "PENDING" - - -class GetOperationStatus(Enum): - ASSERTION_EXISTS_LOCAL_START = "ASSERTION_EXISTS_LOCAL_START" - ASSERTION_EXISTS_LOCAL_END = "ASSERTION_EXISTS_LOCAL_END" - GET_START = "GET_START" - GET_INIT_START = "GET_INIT_START" - GET_INIT_END = "GET_INIT_END" - GET_LOCAL_START = "GET_LOCAL_START" - GET_LOCAL_END = "GET_LOCAL_END" - GET_REMOTE_START = "GET_REMOTE_START" - GET_REMOTE_END = "GET_REMOTE_END" - GET_FETCH_FROM_NODES_START = "GET_FETCH_FROM_NODES_START" - GET_FETCH_FROM_NODES_END = "GET_FETCH_FROM_NODES_END" - GET_END = "GET_END" - - -class QueryOperationStatus(Enum): - QUERY_INIT_START = "QUERY_INIT_START" - QUERY_INIT_END = "QUERY_INIT_END" - QUERY_START = "QUERY_START" - QUERY_END = "QUERY_END" - - -class OperationStatus(Enum): - PENDING = "PENDING" - FAILED = "FAILED" - COMPLETED = "COMPLETED" - FIND_NODES_START = "FIND_NODES_START" - FIND_NODES_END = "FIND_NODES_END" - FIND_NODES_LOCAL_START = "FIND_NODES_LOCAL_START" - FIND_NODES_LOCAL_END = "FIND_NODES_LOCAL_END" - FIND_NODES_OPEN_CONNECTION_START = "FIND_NODES_OPEN_CONNECTION_START" - FIND_NODES_OPEN_CONNECTION_END = "FIND_NODES_OPEN_CONNECTION_END" - FIND_NODES_CREATE_STREAM_START = "FIND_NODES_CREATE_STREAM_START" - FIND_NODES_CREATE_STREAM_END = "FIND_NODES_CREATE_STREAM_END" - FIND_NODES_SEND_MESSAGE_START = "FIND_NODES_SEND_MESSAGE_START" - FIND_NODES_SEND_MESSAGE_END = "FIND_NODES_SEND_MESSAGE_END" - DIAL_PROTOCOL_START = "DIAL_PROTOCOL_START" - DIAL_PROTOCOL_END = "DIAL_PROTOCOL_END" + VALIDATING_PUBLISH_ASSERTION_REMOTE_START = auto() + VALIDATING_PUBLISH_ASSERTION_REMOTE_END = auto() + INSERTING_ASSERTION = auto() + PUBLISHING_ASSERTION = auto() + PUBLISH_START = auto() + PUBLISH_INIT_START = auto() + PUBLISH_INIT_END = auto() + PUBLISH_LOCAL_STORE_START = auto() + PUBLISH_LOCAL_STORE_END = auto() + PUBLISH_REPLICATE_START = auto() + PUBLISH_REPLICATE_END = auto() + PUBLISH_END = auto() + + +class UpdateOperationStatus(AutoStrEnumUpperCase): + UPDATE_START = auto() + UPDATE_INIT_START = auto() + UPDATE_INIT_END = auto() + UPDATE_REPLICATE_START = auto() + UPDATE_REPLICATE_END = auto() + VALIDATING_UPDATE_ASSERTION_REMOTE_START = auto() + VALIDATING_UPDATE_ASSERTION_REMOTE_END = auto() + UPDATE_END = auto() + + +class StoreTypes(AutoStrEnumUpperCase): + TRIPLE = auto() + PENDING = auto() + + +class GetOperationStatus(AutoStrEnumUpperCase): + ASSERTION_EXISTS_LOCAL_START = auto() + ASSERTION_EXISTS_LOCAL_END = auto() + GET_START = auto() + GET_INIT_START = auto() + GET_INIT_END = auto() + GET_LOCAL_START = auto() + GET_LOCAL_END = auto() + GET_REMOTE_START = auto() + GET_REMOTE_END = auto() + GET_FETCH_FROM_NODES_START = auto() + GET_FETCH_FROM_NODES_END = auto() + GET_END = auto() + + +class QueryOperationStatus(AutoStrEnumUpperCase): + QUERY_INIT_START = auto() + QUERY_INIT_END = auto() + QUERY_START = auto() + QUERY_END = auto() + + +class OperationStatus(AutoStrEnumUpperCase): + PENDING = auto() + FAILED = auto() + COMPLETED = auto() + FIND_NODES_START = auto() + FIND_NODES_END = auto() + FIND_NODES_LOCAL_START = auto() + FIND_NODES_LOCAL_END = auto() + FIND_NODES_OPEN_CONNECTION_START = auto() + FIND_NODES_OPEN_CONNECTION_END = auto() + FIND_NODES_CREATE_STREAM_START = auto() + FIND_NODES_CREATE_STREAM_END = auto() + FIND_NODES_SEND_MESSAGE_START = auto() + FIND_NODES_SEND_MESSAGE_END = auto() + DIAL_PROTOCOL_START = auto() + DIAL_PROTOCOL_END = auto() LOCAL_STORE = LocalStoreOperationStatus PUBLISH = PublishOperationStatus UPDATE = UpdateOperationStatus diff --git a/pyproject.toml b/pyproject.toml index 6281cdd..3fcb427 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dkg" -version = "0.1.0-beta.6" +version = "0.1.0-beta.7" description = "Python library for interacting with the OriginTrail Decentralized Knowledge Graph" authors = ["Uladzislau Hubar "] license = "Apache-2.0"