def run_test(self):
        file_dir = os.path.dirname(os.path.realpath(__file__))

        storage_contract = get_contract_instance(
            abi_file=os.path.join(file_dir, "contracts/simple_storage.abi"),
            bytecode_file=os.path.join(file_dir,
                                       "contracts/simple_storage.dat"),
        )

        start_p2p_connection(self.nodes)

        self.log.info("Initializing contract")
        genesis_addr = self.genesis_addr
        self.log.info("genesis_addr={}".format(encode_hex_0x(genesis_addr)))

        block_gen_thread = BlockGenThread(self.nodes,
                                          self.log,
                                          interval_fixed=1)
        block_gen_thread.start()

        client = RpcClient(self.nodes[0])

        tx = client.new_tx(value=int(0.625 * CFX) + GDrip,
                           receiver=self.eth_hex_addr,
                           nonce=self.get_nonce(genesis_addr))
        client.send_tx(tx, True)
        assert_equal(client.get_balance(self.eth_hex_addr),
                     int(0.625 * CFX) + GDrip)

        # deploy contract
        self.log.info("Deploying contract")
        tx = self.call_contract_function(
            contract=storage_contract,
            name="constructor",
            args=[],
            sender_key=self.eth_priv_key,
            eth_tx=True,
        )

        wait_until(lambda: self.nodes[1].tx_inspect(tx.hash_hex())['exist'],
                   timeout=10)
        block_gen_thread.stop()

        client.generate_blocks(40)
        block_hash = client.generate_custom_block(client.best_block_hash(), [],
                                                  [tx])
        wait_until(lambda: client.best_block_hash() == block_hash, timeout=10)
        client.generate_blocks(40)

        BlockGenThread(self.nodes, self.log, interval_fixed=1).start()

        receipt = self.wait_for_tx([tx])[0]

        assert_greater_than_or_equal(int(receipt['epochNumber'], 0), 80)

        self.log.info("All test done")
Beispiel #2
0
    def run_test(self):
        block_number = 8000

        client = RpcClient(self.nodes[0])
        start = time.time()
        genesis = client.best_block_hash()
        parent = genesis
        # generate main chain
        for i in range(block_number):
            parent = client.generate_block_with_parent(parent, referee=[])
            if i % 100 == 0:
                self.log.info("generate %d blocks", i)
        prev_end = parent
        now = time.time()
        self.log.info("Time to process main chain of %d blocks: %f",
                      block_number, now - start)
        start = now
        # process fork
        parent = genesis
        for i in range(block_number + 1):
            parent = client.generate_block_with_parent(parent, referee=[])
            self.log.debug("block hash: %s", parent)
            if i % 100 == 0:
                self.log.info("generate %d blocks", i)
        now = time.time()
        self.log.info("Time to process fork of %d blocks: %f",
                      block_number + 1, now - start)
        # switch back to main chain
        parent = prev_end
        start = time.time()
        for i in range(2):
            parent = client.generate_block_with_parent(parent, referee=[])
        now = time.time()
        self.log.info("Time to switch back %f", now - start)
Beispiel #3
0
    def run_test(self):
        client0 = RpcClient(self.nodes[0])
        genesis = client0.best_block_hash()
        # print(client0.block_by_hash(genesis))

        a = self.nodes[0].generatefixedblock(genesis, [], 0, False,
                                             INITIAL_DIFFICULTY)
        block_a = client0.block_by_hash(a)
        assert (block_a['stable'] == True)
        b = self.nodes[0].generatefixedblock(a, [], 0, False,
                                             INITIAL_DIFFICULTY)
        c = self.nodes[0].generatefixedblock(genesis, [], 0, False,
                                             INITIAL_DIFFICULTY)
        d = self.nodes[0].generatefixedblock(c, [], 0, False,
                                             INITIAL_DIFFICULTY)
        if a > c:
            e = self.nodes[0].generatefixedblock(b, [d], 0, True,
                                                 INITIAL_DIFFICULTY)
        else:
            e = self.nodes[0].generatefixedblock(d, [b], 0, True,
                                                 INITIAL_DIFFICULTY)
        block_e = client0.block_by_hash(e)
        assert (block_e['stable'] == False)
Beispiel #4
0
    def run_test(self):
        client0 = RpcClient(self.nodes[0])
        client1 = RpcClient(self.nodes[1])
        genesis = client0.best_block_hash()

        self.log.info("Generating two initial blocks")

        a1 = client0.generate_block()
        a2 = client0.generate_block()
        block_a2 = client0.block_by_hash(a2)
        assert(int(block_a2['height'], 16) == 2)

        self.log.info("Generating two invalid blocks")

        invalid = self.nodes[0].generatefixedblock(genesis, [a2], 0, False, INITIAL_DIFFICULTY)
        invalid2 = self.nodes[0].generatefixedblock(a2, [invalid], 0, False, INITIAL_DIFFICULTY)

        self.log.info("Sync two nodes")
        connect_nodes(self.nodes, 0, 1)
        wait_until(lambda: self.nodes[1].getblockcount() >= 3, timeout = 10)
        self.log.info("Node0 block count " + str(self.nodes[0].getblockcount()))
        self.log.info("Node1 block count " + str(self.nodes[1].getblockcount()))

        self.log.info("Generating a block without referencing partial invalid blocks")

        b1 = client1.generate_block()
        block_b1 = client1.block_by_hash(b1)
        assert(block_b1['parentHash'] == a2)
        assert(len(block_b1['refereeHashes']) == 0)

        self.log.info("Sync two nodes")
        connect_nodes(self.nodes, 1, 0)
        wait_until(lambda: self.nodes[0].getblockcount() >= 6, timeout = 40)
        wait_until(lambda: self.nodes[1].getblockcount() >= 4, timeout = 40)

        timer_cnt = 0
        diff = int(block_b1['difficulty'], 16)
        pow_qual = int(block_b1['powQuality'], 16)
        if diff * TIMER_RATIO <= pow_qual:
            timer_cnt = 1

        self.log.info("Start timer tick " + str(timer_cnt))

        while timer_cnt < TIMER_BETA:
            a = client0.generate_block()
            self.log.info("Generated a block " + a)
            block_a = client0.block_by_hash(a)
            assert(len(block_a['refereeHashes']) == 0)
            diff = int(block_a['difficulty'], 16)
            pow_qual = int(block_a['powQuality'], 16)
            if diff * TIMER_RATIO <= pow_qual:
                timer_cnt += 1
                self.log.info("Timer increased to " + str(timer_cnt))

        self.log.info("Sync two nodes")
        connect_nodes(self.nodes, 0, 1)
        sync_blocks(self.nodes)

        self.log.info("Node1 generating a block to reference two partial invalid blocks")

        b2 = client1.generate_block()
        block_b2 = client1.block_by_hash(b2)
        assert(len(block_b2['refereeHashes']) > 0)
        assert(block_b2['refereeHashes'][0] == invalid2)

        self.log.info("Pass!")
Beispiel #5
0
    def run_test(self):
        genesis_key = default_config["GENESIS_PRI_KEY"]
        receiver_sk, _ = ec_random_keys()
        receiver_addr = priv_to_addr(receiver_sk)
        client = RpcClient(self.nodes[0])

        value = 100000000
        tx = create_transaction(pri_key=genesis_key,
                                receiver=receiver_addr,
                                value=value,
                                nonce=0,
                                gas_price=1,
                                epoch_height=0)
        client.send_tx(tx)
        block_gen_thread = BlockGenThread(self.nodes,
                                          self.log,
                                          interval_base=0.1)
        block_gen_thread.start()
        self.log.info(
            "Wait for the first transaction to go through with epoch_height = 0..."
        )
        wait_until(lambda: parse_as_int(self.nodes[0].cfx_getBalance(
            eth_utils.encode_hex(receiver_addr))) == value)
        self.log.info("Wait for generating more than 50 epochs")
        wait_until(lambda: parse_as_int(
            client.block_by_hash(client.best_block_hash())['height']) > 50)
        block_gen_thread.stop()
        self.log.info("Now block count:%d", self.nodes[0].getblockcount())

        tx = create_transaction(pri_key=genesis_key,
                                receiver=receiver_addr,
                                value=value,
                                nonce=1,
                                gas_price=1,
                                epoch_height=0)
        try:
            client.send_tx(tx)
            self.log.info("Bad transaction not rejected!")
            assert (False)
        except ReceivedErrorResponseError:
            self.log.info("Bad transaction rejected.")
        except:
            self.log.info("Unexpected error!")
            assert (False)
        assert (parse_as_int(self.nodes[0].cfx_getBalance(
            eth_utils.encode_hex(receiver_addr))) == value)

        epoch_height = parse_as_int(
            client.block_by_hash(client.best_block_hash())['height'])
        tx = create_transaction(pri_key=genesis_key,
                                receiver=receiver_addr,
                                value=value,
                                nonce=1,
                                gas_price=1,
                                epoch_height=epoch_height)
        client.send_tx(tx)
        block_gen_thread = BlockGenThread(self.nodes,
                                          self.log,
                                          interval_base=0.1)
        block_gen_thread.start()
        self.log.info(
            "Wait for the first transaction to go through with epoch_height = "
            + str(epoch_height) + "...")
        wait_until(lambda: parse_as_int(self.nodes[0].cfx_getBalance(
            eth_utils.encode_hex(receiver_addr))) == 2 * value)
        block_gen_thread.stop()
        self.log.info("Now block count:%d", self.nodes[0].getblockcount())
        self.log.info("Pass!")
    def _test_new_block(self):
        self.log.info("Test New Block")
        client = RpcClient(self.nodes[0])
        best_block = client.best_block_hash()
        best_epoch = client.epoch_number()
        new_block = create_block(decode_hex(best_block), best_epoch + 1)
        self.send_msg(NewBlock(block=new_block))
        wait_until(
            lambda: self.nodes[0].best_block_hash() == new_block.hash_hex())

        # Wrong payload
        self.nodes[0].p2p.send_protocol_packet(
            rlp.encode([0]) + int_to_bytes(NEW_BLOCK))
        time.sleep(1)
        assert_equal(self.nodes[0].best_block_hash(), new_block.hash_hex())
        assert_equal(self.nodes[0].getblockcount(), 3)
        self.reconnect(self.nodes[0])

        # Wrong-length parent hash
        invalid_block = create_block(parent_hash=b'', height=2)
        self.send_msg(NewBlock(block=invalid_block))
        time.sleep(1)
        assert_equal(self.nodes[0].best_block_hash(), new_block.hash_hex())
        assert_equal(self.nodes[0].getblockcount(), 3)
        self.reconnect(self.nodes[0])

        # Wrong-length author
        invalid_block = create_block(author=b'', height=2)
        self.send_msg(NewBlock(block=invalid_block))
        time.sleep(1)
        assert_equal(self.nodes[0].best_block_hash(), new_block.hash_hex())
        assert_equal(self.nodes[0].getblockcount(), 3)
        self.reconnect(self.nodes[0])

        # Wrong-length root
        invalid_block = create_block(deferred_state_root=b'',
                                     height=2,
                                     deferred_receipts_root=b'')
        self.send_msg(NewBlock(block=invalid_block))
        time.sleep(1)
        assert_equal(self.nodes[0].best_block_hash(), new_block.hash_hex())
        assert_equal(self.nodes[0].getblockcount(), 3)
        self.reconnect(self.nodes[0])

        # Nonexistent parent
        invalid_block = create_block(parent_hash=b'\x00' * 32, height=2)
        self.send_msg(NewBlock(block=invalid_block))
        time.sleep(1)
        assert_equal(self.nodes[0].best_block_hash(), new_block.hash_hex())
        assert_equal(self.nodes[0].getblockcount(), 3)
        self.reconnect(self.nodes[0])

        # Invalid height
        invalid_block = create_block(new_block.hash, 1)
        self.send_msg(NewBlock(block=invalid_block))
        time.sleep(1)
        assert_equal(self.nodes[0].best_block_hash(), new_block.hash_hex())
        assert_equal(self.nodes[0].getblockcount(), 3)
        self.reconnect(self.nodes[0])

        sync_blocks(self.nodes)

        # TODO Generate some random blocks that have wrong ref edges
        pass