<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pynunzen.helpers import double_sha256
from pynunzen.ledger.transaction import Transaction

__block_version__ = "1.0"
"""Version of the block. Used to versionize the block."""
__block_max_size__ = 256
"""Max length of the `data` attribute within a block. This is not a
limitation in bytes but in general length."""


def generate_block_address(index, timestamp, parent, data):
    """Will calculate a doubled SHA256 hash which will be used as the
    address of a new created block in the blockchain.

    :index: index of the block
    :timestamp: timestamp of the block
    :parent: address of the previous block
    :data: data within the the block
    :returns: SHA256 hash

    """
    hash_source = str(index) + str(timestamp) + str(parent) + str(data)
    return double_sha256(hash_source)


class Block(object):

    """Single block in a blockchain. A block is a container data
    structure that holds all transactions/data for inclusion in the
    blockchain.

    The block header consists a reference to a previous block hash,
    which links the block to block in the blockchain. Further there are
    some fields related to the mining like difficulty, timestamp, and
    nonce.  The last part is the merkle tree root which is used to
    summarize all transactions or data in the block.

    The header is followed by a long list of transactions/data."""

    def __init__(self, index, timestamp, parent, data, address=None):

        #
        # Block data/transactions
        #
        if not isinstance(data, list):
            raise ValueError("Data must be a list")
        if len(data) > __block_max_size__:
            raise ValueError("Data must must not be longer than {}".format(__block_max_size__))
        if len(data) == 0:
            raise ValueError("Data must must not empty")
        for transaction in data:
            if not isinstance(transaction, Transaction):
                raise ValueError("Data must contain only transactions. Found {}".format(transaction))

        self.data = data
        """Holds the data oft the block. Data must be a list."""

        #
        # Header data
        #
        self.version = __block_version__
        """Version of the block"""
        # Mining related fields
        self.timestamp = timestamp
        """UTC timestamp when was this block created."""
        self.difficulty = None
        self.nonce = None
<commit_msg>Add checks that the first transaction in the block is actually a coinbasetransaction.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pynunzen.helpers import double_sha256
from pynunzen.ledger.transaction import Transaction, CoinbaseInput

__block_version__ = "1.0"
"""Version of the block. Used to versionize the block."""
__block_max_size__ = 256
"""Max length of the `data` attribute within a block. This is not a
limitation in bytes but in general length."""


def generate_block_address(index, timestamp, parent, data):
    """Will calculate a doubled SHA256 hash which will be used as the
    address of a new created block in the blockchain.

    :index: index of the block
    :timestamp: timestamp of the block
    :parent: address of the previous block
    :data: data within the the block
    :returns: SHA256 hash

    """
    hash_source = str(index) + str(timestamp) + str(parent) + str(data)
    return double_sha256(hash_source)


class Block(object):

    """Single block in a blockchain. A block is a container data
    structure that holds all transactions/data for inclusion in the
    blockchain.

    The block header consists a reference to a previous block hash,
    which links the block to block in the blockchain. Further there are
    some fields related to the mining like difficulty, timestamp, and
    nonce.  The last part is the merkle tree root which is used to
    summarize all transactions or data in the block.

    The header is followed by a long list of transactions/data."""

    def __init__(self, index, timestamp, parent, data, address=None):

        #
        # Block data/transactions
        #
        if not isinstance(data, list):
            raise ValueError("Data must be a list")
        if len(data) > __block_max_size__:
            raise ValueError("Data must must not be longer than {}".format(__block_max_size__))
        if len(data) == 0:
            raise ValueError("Data must must not empty")
        for transaction in data:
            if not isinstance(transaction, Transaction):
                raise ValueError("Data must contain only transactions. Found {}".format(transaction))
        # Check that the first transaction in this block is a coinbase
        # transaction.
        if len(data[0].inputs) != 1 or len(data[0].outputs) != 1:
                raise ValueError("First transaction must have exactly one input and output")
        if not isinstance(data[0].inputs[0], CoinbaseInput):
                raise ValueError("First transaction in block seems not to be a coinbase transaction")

        self.data = data
        """Holds the data oft the block. Data must be a list."""

        #
        # Header data
        #
        self.version = __block_version__
        """Version of the block"""
        # Mining related fields
        self.timestamp = timestamp
        """UTC timestamp when was this block created."""
        self.difficulty = None
        self.nonce = None

{'fraction_non_alphanumeric': 0.0777430619371439, 'fraction_numerical': 0.008086748759419225, 'mean_word_length': 2.7765440666204024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3794585', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1374, 'n_words': 623}