コード例 #1
0
def test_read_genesis_block(monkeypatch):
    """
    test reading the genesis block from the blockchain
    """
    hblockchain.blockchain.clear()
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))

    hblockchain.add_block(block_0)
    assert hblockchain.read_block(0) == block_0
    hblockchain.blockchain.clear()
コード例 #2
0
def test_add_good_block(monkeypatch):
    """
    test add a good block
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    assert hblockchain.add_block(block_0) == True

    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    assert hblockchain.add_block(block_1) == True
    hblockchain.blockchain.clear()
コード例 #3
0
def test_read_genesis_block(monkeypatch):
    """
    test reading the genesis block from the blockchain
    """
    hblockchain.blockchain.clear()
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(hblockchain, "merkle_root", lambda x, y: "")

    hblockchain.add_block(block_0)
    assert hblockchain.read_block(0) == block_0
    hblockchain.blockchain.clear()
コード例 #4
0
def test_add_good_block(monkeypatch):
    """
    test add a good block
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)

    assert hblockchain.add_block(block_0) == True
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    assert hblockchain.add_block(block_1) == True
    hblockchain.blockchain.clear()
コード例 #5
0
def test_merkle_root_block_transaction(monkeypatch):
    """
    test merkle root for synthetic random transactions
    """
    hblockchain.blockchain.clear()
    block_0["merkle_root"] = hblockchain.merkle_root(block_0["tx"], True)
    assert hblockchain.add_block(block_0) == True

    block_1["merkle_root"] = hblockchain.merkle_root(block_1["tx"], True)
    block_1["prevblockhash"] = hblockchain.blockheader_hash(block_0)
    assert hblockchain.add_block(block_1) == True

    block_2["merkle_root"] = hblockchain.merkle_root(block_2["tx"], True)
    block_2["prevblockhash"] = hblockchain.blockheader_hash(block_1)
    assert hblockchain.add_block(block_2) == True
コード例 #6
0
def test_missing_timestamp(monkeypatch):
    """
    test for a missing timestamp
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setitem(block_1, "timestamp", "")

    assert hblockchain.add_block(block_1) == False
コード例 #7
0
def test_block_nonce_type(monkeypatch):
    """
    test nonce has the wrong type"
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_0, "nonce", "0")

    assert hblockchain.add_block(block_0) == False
コード例 #8
0
def test_bad_timestamp_type(monkeypatch):
    """
    test for a bad timestamp type
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setitem(block_1, "timestamp", "12345")

    assert hblockchain.add_block(block_1) == False
コード例 #9
0
def test_negative_timestamp(monkeypatch):
    """
    test for a negative timestamp
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_0, "timestamp", -2)

    assert hblockchain.add_block(block_0) == False
コード例 #10
0
def test_negative_difficulty_bit(monkeypatch):
    """
    test for negative difficulty bits
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setitem(block_1, "difficulty_bits", -5)

    assert hblockchain.add_block(block_1) == False
コード例 #11
0
def test_version_bad(monkeypatch):
    """
    test for an unknown version number
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setitem(block_1, "version", -1)

    assert hblockchain.add_block(block_1) == False
コード例 #12
0
def test_difficulty_type(monkeypatch):
    """
    test difficulty bits has the wrong type"
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_0, "difficulty_bits", "20")

    assert hblockchain.add_block(block_0) == False
コード例 #13
0
def test_missing_difficulty_bit(monkeypatch):
    """
    test for missing difficulty bits
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('data'))
    monkeypatch.setitem(block_1, "difficulty_bits", '')

    assert hblockchain.add_block(block_1) == False
コード例 #14
0
def test_no_consecutive_duplicate_blocks(monkeypatch):
    """
    test cannot add the same block twice consecutively to the blockchain
    """
    hblockchain.blockchain.clear()
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    assert hblockchain.add_block(block_0) == True

    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setitem(block_1, "prevblockhash",
                        hblockchain.blockheader_hash(block_0))
    assert hblockchain.add_block(block_1) == True

    monkeypatch.setitem(block_1, "height", 2)
    assert hblockchain.add_block(block_1) == False
    hblockchain.blockchain.clear()
コード例 #15
0
def test_bad_nonce(monkeypatch):
    """
    test for a negative nonce
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setitem(block_1, "nonce", -1)

    assert hblockchain.add_block(block_1) == False
コード例 #16
0
def test_missing_nonce(monkeypatch):
    """
    test for a missing nonce
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_0, "nonce", "")

    assert hblockchain.add_block(block_0) == False
コード例 #17
0
def make_blocks(num_blocks):

    ctr = 0 
    total_tx = 0 
    blocks = []

    global unspent_fragments
    unspent_fragments.clear()
    hblockchain.blockchain.clear()

    while ctr < num_blocks:
        block = {}
        block["prevblockhash"] = ""
        block["version"] = hconfig.conf["VERSION_NO"]
        block["timestamp"] = int(time.time())
        block["difficulty_bits"] = hconfig.conf["DIFFICULTY_BITS"]
        block["nonce"] = hconfig.conf["NONCE"]
        block["merkle_root"] = ""
        block["height"] = ctr
        block["tx"] = []

        # make a random number of transactions for this block
        # genesis block is ctr == 0
        if ctr == 0: num_transactions = 200                      
        else: 
            num_transactions = secrets.randbelow(50)          
            if num_transactions <= 1: num_transactions = 25

        txctr = 0
        while txctr < num_transactions:
            if ctr > 0 and txctr == 0: coinbase_trans = True
            else: coinbase_trans = False 
            
            trx = make_random_transaction(ctr, coinbase_trans)
            assert trx != False 
            block["tx"].append(trx)
            total_tx += 1
            txctr += 1

        if ctr > 0:
            block["prevblockhash"] = hblockchain.blockheader_hash(hblockchain.blockchain[ctr - 1]) 
             
        ret = hblockchain.merkle_root(block["tx"], True)
        assert ret != False
        block["merkle_root"] = ret

        ret = hblockchain.add_block(block)
        assert ret == True
        blocks.append(block)
        
        ctr+= 1

    print("blockchain height: " + str(blocks[-1]["height"]))
    print("total transactions count: " + str(total_tx))

    return blocks
コード例 #18
0
def test_block_height_type(monkeypatch):
    """
    test the type of the block height parameter
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_0, "height", "0")

    hblockchain.blockchain.clear()
    assert hblockchain.add_block(block_0) == False
コード例 #19
0
def test_difficulty_type(monkeypatch):
    """
    test difficulty bits has the wrong type"
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setitem(block_0, "difficulty_bits", "20")

    assert hblockchain.add_block(block_0) == False
コード例 #20
0
def test_missing_nonce(monkeypatch):
    """
    test for a missing nonce
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setitem(block_0, "nonce", "")

    assert hblockchain.add_block(block_0) == False
コード例 #21
0
def test_bad_timestamp_type(monkeypatch):
    """
    test for a bad timestamp type
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setitem(block_1, "timestamp", "12345")

    assert hblockchain.add_block(block_1) == False
コード例 #22
0
def test_bad_nonce(monkeypatch):
    """
    test for a negative nonce
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setitem(block_1, "nonce", -1)

    assert hblockchain.add_block(block_1) == False
コード例 #23
0
def test_negative_timestamp(monkeypatch):
    """
    test for a negative timestamp
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setitem(block_0, "timestamp", -2)

    assert hblockchain.add_block(block_0) == False
コード例 #24
0
def test_version_bad(monkeypatch):
    """
    test for an unknown version number
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setitem(block_1, "version", -1)

    assert hblockchain.add_block(block_1) == False
コード例 #25
0
def test_missing_difficulty_bit(monkeypatch):
    """
    test for missing difficulty bits
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    monkeypatch.setattr(blk_index, "put_index", lambda x, y: None)
    monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True)
    monkeypatch.setitem(block_1, "difficulty_bits", '')

    assert hblockchain.add_block(block_1) == False
コード例 #26
0
def test_invalid_previous_hash(monkeypatch):
    """
    test block's prevblockhash is invalid
    """
    hblockchain.blockchain.clear()

    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_2, "prevblockhash", \
        "188a1fd32a1f83af966b31ca781d71c40f756a3dc2a7ac44ce89734d2186f632")

    hblockchain.blockchain.clear()
    assert hblockchain.add_block(block_0) == True
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg1'))
    assert hblockchain.add_block(block_1) == True

    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg2'))
    assert hblockchain.add_block(block_2) == False
    hblockchain.blockchain.clear()
コード例 #27
0
def test_genesis_block_prev_hash(monkeypatch):
    """
    test that the previous block hash for the genesis block is empty
    """
    hblockchain.blockchain.clear()
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_0, "height", 0)
    monkeypatch.setitem(block_0, "prevblockhash", rcrypt.make_uuid())

    assert len(hblockchain.blockchain) == 0
    assert hblockchain.add_block(block_0) == False
コード例 #28
0
def test_block_size(monkeypatch):
    """
    The block size must be less than hconfig["MAX_BLOCKS"]
    """
    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    arry = []
    filler = "0" * 2000000
    arry.append(filler)
    monkeypatch.setitem(block_0, "tx", arry)
    hblockchain.blockchain.clear()
    assert hblockchain.add_block(block_0) == False
コード例 #29
0
def test_read_second_block(monkeypatch):
    """
    test reading the second block from the blockchain
    """
    hblockchain.blockchain.clear()
    assert len(hblockchain.blockchain) == 0

    monkeypatch.setattr(hblockchain, "merkle_root",
                        lambda x, y: rcrypt.make_SHA256_hash('msg0'))
    monkeypatch.setitem(block_1, "prevblockhash",
                        hblockchain.blockheader_hash(block_0))

    ret = hblockchain.add_block(block_0)
    assert ret == True
    monkeypatch.setattr(hblockchain, "merkle_root", lambda x, y: \
        rcrypt.make_SHA256_hash('msg1'))
    ret = hblockchain.add_block(block_1)
    assert ret == True
    block = hblockchain.read_block(1)
    assert block != False
    hblockchain.blockchain.clear()
def make_blocks(num_blocks):

    ctr = 0
    blocks = []

    global unspent_fragments
    unspent_fragments.clear()
    hblockchain.blockchain.clear()

    while ctr < num_blocks:
        block = {
            "prevblockhash": "",
            "version": "1",
            "timestamp": int(time.time()),
            "difficulty_bits": 20,
            "nonce": 0,
            "merkle_root": "",
            "height": ctr,
            "tx": []
        }

        # make a random number of transactions for this block
        # genesis block is ctr == 0
        if ctr == 0: num_transactions = 200
        else:
            num_transactions = secrets.randbelow(50)
            if num_transactions == 0: num_transactions = 40
            num_transactions = 2

        txctr = 0
        while txctr < num_transactions:
            if ctr > 0 and txctr == 0: is_coinbase = True
            else: is_coinbase = False

            trx = make_random_transaction(ctr, is_coinbase)
            assert trx != False
            block["tx"].append(trx)
            txctr += 1

        if ctr > 0:
            block["prevblockhash"] = hblockchain.blockheader_hash(
                hblockchain.blockchain[ctr - 1])

        ret = hblockchain.merkle_root(block["tx"], True)
        assert ret != False
        block["merkle_root"] = ret

        ret = hblockchain.add_block(block)
        assert ret == True
        blocks.append(block)
        ctr += 1

    return blocks