mirror of
https://github.com/planetmint/planetmint.git
synced 2025-03-30 15:08:31 +00:00
renamed abci/core.py and class names, merged utils files
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
4632a52599
commit
c43a176377
@ -24,11 +24,7 @@ from tendermint.abci.types_pb2 import (
|
||||
|
||||
from planetmint.application.validator import Validator
|
||||
from planetmint.model.models import Models
|
||||
from planetmint.abci.tendermint_utils import (
|
||||
decode_transaction,
|
||||
calculate_hash,
|
||||
decode_validator,
|
||||
)
|
||||
from planetmint.abci.utils import decode_validator, decode_transaction, calculate_hash
|
||||
from planetmint.abci.block import Block
|
||||
from planetmint.ipc.events import EventTypes, Event
|
||||
|
||||
@ -36,7 +32,7 @@ CodeTypeError = 1
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class App(BaseApplication):
|
||||
class ApplicationLogic(BaseApplication):
|
||||
"""Bridge between Planetmint and Tendermint.
|
||||
|
||||
The role of this class is to expose the Planetmint
|
@ -6,9 +6,9 @@
|
||||
import multiprocessing
|
||||
|
||||
from collections import defaultdict
|
||||
from planetmint.abci.core import App
|
||||
from planetmint.abci.application_logic import ApplicationLogic
|
||||
from planetmint.application.validator import Validator
|
||||
from planetmint.abci.tendermint_utils import decode_transaction
|
||||
from planetmint.abci.utils import decode_transaction
|
||||
from abci.application import OkCode
|
||||
from tendermint.abci.types_pb2 import (
|
||||
ResponseCheckTx,
|
||||
@ -16,7 +16,7 @@ from tendermint.abci.types_pb2 import (
|
||||
)
|
||||
|
||||
|
||||
class ParallelValidationApp(App):
|
||||
class ParallelValidationApp(ApplicationLogic):
|
||||
def __init__(self, planetmint=None, events_queue=None):
|
||||
super().__init__(planetmint, events_queue)
|
||||
self.parallel_validator = ParallelValidator()
|
||||
|
@ -8,7 +8,7 @@ from transactions.common.transaction_mode_types import (
|
||||
)
|
||||
|
||||
from planetmint.utils import Singleton
|
||||
from planetmint.abci.tendermint_utils import encode_transaction
|
||||
from planetmint.abci.utils import encode_transaction
|
||||
from planetmint.application.validator import logger
|
||||
from planetmint.config_utils import autoconfigure
|
||||
from planetmint.config import Config
|
||||
|
@ -3,149 +3,7 @@
|
||||
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import codecs
|
||||
|
||||
from binascii import hexlify
|
||||
from tendermint.abci import types_pb2
|
||||
from tendermint.crypto import keys_pb2
|
||||
from hashlib import sha3_256
|
||||
from transactions.common.exceptions import InvalidPublicKey
|
||||
|
||||
|
||||
def encode_validator(v):
|
||||
ed25519_public_key = v["public_key"]["value"]
|
||||
pub_key = keys_pb2.PublicKey(ed25519=bytes.fromhex(ed25519_public_key))
|
||||
|
||||
return types_pb2.ValidatorUpdate(pub_key=pub_key, power=v["power"])
|
||||
|
||||
|
||||
def decode_validator(v):
|
||||
return {
|
||||
"public_key": {
|
||||
"type": "ed25519-base64",
|
||||
"value": codecs.encode(v.pub_key.ed25519, "base64").decode().rstrip("\n"),
|
||||
},
|
||||
"voting_power": v.power,
|
||||
}
|
||||
|
||||
|
||||
def new_validator_set(validators, updates):
|
||||
validators_dict = {}
|
||||
for v in validators:
|
||||
validators_dict[v["public_key"]["value"]] = v
|
||||
|
||||
updates_dict = {}
|
||||
for u in updates:
|
||||
decoder = get_public_key_decoder(u["public_key"])
|
||||
public_key64 = base64.b64encode(decoder(u["public_key"]["value"])).decode("utf-8")
|
||||
updates_dict[public_key64] = {
|
||||
"public_key": {"type": "ed25519-base64", "value": public_key64},
|
||||
"voting_power": u["power"],
|
||||
}
|
||||
|
||||
new_validators_dict = {**validators_dict, **updates_dict}
|
||||
return list(new_validators_dict.values())
|
||||
|
||||
|
||||
def get_public_key_decoder(pk):
|
||||
encoding = pk["type"]
|
||||
decoder = base64.b64decode
|
||||
|
||||
if encoding == "ed25519-base16":
|
||||
decoder = base64.b16decode
|
||||
elif encoding == "ed25519-base32":
|
||||
decoder = base64.b32decode
|
||||
elif encoding == "ed25519-base64":
|
||||
decoder = base64.b64decode
|
||||
else:
|
||||
raise InvalidPublicKey("Invalid `type` specified for public key `value`")
|
||||
|
||||
return decoder
|
||||
|
||||
|
||||
def encode_transaction(value):
|
||||
"""Encode a transaction (dict) to Base64."""
|
||||
|
||||
return base64.b64encode(json.dumps(value).encode("utf8")).decode("utf8")
|
||||
|
||||
|
||||
def decode_transaction(raw):
|
||||
"""Decode a transaction from bytes to a dict."""
|
||||
|
||||
return json.loads(raw.decode("utf8"))
|
||||
|
||||
|
||||
def decode_transaction_base64(value):
|
||||
"""Decode a transaction from Base64."""
|
||||
|
||||
return json.loads(base64.b64decode(value.encode("utf8")).decode("utf8"))
|
||||
|
||||
|
||||
def calculate_hash(key_list):
|
||||
if not key_list:
|
||||
return ""
|
||||
|
||||
full_hash = sha3_256()
|
||||
for key in key_list:
|
||||
full_hash.update(key.encode("utf8"))
|
||||
|
||||
return full_hash.hexdigest()
|
||||
|
||||
|
||||
def merkleroot(hashes):
|
||||
"""Computes the merkle root for a given list.
|
||||
|
||||
Args:
|
||||
hashes (:obj:`list` of :obj:`bytes`): The leaves of the tree.
|
||||
|
||||
Returns:
|
||||
str: Merkle root in hexadecimal form.
|
||||
|
||||
"""
|
||||
# XXX TEMPORARY -- MUST REVIEW and possibly CHANGE
|
||||
# The idea here is that the UTXO SET would be empty and this function
|
||||
# would be invoked to compute the merkle root, and since there is nothing,
|
||||
# i.e. an empty list, then the hash of the empty string is returned.
|
||||
# This seems too easy but maybe that is good enough? TO REVIEW!
|
||||
if not hashes:
|
||||
return sha3_256(b"").hexdigest()
|
||||
# XXX END TEMPORARY -- MUST REVIEW ...
|
||||
if len(hashes) == 1:
|
||||
return hexlify(hashes[0]).decode()
|
||||
if len(hashes) % 2 == 1:
|
||||
hashes.append(hashes[-1])
|
||||
parent_hashes = [sha3_256(hashes[i] + hashes[i + 1]).digest() for i in range(0, len(hashes) - 1, 2)]
|
||||
return merkleroot(parent_hashes)
|
||||
|
||||
|
||||
# ripemd160 is only available below python 3.9.13
|
||||
@DeprecationWarning
|
||||
def public_key64_to_address(base64_public_key):
|
||||
"""Note this only compatible with Tendermint 0.19.x"""
|
||||
ed25519_public_key = public_key_from_base64(base64_public_key)
|
||||
encoded_public_key = amino_encoded_public_key(ed25519_public_key)
|
||||
return hashlib.new("ripemd160", encoded_public_key).hexdigest().upper()
|
||||
|
||||
|
||||
def public_key_from_base64(base64_public_key):
|
||||
return key_from_base64(base64_public_key)
|
||||
|
||||
|
||||
def key_from_base64(base64_key):
|
||||
return base64.b64decode(base64_key).hex().upper()
|
||||
|
||||
|
||||
def public_key_to_base64(ed25519_public_key):
|
||||
return key_to_base64(ed25519_public_key)
|
||||
|
||||
|
||||
def key_to_base64(ed25519_key):
|
||||
ed25519_key = bytes.fromhex(ed25519_key)
|
||||
return base64.b64encode(ed25519_key).decode("utf-8")
|
||||
|
||||
|
||||
def amino_encoded_public_key(ed25519_public_key):
|
||||
return bytes.fromhex("1624DE6220{}".format(ed25519_public_key))
|
||||
|
@ -1,9 +1,16 @@
|
||||
import base64
|
||||
import codecs
|
||||
import hashlib
|
||||
import json
|
||||
from binascii import hexlify
|
||||
from hashlib import sha3_256
|
||||
|
||||
from packaging import version
|
||||
from tendermint.abci import types_pb2
|
||||
from tendermint.crypto import keys_pb2
|
||||
from transactions.common.crypto import key_pair_from_ed25519_key
|
||||
from transactions.common.exceptions import InvalidPublicKey
|
||||
|
||||
from planetmint.abci.tendermint_utils import key_from_base64
|
||||
from planetmint.version import __tm_supported_versions__
|
||||
|
||||
|
||||
@ -33,3 +40,138 @@ def tendermint_version_is_compatible(running_tm_ver):
|
||||
if version.parse(ver) == version.parse(tm_ver[0]):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def encode_validator(v):
|
||||
ed25519_public_key = v["public_key"]["value"]
|
||||
pub_key = keys_pb2.PublicKey(ed25519=bytes.fromhex(ed25519_public_key))
|
||||
|
||||
return types_pb2.ValidatorUpdate(pub_key=pub_key, power=v["power"])
|
||||
|
||||
|
||||
def decode_validator(v):
|
||||
return {
|
||||
"public_key": {
|
||||
"type": "ed25519-base64",
|
||||
"value": codecs.encode(v.pub_key.ed25519, "base64").decode().rstrip("\n"),
|
||||
},
|
||||
"voting_power": v.power,
|
||||
}
|
||||
|
||||
|
||||
def new_validator_set(validators, updates):
|
||||
validators_dict = {}
|
||||
for v in validators:
|
||||
validators_dict[v["public_key"]["value"]] = v
|
||||
|
||||
updates_dict = {}
|
||||
for u in updates:
|
||||
decoder = get_public_key_decoder(u["public_key"])
|
||||
public_key64 = base64.b64encode(decoder(u["public_key"]["value"])).decode("utf-8")
|
||||
updates_dict[public_key64] = {
|
||||
"public_key": {"type": "ed25519-base64", "value": public_key64},
|
||||
"voting_power": u["power"],
|
||||
}
|
||||
|
||||
new_validators_dict = {**validators_dict, **updates_dict}
|
||||
return list(new_validators_dict.values())
|
||||
|
||||
|
||||
def get_public_key_decoder(pk):
|
||||
encoding = pk["type"]
|
||||
decoder = base64.b64decode
|
||||
|
||||
if encoding == "ed25519-base16":
|
||||
decoder = base64.b16decode
|
||||
elif encoding == "ed25519-base32":
|
||||
decoder = base64.b32decode
|
||||
elif encoding == "ed25519-base64":
|
||||
decoder = base64.b64decode
|
||||
else:
|
||||
raise InvalidPublicKey("Invalid `type` specified for public key `value`")
|
||||
|
||||
return decoder
|
||||
|
||||
|
||||
def encode_transaction(value):
|
||||
"""Encode a transaction (dict) to Base64."""
|
||||
|
||||
return base64.b64encode(json.dumps(value).encode("utf8")).decode("utf8")
|
||||
|
||||
|
||||
def decode_transaction(raw):
|
||||
"""Decode a transaction from bytes to a dict."""
|
||||
|
||||
return json.loads(raw.decode("utf8"))
|
||||
|
||||
|
||||
def decode_transaction_base64(value):
|
||||
"""Decode a transaction from Base64."""
|
||||
|
||||
return json.loads(base64.b64decode(value.encode("utf8")).decode("utf8"))
|
||||
|
||||
|
||||
def calculate_hash(key_list):
|
||||
if not key_list:
|
||||
return ""
|
||||
|
||||
full_hash = sha3_256()
|
||||
for key in key_list:
|
||||
full_hash.update(key.encode("utf8"))
|
||||
|
||||
return full_hash.hexdigest()
|
||||
|
||||
|
||||
def merkleroot(hashes):
|
||||
"""Computes the merkle root for a given list.
|
||||
|
||||
Args:
|
||||
hashes (:obj:`list` of :obj:`bytes`): The leaves of the tree.
|
||||
|
||||
Returns:
|
||||
str: Merkle root in hexadecimal form.
|
||||
|
||||
"""
|
||||
# XXX TEMPORARY -- MUST REVIEW and possibly CHANGE
|
||||
# The idea here is that the UTXO SET would be empty and this function
|
||||
# would be invoked to compute the merkle root, and since there is nothing,
|
||||
# i.e. an empty list, then the hash of the empty string is returned.
|
||||
# This seems too easy but maybe that is good enough? TO REVIEW!
|
||||
if not hashes:
|
||||
return sha3_256(b"").hexdigest()
|
||||
# XXX END TEMPORARY -- MUST REVIEW ...
|
||||
if len(hashes) == 1:
|
||||
return hexlify(hashes[0]).decode()
|
||||
if len(hashes) % 2 == 1:
|
||||
hashes.append(hashes[-1])
|
||||
parent_hashes = [sha3_256(hashes[i] + hashes[i + 1]).digest() for i in range(0, len(hashes) - 1, 2)]
|
||||
return merkleroot(parent_hashes)
|
||||
|
||||
|
||||
@DeprecationWarning
|
||||
def public_key64_to_address(base64_public_key):
|
||||
"""Note this only compatible with Tendermint 0.19.x"""
|
||||
ed25519_public_key = public_key_from_base64(base64_public_key)
|
||||
encoded_public_key = amino_encoded_public_key(ed25519_public_key)
|
||||
return hashlib.new("ripemd160", encoded_public_key).hexdigest().upper()
|
||||
|
||||
|
||||
def public_key_from_base64(base64_public_key):
|
||||
return key_from_base64(base64_public_key)
|
||||
|
||||
|
||||
def key_from_base64(base64_key):
|
||||
return base64.b64decode(base64_key).hex().upper()
|
||||
|
||||
|
||||
def public_key_to_base64(ed25519_public_key):
|
||||
return key_to_base64(ed25519_public_key)
|
||||
|
||||
|
||||
def key_to_base64(ed25519_key):
|
||||
ed25519_key = bytes.fromhex(ed25519_key)
|
||||
return base64.b64encode(ed25519_key).decode("utf-8")
|
||||
|
||||
|
||||
def amino_encoded_public_key(ed25519_public_key):
|
||||
return bytes.fromhex("1624DE6220{}".format(ed25519_public_key))
|
||||
|
@ -22,12 +22,7 @@ from transactions.common.transaction import VALIDATOR_ELECTION, CHAIN_MIGRATION_
|
||||
from transactions.types.elections.election import Election
|
||||
from transactions.types.elections.validator_utils import election_id_to_public_key
|
||||
|
||||
from planetmint.abci.tendermint_utils import (
|
||||
key_from_base64,
|
||||
public_key_to_base64,
|
||||
encode_validator,
|
||||
new_validator_set,
|
||||
)
|
||||
from planetmint.abci.utils import encode_validator, new_validator_set, key_from_base64, public_key_to_base64
|
||||
from planetmint.application.basevalidationrules import BaseValidationRules
|
||||
from planetmint.backend.models.output import Output
|
||||
from planetmint.model.models import Models
|
||||
|
@ -25,13 +25,12 @@ from transactions.common.transaction import Transaction
|
||||
|
||||
|
||||
from planetmint.abci.rpc import ABCI_RPC
|
||||
from planetmint.abci.utils import load_node_key
|
||||
from planetmint.abci.utils import load_node_key, public_key_from_base64
|
||||
from planetmint.application.validator import Validator
|
||||
from planetmint.backend import schema
|
||||
from planetmint.commands import utils
|
||||
from planetmint.commands.utils import configure_planetmint, input_on_stderr
|
||||
from planetmint.config_utils import setup_logging
|
||||
from planetmint.abci.tendermint_utils import public_key_from_base64
|
||||
from planetmint.abci.rpc import MODE_COMMIT, MODE_LIST
|
||||
from planetmint.commands.election_types import elections
|
||||
from planetmint.version import __tm_supported_versions__
|
||||
|
@ -9,7 +9,7 @@ from transactions.common.exceptions import InputDoesNotExist
|
||||
from planetmint import config_utils, backend
|
||||
from planetmint.const import GOVERNANCE_TRANSACTION_TYPES
|
||||
from planetmint.model.fastquery import FastQuery
|
||||
from planetmint.abci.tendermint_utils import key_from_base64
|
||||
from planetmint.abci.utils import key_from_base64
|
||||
from planetmint.backend.connection import Connection
|
||||
from planetmint.backend.tarantool.const import TARANT_TABLE_TRANSACTION, TARANT_TABLE_GOVERNANCE
|
||||
from planetmint.backend.models.block import Block
|
||||
|
@ -8,7 +8,7 @@ import setproctitle
|
||||
|
||||
from planetmint.config import Config
|
||||
from planetmint.application.validator import Validator
|
||||
from planetmint.abci.core import App
|
||||
from planetmint.abci.application_logic import ApplicationLogic
|
||||
from planetmint.abci.parallel_validation import ParallelValidationApp
|
||||
from planetmint.web import server, websocket_server
|
||||
from planetmint.ipc.events import EventTypes
|
||||
@ -76,7 +76,7 @@ def start(args):
|
||||
)
|
||||
else:
|
||||
app = ABCIServer(
|
||||
app=App(
|
||||
app=ApplicationLogic(
|
||||
events_queue=exchange.get_publisher_queue(),
|
||||
)
|
||||
)
|
||||
|
@ -25,7 +25,7 @@ from planetmint.backend.connection import Connection
|
||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||
from transactions.common import crypto
|
||||
from transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT
|
||||
from planetmint.abci.tendermint_utils import key_from_base64
|
||||
from planetmint.abci.utils import key_from_base64
|
||||
from planetmint.backend import schema, query
|
||||
from transactions.common.crypto import key_pair_from_ed25519_key, public_key_from_ed25519_key
|
||||
from planetmint.abci.block import Block
|
||||
@ -453,10 +453,10 @@ def abci_server():
|
||||
from abci.server import ABCIServer
|
||||
|
||||
# from tendermint.abci import types_pb2 as types_v0_34_11
|
||||
from planetmint.abci.core import App
|
||||
from planetmint.abci.application_logic import ApplicationLogic
|
||||
from planetmint.utils import Process
|
||||
|
||||
app = ABCIServer(app=App())
|
||||
app = ABCIServer(app=ApplicationLogic())
|
||||
abci_proxy = Process(name="ABCI", target=app.run)
|
||||
yield abci_proxy.start()
|
||||
abci_proxy.terminate()
|
||||
|
@ -15,12 +15,11 @@ from transactions import ValidatorElection, ChainMigrationElection
|
||||
from transactions.common.crypto import generate_key_pair
|
||||
from transactions.types.assets.create import Create
|
||||
from transactions.types.assets.transfer import Transfer
|
||||
from planetmint.abci.core import App
|
||||
from planetmint.abci.application_logic import ApplicationLogic
|
||||
from planetmint.backend import query
|
||||
from planetmint.abci.core import OkCode, CodeTypeError
|
||||
from planetmint.abci.application_logic import OkCode, CodeTypeError
|
||||
from planetmint.abci.block import Block
|
||||
from planetmint.abci.tendermint_utils import new_validator_set
|
||||
from planetmint.abci.tendermint_utils import public_key_to_base64
|
||||
from planetmint.abci.utils import new_validator_set, public_key_to_base64
|
||||
from planetmint.version import __tm_supported_versions__
|
||||
from tests.utils import generate_election, generate_validators
|
||||
|
||||
@ -49,7 +48,7 @@ def generate_init_chain_request(chain_id, vals=None):
|
||||
|
||||
def test_init_chain_successfully_registers_chain(b):
|
||||
request = generate_init_chain_request("chain-XYZ")
|
||||
res = App(b).init_chain(request)
|
||||
res = ApplicationLogic(b).init_chain(request)
|
||||
assert res == types.ResponseInitChain()
|
||||
chain = query.get_latest_abci_chain(b.models.connection)
|
||||
assert chain == {"height": 0, "chain_id": "chain-XYZ", "is_synced": True}
|
||||
@ -63,7 +62,7 @@ def test_init_chain_successfully_registers_chain(b):
|
||||
def test_init_chain_ignores_invalid_init_chain_requests(b):
|
||||
validators = [generate_validator()]
|
||||
request = generate_init_chain_request("chain-XYZ", validators)
|
||||
res = App(b).init_chain(request)
|
||||
res = ApplicationLogic(b).init_chain(request)
|
||||
assert res == types.ResponseInitChain()
|
||||
|
||||
validator_set = query.get_validator_set(b.models.connection)
|
||||
@ -77,7 +76,7 @@ def test_init_chain_ignores_invalid_init_chain_requests(b):
|
||||
]
|
||||
for r in invalid_requests:
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).init_chain(r)
|
||||
ApplicationLogic(b).init_chain(r)
|
||||
# assert nothing changed - neither validator set, nor chain ID
|
||||
new_validator_set = query.get_validator_set(b.models.connection)
|
||||
assert new_validator_set == validator_set
|
||||
@ -93,7 +92,7 @@ def test_init_chain_ignores_invalid_init_chain_requests(b):
|
||||
def test_init_chain_recognizes_new_chain_after_migration(b):
|
||||
validators = [generate_validator()]
|
||||
request = generate_init_chain_request("chain-XYZ", validators)
|
||||
res = App(b).init_chain(request)
|
||||
res = ApplicationLogic(b).init_chain(request)
|
||||
assert res == types.ResponseInitChain()
|
||||
|
||||
validator_set = query.get_validator_set(b.models.connection)["validators"]
|
||||
@ -111,7 +110,7 @@ def test_init_chain_recognizes_new_chain_after_migration(b):
|
||||
]
|
||||
for r in invalid_requests:
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).init_chain(r)
|
||||
ApplicationLogic(b).init_chain(r)
|
||||
assert query.get_latest_abci_chain(b.models.connection) == {
|
||||
"chain_id": "chain-XYZ-migrated-at-height-1",
|
||||
"is_synced": False,
|
||||
@ -123,7 +122,7 @@ def test_init_chain_recognizes_new_chain_after_migration(b):
|
||||
# a request with the matching chain ID and matching validator set
|
||||
# completes the migration
|
||||
request = generate_init_chain_request("chain-XYZ-migrated-at-height-1", validators)
|
||||
res = App(b).init_chain(request)
|
||||
res = ApplicationLogic(b).init_chain(request)
|
||||
assert res == types.ResponseInitChain()
|
||||
assert query.get_latest_abci_chain(b.models.connection) == {
|
||||
"chain_id": "chain-XYZ-migrated-at-height-1",
|
||||
@ -144,7 +143,7 @@ def test_init_chain_recognizes_new_chain_after_migration(b):
|
||||
]
|
||||
for r in invalid_requests:
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).init_chain(r)
|
||||
ApplicationLogic(b).init_chain(r)
|
||||
assert query.get_latest_abci_chain(b.models.connection) == {
|
||||
"chain_id": "chain-XYZ-migrated-at-height-1",
|
||||
"is_synced": True,
|
||||
@ -161,7 +160,7 @@ def test_init_chain_recognizes_new_chain_after_migration(b):
|
||||
|
||||
def test_info(b):
|
||||
r = types.RequestInfo(version=__tm_supported_versions__[0])
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
|
||||
res = app.info(r)
|
||||
assert res.last_block_height == 0
|
||||
@ -174,7 +173,7 @@ def test_info(b):
|
||||
|
||||
# simulate a migration and assert the height is shifted
|
||||
b.models.store_abci_chain(2, "chain-XYZ")
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
b.models.store_block(Block(app_hash="2", height=2, transactions=[])._asdict())
|
||||
res = app.info(r)
|
||||
assert res.last_block_height == 0
|
||||
@ -187,7 +186,7 @@ def test_info(b):
|
||||
|
||||
# it's always the latest migration that is taken into account
|
||||
b.models.store_abci_chain(4, "chain-XYZ-new")
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
b.models.store_block(Block(app_hash="4", height=4, transactions=[])._asdict())
|
||||
res = app.info(r)
|
||||
assert res.last_block_height == 0
|
||||
@ -200,7 +199,7 @@ def test_check_tx__signed_create_is_ok(b):
|
||||
|
||||
tx = Create.generate([alice.public_key], [([bob.public_key], 1)]).sign([alice.private_key])
|
||||
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
result = app.check_tx(encode_tx_to_bytes(tx))
|
||||
assert result.code == OkCode
|
||||
|
||||
@ -211,7 +210,7 @@ def test_check_tx__unsigned_create_is_error(b):
|
||||
|
||||
tx = Create.generate([alice.public_key], [([bob.public_key], 1)])
|
||||
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
result = app.check_tx(encode_tx_to_bytes(tx))
|
||||
assert result.code == CodeTypeError
|
||||
|
||||
@ -223,7 +222,7 @@ def test_deliver_tx__valid_create_updates_db_and_emits_event(b, init_chain_reque
|
||||
|
||||
tx = Create.generate([alice.public_key], [([bob.public_key], 1)]).sign([alice.private_key])
|
||||
|
||||
app = App(b, events)
|
||||
app = ApplicationLogic(b, events)
|
||||
|
||||
app.init_chain(init_chain_request)
|
||||
|
||||
@ -253,7 +252,7 @@ def test_deliver_tx__double_spend_fails(b, init_chain_request):
|
||||
|
||||
tx = Create.generate([alice.public_key], [([bob.public_key], 1)]).sign([alice.private_key])
|
||||
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
app.init_chain(init_chain_request)
|
||||
|
||||
begin_block = types.RequestBeginBlock()
|
||||
@ -270,7 +269,7 @@ def test_deliver_tx__double_spend_fails(b, init_chain_request):
|
||||
|
||||
|
||||
def test_deliver_transfer_tx__double_spend_fails(b, init_chain_request):
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
app.init_chain(init_chain_request)
|
||||
|
||||
begin_block = types.RequestBeginBlock()
|
||||
@ -303,7 +302,7 @@ def test_deliver_transfer_tx__double_spend_fails(b, init_chain_request):
|
||||
|
||||
|
||||
def test_end_block_return_validator_updates(b, init_chain_request):
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
app.init_chain(init_chain_request)
|
||||
|
||||
begin_block = types.RequestBeginBlock()
|
||||
@ -335,7 +334,7 @@ def test_end_block_return_validator_updates(b, init_chain_request):
|
||||
|
||||
|
||||
def test_store_pre_commit_state_in_end_block(b, alice, init_chain_request):
|
||||
from planetmint.abci.core import App
|
||||
from planetmint.abci.application_logic import ApplicationLogic
|
||||
from planetmint.backend import query
|
||||
|
||||
tx = Create.generate(
|
||||
@ -344,7 +343,7 @@ def test_store_pre_commit_state_in_end_block(b, alice, init_chain_request):
|
||||
assets=[{"data": "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"}],
|
||||
).sign([alice.private_key])
|
||||
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
app.init_chain(init_chain_request)
|
||||
|
||||
begin_block = types.RequestBeginBlock()
|
||||
@ -365,7 +364,7 @@ def test_store_pre_commit_state_in_end_block(b, alice, init_chain_request):
|
||||
|
||||
# simulate a chain migration and assert the height is shifted
|
||||
b.models.store_abci_chain(100, "new-chain")
|
||||
app = App(b)
|
||||
app = ApplicationLogic(b)
|
||||
app.begin_block(begin_block)
|
||||
app.deliver_tx(encode_tx_to_bytes(tx))
|
||||
app.end_block(types.RequestEndBlock(height=1))
|
||||
@ -470,39 +469,39 @@ def test_info_aborts_if_chain_is_not_synced(b):
|
||||
b.models.store_abci_chain(0, "chain-XYZ", False)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).info(types.RequestInfo())
|
||||
ApplicationLogic(b).info(types.RequestInfo())
|
||||
|
||||
|
||||
def test_check_tx_aborts_if_chain_is_not_synced(b):
|
||||
b.models.store_abci_chain(0, "chain-XYZ", False)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).check_tx("some bytes")
|
||||
ApplicationLogic(b).check_tx("some bytes")
|
||||
|
||||
|
||||
def test_begin_aborts_if_chain_is_not_synced(b):
|
||||
b.models.store_abci_chain(0, "chain-XYZ", False)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).info(types.RequestBeginBlock())
|
||||
ApplicationLogic(b).info(types.RequestBeginBlock())
|
||||
|
||||
|
||||
def test_deliver_tx_aborts_if_chain_is_not_synced(b):
|
||||
b.models.store_abci_chain(0, "chain-XYZ", False)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).deliver_tx("some bytes")
|
||||
ApplicationLogic(b).deliver_tx("some bytes")
|
||||
|
||||
|
||||
def test_end_block_aborts_if_chain_is_not_synced(b):
|
||||
b.models.store_abci_chain(0, "chain-XYZ", False)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).info(types.RequestEndBlock())
|
||||
ApplicationLogic(b).info(types.RequestEndBlock())
|
||||
|
||||
|
||||
def test_commit_aborts_if_chain_is_not_synced(b):
|
||||
b.models.store_abci_chain(0, "chain-XYZ", False)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
App(b).commit()
|
||||
ApplicationLogic(b).commit()
|
||||
|
@ -21,11 +21,11 @@ from io import BytesIO
|
||||
|
||||
@pytest.mark.bdb
|
||||
def test_app(b, eventqueue_fixture, init_chain_request):
|
||||
from planetmint.abci.core import App
|
||||
from planetmint.abci.tendermint_utils import calculate_hash
|
||||
from planetmint.abci.application_logic import ApplicationLogic
|
||||
from planetmint.abci.utils import calculate_hash
|
||||
from transactions.common.crypto import generate_key_pair
|
||||
|
||||
app = App(b, eventqueue_fixture)
|
||||
app = ApplicationLogic(b, eventqueue_fixture)
|
||||
p = ProtocolHandler(app)
|
||||
|
||||
data = p.process("info", types.Request(info=types.RequestInfo(version=__tm_supported_versions__[0])))
|
||||
|
@ -102,7 +102,7 @@ def test_validation_error(b):
|
||||
@patch("requests.post")
|
||||
def test_write_and_post_transaction(mock_post, b, test_abci_rpc):
|
||||
from transactions.common.crypto import generate_key_pair
|
||||
from planetmint.abci.tendermint_utils import encode_transaction
|
||||
from planetmint.abci.utils import encode_transaction
|
||||
|
||||
alice = generate_key_pair()
|
||||
tx = (
|
||||
|
@ -11,7 +11,8 @@ from hashlib import sha3_256
|
||||
|
||||
|
||||
def test_encode_decode_transaction(b):
|
||||
from planetmint.abci.tendermint_utils import encode_transaction, decode_transaction
|
||||
from planetmint.abci.utils import decode_transaction
|
||||
from planetmint.abci.utils import encode_transaction
|
||||
|
||||
asset = {"value": "key"}
|
||||
|
||||
@ -25,7 +26,7 @@ def test_encode_decode_transaction(b):
|
||||
|
||||
|
||||
def test_calculate_hash_no_key(b):
|
||||
from planetmint.abci.tendermint_utils import calculate_hash
|
||||
from planetmint.abci.utils import calculate_hash
|
||||
|
||||
# pass an empty list
|
||||
assert calculate_hash([]) == ""
|
||||
@ -33,7 +34,7 @@ def test_calculate_hash_no_key(b):
|
||||
|
||||
# TODO test for the case of an empty list of hashes, and possibly other cases.
|
||||
def test_merkleroot():
|
||||
from planetmint.abci.tendermint_utils import merkleroot
|
||||
from planetmint.abci.utils import merkleroot
|
||||
|
||||
hashes = [sha3_256(i.encode()).digest() for i in "abc"]
|
||||
assert merkleroot(hashes) == ("78c7c394d3158c218916b7ae0ebdea502e0f4e85c08e3b371e3dfd824d389fa3")
|
||||
@ -49,14 +50,15 @@ SAMPLE_PUBLIC_KEY = {
|
||||
reason="ripemd160, the core of pulbic_key64_to_address is no longer supported by hashlib (from python 3.9.13 on)"
|
||||
)
|
||||
def test_convert_base64_public_key_to_address():
|
||||
from planetmint.abci.tendermint_utils import public_key64_to_address
|
||||
from planetmint.abci.utils import public_key64_to_address
|
||||
|
||||
address = public_key64_to_address(SAMPLE_PUBLIC_KEY["pub_key"]["value"])
|
||||
assert address == SAMPLE_PUBLIC_KEY["address"]
|
||||
|
||||
|
||||
def test_public_key_encoding_decoding():
|
||||
from planetmint.abci.tendermint_utils import public_key_from_base64, public_key_to_base64
|
||||
from planetmint.abci.utils import public_key_to_base64
|
||||
from planetmint.abci.utils import public_key_from_base64
|
||||
|
||||
public_key = public_key_from_base64(SAMPLE_PUBLIC_KEY["pub_key"]["value"])
|
||||
base64_public_key = public_key_to_base64(public_key)
|
||||
|
@ -7,7 +7,7 @@ import pytest
|
||||
import codecs
|
||||
|
||||
from planetmint.abci.rpc import MODE_LIST, MODE_COMMIT
|
||||
from planetmint.abci.tendermint_utils import public_key_to_base64
|
||||
from planetmint.abci.utils import public_key_to_base64
|
||||
|
||||
from transactions.types.elections.validator_election import ValidatorElection
|
||||
from transactions.common.exceptions import AmountError
|
||||
|
@ -7,7 +7,7 @@ import pytest
|
||||
|
||||
from argparse import Namespace
|
||||
from unittest.mock import patch
|
||||
from planetmint.abci.tendermint_utils import public_key_to_base64
|
||||
from planetmint.abci.utils import public_key_to_base64
|
||||
from transactions.types.elections.validator_election import ValidatorElection
|
||||
from transactions.common.exceptions import (
|
||||
DuplicateTransaction,
|
||||
|
@ -20,7 +20,7 @@ from transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT
|
||||
from transactions.types.assets.create import Create
|
||||
from transactions.types.elections.vote import Vote
|
||||
from transactions.types.elections.validator_utils import election_id_to_public_key
|
||||
from planetmint.abci.tendermint_utils import key_to_base64, merkleroot
|
||||
from planetmint.abci.utils import merkleroot, key_to_base64
|
||||
from planetmint.abci.rpc import MODE_COMMIT, MODE_LIST
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user