mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-06-28 00:27:45 +02:00
consolidate Asset model in common
This commit is contained in:
parent
dd382ee4e6
commit
63f5879cb2
|
@ -10,7 +10,7 @@ from cryptoconditions.exceptions import ParsingError
|
|||
from bigchaindb.common.crypto import SigningKey, hash_data
|
||||
from bigchaindb.common.exceptions import (KeypairMismatchException,
|
||||
InvalidHash, InvalidSignature,
|
||||
AmountError)
|
||||
AmountError, AssetIdMismatch)
|
||||
from bigchaindb.common.util import serialize, gen_timestamp
|
||||
|
||||
|
||||
|
@ -306,7 +306,7 @@ class Condition(object):
|
|||
owners_after, threshold = owners_after
|
||||
else:
|
||||
threshold = len(owners_after)
|
||||
|
||||
|
||||
if not isinstance(amount, int):
|
||||
raise TypeError('`amount` must be a int')
|
||||
if not isinstance(owners_after, list):
|
||||
|
@ -324,7 +324,7 @@ class Condition(object):
|
|||
initial_cond = ThresholdSha256Fulfillment(threshold=threshold)
|
||||
threshold_cond = reduce(cls._gen_condition, owners_after,
|
||||
initial_cond)
|
||||
return cls(threshold_cond, owners_afteri, amount=amount)
|
||||
return cls(threshold_cond, owners_after, amount=amount)
|
||||
|
||||
@classmethod
|
||||
def _gen_condition(cls, initial, current):
|
||||
|
@ -469,6 +469,34 @@ class Asset(object):
|
|||
"""Generates a unqiue uuid for an Asset"""
|
||||
return str(uuid4())
|
||||
|
||||
@staticmethod
|
||||
def get_asset_id(transactions):
|
||||
"""Get the asset id from a list of transaction ids.
|
||||
|
||||
This is useful when we want to check if the multiple inputs of a transaction
|
||||
are related to the same asset id.
|
||||
|
||||
Args:
|
||||
transactions (list): list of transaction usually inputs that should have a matching asset_id
|
||||
|
||||
Returns:
|
||||
str: uuid of the asset.
|
||||
|
||||
Raises:
|
||||
AssetIdMismatch: If the inputs are related to different assets.
|
||||
"""
|
||||
|
||||
if not isinstance(transactions, list):
|
||||
transactions = [transactions]
|
||||
|
||||
# create a set of asset_ids
|
||||
asset_ids = {tx.asset.data_id for tx in transactions}
|
||||
|
||||
# check that all the transasctions have the same asset_id
|
||||
if len(asset_ids) > 1:
|
||||
raise AssetIdMismatch("All inputs of a transaction need to have the same asset id.")
|
||||
return asset_ids.pop()
|
||||
|
||||
def _validate_asset(self, amount=None):
|
||||
"""Validates the asset"""
|
||||
if self.data is not None and not isinstance(self.data, dict):
|
||||
|
|
|
@ -8,36 +8,6 @@ from bigchaindb.common.transaction import Transaction, Asset
|
|||
from bigchaindb.common.util import gen_timestamp, serialize
|
||||
|
||||
|
||||
class Asset(Asset):
|
||||
@staticmethod
|
||||
def get_asset_id(transactions):
|
||||
"""Get the asset id from a list of transaction ids.
|
||||
|
||||
This is useful when we want to check if the multiple inputs of a transaction
|
||||
are related to the same asset id.
|
||||
|
||||
Args:
|
||||
transactions (list): list of transaction usually inputs that should have a matching asset_id
|
||||
|
||||
Returns:
|
||||
str: uuid of the asset.
|
||||
|
||||
Raises:
|
||||
AssetIdMismatch: If the inputs are related to different assets.
|
||||
"""
|
||||
|
||||
if not isinstance(transactions, list):
|
||||
transactions = [transactions]
|
||||
|
||||
# create a set of asset_ids
|
||||
asset_ids = {tx.asset.data_id for tx in transactions}
|
||||
|
||||
# check that all the transasctions have the same asset_id
|
||||
if len(asset_ids) > 1:
|
||||
raise AssetIdMismatch("All inputs of a transaction need to have the same asset id.")
|
||||
return asset_ids.pop()
|
||||
|
||||
|
||||
class Transaction(Transaction):
|
||||
def validate(self, bigchain):
|
||||
"""Validate a transaction.
|
||||
|
@ -190,7 +160,7 @@ class Block(object):
|
|||
|
||||
def is_signature_valid(self):
|
||||
block = self.to_dict()['block']
|
||||
# cc only accepts bytesting messages
|
||||
# cc only accepts bytesting messages
|
||||
block_serialized = serialize(block).encode()
|
||||
verifying_key = VerifyingKey(block['node_pubkey'])
|
||||
try:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import pytest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ..db.conftest import inputs
|
||||
from ..db.conftest import inputs # noqa
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('inputs')
|
||||
|
@ -171,7 +171,7 @@ def test_create_invalid_divisible_asset(b, user_vk, user_sk):
|
|||
|
||||
# non divisible assets cannot have amount > 1
|
||||
# Transaction.__init__ should raise an exception
|
||||
asset = Asset(divisible=False)
|
||||
asset = Asset(divisible=False)
|
||||
with pytest.raises(AmountError):
|
||||
Transaction.create([user_vk], [user_vk], asset=asset, amount=2)
|
||||
|
||||
|
@ -202,7 +202,7 @@ def test_create_invalid_divisible_asset(b, user_vk, user_sk):
|
|||
|
||||
def test_create_valid_divisible_asset(b, user_vk, user_sk):
|
||||
from bigchaindb.models import Transaction, Asset
|
||||
|
||||
|
||||
asset = Asset(divisible=True)
|
||||
tx = Transaction.create([user_vk], [user_vk], asset=asset, amount=2)
|
||||
tx_signed = tx.sign([user_sk])
|
||||
|
|
Loading…
Reference in New Issue
Block a user