mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 14:35:45 +00:00
fixed last abci issues and blackified the code
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
0d8182f607
commit
0d375b6b60
@ -6,6 +6,7 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Asset:
|
||||
key: str = ""
|
||||
@ -16,7 +17,7 @@ class Asset:
|
||||
key = "data" if "data" in asset_dict.keys() else "id"
|
||||
data = asset_dict[key]
|
||||
return Asset(key, data)
|
||||
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {self.key: self.data}
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ from dataclasses import dataclass, field
|
||||
|
||||
from planetmint.backend.models import Asset, MetaData, Input, Script, Output
|
||||
|
||||
|
||||
@dataclass
|
||||
class DbTransaction:
|
||||
id: str = ""
|
||||
@ -42,10 +43,12 @@ class DbTransaction:
|
||||
inputs=Input.from_list_dict(transaction[5]),
|
||||
script=Script.from_dict(transaction[6]),
|
||||
)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def remove_generated_fields(tx_dict: dict):
|
||||
tx_dict["outputs"] = [DbTransaction.remove_generated_or_none_output_keys(output) for output in tx_dict["outputs"]]
|
||||
tx_dict["outputs"] = [
|
||||
DbTransaction.remove_generated_or_none_output_keys(output) for output in tx_dict["outputs"]
|
||||
]
|
||||
if "script" in tx_dict and tx_dict["script"] is None:
|
||||
tx_dict.pop("script")
|
||||
return tx_dict
|
||||
@ -57,7 +60,6 @@ class DbTransaction:
|
||||
output.pop("id")
|
||||
return output
|
||||
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
tx = {
|
||||
"inputs": Input.list_to_dict(self.inputs),
|
||||
@ -67,7 +69,7 @@ class DbTransaction:
|
||||
"assets": Asset.list_to_dict(self.assets),
|
||||
"version": self.version,
|
||||
"id": self.id,
|
||||
"script": self.script.to_dict() if self.script is not None else None,
|
||||
"script": self.script.to_dict() if self.script is not None else None,
|
||||
}
|
||||
tx = DbTransaction.remove_generated_fields(tx)
|
||||
return tx
|
||||
|
||||
@ -121,7 +121,7 @@ class Output:
|
||||
public_keys=output_dict["public_keys"],
|
||||
condition=Condition.from_dict(output_dict["condition"]),
|
||||
index=index,
|
||||
transaction_id=transaction_id
|
||||
transaction_id=transaction_id,
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
|
||||
@ -35,13 +35,20 @@ from transactions.common.exceptions import (
|
||||
InvalidPowerChange,
|
||||
)
|
||||
from transactions.common.transaction import VALIDATOR_ELECTION, CHAIN_MIGRATION_ELECTION
|
||||
from transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT, BROADCAST_TX_ASYNC, BROADCAST_TX_SYNC
|
||||
from transactions.common.transaction_mode_types import (
|
||||
BROADCAST_TX_COMMIT,
|
||||
BROADCAST_TX_ASYNC,
|
||||
BROADCAST_TX_SYNC,
|
||||
)
|
||||
from transactions.common.output import Output as TransactionOutput
|
||||
from transactions.types.elections.election import Election
|
||||
from transactions.types.elections.validator_utils import election_id_to_public_key
|
||||
|
||||
from planetmint.backend.models import Output, DbTransaction
|
||||
from planetmint.backend.tarantool.const import TARANT_TABLE_GOVERNANCE, TARANT_TABLE_TRANSACTION
|
||||
from planetmint.backend.tarantool.const import (
|
||||
TARANT_TABLE_GOVERNANCE,
|
||||
TARANT_TABLE_TRANSACTION,
|
||||
)
|
||||
from planetmint.config import Config
|
||||
from planetmint import backend, config_utils, fastquery
|
||||
from planetmint.tendermint_utils import (
|
||||
@ -103,7 +110,12 @@ class Planetmint(object):
|
||||
raise ValidationError("Mode must be one of the following {}.".format(", ".join(self.mode_list)))
|
||||
|
||||
tx_dict = transaction.tx_dict if transaction.tx_dict else transaction.to_dict()
|
||||
payload = {"method": mode, "jsonrpc": "2.0", "params": [encode_transaction(tx_dict)], "id": str(uuid4())}
|
||||
payload = {
|
||||
"method": mode,
|
||||
"jsonrpc": "2.0",
|
||||
"params": [encode_transaction(tx_dict)],
|
||||
"id": str(uuid4()),
|
||||
}
|
||||
# TODO: handle connection errors!
|
||||
return requests.post(self.endpoint, json=payload)
|
||||
|
||||
@ -328,7 +340,7 @@ class Planetmint(object):
|
||||
|
||||
if block:
|
||||
transactions = backend.query.get_transactions(self.connection, block["transactions"])
|
||||
result["transactions"] = [ Transaction.from_dict(t.to_dict()).to_dict() for t in transactions]
|
||||
result["transactions"] = [Transaction.from_dict(t.to_dict()).to_dict() for t in transactions]
|
||||
|
||||
return result
|
||||
|
||||
@ -391,7 +403,10 @@ class Planetmint(object):
|
||||
if ctxn.id == input_txid:
|
||||
ctxn_dict = ctxn.to_dict()
|
||||
input_tx = DbTransaction.from_dict(ctxn_dict)
|
||||
_output = [Output.from_dict(output, index, ctxn.id) for index, output in enumerate(ctxn_dict["outputs"])]
|
||||
_output = [
|
||||
Output.from_dict(output, index, ctxn.id)
|
||||
for index, output in enumerate(ctxn_dict["outputs"])
|
||||
]
|
||||
|
||||
if input_tx is None:
|
||||
raise InputDoesNotExist("input `{}` doesn't exist".format(input_txid))
|
||||
@ -417,13 +432,13 @@ class Planetmint(object):
|
||||
asset_id = tx.get_asset_id(input_txs)
|
||||
if asset_id != tx.assets[0]["id"]:
|
||||
raise AssetIdMismatch(("The asset id of the input does not" " match the asset id of the" " transaction"))
|
||||
|
||||
|
||||
# convert planetmint.Output objects to transactions.common.Output objects
|
||||
input_conditions_dict = Output.list_to_dict(input_conditions)
|
||||
input_conditions_converted = []
|
||||
for input_cond in input_conditions_dict:
|
||||
input_conditions_converted.append(TransactionOutput.from_dict( input_cond ))
|
||||
|
||||
input_conditions_converted.append(TransactionOutput.from_dict(input_cond))
|
||||
|
||||
if not tx.inputs_valid(input_conditions_converted):
|
||||
raise InvalidSignature("Transaction signature is invalid.")
|
||||
|
||||
@ -914,4 +929,3 @@ class Planetmint(object):
|
||||
|
||||
|
||||
Block = namedtuple("Block", ("app_hash", "height", "transactions"))
|
||||
|
||||
|
||||
@ -359,7 +359,7 @@ def test_election_new_chain_migration_with_tendermint(b, priv_validator_path, us
|
||||
|
||||
election_id = run_election_new_chain_migration(new_args, b)
|
||||
|
||||
assert b.get_transaction(election_id,TARANT_TABLE_GOVERNANCE)
|
||||
assert b.get_transaction(election_id, TARANT_TABLE_GOVERNANCE)
|
||||
|
||||
|
||||
@pytest.mark.bdb
|
||||
@ -445,7 +445,7 @@ def test_election_approve_with_tendermint(b, priv_validator_path, user_sk, valid
|
||||
args = Namespace(action="approve", election_id=election_id, sk=priv_validator_path, config={})
|
||||
approve = run_election_approve(args, b)
|
||||
|
||||
assert b.get_transaction(approve,TARANT_TABLE_GOVERNANCE)
|
||||
assert b.get_transaction(approve, TARANT_TABLE_GOVERNANCE)
|
||||
|
||||
|
||||
@pytest.mark.bdb
|
||||
|
||||
@ -55,8 +55,7 @@ class TestBigchainApi(object):
|
||||
from tarantool.error import DatabaseError
|
||||
|
||||
from planetmint.backend.exceptions import OperationError
|
||||
from planetmint.backend.tarantool.connection import \
|
||||
TarantoolDBConnection
|
||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||
|
||||
tx = Create.generate([alice.public_key], [([alice.public_key], 1)])
|
||||
tx = tx.sign([alice.private_key])
|
||||
@ -70,8 +69,7 @@ class TestBigchainApi(object):
|
||||
b.store_bulk_transactions([tx])
|
||||
|
||||
def test_text_search(self, b, alice):
|
||||
from planetmint.backend.tarantool.connection import \
|
||||
TarantoolDBConnection
|
||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||
|
||||
if isinstance(b.connection, TarantoolDBConnection):
|
||||
warnings.warn(" :::::: This function is used only with :::::: ")
|
||||
@ -166,8 +164,8 @@ class TestMultipleInputs(object):
|
||||
user2_sk, user2_pk = crypto.generate_key_pair()
|
||||
tx_link = b.fastquery.get_outputs_by_public_key(user_pk).pop()
|
||||
input_tx = b.get_transaction(tx_link.txid)
|
||||
tx_converted = Transaction.from_dict( input_tx.to_dict(), True)
|
||||
|
||||
tx_converted = Transaction.from_dict(input_tx.to_dict(), True)
|
||||
|
||||
tx = Transfer.generate(tx_converted.to_inputs(), [([user2_pk], 1)], asset_ids=[input_tx.id])
|
||||
tx = tx.sign([user_sk])
|
||||
|
||||
@ -182,7 +180,7 @@ class TestMultipleInputs(object):
|
||||
tx_link = b.fastquery.get_outputs_by_public_key(user_pk).pop()
|
||||
|
||||
input_tx = b.get_transaction(tx_link.txid)
|
||||
tx_converted = Transaction.from_dict( input_tx.to_dict(), True)
|
||||
tx_converted = Transaction.from_dict(input_tx.to_dict(), True)
|
||||
|
||||
tx = Transfer.generate(tx_converted.to_inputs(), [([user2_pk, user3_pk], 1)], asset_ids=[input_tx.id])
|
||||
tx = tx.sign([user_sk])
|
||||
@ -202,8 +200,7 @@ class TestMultipleInputs(object):
|
||||
|
||||
owned_input = b.fastquery.get_outputs_by_public_key(user_pk).pop()
|
||||
input_tx = b.get_transaction(owned_input.txid)
|
||||
input_tx_converted = Transaction.from_dict( input_tx.to_dict(), True)
|
||||
|
||||
input_tx_converted = Transaction.from_dict(input_tx.to_dict(), True)
|
||||
|
||||
transfer_tx = Transfer.generate(input_tx_converted.to_inputs(), [([user3_pk], 1)], asset_ids=[input_tx.id])
|
||||
transfer_tx = transfer_tx.sign([user_sk, user2_sk])
|
||||
@ -226,8 +223,7 @@ class TestMultipleInputs(object):
|
||||
# get input
|
||||
tx_link = b.fastquery.get_outputs_by_public_key(user_pk).pop()
|
||||
tx_input = b.get_transaction(tx_link.txid)
|
||||
input_tx_converted = Transaction.from_dict( tx_input.to_dict(), True)
|
||||
|
||||
input_tx_converted = Transaction.from_dict(tx_input.to_dict(), True)
|
||||
|
||||
tx = Transfer.generate(input_tx_converted.to_inputs(), [([user3_pk, user4_pk], 1)], asset_ids=[tx_input.id])
|
||||
tx = tx.sign([user_sk, user2_sk])
|
||||
|
||||
@ -112,7 +112,11 @@ def test_post_transaction_responses(tendermint_ws_url, b):
|
||||
|
||||
alice = generate_key_pair()
|
||||
bob = generate_key_pair()
|
||||
tx = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=None).sign([alice.private_key])
|
||||
tx = Create.generate(
|
||||
[alice.public_key],
|
||||
[([alice.public_key], 1)],
|
||||
assets=[{"data": "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"}],
|
||||
).sign([alice.private_key])
|
||||
|
||||
code, message = b.write_transaction(tx, BROADCAST_TX_COMMIT)
|
||||
assert code == 202
|
||||
|
||||
@ -7,6 +7,8 @@ import pytest
|
||||
import codecs
|
||||
|
||||
from planetmint.tendermint_utils import public_key_to_base64
|
||||
from planetmint.backend.tarantool.const import TARANT_TABLE_GOVERNANCE
|
||||
|
||||
from transactions.types.elections.validator_election import ValidatorElection
|
||||
from transactions.common.exceptions import AmountError
|
||||
from transactions.common.crypto import generate_key_pair
|
||||
@ -246,7 +248,7 @@ def test_upsert_validator(b, node_key, node_keys, ed25519_node_keys):
|
||||
)
|
||||
code, message = b.write_transaction(election, BROADCAST_TX_COMMIT)
|
||||
assert code == 202
|
||||
assert b.get_transaction(election.id)
|
||||
assert b.get_transaction(election.id, TARANT_TABLE_GOVERNANCE)
|
||||
|
||||
tx_vote = gen_vote(election, 0, ed25519_node_keys)
|
||||
assert b.validate_transaction(tx_vote)
|
||||
|
||||
@ -139,7 +139,6 @@ async def test_bridge_sync_async_queue(event_loop):
|
||||
@pytest.mark.asyncio
|
||||
async def test_websocket_block_event(aiohttp_client, event_loop):
|
||||
|
||||
|
||||
user_priv, user_pub = crypto.generate_key_pair()
|
||||
tx = Create.generate([user_pub], [([user_pub], 1)])
|
||||
tx = tx.sign([user_priv])
|
||||
@ -171,7 +170,6 @@ async def test_websocket_block_event(aiohttp_client, event_loop):
|
||||
@pytest.mark.asyncio
|
||||
async def test_websocket_transaction_event(aiohttp_client, event_loop):
|
||||
|
||||
|
||||
user_priv, user_pub = crypto.generate_key_pair()
|
||||
tx = Create.generate([user_pub], [([user_pub], 1)])
|
||||
tx = tx.sign([user_priv])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user