From 380482b9674125d79be558373f43ef50ebaadf4b Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Mon, 29 Feb 2016 19:19:08 -0800 Subject: [PATCH] Added an abstract base class so plugins don't need to add stubs for functions they don't care about. --- bigchaindb/consensus/__init__.py | 43 ++++++++++++++++++++++++++++++++ bigchaindb/consensus/base.py | 3 ++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/bigchaindb/consensus/__init__.py b/bigchaindb/consensus/__init__.py index e69de29b..24806bd0 100644 --- a/bigchaindb/consensus/__init__.py +++ b/bigchaindb/consensus/__init__.py @@ -0,0 +1,43 @@ +# TODO: no real reason to use abc yet, but later we can enforce inheritance from +# this class when loading plugins if that's desirable. +# from abc import ABCMeta + +class AbstractConsensusRules: + + # TODO: rather than having plugin-authors inherit and override, + # it'd be cleaner to make a `transactionrule` decorator and etc + @classmethod + def validate_transaction(cls, bigchain, transaction): + """Validate a transaction. + + Args: + bigchain (Bigchain): an instantiated bigchaindb.Bigchain object. + transaction (dict): transaction to validate. + + Returns: + The transaction if the transaction is valid else it raises an + exception describing the reason why the transaction is invalid. + + Raises: + Descriptive exceptions indicating the reason the transaction failed. + See the `exceptions` module for bigchain-native error classes. + """ + return transaction + + @classmethod + def validate_block(cls, bigchain, block): + """Validate a block. + + Args: + bigchain (Bigchain): an instantiated bigchaindb.Bigchain object. + block (dict): block to validate. + + Returns: + The block if the block is valid else it raises and exception + describing the reason why the block is invalid. + + Raises: + Descriptive exceptions indicating the reason the block failed. + See the `exceptions` module for bigchain-native error classes. + """ + return block diff --git a/bigchaindb/consensus/base.py b/bigchaindb/consensus/base.py index 1943c543..ab01d26e 100644 --- a/bigchaindb/consensus/base.py +++ b/bigchaindb/consensus/base.py @@ -1,7 +1,8 @@ import bigchaindb.exceptions as exceptions from bigchaindb.crypto import hash_data +from bigchaindb.consensus import AbstractConsensusRules -class ConsensusRules(object): +class ConsensusRules(AbstractConsensusRules): """Base consensus rules for Bigchain. This class can be copied to write your own consensus rules!