Sylvain Bellemare 50b0b3cef2 Rebase/feat/586/integrate tx model (#641)
* Adjust imports to bigchaindb_common

* Adjust get_spent function signature

* Adjust block serialization

* Fix BigchainApi Test

* Fix TestTransactionValidation tests

* Fix TestBlockValidation tests

* WIP: TestMultipleInputs

* Adjust tests to tx-model interface changes

- Fix old tests
- Fix tests in TestMultipleInputs class

* Remove fulfillment message tests

* Fix TransactionMalleability tests

* Remove Cryptoconditions tests

* Remove create_transaction

* Remove signing logic

* Remove consensus plugin

* Fix block_creation pipeline

* Fix election pipeline

* Replace some util functions with bdb_common ones

- timestamp ==> gen_timestamp
- serialize.

* Implement Block model

* Simplify function signatures for vote functions

Change parameter interface for the following functions:

- has_previous_vote
- verify_vote_signature
- block_election_status

so that they take a block's id and voters instead of a fake block.

* Integrate Block and Transaction model

* Fix leftover tests and cleanup conftest

* Add bigchaindb-common to install_requires

* Delete transactions after block is written (#609)

* delete transactions after block is written

* cleanup transaction_exists

* check for duplicate transactions

* delete invalid tx from backlog

* test duplicate transaction

* Remove dead code

* Test processes.py

* Test invalid tx in on server

* Fix tests for core.py

* Fix models tests

* Test commands main fn

* Add final coverage to vote pipeline

* Add more tests to voting pipeline

* Remove consensus plugin docs and misc

* Post rebase fixes

* Fix rebase mess

* Remove extra blank line

* Improve docstring

* Remove comment

handled in bigchaindb/cryptoconditions#27;
see https://github.com/bigchaindb/cryptoconditions/issues/27

* Fix block serialization in block creation

* Add signed_ prefix to transfer_tx

* Improve docs

* Add library documentation page on pipelines

* PR feedback for models.py

* Impr. readability of get_last_voted_block

* Use dict comprehension

* Add docker-compose file to build and serve docs

locally for development purposes

* Change private_key for signing_key

* Improve docstrings

* Remove consensus docs

* Document new consensus module

* Create different transactions for the block

* Cleanup variable names in block.py

* Create different transactions for the block

* Cleanup variable names in block.py
2016-09-29 10:29:41 +02:00

209 lines
7.8 KiB
Python

from bigchaindb_common.crypto import hash_data, VerifyingKey, SigningKey
from bigchaindb_common.exceptions import (InvalidHash, InvalidSignature,
OperationError, DoubleSpend,
TransactionDoesNotExist)
from bigchaindb_common.transaction import Transaction
from bigchaindb_common.util import gen_timestamp, serialize
class Transaction(Transaction):
def validate(self, bigchain):
"""Validate a transaction.
Args:
bigchain (Bigchain): an instantiated bigchaindb.Bigchain object.
Returns:
The transaction (Transaction) if the transaction is valid else it
raises an exception describing the reason why the transaction is
invalid.
Raises:
OperationError: if the transaction operation is not supported
TransactionDoesNotExist: if the input of the transaction is not
found
TransactionOwnerError: if the new transaction is using an input it
doesn't own
DoubleSpend: if the transaction is a double spend
InvalidHash: if the hash of the transaction is wrong
InvalidSignature: if the signature of the transaction is wrong
"""
if len(self.fulfillments) == 0:
raise ValueError('Transaction contains no fulfillments')
input_conditions = []
inputs_defined = all([ffill.tx_input for ffill in self.fulfillments])
if self.operation in (Transaction.CREATE, Transaction.GENESIS):
if inputs_defined:
raise ValueError('A CREATE operation has no inputs')
elif self.operation == Transaction.TRANSFER:
if not inputs_defined:
raise ValueError('Only `CREATE` transactions can have null '
'inputs')
for ffill in self.fulfillments:
input_txid = ffill.tx_input.txid
input_cid = ffill.tx_input.cid
input_tx = bigchain.get_transaction(input_txid)
if input_tx is None:
raise TransactionDoesNotExist("input `{}` doesn't exist"
.format(input_txid))
spent = bigchain.get_spent(input_txid, ffill.tx_input.cid)
if spent and spent.id != self.id:
raise DoubleSpend('input `{}` was already spent'
.format(input_txid))
input_conditions.append(input_tx.conditions[input_cid])
else:
allowed_operations = ', '.join(Transaction.ALLOWED_OPERATIONS)
raise TypeError('`operation`: `{}` must be either {}.'
.format(self.operation, allowed_operations))
if not self.fulfillments_valid(input_conditions):
raise InvalidSignature()
else:
return self
class Block(object):
def __init__(self, transactions=None, node_pubkey=None, timestamp=None,
voters=None, signature=None):
if transactions is not None and not isinstance(transactions, list):
raise TypeError('`transactions` must be a list instance or None')
else:
self.transactions = transactions or []
if voters is not None and not isinstance(voters, list):
raise TypeError('`voters` must be a list instance or None')
else:
self.voters = voters or []
if timestamp is not None:
self.timestamp = timestamp
else:
self.timestamp = gen_timestamp()
self.node_pubkey = node_pubkey
self.signature = signature
def __eq__(self, other):
try:
other = other.to_dict()
except AttributeError:
return False
return self.to_dict() == other
def validate(self, bigchain):
"""Validate a block.
Args:
bigchain (Bigchain): an instantiated bigchaindb.Bigchain object.
Returns:
block (Block): The block as a `Block` object if it is valid.
Else it raises an appropriate exception describing
the reason of invalidity.
Raises:
OperationError: if a non-federation node signed the block.
"""
# First, make sure this node hasn't already voted on this block
if bigchain.has_previous_vote(self.id, self.voters):
return self
# Check if the block was created by a federation node
possible_voters = (bigchain.nodes_except_me + [bigchain.me])
if self.node_pubkey not in possible_voters:
raise OperationError('Only federation nodes can create blocks')
if not self.is_signature_valid():
raise InvalidSignature('Block signature invalid')
# Finally: Tentative assumption that every blockchain will want to
# validate all transactions in each block
for tx in self.transactions:
# NOTE: If a transaction is not valid, `is_valid` will throw an
# an exception and block validation will be canceled.
bigchain.validate_transaction(tx)
return self
def sign(self, signing_key):
block_body = self.to_dict()
block_serialized = serialize(block_body['block'])
signing_key = SigningKey(signing_key)
self.signature = signing_key.sign(block_serialized)
return self
def is_signature_valid(self):
block = self.to_dict()['block']
block_serialized = serialize(block)
verifying_key = VerifyingKey(block['node_pubkey'])
try:
# NOTE: CC throws a `ValueError` on some wrong signatures
# https://github.com/bigchaindb/cryptoconditions/issues/27
return verifying_key.verify(block_serialized, self.signature)
except (ValueError, AttributeError):
return False
@classmethod
def from_dict(cls, block_body):
block = block_body['block']
block_serialized = serialize(block)
block_id = hash_data(block_serialized)
verifying_key = VerifyingKey(block['node_pubkey'])
try:
signature = block_body['signature']
except KeyError:
signature = None
if block_id != block_body['id']:
raise InvalidHash()
if signature is not None:
# NOTE: CC throws a `ValueError` on some wrong signatures
# https://github.com/bigchaindb/cryptoconditions/issues/27
try:
signature_valid = verifying_key.verify(block_serialized,
signature)
except ValueError:
signature_valid = False
if signature_valid is False:
raise InvalidSignature('Invalid block signature')
transactions = [Transaction.from_dict(tx) for tx
in block['transactions']]
return cls(transactions, block['node_pubkey'],
block['timestamp'], block['voters'], signature)
@property
def id(self):
return self.to_dict()['id']
def to_dict(self):
if len(self.transactions) == 0:
raise OperationError('Empty block creation is not allowed')
block = {
'timestamp': self.timestamp,
'transactions': [tx.to_dict() for tx in self.transactions],
'node_pubkey': self.node_pubkey,
'voters': self.voters,
}
block_serialized = serialize(block)
block_id = hash_data(block_serialized)
return {
'id': block_id,
'block': block,
'signature': self.signature,
}
def to_str(self):
return serialize(self.to_dict())