mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00

* Problem: Node operator cannot vote on a ValidatorElection Solution: Implement validator election voting spec * Problem: Incorrent code comments Solution: Update comments with correct context * Problem: Delegated vote not casted back to election Solution: Update test to cast votes back to election id and assert their validity
71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
from bigchaindb.common.exceptions import (InvalidSignature,
|
|
DuplicateTransaction)
|
|
from bigchaindb.common.transaction import Transaction
|
|
from bigchaindb.common.utils import (validate_txn_obj, validate_key)
|
|
from bigchaindb.common.schema import validate_transaction_schema
|
|
from bigchaindb.backend.schema import validate_language_key
|
|
|
|
|
|
class Transaction(Transaction):
|
|
|
|
def validate(self, bigchain, current_transactions=[]):
|
|
"""Validate transaction spend
|
|
|
|
Args:
|
|
bigchain (BigchainDB): an instantiated bigchaindb.BigchainDB object.
|
|
|
|
Returns:
|
|
The transaction (Transaction) if the transaction is valid else it
|
|
raises an exception describing the reason why the transaction is
|
|
invalid.
|
|
|
|
Raises:
|
|
ValidationError: If the transaction is invalid
|
|
"""
|
|
input_conditions = []
|
|
|
|
if self.operation == Transaction.CREATE:
|
|
duplicates = any(txn for txn in current_transactions if txn.id == self.id)
|
|
if bigchain.get_transaction(self.to_dict()['id']) or duplicates:
|
|
raise DuplicateTransaction('transaction `{}` already exists'
|
|
.format(self.id))
|
|
|
|
if not self.inputs_valid(input_conditions):
|
|
raise InvalidSignature('Transaction signature is invalid.')
|
|
|
|
elif self.operation == Transaction.TRANSFER:
|
|
self.validate_transfer_inputs(bigchain, current_transactions)
|
|
|
|
return self
|
|
|
|
@classmethod
|
|
def from_dict(cls, tx_body):
|
|
return super().from_dict(tx_body, False)
|
|
|
|
@classmethod
|
|
def validate_schema(cls, tx_body):
|
|
cls.validate_id(tx_body)
|
|
validate_transaction_schema(tx_body)
|
|
validate_txn_obj('asset', tx_body['asset'], 'data', validate_key)
|
|
validate_txn_obj('metadata', tx_body, 'metadata', validate_key)
|
|
validate_language_key(tx_body['asset'], 'data')
|
|
|
|
|
|
class FastTransaction:
|
|
"""A minimal wrapper around a transaction dictionary. This is useful for
|
|
when validation is not required but a routine expects something that looks
|
|
like a transaction, for example during block creation.
|
|
|
|
Note: immutability could also be provided
|
|
"""
|
|
|
|
def __init__(self, tx_dict):
|
|
self.data = tx_dict
|
|
|
|
@property
|
|
def id(self):
|
|
return self.data['id']
|
|
|
|
def to_dict(self):
|
|
return self.data
|