예제 #1
0
def test_save_min_gas_price():
    testdb = MemoryDB()
    chain = TestnetChain.from_genesis(testdb, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(), TESTNET_GENESIS_PARAMS,TESTNET_GENESIS_STATE, TESTNET_GENESIS_PRIVATE_KEY)

    expected = [[1,1]]
    chain.min_gas_db.save_historical_minimum_gas_price(expected)
    loaded_min_gas_price = chain.min_gas_db.load_historical_minimum_gas_price()
    assert(expected == loaded_min_gas_price)

    #
    # preserve decimal
    #
    expected = [[1, 1.23]]
    chain.min_gas_db.save_historical_minimum_gas_price(expected)
    loaded_min_gas_price = chain.min_gas_db.load_historical_minimum_gas_price(return_int=False)
    assert (expected == loaded_min_gas_price)

    #
    # appending
    #

    chain.min_gas_db.save_historical_minimum_gas_price([])
    chain.min_gas_db.append_historical_min_gas_price_now(1.23)
    loaded_min_gas_price = chain.min_gas_db.load_historical_minimum_gas_price(return_int=False)
    assert(loaded_min_gas_price[0][1] == 1.23)
예제 #2
0
def test_save_single_historical_root_hash():
    testdb1 = MemoryDB()
    chain = TestnetChain.from_genesis(
        testdb1, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(),
        TESTNET_GENESIS_PARAMS, TESTNET_GENESIS_STATE)

    chain.chain_head_db.initialize_historical_root_hashes(
        test_hashes[0], test_timestamps[0])

    # make sure it doesn't duplicate if saved the same timestamp
    chain.chain_head_db.save_single_historical_root_hash(
        test_hashes[1], test_timestamps[0])

    historical_root_hashes = chain.chain_head_db.get_historical_root_hashes()

    assert (historical_root_hashes == [[test_timestamps[0], test_hashes[1]]])

    # Make sure it saves when different timestamp.
    chain.chain_head_db.save_single_historical_root_hash(
        test_hashes[1], test_timestamps[1])

    historical_root_hashes = chain.chain_head_db.get_historical_root_hashes()

    assert (historical_root_hashes == [[test_timestamps[1], test_hashes[1]],
                                       [test_timestamps[0], test_hashes[1]]])
예제 #3
0
async def test_consensus_match_sync_4(request, event_loop):
    '''
    Blockchain databases of client and server match up to a point within the consensus match stage, but there are additional
    blocks in the server's db after that time.
    :param request:
    :param event_loop:
    :return:
    '''

    genesis_time = int(time.time() / 1000) * 1000 - 1000 * 900
    equal_to_time = int(time.time() / 1000) * 1000 - 1000 * 890
    new_blocks_start_time = int(time.time() / 1000) * 1000 - 1000 * 25
    new_blocks_end_time = int(time.time() / 1000) * 1000 - 1000 * 3

    server_db = get_random_blockchain_to_time(genesis_time, equal_to_time)
    client_db = MemoryDB(kv_store=server_db.kv_store.copy())

    add_random_transactions_to_db_for_time_window(server_db, equal_to_time,
                                                  equal_to_time + 1000 * 5)

    add_random_transactions_to_db_for_time_window(server_db,
                                                  new_blocks_start_time,
                                                  new_blocks_end_time)

    client_node = MainnetChain(
        client_db, GENESIS_PRIVATE_KEY.public_key.to_canonical_address())
    client_node.chaindb.initialize_historical_minimum_gas_price_at_genesis(
        min_gas_price=1, net_tpc_cap=100, tpc=1)

    await _test_sync_with_variable_sync_parameters(
        request, event_loop, client_db, server_db,
        ensure_blockchain_databases_identical)
예제 #4
0
def get_random_long_time_blockchain_db(length_in_centiseconds=50):
    testdb1 = MemoryDB()
    create_blockchain_database_for_exceeding_tpc_cap(testdb1,
                                                     1,
                                                     length_in_centiseconds,
                                                     use_real_genesis=True)
    return testdb1
예제 #5
0
def get_random_blockchain_to_time(start_time, end_time):
    testdb1 = MemoryDB()
    create_random_blockchain_database_to_time(testdb1,
                                              start_time,
                                              end_time,
                                              tx_per_1000_seconds=1)
    return testdb1
예제 #6
0
async def test_fast_sync_5(request, event_loop):
    '''
    Blockchain databases of client and server match up to a point before chronological block windows starts, but there are additional
    blocks in the server's db after that time.
    :param request:
    :param event_loop:
    :return:
    '''
    for i in range(5):
        genesis_time = int(time.time() / 1000) * 1000 - 1000 * 1100
        equal_to_time = int(time.time() / 1000) * 1000 - 1000 * 1095
        new_blocks_start_time = int(time.time() / 1000) * 1000 - 1000 * 25
        new_blocks_end_time = int(time.time() / 1000) * 1000 - 1000 * 3

        server_db = get_random_blockchain_to_time(genesis_time, equal_to_time)
        client_db = MemoryDB(kv_store=server_db.kv_store.copy())

        add_random_transactions_to_db_for_time_window(client_db, equal_to_time,
                                                      equal_to_time + 1000 * 5)

        add_random_transactions_to_db_for_time_window(client_db,
                                                      new_blocks_start_time,
                                                      new_blocks_end_time)

        client_node = TestnetChain(
            client_db,
            TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address())
        client_node.min_gas_db.initialize_historical_minimum_gas_price_at_genesis(
            min_gas_price=1, net_tpc_cap=100)

        await _test_sync_with_variable_sync_parameters(
            request, event_loop, client_db, server_db,
            ensure_blockchain_databases_identical)
예제 #7
0
def create_block_params():
    from hvm.chains.mainnet import (
        MAINNET_TPC_CAP_TEST_GENESIS_PARAMS,
        MAINNET_TPC_CAP_TEST_GENESIS_STATE,
        TPC_CAP_TEST_GENESIS_PRIVATE_KEY,
    )

    db = MemoryDB()
    chain = MainnetChain.from_genesis(
        db,
        TPC_CAP_TEST_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(),
        MAINNET_TPC_CAP_TEST_GENESIS_PARAMS,
        MAINNET_TPC_CAP_TEST_GENESIS_STATE,
        private_key=TPC_CAP_TEST_GENESIS_PRIVATE_KEY)

    receiver_privkey = keys.PrivateKey(random_private_keys[0])

    chain.create_and_sign_transaction_for_queue_block(
        gas_price=0x01,
        gas=0x0c3500,
        to=receiver_privkey.public_key.to_canonical_address(),
        value=1000,
        data=b"",
        v=0,
        r=0,
        s=0)

    imported_block = chain.import_current_queue_block()

    block_dict = imported_block.to_dict()
    print(block_dict)


# create_block_params()
# sys.exit()
예제 #8
0
def main():

    base_db = LevelDB(test_directory / "leveldb_multiprocess3")
    base_db = MemoryDB()
    database_server_process = ctx.Process(
        target=run_database_process,
        args=(base_db, ),
    )
    print('1')
    # start the processes
    database_server_process.start()
    print('2')
    try:
        print('3')
        wait_for_ipc(db_ipc_path)
    except TimeoutError as e:
        print("Timeout when starting db process")

    db_manager = create_db_manager(db_ipc_path)
    db_manager.connect()

    mp_db = db_manager.get_db()

    db = LevelDB(test_directory / "leveldb")
    speed_test(db)
    speed_test(mp_db)

    database_server_process.join()
예제 #9
0
 def __init__(self,
              event_bus: Endpoint,
              token: CancelToken = None,
              loop: asyncio.AbstractEventLoop = None) -> None:
     super().__init__(token=token, loop=loop)
     self.db = MemoryDB()
     self.event_bus = event_bus
예제 #10
0
def _test_update_current_network_tpc_capability_2():
    testdb = MemoryDB()
    chain = TestnetChain.from_genesis(testdb, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(), TESTNET_GENESIS_PARAMS,TESTNET_GENESIS_STATE, TESTNET_GENESIS_PRIVATE_KEY)

    while True:

        chain.min_gas_db.append_transaction_count_to_historical_tx_per_decisecond_from_imported(1000)
        chain.update_current_network_tpc_capability(7000, True)
        time.sleep(10)
예제 #11
0
def db(request):
    base_db = MemoryDB()
    if request.param is JournalDB:
        return JournalDB(base_db)
    elif request.param is BatchDB:
        return BatchDB(base_db)
    elif request.param is MemoryDB:
        return base_db
    else:
        raise Exception("Invariant")
예제 #12
0
def test_last_time_PID_ran():
    testdb = MemoryDB()
    chain = TestnetChain.from_genesis(testdb, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(), TESTNET_GENESIS_PARAMS,TESTNET_GENESIS_STATE, TESTNET_GENESIS_PRIVATE_KEY)

    chain.min_gas_db.save_now_as_last_min_gas_price_PID_update()

    time.sleep(1)

    time_since_set = chain.min_gas_db.get_time_since_last_min_gas_price_PID_update()

    assert(time_since_set == 1)
예제 #13
0
def test_root_hash_backup():
    testdb1 = MemoryDB()

    chain = TestnetChain.from_genesis(
        testdb1, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(),
        TESTNET_GENESIS_PARAMS, TESTNET_GENESIS_STATE,
        TESTNET_GENESIS_PRIVATE_KEY)

    #
    # Auto delete old ones
    #
    root_hash_backup = [[
        int(time.time() - AMOUNT_OF_TIME_TO_KEEP_CHAIN_HEAD_ROOT_HASH_BACKUP),
        ZERO_HASH32
    ]]
    chain.chain_head_db.save_root_hash_backup(root_hash_backup)

    expected_root_hash_backup = []
    expected_root_hash_backup.append(
        [int(time.time()), chain.chain_head_db.root_hash])
    chain.chain_head_db.save_current_root_hash_to_backup()

    chain.create_and_sign_transaction_for_queue_block(
        gas_price=1,
        gas=21000,
        to=RECEIVER.public_key.to_canonical_address(),
        value=1,
        data=b"",
        v=0,
        r=0,
        s=0)

    chain.import_current_queue_block()

    time.sleep(1)

    expected_root_hash_backup.append(
        [int(time.time()), chain.chain_head_db.root_hash])
    chain.chain_head_db.save_current_root_hash_to_backup()

    chain = TestnetChain(testdb1, RECEIVER.public_key.to_canonical_address(),
                         RECEIVER3)
    chain.populate_queue_block_with_receive_tx()
    chain.import_current_queue_block()

    time.sleep(1)

    expected_root_hash_backup.append(
        [int(time.time()), chain.chain_head_db.root_hash])
    chain.chain_head_db.save_current_root_hash_to_backup()

    root_hash_backup = chain.chain_head_db.load_root_hash_backup()

    assert (root_hash_backup == expected_root_hash_backup)
예제 #14
0
def _test_aggressive_min_gas_price_pid():

    wanted_txpc = 3000
    wanted_txpd = wanted_txpc/10
    wanted_txpd = 1
    historical_min_gas_price = [1]
    historical_txpd = [0]

    from pymouse import PyMouseEvent

    class event(PyMouseEvent):
        mouse_y = 0

        def __init__(self):
            super(event, self).__init__()

        def move(self, x, y):
            self.mouse_y = y


    e = event()
    e.capture = False
    e.daemon = False
    e.start()

    testdb1 = MemoryDB()

    chain = TestnetChain(testdb1, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(),TESTNET_GENESIS_PRIVATE_KEY)

    # assume we have a system we want to control in controlled_system
    limit = 100
    x = range(limit)

    for i in range(limit - 1):
        historical_txpd.append(e.mouse_y)
        # compute new ouput from the PID according to the systems current value
        print(historical_txpd[-2:], historical_min_gas_price[-1], wanted_txpd)
        min_gas_price = chain.min_gas_db._calculate_next_min_gas_price_pid(historical_txpd[-2:], historical_min_gas_price[-1], wanted_txpd)

        historical_min_gas_price.append(min_gas_price)

        print('v {}'.format(historical_txpd[-1]))
        print('min_gas_price {}'.format(min_gas_price))


        time.sleep(0.5)

    fig, axs = plt.subplots(2)
    axs[0].plot(x, historical_txpd)
    axs[1].plot(x, historical_min_gas_price)
    plt.show()
예제 #15
0
async def test_sparse_sync_2(request, event_loop):
    '''
    Blockchain databases of client and server match up to a point within the consensus match stage, but there are additional
    blocks in the server's db after that time.
    :param request:
    :param event_loop:
    :return:
    '''

    genesis_time = int(time.time() / 1000) * 1000 - 1000 * 900
    equal_to_time = int(time.time() / 1000) * 1000 - 1000 * 890

    server_db = get_random_blockchain_to_time(genesis_time, equal_to_time)
    client_db = MemoryDB(kv_store=server_db.kv_store.copy())

    add_random_transactions_to_db_for_time_window(server_db, equal_to_time,
                                                  equal_to_time + 1000 * 5)

    tx_list = [[
        TESTNET_GENESIS_PRIVATE_KEY, RECEIVER, 100,
        int(time.time() / 1000) * 1000 - 1000 * 800
    ],
               [
                   TESTNET_GENESIS_PRIVATE_KEY, RECEIVER, 100,
                   int(time.time() / 1000) * 1000 - 1000 * 700
               ],
               [
                   TESTNET_GENESIS_PRIVATE_KEY, RECEIVER, 100,
                   int(time.time() / 1000) * 1000 - 1000 * 100
               ],
               [
                   TESTNET_GENESIS_PRIVATE_KEY, RECEIVER, 100,
                   int(time.time() / 1000) * 1000 - 1000 * 5
               ],
               [
                   TESTNET_GENESIS_PRIVATE_KEY, RECEIVER, 100,
                   int(time.time() / 1000) * 1000 - 1000 * 1
               ]]

    add_transactions_to_blockchain_db(client_db, tx_list)

    client_node = TestnetChain(
        client_db,
        TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address())
    client_node.min_gas_db.initialize_historical_minimum_gas_price_at_genesis(
        min_gas_price=1, net_tpc_cap=100)

    await _test_sync_with_variable_sync_parameters(
        request, event_loop, client_db, server_db,
        ensure_blockchain_databases_identical)
예제 #16
0
def test_update_current_network_tpc_capability():
    testdb = MemoryDB()
    chain = TestnetChain.from_genesis(testdb, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(), TESTNET_GENESIS_PARAMS,TESTNET_GENESIS_STATE, TESTNET_GENESIS_PRIVATE_KEY)

    chain.create_and_sign_transaction_for_queue_block(
        gas_price=1,
        gas=21000,
        to=RECEIVER.public_key.to_canonical_address(),
        value=1,
        data=b"",
        v=0,
        r=0,
        s=0
    )

    chain.import_current_queue_block()

    #
    # On a fresh db
    #
    current_centisecond = int(time.time() / 100) * 100
    net_tpc_cap = 10
    chain.update_current_network_tpc_capability(net_tpc_cap, True)

    # Make sure it adds to 1) hist net tpc cap, 2) hist tpc, 3) hist min_gas_price
    hist_net_tpc_cap = chain.min_gas_db.load_historical_network_tpc_capability()
    hist_gas_price = chain.min_gas_db.load_historical_minimum_gas_price()

    assert(hist_net_tpc_cap == [[current_centisecond, net_tpc_cap]])
    assert(hist_gas_price == [[current_centisecond, 1]])

    #
    # On a normal db
    #
    time.sleep(1)

    data = [[current_centisecond-100, 10],[current_centisecond, 20]]
    chain.min_gas_db.save_historical_minimum_gas_price(data)
    chain.min_gas_db.save_historical_network_tpc_capability(data)

    current_centisecond = int(time.time() / 100) * 100

    chain.update_current_network_tpc_capability(net_tpc_cap, True)

    # Make sure it adds to 1) hist net tpc cap, 2) hist tpc, 3) hist min_gas_price
    hist_net_tpc_cap = chain.min_gas_db.load_historical_network_tpc_capability()
    hist_gas_price = chain.min_gas_db.load_historical_minimum_gas_price()

    assert (hist_net_tpc_cap == [[current_centisecond-100, 10],[current_centisecond, net_tpc_cap]])
    assert (len(hist_gas_price) == 2)
예제 #17
0
def create_new_genesis_params_and_state():
    #
    # GENESIS STATE, HEADER PARAMS
    #

    new_genesis_private_key = genesis_private_key
    print("Ceating new genesis params and state for genesis wallet address:")
    print(new_genesis_private_key.public_key.to_canonical_address())

    total_supply = 350000000 * 10**18
    new_mainnet_genesis_params = {
        'chain_address':
        new_genesis_private_key.public_key.to_canonical_address(),
        'parent_hash': constants.GENESIS_PARENT_HASH,
        'transaction_root': constants.BLANK_ROOT_HASH,
        'receive_transaction_root': constants.BLANK_ROOT_HASH,
        'receipt_root': constants.BLANK_ROOT_HASH,
        'bloom': 0,
        'block_number': constants.GENESIS_BLOCK_NUMBER,
        'gas_limit': constants.GENESIS_GAS_LIMIT,
        'gas_used': 0,
        'timestamp': 1556733839,
        'extra_data': constants.GENESIS_EXTRA_DATA,
        'reward_hash': constants.GENESIS_REWARD_HASH,
        'account_balance': total_supply,
    }

    new_genesis_state = {
        new_genesis_private_key.public_key.to_canonical_address(): {
            "balance": total_supply,
            "code": b"",
            "nonce": 0,
            "storage": {}
        }
    }

    testdb1 = MemoryDB()
    genesis_header = MainnetChain.create_genesis_header(
        testdb1, new_genesis_private_key.public_key.to_canonical_address(),
        new_genesis_private_key, new_mainnet_genesis_params, new_genesis_state)

    print()
    print("New completed and signed genesis header params")
    parameter_names = list(dict(genesis_header._meta.fields).keys())
    header_params = {}
    for parameter_name in parameter_names:
        header_params[parameter_name] = getattr(genesis_header, parameter_name)
    print(header_params)
    print()
예제 #18
0
def create_dev_test_random_blockchain_database(base_db = None, num_iterations = None, timestamp = None):
    logger.debug("generating test blockchain db")
    if base_db == None:
        base_db = MemoryDB()

    if num_iterations == None:
        num_iterations = 5

    #initialize db
    sender_chain = import_genesis_block(base_db)
    # sender_chain.chaindb.initialize_historical_minimum_gas_price_at_genesis(min_gas_price = 1, net_tpc_cap=5)\
    MIN_TIME_BETWEEN_BLOCKS = sender_chain.get_vm(timestamp=Timestamp(int(time.time()))).min_time_between_blocks

    if timestamp == None:
        timestamp = int(time.time()) - num_iterations*MIN_TIME_BETWEEN_BLOCKS
    elif timestamp == "genesis":
        timestamp = MAINNET_GENESIS_PARAMS['timestamp'] + TIME_BETWEEN_HEAD_HASH_SAVE

    tx_list = []
    for i in range (num_iterations):
        if i == 0:
            numbers = [x for x in range(0, len(random_private_keys) - 1)]
            random_int = random.choice(numbers)
            privkey = GENESIS_PRIVATE_KEY
            receiver_privkey = keys.PrivateKey(random_private_keys[random_int])
        else:
            numbers = [x for x in range(0, len(random_private_keys) - 1) if x != random_int]
            random_int = random.choice(numbers)
            privkey = receiver_privkey
            receiver_privkey = keys.PrivateKey(random_private_keys[random_int])

        # random.shuffle(random_private_keys)
        # if i == 0:
        #     privkey = GENESIS_PRIVATE_KEY
        #     receiver_privkey = keys.PrivateKey(random_private_keys[0])
        # else:
        #     privkey = receiver_privkey
        #     receiver_privkey = keys.PrivateKey(random_private_keys[0])

        tx_timestamp = timestamp+i*MIN_TIME_BETWEEN_BLOCKS
        tx_list.append([privkey, receiver_privkey, 10000000 * 10 ** 18 - i * 100000 * 10 ** 18 - random.randint(0, 1000), tx_timestamp])

    print('ZZZZZZZZZZZ')
    pprint(tx_list)
    add_transactions_to_blockchain_db(base_db, tx_list)

    return base_db
 async def _test_trie_sync():
     src_trie, contents = make_random_trie(random)
     dest_db = FakeAsyncMemoryDB()
     nodes_cache = MemoryDB()
     scheduler = HexaryTrieSync(src_trie.root_hash, dest_db, nodes_cache,
                                TraceLogger("test"))
     requests = scheduler.next_batch()
     while len(requests) > 0:
         results = []
         for request in requests:
             results.append(
                 [request.node_key, src_trie.db[request.node_key]])
         await scheduler.process(results)
         requests = scheduler.next_batch(10)
     dest_trie = HexaryTrie(dest_db, src_trie.root_hash)
     for key, value in contents.items():
         assert dest_trie[key] == value
예제 #20
0
def test_initialize_historical_root_hashes():
    testdb1 = MemoryDB()
    chain = TestnetChain.from_genesis(
        testdb1, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(),
        TESTNET_GENESIS_PARAMS, TESTNET_GENESIS_STATE)

    chain.chain_head_db.initialize_historical_root_hashes(
        test_hashes[0], test_timestamps[0])
    historical_root_hashes = chain.chain_head_db.get_historical_root_hashes()

    assert (historical_root_hashes == [[test_timestamps[0], test_hashes[0]]])

    chain.chain_head_db.initialize_historical_root_hashes(
        test_hashes[1], test_timestamps[0])
    historical_root_hashes = chain.chain_head_db.get_historical_root_hashes()

    assert (historical_root_hashes == [[test_timestamps[0], test_hashes[1]]])
예제 #21
0
def test_append_historical_min_gas_price_now():

    testdb = MemoryDB()
    chain = TestnetChain.from_genesis(testdb, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(), TESTNET_GENESIS_PARAMS,TESTNET_GENESIS_STATE, TESTNET_GENESIS_PRIVATE_KEY)

    #
    # starting with nothing saved
    #
    expected = []
    min_gas_price = 12
    current_centisecond = int(time.time() / 100) * 100
    expected.append([current_centisecond, min_gas_price])
    chain.min_gas_db.append_historical_min_gas_price_now(min_gas_price)

    saved_min_gas_price = chain.min_gas_db.load_historical_minimum_gas_price()

    assert(saved_min_gas_price == expected)

    #
    # Delete ones newer than now
    #
    current_centisecond = int(time.time() / 100) * 100

    chain.min_gas_db.save_historical_minimum_gas_price([[current_centisecond,0],[current_centisecond+100,2]])

    expected = []
    expected.append([current_centisecond, min_gas_price])
    chain.min_gas_db.append_historical_min_gas_price_now(min_gas_price)

    saved_min_gas_price = chain.min_gas_db.load_historical_minimum_gas_price()

    assert (saved_min_gas_price == expected)

    #
    # Append to already existing list
    #
    current_centisecond = int(time.time() / 100) * 100
    chain.min_gas_db.save_historical_minimum_gas_price([[current_centisecond-100, 0], [current_centisecond, 2]])


    expected = [[current_centisecond-100, 0], [current_centisecond, min_gas_price]]
    chain.min_gas_db.append_historical_min_gas_price_now(min_gas_price)
    saved_min_gas_price = chain.min_gas_db.load_historical_minimum_gas_price()

    assert (saved_min_gas_price == expected)
예제 #22
0
async def test_fast_sync_2(request, event_loop):
    genesis_time = int(time.time() / 1000) * 1000 - 1000 * 1100
    equal_to_time = int(time.time() / 1000) * 1000 - 1000 * 1095

    server_db = get_random_blockchain_to_time(genesis_time, equal_to_time)
    client_db = MemoryDB(kv_store=server_db.kv_store.copy())

    add_random_transactions_to_db_for_time_window(client_db, equal_to_time,
                                                  equal_to_time + 1000 * 5)

    node_2 = MainnetChain(
        client_db, GENESIS_PRIVATE_KEY.public_key.to_canonical_address())
    node_2.chaindb.initialize_historical_minimum_gas_price_at_genesis(
        min_gas_price=1, net_tpc_cap=100, tpc=1)

    await _test_sync_with_variable_sync_parameters(
        request, event_loop, client_db, server_db,
        ensure_blockchain_databases_identical, FAST_SYNC_STAGE_ID)
def create_reward_test_blockchain_database():
    testdb = MemoryDB()

    chain = MainnetChain(testdb, GENESIS_PRIVATE_KEY.public_key.to_canonical_address(), GENESIS_PRIVATE_KEY)
    coin_mature_time = chain.get_vm(timestamp=Timestamp(int(time.time()))).consensus_db.coin_mature_time_for_staking
    min_time_between_blocks = chain.get_vm(timestamp=Timestamp(int(time.time()))).min_time_between_blocks
    required_stake_for_reward_type_2_proof = chain.get_consensus_db(timestamp=Timestamp(int(time.time()))).required_stake_for_reward_type_2_proof


    now = int(time.time())
    start = now - max((coin_mature_time * 2), (min_time_between_blocks * 20))
    key_balance_dict = {}
    for i in range(10):
        key_balance_dict[private_keys[i]] = (
        required_stake_for_reward_type_2_proof, start + min_time_between_blocks * i)

    create_dev_fixed_blockchain_database(testdb, key_balance_dict)
    return testdb
예제 #24
0
def copy(chain):
    """
    Make a copy of the chain at the given state.  Actions performed on the
    resulting chain will not affect the original chain.
    """
    if not isinstance(chain, MiningChain):
        raise ValidationError("`at_block_number` may only be used with 'MiningChain")
    base_db = chain.chaindb.db
    if not isinstance(base_db, AtomicDB):
        raise ValidationError("Unsupported database type: {0}".format(type(base_db)))

    if isinstance(base_db.wrapped_db, MemoryDB):
        db = AtomicDB(MemoryDB(base_db.wrapped_db.kv_store.copy()))
    else:
        raise ValidationError("Unsupported wrapped database: {0}".format(type(base_db.wrapped_db)))

    chain_copy = type(chain)(db, chain.header)
    return chain_copy
def make_random_state(n):
    raw_db = MemoryDB()
    account_db = AccountDB(raw_db)
    contents = {}
    for _ in range(n):
        addr = os.urandom(20)
        account_db.touch_account(addr)
        balance = random.randint(0, 10000)
        account_db.set_balance(addr, balance)
        nonce = random.randint(0, 10000)
        account_db.set_nonce(addr, nonce)
        storage = random.randint(0, 10000)
        account_db.set_storage(addr, 0, storage)
        code = b'not-real-code'
        account_db.set_code(addr, code)
        contents[addr] = (balance, nonce, storage, code)
    account_db.persist()
    return raw_db, account_db.state_root, contents
예제 #26
0
def create_new_genesis_params_and_state(private_key, total_supply = 100000000 * 10 ** 18, timestamp = int(time.time())):
    print("CREATING GENESIS BLOCK WITH TOTAL SUPPLY = ", total_supply)
    new_genesis_private_key = private_key

    print("Ceating new genesis params and state for genesis wallet address:")
    print(new_genesis_private_key.public_key.to_canonical_address())
    new_testnet_genesis_params = {
        'chain_address': new_genesis_private_key.public_key.to_canonical_address(),
        'parent_hash': constants.GENESIS_PARENT_HASH,
        'transaction_root': constants.BLANK_ROOT_HASH,
        'receive_transaction_root': constants.BLANK_ROOT_HASH,
        'receipt_root': constants.BLANK_ROOT_HASH,
        'bloom': 0,
        'block_number': constants.GENESIS_BLOCK_NUMBER,
        'gas_limit': constants.GENESIS_GAS_LIMIT,
        'gas_used': 0,
        'timestamp': timestamp,
        'extra_data': constants.GENESIS_EXTRA_DATA,
        'reward_hash': constants.GENESIS_REWARD_HASH,
        'account_balance': total_supply,
    }

    new_genesis_state = {
        new_genesis_private_key.public_key.to_canonical_address(): {
            "balance": total_supply,
            "code": b"",
            "nonce": 0,
            "storage": {}
        }
    }

    testdb1 = MemoryDB()
    genesis_header = TestnetChain.create_genesis_header(testdb1,
                                                        new_genesis_private_key.public_key.to_canonical_address(),
                                                        new_genesis_private_key, new_testnet_genesis_params,
                                                        new_genesis_state)


    parameter_names = list(dict(genesis_header._meta.fields).keys())
    header_params = {}
    for parameter_name in parameter_names:
        header_params[parameter_name] = getattr(genesis_header, parameter_name)
    return header_params, new_genesis_state
async def test_state_sync():
    raw_db, state_root, contents = make_random_state(1000)
    dest_db = FakeAsyncMemoryDB()
    nodes_cache = MemoryDB()
    scheduler = StateSync(state_root, dest_db, nodes_cache,
                          TraceLogger('test'))
    requests = scheduler.next_batch(10)
    while requests:
        results = []
        for request in requests:
            results.append([request.node_key, raw_db[request.node_key]])
        await scheduler.process(results)
        requests = scheduler.next_batch(10)

    result_account_db = AccountDB(dest_db, state_root)
    for addr, account_data in contents.items():
        balance, nonce, storage, code = account_data
        assert result_account_db.get_balance(addr) == balance
        assert result_account_db.get_nonce(addr) == nonce
        assert result_account_db.get_storage(addr, 0) == storage
        assert result_account_db.get_code(addr) == code
예제 #28
0
async def test_additive_sync_4(request, event_loop):
    '''
    Blockchain databases of client and server match up to a point within the consensus match stage, but there are additional
    blocks in the server's db after that time.
    :param request:
    :param event_loop:
    :return:
    '''

    genesis_time = int(time.time() / 1000) * 1000 - 1000 * 25
    equal_to_time = int(time.time() / 1000) * 1000 - 1000 * 2

    server_db = get_random_blockchain_to_time(genesis_time, equal_to_time)
    client_db = MemoryDB(kv_store=server_db.kv_store.copy())

    tx_list = [[GENESIS_PRIVATE_KEY, RECEIVER, 100,
                int(time.time() - 2000)],
               [GENESIS_PRIVATE_KEY, RECEIVER, 100,
                int(time.time() - 1500)],
               [GENESIS_PRIVATE_KEY, RECEIVER, 100,
                int(time.time() - 1000)]]

    add_transactions_to_blockchain_db(server_db, tx_list)

    client_node = MainnetChain(
        client_db, GENESIS_PRIVATE_KEY.public_key.to_canonical_address())
    client_node.chaindb.initialize_historical_minimum_gas_price_at_genesis(
        min_gas_price=1, net_tpc_cap=100, tpc=1)
    server_node = MainnetChain(
        server_db, GENESIS_PRIVATE_KEY.public_key.to_canonical_address())
    server_node.chaindb.initialize_historical_minimum_gas_price_at_genesis(
        min_gas_price=1, net_tpc_cap=100, tpc=1)

    await _test_sync_with_variable_sync_parameters(
        request, event_loop, client_db, server_db,
        ensure_blockchain_databases_identical)
def memory_db():
    return MemoryDB()
예제 #30
0
def get_predefined_blockchain_db(instance_number):
    testdb1 = MemoryDB()
    create_predefined_blockchain_database(testdb1, instance=instance_number)
    return testdb1