Ejemplo n.º 1
0
def contract_creation_tx(
    shard_state,
    key,
    from_address,
    to_full_shard_key,
    bytecode,
    gas=100000,
    gas_token_id=None,
    transfer_token_id=None,
):
    if gas_token_id is None:
        gas_token_id = shard_state.env.quark_chain_config.genesis_token
    if transfer_token_id is None:
        transfer_token_id = shard_state.env.quark_chain_config.genesis_token
    evm_tx = EvmTransaction(
        nonce=shard_state.get_transaction_count(from_address.recipient),
        gasprice=1,
        startgas=gas,
        value=0,
        to=b"",
        data=bytes.fromhex(bytecode),
        from_full_shard_key=from_address.full_shard_key,
        to_full_shard_key=to_full_shard_key,
        network_id=shard_state.env.quark_chain_config.NETWORK_ID,
        gas_token_id=gas_token_id,
        transfer_token_id=transfer_token_id,
    )
    evm_tx.sign(key)
    return TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
Ejemplo n.º 2
0
def create_transfer_transaction(
    shard_state,
    key,
    from_address,
    to_address,
    value,
    gas=21000,  # transfer tx min gas
    gas_price=1,
    nonce=None,
    data=b"",
):
    """ Create an in-shard xfer tx
    """
    evm_tx = EvmTransaction(
        nonce=shard_state.get_transaction_count(from_address.recipient)
        if nonce is None else nonce,
        gasprice=gas_price,
        startgas=gas,
        to=to_address.recipient,
        value=value,
        data=data,
        from_full_shard_id=from_address.full_shard_id,
        to_full_shard_id=to_address.full_shard_id,
        network_id=shard_state.env.quark_chain_config.NETWORK_ID,
    )
    evm_tx.sign(key=key)
    return Transaction(in_list=[],
                       code=Code.create_evm_code(evm_tx),
                       out_list=[])
Ejemplo n.º 3
0
def create_transfer_transaction(
    shard_state,
    key,
    from_address,
    to_address,
    value,
    gas=21000,  # transfer tx min gas
    gas_price=1,
    nonce=None,
    data=b"",
    gas_token_id=None,
    transfer_token_id=None,
):
    if gas_token_id is None:
        gas_token_id = shard_state.env.quark_chain_config.genesis_token
    if transfer_token_id is None:
        transfer_token_id = shard_state.env.quark_chain_config.genesis_token
    """ Create an in-shard xfer tx
    """
    evm_tx = EvmTransaction(
        nonce=shard_state.get_transaction_count(from_address.recipient)
        if nonce is None else nonce,
        gasprice=gas_price,
        startgas=gas,
        to=to_address.recipient,
        value=value,
        data=data,
        from_full_shard_key=from_address.full_shard_key,
        to_full_shard_key=to_address.full_shard_key,
        network_id=shard_state.env.quark_chain_config.NETWORK_ID,
        gas_token_id=gas_token_id,
        transfer_token_id=transfer_token_id,
    )
    evm_tx.sign(key=key)
    return TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
Ejemplo n.º 4
0
    def create_transaction(self, account, nonce, x_shard_percent,
                           sample_evm_tx) -> Optional[Transaction]:
        shard_size = self.qkc_config.SHARD_SIZE
        shard_mask = shard_size - 1
        from_shard = self.shard_id

        # skip if from shard is specified and not matching current branch
        # FIXME: it's possible that clients want to specify '0x0' as the full shard ID, however it will not be supported
        if (sample_evm_tx.from_full_shard_id and
            (sample_evm_tx.from_full_shard_id & shard_mask) != from_shard):
            return None

        if sample_evm_tx.from_full_shard_id:
            from_full_shard_id = sample_evm_tx.from_full_shard_id
        else:
            from_full_shard_id = (account.address.full_shard_id & (~shard_mask)
                                  | from_shard)

        if not sample_evm_tx.to:
            to_address = random.choice(self.accounts).address
            recipient = to_address.recipient
            to_full_shard_id = to_address.full_shard_id & (
                ~shard_mask) | from_shard
        else:
            recipient = sample_evm_tx.to
            to_full_shard_id = from_full_shard_id

        if random.randint(1, 100) <= x_shard_percent:
            # x-shard tx
            to_shard = random.randint(0, self.qkc_config.SHARD_SIZE - 1)
            if to_shard == self.shard_id:
                to_shard = (to_shard + 1) % self.qkc_config.SHARD_SIZE
            to_full_shard_id = to_full_shard_id & (~shard_mask) | to_shard

        value = sample_evm_tx.value
        if not sample_evm_tx.data:
            value = random.randint(1, 100) * (10**15)

        gas_token_id = 1
        transfer_token_id = 1

        evm_tx = EvmTransaction(
            nonce=nonce,
            gasprice=sample_evm_tx.gasprice,
            startgas=sample_evm_tx.startgas,
            gas_token_id=gas_token_id,
            to=recipient,
            value=value,
            transfer_token_id=transfer_token_id,
            data=sample_evm_tx.data,
            from_full_shard_id=from_full_shard_id,
            to_full_shard_id=to_full_shard_id,
            network_id=self.qkc_config.NETWORK_ID,
        )
        evm_tx.sign(account.key)
        return Transaction(code=Code.create_evm_code(evm_tx))
Ejemplo n.º 5
0
    def test_sendTransaction(self):
        id1 = Identity.create_random_identity()
        acc1 = Address.create_from_identity(id1, full_shard_key=0)
        acc2 = Address.create_random_account(full_shard_key=1)

        with ClusterContext(
                1, acc1, small_coinbase=True) as clusters, jrpc_server_context(
                    clusters[0].master):
            slaves = clusters[0].slave_list
            master = clusters[0].master

            block = call_async(
                master.get_next_block_to_mine(address=acc2, branch_value=None))
            call_async(master.add_root_block(block))

            evm_tx = EvmTransaction(
                nonce=0,
                gasprice=6,
                startgas=30000,
                to=acc2.recipient,
                value=15,
                data=b"",
                from_full_shard_key=acc1.full_shard_key,
                to_full_shard_key=acc2.full_shard_key,
                network_id=slaves[0].env.quark_chain_config.NETWORK_ID,
                gas_token_id=master.env.quark_chain_config.genesis_token,
                transfer_token_id=master.env.quark_chain_config.genesis_token,
            )
            evm_tx.sign(id1.get_key())
            request = dict(
                to="0x" + acc2.recipient.hex(),
                gasPrice="0x6",
                gas=hex(30000),
                value="0xf",  # 15
                v=quantity_encoder(evm_tx.v),
                r=quantity_encoder(evm_tx.r),
                s=quantity_encoder(evm_tx.s),
                nonce="0x0",
                fromFullShardKey="0x00000000",
                toFullShardKey="0x00000001",
                network_id=hex(slaves[0].env.quark_chain_config.NETWORK_ID),
            )
            tx = TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
            response = send_request("sendTransaction", [request])

            self.assertEqual(response, "0x" + tx.get_hash().hex() + "00000000")
            state = clusters[0].get_shard_state(2 | 0)
            self.assertEqual(len(state.tx_queue), 1)
            self.assertEqual(
                state.tx_queue.pop_transaction(state.get_transaction_count),
                evm_tx)
def create_transaction(address, key, nonce, data, network_id) -> EvmTransaction:
    evm_tx = EvmTransaction(
        nonce=nonce,
        gasprice=1,
        startgas=1000000,
        to=b"",
        value=0,
        data=data,
        from_full_shard_key=address.full_shard_key,
        to_full_shard_key=address.full_shard_key,
        network_id=network_id,
    )
    evm_tx.sign(key)
    return evm_tx
def create_transaction(address, key, nonce, to, data,
                       network_id) -> EvmTransaction:
    evm_tx = EvmTransaction(
        nonce=nonce,
        gasprice=1,
        startgas=1000000,
        to=to.recipient,
        value=1000000 * (10**18),
        data=data,
        from_full_shard_id=address.full_shard_id,
        to_full_shard_id=to.full_shard_id,
        network_id=network_id,
    )
    evm_tx.sign(key)
    return evm_tx
Ejemplo n.º 8
0
    def create_transaction(self, account, nonce, x_shard_percent,
                           sample_evm_tx) -> Optional[TypedTransaction]:
        # skip if from shard is specified and not matching current branch
        # FIXME: it's possible that clients want to specify '0x0' as the full shard ID, however it will not be supported
        if (sample_evm_tx.from_full_shard_key
                and self.qkc_config.get_full_shard_id_by_full_shard_key(
                    sample_evm_tx.from_full_shard_key) != self.full_shard_id):
            return None

        if sample_evm_tx.from_full_shard_key:
            from_full_shard_key = sample_evm_tx.from_full_shard_key
        else:
            from_full_shard_key = self.full_shard_id

        if not sample_evm_tx.to:
            to_address = random.choice(self.accounts).address
            recipient = to_address.recipient
            to_full_shard_key = self.full_shard_id
        else:
            recipient = sample_evm_tx.to
            to_full_shard_key = from_full_shard_key

        if random.randint(1, 100) <= x_shard_percent:
            # x-shard tx
            to_full_shard_id = random.choice(
                self.qkc_config.get_full_shard_ids() - [self.full_shard_id])
            to_full_shard_key = to_full_shard_id

        value = sample_evm_tx.value
        if not sample_evm_tx.data:
            value = random.randint(1, 100) * (10**15)

        evm_tx = EvmTransaction(
            nonce=nonce,
            gasprice=sample_evm_tx.gasprice,
            startgas=sample_evm_tx.startgas,
            to=recipient,
            value=value,
            data=sample_evm_tx.data,
            gas_token_id=sample_evm_tx.gas_token_id,
            transfer_token_id=sample_evm_tx.transfer_token_id,
            from_full_shard_key=from_full_shard_key,
            to_full_shard_key=to_full_shard_key,
            network_id=self.qkc_config.NETWORK_ID,
        )
        evm_tx.sign(account.key)
        return TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
Ejemplo n.º 9
0
    async def donate(self, from_address, to_address, value=hex(10 ** 18)):
        """Faucet function to send value (default 1 token) from from_address to to_address.
        from_address must be one of the addresses in genesis_data/alloc.json.
        Only allow one pending tx at a time.
        Return tx id if success else None
        """
        if value > 100 * (10 ** 18):
            return None

        key = self.master.env.quark_chain_config.alloc_accounts.get(
            from_address.hex(), None
        )
        if not key:
            return None

        from_address = Address.deserialize(from_address)
        to_address = Address.deserialize(to_address)

        # Do nothing if there is already a pending tx
        result = await self.master.get_transactions_by_address(
            from_address, bytes(1), 1
        )
        if result:
            tx_list, next_token = result
            if tx_list:
                return None

        account_branch_data = await self.master.get_primary_account_data(from_address)
        nonce = account_branch_data.transaction_count
        network_id = self.master.env.quark_chain_config.NETWORK_ID
        evm_tx = EvmTransaction(
            nonce,
            10 ** 9,
            30000,
            to_address.recipient,
            value,
            b"",
            from_full_shard_id=from_address.full_shard_id,
            to_full_shard_id=to_address.full_shard_id,
            network_id=network_id,
        )
        evm_tx.sign(key)
        tx = Transaction(code=Code.create_evm_code(evm_tx))
        success = await self.master.add_transaction(tx)
        if not success:
            return None
        return id_encoder(tx.get_hash(), from_address.full_shard_id)
Ejemplo n.º 10
0
def _contract_tx_gen(shard_state, key, from_address, to_full_shard_id,
                     bytecode):
    evm_tx = EvmTransaction(
        nonce=shard_state.get_transaction_count(from_address.recipient),
        gasprice=1,
        startgas=1000000,
        value=0,
        to=b"",
        data=bytes.fromhex(bytecode),
        from_full_shard_id=from_address.full_shard_id,
        to_full_shard_id=to_full_shard_id,
        network_id=shard_state.env.quark_chain_config.NETWORK_ID,
    )
    evm_tx.sign(key)
    return Transaction(in_list=[],
                       code=Code.create_evm_code(evm_tx),
                       out_list=[])
Ejemplo n.º 11
0
def make_test_tx(s=100000, g=50, data=b"", nonce=0, key=None):
    evm_tx = EvmTransaction(
        nonce=nonce,
        startgas=s,
        gasprice=g,
        value=0,
        data=data,
        to=b"\x35" * 20,
        gas_token_id=0,
        transfer_token_id=0,
        r=1,
        s=1,
        v=28,
    )
    if key:
        evm_tx.sign(key=key)

    return TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
Ejemplo n.º 12
0
    def test_sendTransaction(self):
        id1 = Identity.create_random_identity()
        acc1 = Address.create_from_identity(id1, full_shard_id=0)
        acc2 = Address.create_random_account(full_shard_id=1)

        with ClusterContext(
                1, acc1, small_coinbase=True) as clusters, jrpc_server_context(
                    clusters[0].master):
            slaves = clusters[0].slave_list

            branch = Branch.create(2, 0)
            evm_tx = EvmTransaction(
                nonce=0,
                gasprice=6,
                startgas=30000,
                to=acc2.recipient,
                value=15,
                data=b"",
                from_full_shard_id=acc1.full_shard_id,
                to_full_shard_id=acc2.full_shard_id,
                network_id=slaves[0].env.quark_chain_config.NETWORK_ID,
            )
            evm_tx.sign(id1.get_key())
            request = dict(
                to="0x" + acc2.recipient.hex(),
                gasPrice="0x6",
                gas=hex(30000),
                value="0xf",  # 15
                v=quantity_encoder(evm_tx.v),
                r=quantity_encoder(evm_tx.r),
                s=quantity_encoder(evm_tx.s),
                nonce="0x0",
                fromFullShardId="0x00000000",
                toFullShardId="0x00000001",
                network_id=hex(slaves[0].env.quark_chain_config.NETWORK_ID),
            )
            tx = Transaction(code=Code.create_evm_code(evm_tx))
            response = send_request("sendTransaction", [request])

            self.assertEqual(response, "0x" + tx.get_hash().hex() + "00000000")
            self.assertEqual(len(slaves[0].shards[branch].state.tx_queue), 1)
            self.assertEqual(
                slaves[0].shards[branch].state.tx_queue.pop_transaction(),
                evm_tx)
Ejemplo n.º 13
0
def test_perf_evm():
    N = 5000
    IDN = 10
    print("Creating %d identities" % IDN)
    id_list = []
    for i in range(IDN):
        id_list.append(Identity.create_random_identity())

    acc_list = []
    for i in range(IDN):
        acc_list.append(Address.create_from_identity(id_list[i]))

    print("Creating %d transactions..." % N)
    start_time = time.time()
    tx_list = []
    from_list = []
    for i in range(N):
        from_id = id_list[random.randint(0, IDN - 1)]
        to_addr = acc_list[random.randint(0, IDN - 1)]
        evm_tx = EvmTransaction(
            nonce=0,
            gasprice=1,
            startgas=2,
            to=to_addr.recipient,
            value=3,
            data=b"",
            from_full_shard_key=0,
            to_full_shard_key=0,
            network_id=1,
        )
        evm_tx.sign(key=from_id.get_key())
        tx_list.append(evm_tx)
        from_list.append(from_id.get_recipient())
    duration = time.time() - start_time
    print("Creations PS: %.2f" % (N / duration))

    print("Verifying transactions")
    start_time = time.time()
    for i in range(N):
        tx_list[i]._sender = None
        assert tx_list[i].sender == from_list[i]
    duration = time.time() - start_time
    print("Verifications PS: %.2f" % (N / duration))
Ejemplo n.º 14
0
    def test_tx_size(self):
        id1 = Identity.create_from_key(b"0" * 32)
        acc1 = Address.create_from_identity(id1, full_shard_key=0)

        evm_tx = EvmTransaction(
            nonce=0,
            gasprice=1,
            startgas=30000,
            to=acc1.recipient,
            value=0,
            data=b"",
            from_full_shard_key=0xFFFF,
            to_full_shard_key=0xFFFF,
            network_id=1,
            gas_token_id=12345,
            transfer_token_id=1234,
        )
        evm_tx.sign(key=id1.get_key())
        tx = TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
        self.assertEqual(len(tx.serialize()), TX_MIN_SIZE)

        evm_tx = EvmTransaction(
            nonce=TT256 - 1,
            gasprice=TT256 - 1,
            startgas=TT256 - 1,
            to=acc1.recipient,
            value=TT256 - 1,
            data=b"",
            from_full_shard_key=SHARD_KEY_MAX,
            to_full_shard_key=SHARD_KEY_MAX,
            network_id=1,
            gas_token_id=TOKEN_ID_MAX,
            transfer_token_id=TOKEN_ID_MAX,
        )
        evm_tx.sign(key=id1.get_key())
        tx = TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
        self.assertEqual(len(tx.serialize()), TX_MAX_SIZE)