mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 06:25:45 +00:00
created backend models folder, replaced token_hex with uuid
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
caa2fc54fc
commit
101706a3f5
@ -4,30 +4,11 @@
|
|||||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
# Asset should represent a single asset (e.g.: tarantool tuple (data, tx_id, asset_id))
|
|
||||||
# If multiple assets are stored at once this should remain the same.
|
|
||||||
# For Create ({'data': 'values'}, c_tx_id, c_tx_id), For Transfer ({'id': c_tx_id}, tx_id, c_tx_id)
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Asset:
|
|
||||||
id: str = ""
|
|
||||||
tx_id: str = ""
|
|
||||||
data: str = ""
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class MetaData:
|
|
||||||
id: str = ""
|
|
||||||
metadata: str = ""
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Input:
|
|
||||||
tx_id: str = ""
|
|
||||||
fulfills: Union[dict, None] = None
|
|
||||||
owners_before: list[str] = None
|
|
||||||
fulfillment: str = ""
|
|
||||||
|
|
||||||
|
# NOTE: only here temporarily
|
||||||
|
from planetmint.backend.models import Asset, MetaData, Input
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Output:
|
class Output:
|
||||||
id: str = None
|
id: str = None
|
||||||
|
|||||||
4
planetmint/backend/models/__init__.py
Normal file
4
planetmint/backend/models/__init__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from .asset import Asset
|
||||||
|
from .fulfills import Fulfills
|
||||||
|
from .input import Input
|
||||||
|
from .metadata import MetaData
|
||||||
12
planetmint/backend/models/asset.py
Normal file
12
planetmint/backend/models/asset.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# Copyright © 2020 Interplanetary Database Association e.V.,
|
||||||
|
# Planetmint and IPDB software contributors.
|
||||||
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Asset:
|
||||||
|
id: str = ""
|
||||||
|
tx_id: str = ""
|
||||||
|
data: str = ""
|
||||||
11
planetmint/backend/models/fulfills.py
Normal file
11
planetmint/backend/models/fulfills.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Copyright © 2020 Interplanetary Database Association e.V.,
|
||||||
|
# Planetmint and IPDB software contributors.
|
||||||
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Fulfills:
|
||||||
|
transaction_id: str = ""
|
||||||
|
output_index: int = 0
|
||||||
16
planetmint/backend/models/input.py
Normal file
16
planetmint/backend/models/input.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# Copyright © 2020 Interplanetary Database Association e.V.,
|
||||||
|
# Planetmint and IPDB software contributors.
|
||||||
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from .fulfills import Fulfills
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Input:
|
||||||
|
tx_id: str = ""
|
||||||
|
fulfills: Optional[Fulfills] = None
|
||||||
|
owners_before: list[str] = field(default_factory=list)
|
||||||
|
fulfillment: str = ""
|
||||||
11
planetmint/backend/models/metadata.py
Normal file
11
planetmint/backend/models/metadata.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Copyright © 2020 Interplanetary Database Association e.V.,
|
||||||
|
# Planetmint and IPDB software contributors.
|
||||||
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MetaData:
|
||||||
|
id: str = ""
|
||||||
|
metadata: str = ""
|
||||||
@ -5,14 +5,13 @@
|
|||||||
|
|
||||||
"""Query implementation for Tarantool"""
|
"""Query implementation for Tarantool"""
|
||||||
import json
|
import json
|
||||||
|
from uuid import uuid4
|
||||||
from secrets import token_hex
|
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from tarantool.error import DatabaseError
|
from tarantool.error import DatabaseError
|
||||||
from planetmint.backend import query
|
from planetmint.backend import query
|
||||||
from planetmint.backend.utils import module_dispatch_registrar
|
from planetmint.backend.utils import module_dispatch_registrar
|
||||||
from planetmint.backend.interfaces import Asset, MetaData, Input
|
from planetmint.backend.models import Asset, MetaData, Input, Fulfills
|
||||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||||
from planetmint.backend.tarantool.transaction.tools import TransactionCompose, TransactionDecompose
|
from planetmint.backend.tarantool.transaction.tools import TransactionCompose, TransactionDecompose
|
||||||
|
|
||||||
@ -65,10 +64,8 @@ def get_inputs_by_tx_id(connection, tx_id: str) -> list[Input]:
|
|||||||
fulfills_tx_id = input[3]
|
fulfills_tx_id = input[3]
|
||||||
|
|
||||||
if fulfills_tx_id:
|
if fulfills_tx_id:
|
||||||
fulfills = {
|
# TODO: the output_index should be an unsigned int
|
||||||
"transaction_id": fulfills_tx_id,
|
fulfills = Fulfills(fulfills_tx_id, int(input[4]))
|
||||||
"output_index": int(input[4])
|
|
||||||
}
|
|
||||||
|
|
||||||
inputs.append(Input(tx_id, fulfills, owners_before, fulfillment))
|
inputs.append(Input(tx_id, fulfills, owners_before, fulfillment))
|
||||||
|
|
||||||
@ -80,8 +77,11 @@ def store_transaction_inputs(connection, inputs: list[Input]):
|
|||||||
connection.run(connection.space("inputs").insert((
|
connection.run(connection.space("inputs").insert((
|
||||||
input.tx_id,
|
input.tx_id,
|
||||||
input.fulfillment,
|
input.fulfillment,
|
||||||
input.fulfills["transaction_id"] if input.fulfills else "",
|
input.owners_before,
|
||||||
input.fulfills["output_index"] if input.fulfills else "",
|
input.fulfills.transaction_id if input.fulfills else "",
|
||||||
|
# TODO: the output_index should be an unsigned int
|
||||||
|
str(input.fulfills.output_index) if input.fulfills else "",
|
||||||
|
uuid4().hex,
|
||||||
index
|
index
|
||||||
)))
|
)))
|
||||||
|
|
||||||
@ -208,7 +208,7 @@ def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR
|
|||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def store_block(connection, block: dict):
|
def store_block(connection, block: dict):
|
||||||
block_unique_id = token_hex(8)
|
block_unique_id = uuid4().hex
|
||||||
connection.run(
|
connection.run(
|
||||||
connection.space("blocks").insert((block["app_hash"], block["height"], block_unique_id)), only_data=False
|
connection.space("blocks").insert((block["app_hash"], block["height"], block_unique_id)), only_data=False
|
||||||
)
|
)
|
||||||
@ -370,7 +370,7 @@ def get_unspent_outputs(connection, query=None): # for now we don't have implem
|
|||||||
def store_pre_commit_state(connection, state: dict):
|
def store_pre_commit_state(connection, state: dict):
|
||||||
_precommit = connection.run(connection.space("pre_commits").select([], limit=1))
|
_precommit = connection.run(connection.space("pre_commits").select([], limit=1))
|
||||||
_precommitTuple = (
|
_precommitTuple = (
|
||||||
(token_hex(8), state["height"], state["transactions"])
|
(uuid4().hex, state["height"], state["transactions"])
|
||||||
if _precommit is None or len(_precommit) == 0
|
if _precommit is None or len(_precommit) == 0
|
||||||
else _precommit[0]
|
else _precommit[0]
|
||||||
)
|
)
|
||||||
@ -394,7 +394,7 @@ def get_pre_commit_state(connection):
|
|||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def store_validator_set(conn, validators_update: dict):
|
def store_validator_set(conn, validators_update: dict):
|
||||||
_validator = conn.run(conn.space("validators").select(validators_update["height"], index="height_search", limit=1))
|
_validator = conn.run(conn.space("validators").select(validators_update["height"], index="height_search", limit=1))
|
||||||
unique_id = token_hex(8) if _validator is None or len(_validator) == 0 else _validator[0][0]
|
unique_id = uuid4().hex if _validator is None or len(_validator) == 0 else _validator[0][0]
|
||||||
conn.run(
|
conn.run(
|
||||||
conn.space("validators").upsert(
|
conn.space("validators").upsert(
|
||||||
(unique_id, validators_update["height"], validators_update["validators"]),
|
(unique_id, validators_update["height"], validators_update["validators"]),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user