mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 14:35:45 +00:00
used new indexes on block related db operations
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
488053fb60
commit
1832bf4814
@ -11,3 +11,4 @@ from .script import Script
|
|||||||
from .output import Output
|
from .output import Output
|
||||||
from .keys import Keys
|
from .keys import Keys
|
||||||
from .dbtransaction import DbTransaction
|
from .dbtransaction import DbTransaction
|
||||||
|
from .block import Block
|
||||||
|
|||||||
27
planetmint/backend/models/block.py
Normal file
27
planetmint/backend/models/block.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# 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 __future__ import annotations
|
||||||
|
import json
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Block:
|
||||||
|
id: str = ""
|
||||||
|
app_hash: str = ""
|
||||||
|
height: int = 0
|
||||||
|
transactions: list[str] = field(default_factory=list)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_tuple(block_tuple: tuple) -> Block:
|
||||||
|
return Block(block_tuple[0], block_tuple[1], block_tuple[2], block_tuple[3])
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
"app_hash": self.app_hash,
|
||||||
|
"height": self.height,
|
||||||
|
"transactions": self.transactions
|
||||||
|
}
|
||||||
@ -25,7 +25,7 @@ from planetmint.backend.tarantool.const import (
|
|||||||
TARANT_ID_SEARCH,
|
TARANT_ID_SEARCH,
|
||||||
)
|
)
|
||||||
from planetmint.backend.utils import module_dispatch_registrar
|
from planetmint.backend.utils import module_dispatch_registrar
|
||||||
from planetmint.backend.models import Asset, MetaData, Input, Script, Output
|
from planetmint.backend.models import Asset, Block, MetaData, Input, Script, Output
|
||||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||||
|
|
||||||
register_query = module_dispatch_registrar(query)
|
register_query = module_dispatch_registrar(query)
|
||||||
@ -284,27 +284,15 @@ def get_spent(connection, fullfil_transaction_id: str, fullfil_output_index: str
|
|||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR
|
def get_latest_block(connection):
|
||||||
# NOTE:TARANTOOL THROWS ERROR ON ITERATOR 'REQ'
|
blocks = connection.run(connection.space("blocks").select())
|
||||||
latest_blocks = connection.run(connection.space("blocks").select())
|
blocks = sorted(blocks, key=itemgetter(2), reverse=True)
|
||||||
|
latest_block = Block.from_tuple(blocks[0])
|
||||||
|
return latest_block.to_dict()
|
||||||
if not latest_blocks:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# TODO: return Block dataclass instance
|
|
||||||
block = {
|
|
||||||
"app_hash": latest_blocks[0][1],
|
|
||||||
"height": latest_blocks[0][2],
|
|
||||||
TARANT_TABLE_TRANSACTION: latest_blocks[0][3]
|
|
||||||
}
|
|
||||||
|
|
||||||
return block
|
|
||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def store_block(connection, block: dict):
|
def store_block(connection, block: dict):
|
||||||
|
|
||||||
block_unique_id = uuid4().hex
|
block_unique_id = uuid4().hex
|
||||||
connection.run(
|
connection.run(
|
||||||
connection.space("blocks").insert((block_unique_id, block["app_hash"], block["height"], block[TARANT_TABLE_TRANSACTION])), only_data=False
|
connection.space("blocks").insert((block_unique_id, block["app_hash"], block["height"], block[TARANT_TABLE_TRANSACTION])), only_data=False
|
||||||
@ -395,23 +383,15 @@ def get_spending_transactions(connection, inputs):
|
|||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def get_block(connection, block_id=None):
|
def get_block(connection, block_id=None):
|
||||||
if block_id is None:
|
_block = connection.run(connection.space("blocks").select(block_id, index="id", limit=1))
|
||||||
block_id = []
|
_block = Block.from_tuple(_block[0])
|
||||||
_block = connection.run(connection.space("blocks").select(block_id, index="block_search", limit=1))
|
return _block.to_dict()
|
||||||
if _block is None or len(_block) == 0:
|
|
||||||
return []
|
|
||||||
_block = _block[0]
|
|
||||||
_txblock = connection.run(connection.space("blocks_tx").select(_block[2], index="block_search"))
|
|
||||||
return {"app_hash": _block[0], "height": _block[1], TARANT_TABLE_TRANSACTION: [_tx[0] for _tx in _txblock]}
|
|
||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
def get_block_with_transaction(connection, txid: str):
|
def get_block_with_transaction(connection, txid: str):
|
||||||
_all_blocks_tx = connection.run(connection.space("blocks_tx").select(txid, index=TARANT_ID_SEARCH))
|
_block = connection.run(connection.space("blocks").select(txid, index="block_by_transaction_id"))
|
||||||
if _all_blocks_tx is None or len(_all_blocks_tx) == 0:
|
return _block[0] if len(_block) == 1 else []
|
||||||
return []
|
|
||||||
_block = connection.run(connection.space("blocks").select(_all_blocks_tx[0][1], index="block_id_search"))
|
|
||||||
return [{"height": _height[1]} for _height in _block]
|
|
||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDBConnection)
|
@register_query(TarantoolDBConnection)
|
||||||
|
|||||||
@ -335,7 +335,7 @@ class Planetmint(object):
|
|||||||
if len(blocks) > 1:
|
if len(blocks) > 1:
|
||||||
logger.critical("Transaction id %s exists in multiple blocks", txid)
|
logger.critical("Transaction id %s exists in multiple blocks", txid)
|
||||||
|
|
||||||
return [block["height"] for block in blocks]
|
return blocks
|
||||||
|
|
||||||
def validate_transaction(self, tx, current_transactions=[]):
|
def validate_transaction(self, tx, current_transactions=[]):
|
||||||
"""Validate a transaction against the current status of the database."""
|
"""Validate a transaction against the current status of the database."""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user