Ejemplo n.º 1
0
def test_block():
    """ Test serialization and deserialization of a Block.
    """
    h = Header(1, 0, time(), 0, 0, 123)
    t = Transaction('send', 'rec', 50, 5, time(), 'Sign')
    b = Block(h, [t])
    assert b == rebuild_obj(b)
Ejemplo n.º 2
0
def create_transaction(dns: bool, sender: str, recipient: str, amount: int,
                       fee: int, timestamp: int, dns_data: DNS_Data,
                       signing_key: nacl.signing.SigningKey):
    """ Creates a transaction from the given arguments:
        Args:
            dns: whether or not to create a DNS_Transaction
            sender: sender of the transaction
            recipient: recipient of the transaction
            amount: amount of coins sended
            fee: fee for the transaction
            timestamp: timestamp of the transaction
            dns_data: includes information on dns operations
            signing_key: key used to sign the hash
    """
    hash_str = (str(sender) + str(recipient) + str(amount) + str(fee) +
                str(timestamp))
    if not dns:
        transaction_hash = hashlib.sha256(hash_str.encode()).hexdigest()

        transaction = Transaction(sender, recipient, amount, fee, timestamp,
                                  signing_key.sign(transaction_hash.encode()))
        return transaction

    transaction_hash = hashlib.sha256(
        (hash_str + str(dns_data)).encode()).hexdigest()
    transaction = DNS_Transaction(sender, recipient, amount, fee, timestamp,
                                  dns_data,
                                  signing_key.sign(transaction_hash.encode()))
    return transaction
Ejemplo n.º 3
0
    def test_block(self):
        """ Test that the block creation works as intended.
        """
        proof = self.blockchain.create_proof(self.sender_verify)
        block = self.blockchain.create_block(proof)
        mining_transaction = \
            Transaction(sender='0',
                        recipient=self.sender_verify,
                        amount=50,
                        fee=0,
                        timestamp=time.time(),
                        signature='0')

        block.transactions.append(mining_transaction)

        root_hash = self.blockchain.create_merkle_root(block.transactions)
        real_header = Header(block.header.version, block.header.index,
                             block.header.timestamp,
                             block.header.previous_hash, root_hash,
                             block.header.proof)
        real_block = Block(real_header, block.transactions)

        self.blockchain.new_block(real_block)

        assert self.blockchain.latest_block() == real_block
        assert (mining_transaction
                in self.blockchain.latest_block().transactions)
        assert self.blockchain.check_balance(self.sender_verify,
                                             time.time()) == 50
Ejemplo n.º 4
0
    def mine_block(self, chain):
        """ Mine an initial block to add a balance to the test account.

        Args:
            chain: Chain to mine on
        """

        proof = chain.create_proof(self.sender_verify)
        block = chain.create_block(proof)
        block.transactions.append(
            Transaction(sender='0',
                        recipient=self.sender_verify,
                        amount=50,
                        fee=0,
                        timestamp=time.time(),
                        signature='0'))

        root_hash = chain.create_merkle_root(block.transactions)
        real_header = Header(block.header.version, block.header.index,
                             block.header.timestamp,
                             block.header.previous_hash, root_hash,
                             block.header.proof)
        real_block = Block(real_header, block.transactions)

        chain.new_block(real_block)
        try:
            self.sends.get(block=False)  # Remove new_block message
        except Empty:
            pass
Ejemplo n.º 5
0
    def create_transaction(self):
        """ Create simple transaction used in tests.

        Returns:
            A new transaction.
        """

        amount = 10
        timestamp = time.time()
        fee = math.ceil(amount * 0.05)

        transaction_hash = self.create_transaction_hash(amount, fee, timestamp)

        return Transaction(self.sender_verify, self.receiver_verify, amount,
                           fee, timestamp,
                           self.sender_sign.sign(transaction_hash.encode()))
Ejemplo n.º 6
0
    def test_transaction_invalid_signature(self):
        """ Test that the transactions with invalid signatures are recognized
        and not added to the blockchain.
        """
        self.mine_block(self.blockchain)

        transaction = self.create_transaction()
        transaction = Transaction(
            transaction.sender, transaction.recipient, transaction.amount,
            transaction.fee, transaction.timestamp,
            self.receiver_sign.sign(
                self.create_transaction_hash(transaction.amount,
                                             transaction.fee,
                                             transaction.timestamp).encode()))

        assert not self.blockchain.validate_transaction(transaction, False)

        self.blockchain.new_transaction(transaction)

        assert transaction not in self.blockchain.transaction_pool
        assert self.sends.empty()
Ejemplo n.º 7
0
def test_transaction():
    """ Test serialization and deserialization of a Transaction.
    """
    t = Transaction('send', 'rec', 50, 5, time(), 'Sign')
    assert t == rebuild_obj(t)
Ejemplo n.º 8
0
chain1 = [
    # Block id: 000000154275885a72c004d02aaa9524fc0c4896aef0b0f3bcde2de38f9be561
    Block(version=0,
          prev_block_hash=None,
          merkle_hash=
          '7118894203235a955a908c0abfc6d8fe6edec47b0a04ce1bf7263da3b4366d22',
          timestamp=1501821412,
          bits=24,
          nonce=10126761,
          txns=[
              Transaction(
                  txins=[
                      TxIn(to_spend=None,
                           unlock_sig=b'0',
                           unlock_pk=None,
                           sequence=0)
                  ],
                  txouts=[
                      TxOut(value=5000000000,
                            to_address='143UVyz7ooiAv1pMqbwPPpnH4BV9ifJGFF')
                  ],
                  locktime=None)
          ]),

    # Block id: 00000095f785bc8fbd6007b36c2f1c414d66db930e2e7354076c035c8f92700b
    Block(version=0,
          prev_block_hash=
          '000000154275885a72c004d02aaa9524fc0c4896aef0b0f3bcde2de38f9be561',
          merkle_hash=
          '27661bd9b23552832becf6c18cb6035a3d77b4e66b5520505221a93922eb82f2',
          timestamp=1501826444,
          bits=24,