mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 14:35:45 +00:00
fixed asset issues
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
73ce3ec861
commit
aeffe4f760
@ -80,6 +80,7 @@ def store_transactions(connection, signed_transactions: list):
|
||||
metadatasxspace.insert(txtuples["metadata"])
|
||||
|
||||
if txtuples["asset"] is not None:
|
||||
print(f" ASSET : {txtuples['asset']}")
|
||||
assetsxspace.insert(txtuples["asset"])
|
||||
|
||||
|
||||
@ -115,13 +116,16 @@ def get_metadata(connection, transaction_ids: list):
|
||||
@register_query(TarantoolDB)
|
||||
# asset: {"id": "asset_id"}
|
||||
# asset: {"data": any} -> insert (tx_id, asset["data"]).
|
||||
def store_asset(connection, asset: dict, tx_id=None):
|
||||
#def store_asset(connection, asset: dict, tx_id=None):
|
||||
def store_asset(connection, asset: dict):
|
||||
space = connection.space("assets")
|
||||
print(f"DATA store asset: {asset}")
|
||||
try:
|
||||
if tx_id is not None:
|
||||
space.insert((asset, tx_id, tx_id))
|
||||
else:
|
||||
space.insert((asset, str(asset["id"]), str(asset["id"]))) # TODO Review this function
|
||||
space.insert( asset )
|
||||
#if tx_id is not None:
|
||||
# space.insert((asset, tx_id, tx_id))
|
||||
#else:
|
||||
# space.insert((asset, str(asset["id"]), str(asset["id"]))) # TODO Review this function
|
||||
except: # TODO Add Raise For Duplicate
|
||||
print("DUPLICATE ERROR")
|
||||
|
||||
@ -131,29 +135,34 @@ def store_assets(connection, assets: list):
|
||||
space = connection.space("assets")
|
||||
for asset in assets:
|
||||
try:
|
||||
space.insert((asset, asset["id"]))
|
||||
except: # TODO Raise ERROR for Duplicate
|
||||
pass
|
||||
print(f"DATA store assets: {asset}")
|
||||
space.insert( asset )
|
||||
except : # TODO Raise ERROR for Duplicate
|
||||
print( f"EXCEPTION : ")
|
||||
|
||||
|
||||
@register_query(TarantoolDB)
|
||||
def get_asset(connection, asset_id: str):
|
||||
space = connection.space("assets")
|
||||
_data = space.select(asset_id, index="txid_search")
|
||||
data1 = _data
|
||||
_data = _data.data
|
||||
return _data[0][1] if len(_data) == 1 else []
|
||||
print(f"DATA : {data1}")
|
||||
return _data[0] if len(_data) == 1 else []
|
||||
|
||||
|
||||
@register_query(TarantoolDB)
|
||||
def get_assets(connection, assets_ids: list) -> list:
|
||||
_returned_data = []
|
||||
space = connection.space("assets")
|
||||
# space = connection.space("assets")
|
||||
for _id in list(set(assets_ids)):
|
||||
asset = space.select(str(_id), index="txid_search")
|
||||
if len(asset) == 0:
|
||||
continue
|
||||
asset = asset.data[0]
|
||||
_returned_data.append(asset[0])
|
||||
# asset = space.select(str(_id), index="txid_search")
|
||||
# print(f"DATA : {asset}")
|
||||
# if len(asset) == 0:
|
||||
# continue
|
||||
# asset = asset.data[0]
|
||||
asset = get_asset(connection, _id)
|
||||
_returned_data.append(asset)
|
||||
|
||||
return sorted(_returned_data, key=lambda k: k["id"], reverse=False)
|
||||
|
||||
|
||||
@ -139,7 +139,9 @@ class TransactionCompose:
|
||||
return self.db_results["transaction"][0]
|
||||
|
||||
def _get_asset(self):
|
||||
print( f" asset : {self.db_results}" )
|
||||
_asset = iter(self.db_results["asset"])
|
||||
#return _asset
|
||||
return next(iter(next(_asset, iter([]))), None)
|
||||
|
||||
def _get_metadata(self):
|
||||
|
||||
@ -1171,7 +1171,7 @@ class Transaction(object):
|
||||
"""
|
||||
# NOTE: Remove reference to avoid side effects
|
||||
# tx_body = deepcopy(tx_body)
|
||||
print("VERIF " + str(tx_body))
|
||||
print("\n\nVERIF " + str(tx_body))
|
||||
tx_body = rapidjson.loads(rapidjson.dumps(tx_body))
|
||||
|
||||
try:
|
||||
@ -1180,15 +1180,17 @@ class Transaction(object):
|
||||
raise InvalidHash('No transaction id found!')
|
||||
|
||||
tx_body['id'] = None
|
||||
|
||||
print(f"\n\n tx_body2: {tx_body}")
|
||||
#tx_body = Transaction._remove_signatures(tx_body)
|
||||
#print(f"\n\n tx_body3: {tx_body}")
|
||||
tx_body_serialized = Transaction._to_str(tx_body)
|
||||
valid_tx_id = Transaction._to_hash(tx_body_serialized)
|
||||
print( f" valid TX : {valid_tx_id}")
|
||||
print( f" proposed TX id : {proposed_tx_id}")
|
||||
print( f" tx body : {tx_body}")
|
||||
print( f" tx serialized : {tx_body_serialized}")
|
||||
print( f"\n valid TX : {valid_tx_id}")
|
||||
print( f"\n proposed TX id : {proposed_tx_id}")
|
||||
print( f"\n tx body : {tx_body}")
|
||||
print( f"\n tx serialized : {tx_body_serialized}")
|
||||
if proposed_tx_id != valid_tx_id:
|
||||
err_msg = ("The transaction's id '{}' isn't equal to "
|
||||
err_msg= ("The transaction's id '{}' isn't equal to "
|
||||
"the hash of its body, i.e. it's not valid.")
|
||||
raise InvalidHash(err_msg.format(proposed_tx_id))
|
||||
|
||||
@ -1205,10 +1207,23 @@ class Transaction(object):
|
||||
"""
|
||||
operation = tx.get('operation', Transaction.CREATE) if isinstance(tx, dict) else Transaction.CREATE
|
||||
cls = Transaction.resolve_class(operation)
|
||||
|
||||
|
||||
local_dict= {
|
||||
'inputs': tx['inputs'],
|
||||
'outputs': tx['outputs'],
|
||||
'operation': operation,
|
||||
'metadata': tx['metadata'],
|
||||
'asset': tx['asset'],
|
||||
'version': tx['version'],
|
||||
'id': tx['id']
|
||||
}
|
||||
|
||||
print( f" Schema validation {tx}")
|
||||
print( f" Schema validation {local_dict}")
|
||||
if not skip_schema_validation:
|
||||
cls.validate_id(tx)
|
||||
cls.validate_schema(tx)
|
||||
cls.validate_id(local_dict)
|
||||
cls.validate_schema(local_dict)
|
||||
|
||||
inputs = [Input.from_dict(input_) for input_ in tx['inputs']]
|
||||
outputs = [Output.from_dict(output) for output in tx['outputs']]
|
||||
|
||||
@ -134,19 +134,23 @@ class Planetmint(object):
|
||||
txn_metadatas = []
|
||||
for t in transactions:
|
||||
transaction = t.tx_dict if t.tx_dict else rapidjson.loads(rapidjson.dumps(t.to_dict()))
|
||||
if transaction['operation'] == t.CREATE:
|
||||
asset = transaction.pop('asset')
|
||||
asset['id'] = transaction['id']
|
||||
assets.append(asset)
|
||||
print(f"transaction {transaction}")
|
||||
|
||||
asset = transaction.pop('asset')
|
||||
asset_id = transaction['id']
|
||||
if transaction['operation'] != t.CREATE:
|
||||
asset_id = asset['id']
|
||||
assets.append( (asset,
|
||||
transaction['id'],
|
||||
asset_id))
|
||||
|
||||
metadata = transaction.pop('metadata')
|
||||
txn_metadatas.append({'id': transaction['id'],
|
||||
'metadata': metadata})
|
||||
txns.append(transaction)
|
||||
|
||||
backend.query.store_metadatas(self.connection, txn_metadatas)
|
||||
if assets:
|
||||
backend.query.store_assets(self.connection, assets)
|
||||
backend.query.store_assets(self.connection, assets)
|
||||
return backend.query.store_transactions(self.connection, txns)
|
||||
|
||||
def delete_transactions(self, txs):
|
||||
@ -243,20 +247,21 @@ class Planetmint(object):
|
||||
def get_transaction(self, transaction_id):
|
||||
transaction = backend.query.get_transaction(self.connection, transaction_id)
|
||||
|
||||
if transaction:
|
||||
asset = backend.query.get_asset(self.connection, transaction_id)
|
||||
metadata = backend.query.get_metadata(self.connection, [transaction_id])
|
||||
if asset:
|
||||
transaction['asset'] = asset
|
||||
|
||||
if 'metadata' not in transaction:
|
||||
metadata = metadata[0] if metadata else None
|
||||
if metadata:
|
||||
metadata = metadata.get('metadata')
|
||||
|
||||
transaction.update({'metadata': metadata})
|
||||
|
||||
transaction = Transaction.from_dict(transaction)
|
||||
#if transaction:
|
||||
# asset = backend.query.get_asset(self.connection, transaction_id)
|
||||
# metadata = backend.query.get_metadata(self.connection, [transaction_id])
|
||||
# if asset:
|
||||
# transaction['asset'] = asset
|
||||
#
|
||||
# if 'metadata' not in transaction:
|
||||
# metadata = metadata[0] if metadata else None
|
||||
# if metadata:
|
||||
# metadata = metadata.get('metadata')
|
||||
#
|
||||
# transaction.update({'metadata': metadata})
|
||||
#
|
||||
# transaction = Transaction.from_dict(transaction)
|
||||
transaction = Transaction.from_dict(transaction)
|
||||
|
||||
return transaction
|
||||
|
||||
|
||||
@ -75,15 +75,15 @@ def test_get_assets(db_conn):
|
||||
# conn = Connection().get_connection()
|
||||
conn = db_conn.get_connection()
|
||||
assets = [
|
||||
{'id': "1", 'data': '1'},
|
||||
{'id': "2", 'data': '2'},
|
||||
{'id': "3", 'data': '3'},
|
||||
("1", '1', '1'),
|
||||
("2", '2', '2'),
|
||||
("3", '3', '3'),
|
||||
]
|
||||
|
||||
query.store_assets(assets=assets, connection=conn)
|
||||
|
||||
for asset in assets:
|
||||
assert query.get_asset(asset_id=asset['id'], connection=conn)
|
||||
assert query.get_asset(asset_id=asset[2], connection=conn)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('table', ['assets', 'metadata'])
|
||||
|
||||
@ -186,7 +186,7 @@ def test_store_transaction(mocker, b, signed_create_tx,
|
||||
|
||||
mocked_store_asset.assert_called_once_with(
|
||||
b.connection,
|
||||
[{'id': signed_create_tx.id, 'data': signed_create_tx.asset['data']}],
|
||||
[{'data': signed_create_tx.asset['data'], 'tx_id': signed_create_tx.id, 'asset_id': signed_create_tx.id}]
|
||||
)
|
||||
mocked_store_metadata.assert_called_once_with(
|
||||
b.connection,
|
||||
@ -235,7 +235,7 @@ def test_store_bulk_transaction(mocker, b, signed_create_tx,
|
||||
# assert utxo['output_index'] == 0
|
||||
mocked_store_assets.assert_called_once_with(
|
||||
b.connection,
|
||||
[{'id': signed_create_tx.id, 'data': signed_create_tx.asset['data']}],
|
||||
[ ( signed_create_tx.asset['data'], signed_create_tx.id, signed_create_tx.id )],
|
||||
)
|
||||
mocked_store_metadata.assert_called_once_with(
|
||||
b.connection,
|
||||
|
||||
@ -15,8 +15,9 @@ def txlist(b, user_pk, user2_pk, user_sk, user2_sk):
|
||||
from planetmint.models import Transaction
|
||||
|
||||
# Create two CREATE transactions
|
||||
create1 = Transaction.create([user_pk], [([user2_pk], 6)]) \
|
||||
.sign([user_sk])
|
||||
create1 = Transaction.create([user_pk], [([user2_pk], 6)])
|
||||
print(f"tx1: {create1}")
|
||||
create1 = create1.sign([user_sk])
|
||||
|
||||
create2 = Transaction.create([user2_pk],
|
||||
[([user2_pk], 5), ([user_pk], 5)]) \
|
||||
@ -27,6 +28,9 @@ def txlist(b, user_pk, user2_pk, user_sk, user2_sk):
|
||||
[([user_pk], 8)],
|
||||
create1.id).sign([user2_sk])
|
||||
|
||||
print(f"tx1: {create1}")
|
||||
print(f"tx2: {create2}")
|
||||
print(f"tx3: {transfer1}")
|
||||
b.store_bulk_transactions([create1, create2, transfer1])
|
||||
|
||||
return type('', (), {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user