from time import time
from utility.printable import Printable
from collections import OrderedDict
from transaction import Transaction


class Block(Printable):
    """
    Attributes: 
        :index: The index of the current block.
        :node_id: The id of the person who mined this block (public_key)
        :proof: A symbolic proof generated by the miner with the highest production at that time.
        :transactions: A list of transaction which are included in the block.
        :previous_hash: The hash of the previous block in the blockchain.
        :timestamp: The timestamp of the block (automatically generated by default).
        :signature: default: None
    """

    def __init__(self, index, node_id, proof, transactions, previous_hash, time=time(), signature=None):
        self.index = index
        self.node_id = node_id
        self.proof = proof
        self.transactions = transactions
        self.previous_hash = previous_hash
        self.timestamp = time
        self.signature = signature

    def to_ordered_dict(self):
        """ Converts this block into a (hashable) OrderedDict. """
        return OrderedDict([
            ('index', self.index),
            ('node_id', self.node_id),
            ('proof', self.proof),
            ('transactions', [
                tx.to_ordered_dict() for tx in self.transactions
            ]),
            ('previous_hash', self.previous_hash),
            ('timestamp', self.timestamp)
        ])

    def add_signature(self, signature):
        self.signature = signature

    @staticmethod
    def create_object(dict_block):
        converted_tx = [
            Transaction(
                tx['sender'],
                tx['recipient'],
                tx['amount'],
                tx['description'],
                tx['timestamp'],
                tx['signature']
            ) for tx in dict_block["transactions"]]
        block = Block(
            dict_block["index"],
            dict_block["node_id"],
            dict_block["proof"],
            converted_tx,
            dict_block["previous_hash"],
            dict_block["timestamp"],
            dict_block["signature"])
        return block

from time import time
from utility.printable import Printable
from collections import OrderedDict
from transaction import Transaction


class Block(Printable):
    """
    Attributes: 
        :index: The index of the current block.
        :node_id: The id of the person who mined this block (public_key)
        :proof: A symbolic proof generated by the miner with the highest production at that time.
        :transactions: A list of transaction which are included in the block.
        :previous_hash: The hash of the previous block in the blockchain.
        :timestamp: The timestamp of the block (automatically generated by default).
        :signature: default: None
    """

    def __init__(self, index, node_id, proof, transactions, previous_hash, time=time(), signature=None):
        self.index = index
        self.node_id = node_id
        self.proof = proof
        self.transactions = transactions
        self.previous_hash = previous_hash
        self.timestamp = time
        self.signature = signature

    def to_ordered_dict(self):
        """ Converts this block into a (hashable) OrderedDict. """
        return OrderedDict([
            ('index', self.index),
            ('node_id', self.node_id),
            ('proof', self.proof),
            ('transactions', [
                tx.to_ordered_dict() for tx in self.transactions
            ]),
            ('previous_hash', self.previous_hash),
            ('timestamp', self.timestamp)
        ])

    def add_signature(self, signature):
        self.signature = signature

    @staticmethod
    def create_object(dict_block):
        converted_tx = [
            Transaction(
                tx['sender'],
                tx['recipient'],
                tx['amount'],
                tx['description'],
                tx['timestamp'],
                tx['signature']
            ) for tx in dict_block["transactions"]]
        block = Block(
            dict_block["index"],
            dict_block["node_id"],
            dict_block["proof"],
            converted_tx,
            dict_block["previous_hash"],
            dict_block["timestamp"],
            dict_block["signature"])
        return block

Attributes:          :index: The index of the current block.         :node_id: The id of the person who mined this block (public_key)         :proof: A symbolic proof generated by the miner with the highest production at that time.         :transactions: A list of transaction which are included in the block.         :previous_hash: The hash of the previous block in the blockchain.         :timestamp: The timestamp of the block (automatically generated by default).         :signature: default: None Converts this block into a (hashable) OrderedDict.