예제 #1
0
 def test_mining(self):
     bch = block.Blockchain()
     bch.new_block([my_keys[1], my_keys[1], your_pub_key])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.5, 0.25], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], ['mining', my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], ['mining', my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], ['mining', my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     n = 1000
     xs = mining.poc_mine(n, bch, my_keys[1])
     bch.add_miner([n, my_keys[1], xs], 'poc')
     bch.add_miner([n, my_keys[1], xs], 'poc')
     bch.add_miner([n, my_keys[1], xs], 'poc')
     n, t, h = mining.pow_mine(bch, 90000000000000000000000000000000000,
                               my_keys[1])
     bch.add_miner([int(h), n, my_keys[1], t], 'pow')
     bch.add_miner([int(h), n, my_keys[1], t], 'pow')
     bch.add_miner([int(h), n, my_keys[1], t], 'pow')
     bl = block.Block(n, [my_keys[1]], bch, [], [], t)
     b = mining.mine(bch)
     bch.append(b)
     print('n', bch[-1].n)
     self.assertTrue(mining.validate(bch, -1))
예제 #2
0
 def test_tnx_str_encoding(self):
     bch = block.Blockchain()
     bch.clean()
     bch.new_block([my_keys[1], your_pub_key, your_pub_key])
     bch.new_block([my_keys[1], your_pub_key, your_pub_key])
     bch.new_transaction(my_keys[1], [[0, 0], [1, 0]],
                         [your_pub_key, my_keys[1]], [0.5, 0.3], 'signing',
                         my_keys[0])
     b2 = block.Transaction()
     b2.from_json(str(bch[1].txs[0]))
     self.assertEqual(b2, bch[1].txs[0])
     bch.conn.close()
예제 #3
0
 def test_poc(self):
     bch = block.Blockchain()
     bch.new_block([my_keys[1], my_keys[1], your_pub_key])
     bch.new_block([my_keys[1], my_keys[1], your_pub_key])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.5, 0.25], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     xs = mining.poc_mine(1000, bch, my_keys[1])
     bch[-1].pocminers.append([1000, my_keys[1], xs])
     print(len(xs), xs)
예제 #4
0
 def test_pow(self):
     bch = block.Blockchain()
     bch.new_block([my_keys[1], my_keys[1], your_pub_key])
     bch.new_block([my_keys[1], my_keys[1], your_pub_key])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.5, 0.25], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     bch.new_transaction(my_keys[1], [(0, 0)], [your_pub_key, my_keys[1]],
                         [0.05, 0.95], 'signing', my_keys[0])
     n, t, h = mining.pow_mine(bch, 900000000000000000000000000000000000,
                               my_keys[1])
     print(n, t, h)
예제 #5
0
 def test_creations_and_money_counter(self):
     bch = block.Blockchain()
     bch.clean()
     print(len(bch), bch.money(my_keys[1]))
     bch.new_block([my_keys[1], your_pub_key, your_pub_key])
     print(bch.money(my_keys[1]), len(bch))
     bch.new_block([my_keys[1], your_pub_key, your_pub_key])
     print(bch.money(my_keys[1]), bch[1].txs[0].timestamp, time.time())
     print(len(bch))
     bch.new_transaction(my_keys[1], [[0, 0], [1, 0]],
                         [your_pub_key, my_keys[1]], [0.5, 0.3], 'signing',
                         my_keys[0])
     print(bch.money(my_keys[1]))
     print(len(bch))
     self.assertAlmostEqual(bch.money(my_keys[1]), 0.3)
     self.assertAlmostEqual(bch.money(your_pub_key), 1.7)
     bch.conn.close()
예제 #6
0
 def test_block_str_encoding(self):
     bch = block.Blockchain()
     bch.clean()
     bch.new_block([my_keys[1], your_pub_key, your_pub_key])
     bch.new_block([my_keys[1], your_pub_key, your_pub_key])
     bch.new_transaction(my_keys[1], [[0, 0], [1, 0]],
                         [your_pub_key, my_keys[1]], [0.5, 0.3], 'signing',
                         my_keys[0])
     b2 = block.Block()
     b2.from_json(str(bch[1]))
     d = bch[1].__dict__
     d1 = b2.__dict__
     for k in d.keys():
         if d[k] != d1[k]:
             print(k, d[k], d1[k])
     print(bch[1].txs, b2.txs)
     self.assertEqual(b2, bch[1])
     bch.conn.close()
예제 #7
0
import cryptogr as cg
import net
import block
import mining
import time

bch = block.Blockchain()


class Wallet:
    def __init__(self):
        self.privkey, self.pubkey = cg.gen_keys()

    def new_transaction(self, outs, outns):
        """Performs tnx"""
        out = 0
        for outn in outns:
            out += outn
        if out > bch.money(self.pubkey):
            return False
        froms = []
        # todo: write froms generation algorythm
        bch.new_transaction(self.pubkey,
                            froms,
                            outs,
                            outns,
                            privkey=self.privkey)

    def my_money(self):
        return bch.money(self.pubkey)
예제 #8
0
파일: main.py 프로젝트: yongtao-wang/y-bit
from uuid import uuid4
import argparse

from flask import Flask, jsonify, request

import block

app = Flask(__name__)

node_identifier = str(uuid4()).replace('-', '')

blockchain = block.Blockchain()


@app.route('/mine', methods=['GET'])
def mine():
    last_block = blockchain.last_block
    last_proof = last_block['proof']
    proof = blockchain.proof_of_work(last_proof)

    # reward to PoW
    blockchain.new_transaction(
        sender=0,
        recipient=node_identifier,
        amount=1,
    )

    new_block = blockchain.new_block(proof)
    response = {
        'message': "New Block Forged",
        'index': new_block['index'],