mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 06:25:45 +00:00
Further fixes
Signed-off-by: cybnon <stefan.weber93@googlemail.com>
This commit is contained in:
parent
135d049969
commit
0fd2bc41fc
@ -9,3 +9,5 @@ from .input import Input
|
|||||||
from .metadata import MetaData
|
from .metadata import MetaData
|
||||||
from .script import Script
|
from .script import Script
|
||||||
from .output import Output
|
from .output import Output
|
||||||
|
from .keys import Keys
|
||||||
|
from .dbtransaction import DbTransaction
|
||||||
|
|||||||
@ -17,3 +17,14 @@ class Asset:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def from_tuple(asset_tuple: tuple) -> Asset:
|
def from_tuple(asset_tuple: tuple) -> Asset:
|
||||||
return Asset(asset_tuple[2], asset_tuple[1], json.loads(asset_tuple[0])["data"])
|
return Asset(asset_tuple[2], asset_tuple[1], json.loads(asset_tuple[0])["data"])
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"tx_id": self.tx_id,
|
||||||
|
"data": self.data
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def list_to_dict(asset_list: list[Asset]) -> list[dict]:
|
||||||
|
return [asset.to_dict() for asset in asset_list]
|
||||||
|
|||||||
@ -4,15 +4,14 @@
|
|||||||
# 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 __future__ import annotations
|
from __future__ import annotations
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from planetmint.backend.models import Asset, MetaData, Input, Output, Script
|
from planetmint.backend.models import Asset, MetaData, Input, Output, Script
|
||||||
from planetmint.backend.models.keys import Keys
|
from planetmint.backend.models.keys import Keys
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Transaction:
|
class DbTransaction:
|
||||||
id: str = ""
|
id: str = ""
|
||||||
operation: str = ""
|
operation: str = ""
|
||||||
version: str = ""
|
version: str = ""
|
||||||
@ -25,8 +24,8 @@ class Transaction:
|
|||||||
script: Script = None
|
script: Script = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(transaction: dict) -> Transaction:
|
def from_dict(transaction: dict) -> DbTransaction:
|
||||||
return Transaction(
|
return DbTransaction(
|
||||||
id=transaction["id"],
|
id=transaction["id"],
|
||||||
operation=transaction["operation"],
|
operation=transaction["operation"],
|
||||||
version=transaction["version"],
|
version=transaction["version"],
|
||||||
@ -35,8 +34,8 @@ class Transaction:
|
|||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_tuple(transaction: tuple) -> Transaction:
|
def from_tuple(transaction: tuple) -> DbTransaction:
|
||||||
return Transaction(
|
return DbTransaction(
|
||||||
id=transaction[0],
|
id=transaction[0],
|
||||||
operation=transaction[1],
|
operation=transaction[1],
|
||||||
version=transaction[2],
|
version=transaction[2],
|
||||||
@ -48,5 +47,11 @@ class Transaction:
|
|||||||
"id": self.id,
|
"id": self.id,
|
||||||
"operation": self.operation,
|
"operation": self.operation,
|
||||||
"version": self.version,
|
"version": self.version,
|
||||||
|
"inputs": Input.list_to_dict(self.inputs),
|
||||||
|
"assets": Asset.list_to_dict(self.assets),
|
||||||
|
"metadata": self.metadata.to_dict(),
|
||||||
|
"outputs": Output.list_to_dict(self.outputs),
|
||||||
|
"keys": self.keys.to_dict(),
|
||||||
|
"script": self.script.to_dict(),
|
||||||
"transaction": self.raw_transaction,
|
"transaction": self.raw_transaction,
|
||||||
}
|
}
|
||||||
@ -48,3 +48,9 @@ class Input:
|
|||||||
)
|
)
|
||||||
|
|
||||||
return {"fulfills": fulfills, "fulfillment": self.fulfillment, "owners_before": self.owners_before}
|
return {"fulfills": fulfills, "fulfillment": self.fulfillment, "owners_before": self.owners_before}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def list_to_dict(input_list: list[Input]) -> list[dict]:
|
||||||
|
return [input.to_dict() for input in input_list]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -17,3 +17,9 @@ class MetaData:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def from_tuple(meta_data_tuple: tuple) -> MetaData:
|
def from_tuple(meta_data_tuple: tuple) -> MetaData:
|
||||||
return MetaData(meta_data_tuple[0], json.loads(meta_data_tuple[1]))
|
return MetaData(meta_data_tuple[0], json.loads(meta_data_tuple[1]))
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"metadata": self.metadata
|
||||||
|
}
|
||||||
|
|||||||
@ -85,6 +85,10 @@ class Output:
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def list_to_dict(output_list: list[Output]) -> list[dict]:
|
||||||
|
return [output.to_dict() for output in output_list]
|
||||||
|
|
||||||
|
|
||||||
def output_with_public_key(output, tx_id) -> Output:
|
def output_with_public_key(output, tx_id) -> Output:
|
||||||
return Output(
|
return Output(
|
||||||
|
|||||||
@ -16,3 +16,9 @@ class Script:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def from_tuple(script_tuple: tuple) -> Script:
|
def from_tuple(script_tuple: tuple) -> Script:
|
||||||
return Script(script_tuple[0], script_tuple[1])
|
return Script(script_tuple[0], script_tuple[1])
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"script": self.script
|
||||||
|
}
|
||||||
|
|||||||
@ -6,9 +6,13 @@
|
|||||||
"""Query interfaces for backends."""
|
"""Query interfaces for backends."""
|
||||||
|
|
||||||
from functools import singledispatch
|
from functools import singledispatch
|
||||||
|
|
||||||
|
from planetmint.backend.models import Asset, MetaData, Output, Input, Script
|
||||||
|
|
||||||
from planetmint.backend.exceptions import OperationError
|
from planetmint.backend.exceptions import OperationError
|
||||||
from planetmint.backend.interfaces import Asset, Block, MetaData, Input, Script, Output, Transaction
|
|
||||||
from planetmint.backend.models.keys import Keys
|
from planetmint.backend.models.keys import Keys
|
||||||
|
from planetmint.backend.interfaces import Block
|
||||||
|
from planetmint.backend.models.dbtransaction import DbTransaction
|
||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
@ -75,7 +79,7 @@ def get_transaction_space_by_id(connection, transaction_id):
|
|||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def get_transaction_single(connection, transaction_id):
|
def get_transaction_single(connection, transaction_id) -> DbTransaction:
|
||||||
"""Get a single transaction by id."""
|
"""Get a single transaction by id."""
|
||||||
|
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
@ -88,7 +92,7 @@ def get_transaction(connection, transaction_id):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def get_transactions(connection, transactions_ids) -> list[Transaction]:
|
def get_transactions(connection, transactions_ids) -> list[DbTransaction]:
|
||||||
"""Get a transaction from the transactions table.
|
"""Get a transaction from the transactions table.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@ -11,7 +11,7 @@ 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.models.keys import Keys
|
from planetmint.backend.models.keys import Keys
|
||||||
from planetmint.backend.models.transaction import Transaction
|
from planetmint.backend.models.dbtransaction import DbTransaction
|
||||||
from planetmint.backend.tarantool.const import (
|
from planetmint.backend.tarantool.const import (
|
||||||
TARANT_TABLE_META_DATA,
|
TARANT_TABLE_META_DATA,
|
||||||
TARANT_TABLE_ASSETS,
|
TARANT_TABLE_ASSETS,
|
||||||
@ -31,7 +31,7 @@ register_query = module_dispatch_registrar(query)
|
|||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def _group_transaction_by_ids(connection, txids: list) -> list[Transaction]:
|
def _group_transaction_by_ids(connection, txids: list) -> list[DbTransaction]:
|
||||||
_transactions = []
|
_transactions = []
|
||||||
for txid in txids:
|
for txid in txids:
|
||||||
tx = get_transaction_space_by_id(connection, txid)
|
tx = get_transaction_space_by_id(connection, txid)
|
||||||
@ -76,7 +76,7 @@ def get_keys_by_tx_id(connection, tx_id: str) -> list[Keys]:
|
|||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def get_transaction(connection, tx_id: str) -> Transaction:
|
def get_transaction(connection, tx_id: str) -> DbTransaction:
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
@ -174,7 +174,7 @@ def store_transactions(connection, signed_transactions: list):
|
|||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def store_transaction(connection, transaction):
|
def store_transaction(connection, transaction):
|
||||||
tx = (transaction["id"], transaction["operation"], transaction["version"], transaction)
|
tx = (transaction["id"], transaction["operation"], transaction["version"], [transaction])
|
||||||
connection.run(connection.space(TARANT_TABLE_TRANSACTION).insert(tx), only_data=False)
|
connection.run(connection.space(TARANT_TABLE_TRANSACTION).insert(tx), only_data=False)
|
||||||
|
|
||||||
|
|
||||||
@ -183,16 +183,16 @@ def get_transaction_space_by_id(connection, transaction_id):
|
|||||||
txs = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(transaction_id, index=TARANT_ID_SEARCH))
|
txs = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(transaction_id, index=TARANT_ID_SEARCH))
|
||||||
if len(txs) == 0:
|
if len(txs) == 0:
|
||||||
return None
|
return None
|
||||||
return Transaction.from_tuple(txs[0])
|
return DbTransaction.from_tuple(txs[0])
|
||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def get_transaction_single(connection, transaction_id):
|
def get_transaction_single(connection, transaction_id) -> DbTransaction:
|
||||||
return _group_transaction_by_ids(txids=[transaction_id], connection=connection)[0]
|
return _group_transaction_by_ids(txids=[transaction_id], connection=connection)[0]
|
||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def get_transactions(connection, transactions_ids: list) -> list[Transaction]:
|
def get_transactions(connection, transactions_ids: list) -> list[DbTransaction]:
|
||||||
return _group_transaction_by_ids(txids=transactions_ids, connection=connection)
|
return _group_transaction_by_ids(txids=transactions_ids, connection=connection)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -193,7 +193,7 @@ def run_election_approve(args, planet):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
key = load_node_key(args.sk)
|
key = load_node_key(args.sk)
|
||||||
tx = planet.get_transaction_space_by_id(args.election_id)
|
tx = planet.get_transaction(args.election_id)
|
||||||
voting_powers = [v.amount for v in tx.outputs if key.public_key in v.public_keys]
|
voting_powers = [v.amount for v in tx.outputs if key.public_key in v.public_keys]
|
||||||
if len(voting_powers) > 0:
|
if len(voting_powers) > 0:
|
||||||
voting_power = voting_powers[0]
|
voting_power = voting_powers[0]
|
||||||
@ -226,7 +226,7 @@ def run_election_show(args, planet):
|
|||||||
:param planet: an instance of Planetmint
|
:param planet: an instance of Planetmint
|
||||||
"""
|
"""
|
||||||
|
|
||||||
election = planet.get_transaction_space_by_id(args.election_id)
|
election = planet.get_transaction(args.election_id)
|
||||||
if not election:
|
if not election:
|
||||||
logger.error(f"No election found with election_id {args.election_id}")
|
logger.error(f"No election found with election_id {args.election_id}")
|
||||||
return
|
return
|
||||||
|
|||||||
@ -279,8 +279,8 @@ class Planetmint(object):
|
|||||||
elif transactions:
|
elif transactions:
|
||||||
tx_id = transactions[0]["transactions"].id
|
tx_id = transactions[0]["transactions"].id
|
||||||
tx = backend.query.get_transaction_single(self.connection, tx_id)
|
tx = backend.query.get_transaction_single(self.connection, tx_id)
|
||||||
assets = backend.query.get_assets_by_tx_id(self.connection, tx_id)
|
tx.assets = backend.query.get_assets_by_tx_id(self.connection, tx_id)
|
||||||
transaction = {"transactions": tx} | {"assets": [asset.data for asset in assets]}
|
transaction = tx.to_dict()
|
||||||
elif current_spent_transactions:
|
elif current_spent_transactions:
|
||||||
transaction = current_spent_transactions[0]
|
transaction = current_spent_transactions[0]
|
||||||
|
|
||||||
|
|||||||
@ -37,7 +37,7 @@ class TransactionApi(Resource):
|
|||||||
pool = current_app.config["bigchain_pool"]
|
pool = current_app.config["bigchain_pool"]
|
||||||
|
|
||||||
with pool() as planet:
|
with pool() as planet:
|
||||||
tx = planet.get_transaction_space_by_id(tx_id)
|
tx = planet.get_transaction(tx_id)
|
||||||
|
|
||||||
if not tx:
|
if not tx:
|
||||||
return make_error(404)
|
return make_error(404)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user