2021-02-01 18:12:51 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
# external imports
|
2021-02-01 18:12:51 +01:00
|
|
|
import pytest
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.connection import RPCConnection
|
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
from chainlib.eth.gas import (
|
|
|
|
Gas,
|
|
|
|
RPCGasOracle,
|
|
|
|
)
|
|
|
|
from chainlib.eth.tx import (
|
|
|
|
TxFormat,
|
|
|
|
unpack,
|
|
|
|
)
|
|
|
|
from chainlib.eth.nonce import RPCNonceOracle
|
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
)
|
2021-04-02 23:26:13 +02:00
|
|
|
from chainqueue.db.models.tx import TxCache
|
|
|
|
from chainqueue.db.models.otx import Otx
|
2021-03-29 15:27:53 +02:00
|
|
|
|
|
|
|
# test imports
|
|
|
|
from tests.util.gas import StaticGasOracle
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_set(
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec,
|
2021-02-01 18:12:51 +01:00
|
|
|
init_database,
|
2021-03-29 15:27:53 +02:00
|
|
|
eth_rpc,
|
|
|
|
eth_signer,
|
|
|
|
agent_roles,
|
2021-02-01 18:12:51 +01:00
|
|
|
):
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_id = default_chain_spec.chain_id()
|
|
|
|
rpc = RPCConnection.connect(default_chain_spec, 'default')
|
|
|
|
nonce_oracle = RPCNonceOracle(agent_roles['ALICE'], eth_rpc)
|
|
|
|
gas_oracle = RPCGasOracle(eth_rpc)
|
|
|
|
c = Gas(signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, chain_id=chain_id)
|
|
|
|
|
|
|
|
(tx_hash_hex, tx_signed_raw_hex) = c.create(agent_roles['ALICE'], agent_roles['BOB'], 100 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED)
|
|
|
|
tx = unpack(bytes.fromhex(strip_0x(tx_signed_raw_hex)), chain_id)
|
|
|
|
|
|
|
|
otx = Otx(
|
|
|
|
tx['nonce'],
|
|
|
|
tx_hash_hex,
|
|
|
|
tx_signed_raw_hex,
|
|
|
|
)
|
|
|
|
init_database.add(otx)
|
|
|
|
init_database.commit()
|
|
|
|
|
|
|
|
bogus_from_token = add_0x(os.urandom(20).hex())
|
|
|
|
to_value = int(tx['value'] / 2)
|
|
|
|
|
|
|
|
txc = TxCache(
|
|
|
|
tx_hash_hex,
|
|
|
|
tx['from'],
|
|
|
|
tx['to'],
|
|
|
|
bogus_from_token,
|
|
|
|
ZERO_ADDRESS,
|
|
|
|
tx['value'],
|
|
|
|
to_value,
|
|
|
|
666,
|
|
|
|
13,
|
2021-04-02 23:26:13 +02:00
|
|
|
session=init_database,
|
2021-02-01 18:12:51 +01:00
|
|
|
)
|
2021-03-29 15:27:53 +02:00
|
|
|
init_database.add(txc)
|
|
|
|
init_database.commit()
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
tx_stored = init_database.query(TxCache).first()
|
|
|
|
assert (tx_stored.sender == tx['from'])
|
|
|
|
assert (tx_stored.recipient == tx['to'])
|
|
|
|
assert (tx_stored.source_token_address == bogus_from_token)
|
|
|
|
assert (tx_stored.destination_token_address == ZERO_ADDRESS)
|
|
|
|
assert (tx_stored.from_value == tx['value'])
|
|
|
|
assert (tx_stored.to_value == to_value)
|
|
|
|
assert (tx_stored.block_number == 666)
|
|
|
|
assert (tx_stored.tx_index == 13)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_clone(
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec,
|
2021-02-01 18:12:51 +01:00
|
|
|
init_database,
|
2021-03-29 15:27:53 +02:00
|
|
|
eth_rpc,
|
|
|
|
eth_signer,
|
|
|
|
agent_roles,
|
2021-02-01 18:12:51 +01:00
|
|
|
):
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_id = default_chain_spec.chain_id()
|
|
|
|
rpc = RPCConnection.connect(default_chain_spec, 'default')
|
|
|
|
nonce_oracle = RPCNonceOracle(agent_roles['ALICE'], eth_rpc)
|
|
|
|
gas_oracle = StaticGasOracle(2 * (10 ** 9), 21000)
|
|
|
|
c = Gas(signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, chain_id=chain_id)
|
|
|
|
|
|
|
|
txs_rpc = [
|
|
|
|
c.create(agent_roles['ALICE'], agent_roles['BOB'], 100 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED),
|
|
|
|
]
|
|
|
|
|
|
|
|
gas_oracle = StaticGasOracle(4 * (10 ** 9), 21000)
|
|
|
|
c = Gas(signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, chain_id=chain_id)
|
|
|
|
txs_rpc += [
|
|
|
|
c.create(agent_roles['ALICE'], agent_roles['BOB'], 100 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED),
|
|
|
|
]
|
|
|
|
|
2021-02-01 18:12:51 +01:00
|
|
|
txs = []
|
2021-03-29 15:27:53 +02:00
|
|
|
for tx_rpc in txs_rpc:
|
|
|
|
tx_hash_hex = tx_rpc[0]
|
|
|
|
tx_signed_raw_hex = tx_rpc[1]
|
|
|
|
tx_dict = unpack(bytes.fromhex(strip_0x(tx_signed_raw_hex)), chain_id)
|
2021-02-01 18:12:51 +01:00
|
|
|
otx = Otx(
|
2021-03-29 15:27:53 +02:00
|
|
|
tx_dict['nonce'],
|
|
|
|
tx_hash_hex,
|
|
|
|
tx_signed_raw_hex,
|
2021-02-01 18:12:51 +01:00
|
|
|
)
|
|
|
|
init_database.add(otx)
|
2021-03-29 15:27:53 +02:00
|
|
|
tx_dict['hash'] = tx_hash_hex
|
|
|
|
txs.append(tx_dict)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
init_database.commit()
|
|
|
|
|
|
|
|
txc = TxCache(
|
|
|
|
txs[0]['hash'],
|
|
|
|
txs[0]['from'],
|
|
|
|
txs[0]['to'],
|
2021-03-29 15:27:53 +02:00
|
|
|
ZERO_ADDRESS,
|
|
|
|
ZERO_ADDRESS,
|
2021-02-01 18:12:51 +01:00
|
|
|
txs[0]['value'],
|
|
|
|
txs[0]['value'],
|
2021-04-02 23:26:13 +02:00
|
|
|
session=init_database,
|
2021-02-01 18:12:51 +01:00
|
|
|
)
|
|
|
|
init_database.add(txc)
|
|
|
|
init_database.commit()
|
|
|
|
|
2021-04-02 23:26:13 +02:00
|
|
|
TxCache.clone(txs[0]['hash'], txs[1]['hash'], session=init_database)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
q = init_database.query(TxCache)
|
|
|
|
q = q.join(Otx)
|
2021-04-02 23:26:13 +02:00
|
|
|
q = q.filter(Otx.tx_hash==strip_0x(txs[1]['hash']))
|
2021-02-01 18:12:51 +01:00
|
|
|
txc_clone = q.first()
|
|
|
|
|
|
|
|
assert txc_clone != None
|
|
|
|
assert txc_clone.sender == txc.sender
|
|
|
|
assert txc_clone.recipient == txc.recipient
|
|
|
|
assert txc_clone.source_token_address == txc.source_token_address
|
|
|
|
assert txc_clone.destination_token_address == txc.destination_token_address
|
|
|
|
assert txc_clone.from_value == txc.from_value
|
|
|
|
assert txc_clone.to_value == txc.to_value
|