Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add integration tests for sidechain transactions #571

Merged
merged 22 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 76 additions & 26 deletions tests/integration/reusable_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,109 @@

from tests.integration.it_utils import (
ASYNC_JSON_RPC_CLIENT,
MASTER_ACCOUNT,
create_amm_pool_async,
fund_wallet_async,
sign_and_reliable_submission_async,
)
from xrpl.asyncio.clients.async_client import AsyncClient
from xrpl.models import IssuedCurrencyAmount, OfferCreate, PaymentChannelCreate
from xrpl.models.transactions.amm_deposit import AMMDeposit, AMMDepositFlag
from xrpl.models import (
XRP,
AMMDeposit,
AMMDepositFlag,
IssuedCurrencyAmount,
OfferCreate,
PaymentChannelCreate,
SignerEntry,
SignerListSet,
XChainBridge,
XChainCreateBridge,
)
from xrpl.wallet import Wallet


# TODO: use `asyncio.gather` for these, to parallelize
# TODO: set up wallet for each test instead of using one for all tests (now that it's
# faster)
async def _set_up_reusable_values():
WALLET = Wallet.create()
await fund_wallet_async(WALLET)
DESTINATION = Wallet.create()
await fund_wallet_async(DESTINATION)
wallet = Wallet.create()
await fund_wallet_async(wallet)
destination = Wallet.create()
await fund_wallet_async(destination)
door_wallet = Wallet.create()
await fund_wallet_async(door_wallet)
witness_wallet = Wallet.create()
await fund_wallet_async(witness_wallet)

OFFER = await sign_and_reliable_submission_async(
offer = await sign_and_reliable_submission_async(
OfferCreate(
account=WALLET.address,
account=wallet.address,
taker_gets="13100000",
taker_pays=IssuedCurrencyAmount(
currency="USD",
issuer=WALLET.address,
issuer=wallet.address,
value="10",
),
),
WALLET,
wallet,
)

PAYMENT_CHANNEL = await sign_and_reliable_submission_async(
payment_channel = await sign_and_reliable_submission_async(
PaymentChannelCreate(
account=WALLET.address,
account=wallet.address,
amount="1",
destination=DESTINATION.address,
destination=destination.address,
settle_delay=86400,
public_key=WALLET.public_key,
public_key=wallet.public_key,
),
wallet,
)

bridge = XChainCreateBridge(
account=door_wallet.address,
xchain_bridge=XChainBridge(
locking_chain_door=door_wallet.address,
locking_chain_issue=XRP(),
issuing_chain_door=MASTER_ACCOUNT,
issuing_chain_issue=XRP(),
),
WALLET,
signature_reward="200",
min_account_create_amount="10000000",
)
await sign_and_reliable_submission_async(
bridge,
door_wallet,
)
await sign_and_reliable_submission_async(
SignerListSet(
account=door_wallet.classic_address,
signer_entries=[
SignerEntry(
account=witness_wallet.classic_address,
signer_weight=1,
)
],
signer_quorum=1,
),
door_wallet,
)

setup_amm_pool_res = await setup_amm_pool(wallet=WALLET)
AMM_ASSET = setup_amm_pool_res["asset"]
AMM_ASSET2 = setup_amm_pool_res["asset2"]
AMM_ISSUER_WALLET = setup_amm_pool_res["issuer_wallet"]
setup_amm_pool_res = await setup_amm_pool(wallet=wallet)
amm_asset = setup_amm_pool_res["asset"]
amm_asset2 = setup_amm_pool_res["asset2"]
amm_issuer_wallet = setup_amm_pool_res["issuer_wallet"]

return (
WALLET,
DESTINATION,
OFFER,
PAYMENT_CHANNEL,
AMM_ASSET,
AMM_ASSET2,
AMM_ISSUER_WALLET,
wallet,
destination,
door_wallet,
witness_wallet,
offer,
payment_channel,
amm_asset,
amm_asset2,
amm_issuer_wallet,
bridge,
)


Expand Down Expand Up @@ -94,9 +141,12 @@ async def setup_amm_pool(
(
WALLET,
DESTINATION,
DOOR_WALLET,
WITNESS_WALLET,
OFFER,
PAYMENT_CHANNEL,
AMM_ASSET,
AMM_ASSET2,
AMM_ISSUER_WALLET,
BRIDGE,
) = asyncio.run(_set_up_reusable_values())
20 changes: 9 additions & 11 deletions tests/integration/transactions/test_escrow_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
sign_and_reliable_submission_async,
test_async_and_sync,
)
from tests.integration.reusable_values import WALLET
from tests.integration.reusable_values import DESTINATION, WALLET
from xrpl.models import EscrowCreate, Ledger
from xrpl.models.response import ResponseStatus
from xrpl.models.transactions import EscrowCreate

ACCOUNT = WALLET.address

AMOUNT = "10000"
DESTINATION = "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW"
CANCEL_AFTER = 533257958
FINISH_AFTER = 533171558
CONDITION = (
"A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855810100"
)
Expand All @@ -23,18 +20,19 @@
class TestEscrowCreate(IntegrationTestCase):
@test_async_and_sync(globals())
async def test_all_fields(self, client):
ledger = await client.request(Ledger(ledger_index="validated"))
close_time = ledger.result["ledger"]["close_time"]
escrow_create = EscrowCreate(
account=ACCOUNT,
account=WALLET.classic_address,
amount=AMOUNT,
destination=DESTINATION,
destination=DESTINATION.classic_address,
destination_tag=DESTINATION_TAG,
cancel_after=CANCEL_AFTER,
finish_after=FINISH_AFTER,
cancel_after=close_time + 3,
finish_after=close_time + 2,
source_tag=SOURCE_TAG,
)
response = await sign_and_reliable_submission_async(
escrow_create, WALLET, client
)
# Actual engine_result will be `tecNO_PERMISSION`...
# maybe due to CONDITION or something
self.assertEqual(response.status, ResponseStatus.SUCCESS)
self.assertEqual(response.result["engine_result"], "tesSUCCESS")
1 change: 1 addition & 0 deletions tests/integration/transactions/test_ticket_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ async def test_basic_functionality(self, client):
client,
)
self.assertTrue(response.is_successful())
self.assertEqual(response.result["engine_result"], "tesSUCCESS")
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from tests.integration.integration_test_case import IntegrationTestCase
from tests.integration.it_utils import (
sign_and_reliable_submission_async,
test_async_and_sync,
)
from tests.integration.reusable_values import BRIDGE, WALLET
from xrpl.models import AccountInfo, XChainAccountCreateCommit
from xrpl.wallet import Wallet


class TestXChainAccountCreateCommit(IntegrationTestCase):
@test_async_and_sync(globals())
async def test_basic_functionality(self, client):
locking_chain_door = BRIDGE.xchain_bridge.locking_chain_door
account_info1 = await client.request(AccountInfo(account=locking_chain_door))
initial_balance = int(account_info1.result["account_data"]["Balance"])
amount = int(BRIDGE.min_account_create_amount)

response = await sign_and_reliable_submission_async(
XChainAccountCreateCommit(
account=WALLET.classic_address,
xchain_bridge=BRIDGE.xchain_bridge,
amount=str(amount),
signature_reward=BRIDGE.signature_reward,
destination=Wallet.create().classic_address,
),
WALLET,
client,
)
self.assertTrue(response.is_successful())
self.assertEqual(response.result["engine_result"], "tesSUCCESS")

account_info2 = await client.request(AccountInfo(account=locking_chain_door))
final_balance = int(account_info2.result["account_data"]["Balance"])
self.assertEqual(
final_balance, initial_balance + amount + int(BRIDGE.signature_reward)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from tests.integration.integration_test_case import IntegrationTestCase
from tests.integration.it_utils import (
sign_and_reliable_submission_async,
test_async_and_sync,
)
from tests.integration.reusable_values import BRIDGE, WITNESS_WALLET
from xrpl.asyncio.account import does_account_exist
from xrpl.core.binarycodec import encode
from xrpl.core.keypairs import sign
from xrpl.models import (
AccountObjects,
AccountObjectType,
XChainAddAccountCreateAttestation,
)
from xrpl.utils import xrp_to_drops
from xrpl.wallet import Wallet


class TestXChainAddAccountCreateAttestation(IntegrationTestCase):
@test_async_and_sync(globals(), ["xrpl.account.does_account_exist"])
async def test_basic_functionality(self, client):
destination = Wallet.create().classic_address
other_chain_source = Wallet.create().classic_address
self.assertFalse(await does_account_exist(destination, client))

account_objects = await client.request(
AccountObjects(
account=BRIDGE.xchain_bridge.locking_chain_door,
type=AccountObjectType.BRIDGE,
)
)
bridge_obj = account_objects.result["account_objects"][0]

attestation_to_sign = {
"XChainBridge": BRIDGE.to_xrpl()["XChainBridge"],
"OtherChainSource": other_chain_source,
"Amount": xrp_to_drops(300),
"AttestationRewardAccount": WITNESS_WALLET.classic_address,
"WasLockingChainSend": 0,
"XChainAccountCreateCount": int(bridge_obj["XChainAccountClaimCount"]) + 1,
"Destination": destination,
"SignatureReward": BRIDGE.signature_reward,
}
encoded_attestation = encode(attestation_to_sign)
attestation_signature = sign(
bytes.fromhex(encoded_attestation),
WITNESS_WALLET.private_key,
)

response = await sign_and_reliable_submission_async(
XChainAddAccountCreateAttestation.from_xrpl(
{
"Account": WITNESS_WALLET.classic_address,
"AttestationSignerAccount": WITNESS_WALLET.classic_address,
**attestation_to_sign,
"PublicKey": WITNESS_WALLET.public_key,
"Signature": attestation_signature,
}
),
WITNESS_WALLET,
client,
)
self.assertTrue(response.is_successful())
self.assertEqual(response.result["engine_result"], "tesSUCCESS")

self.assertTrue(await does_account_exist(destination, client))
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from tests.integration.integration_test_case import IntegrationTestCase
from tests.integration.it_utils import (
sign_and_reliable_submission_async,
test_async_and_sync,
)
from tests.integration.reusable_values import BRIDGE, DESTINATION, WITNESS_WALLET
from xrpl.core.binarycodec import encode
from xrpl.core.keypairs import sign
from xrpl.models import AccountInfo, Tx, XChainAddClaimAttestation, XChainCreateClaimID
from xrpl.utils import get_xchain_claim_id, xrp_to_drops
from xrpl.wallet import Wallet


class TestXChainAddClaimAttestation(IntegrationTestCase):
@test_async_and_sync(globals())
async def test_basic_functionality(self, client):
other_chain_source = Wallet.create().classic_address

claim_id_response = await sign_and_reliable_submission_async(
XChainCreateClaimID(
account=DESTINATION.classic_address,
xchain_bridge=BRIDGE.xchain_bridge,
signature_reward=BRIDGE.signature_reward,
other_chain_source=other_chain_source,
),
DESTINATION,
client,
)
claim_id_hash = (
claim_id_response.result.get("tx_json") or claim_id_response.result
)["hash"]
claim_id_tx_response = await client.request(Tx(transaction=claim_id_hash))
xchain_claim_id = get_xchain_claim_id(claim_id_tx_response.result["meta"])

account_info1 = await client.request(
AccountInfo(account=DESTINATION.classic_address)
)
initial_balance = int(account_info1.result["account_data"]["Balance"])
amount = xrp_to_drops(3)

attestation_to_sign = {
"XChainBridge": BRIDGE.to_xrpl()["XChainBridge"],
"OtherChainSource": other_chain_source,
"Amount": amount,
"AttestationRewardAccount": WITNESS_WALLET.classic_address,
"WasLockingChainSend": 0,
"XChainClaimID": xchain_claim_id,
"Destination": DESTINATION.classic_address,
}
encoded_attestation = encode(attestation_to_sign)
attestation_signature = sign(
bytes.fromhex(encoded_attestation),
WITNESS_WALLET.private_key,
)

response = await sign_and_reliable_submission_async(
XChainAddClaimAttestation.from_xrpl(
{
"Account": WITNESS_WALLET.classic_address,
"AttestationSignerAccount": WITNESS_WALLET.classic_address,
**attestation_to_sign,
"PublicKey": WITNESS_WALLET.public_key,
"Signature": attestation_signature,
}
),
WITNESS_WALLET,
client,
)
self.assertTrue(response.is_successful())
self.assertEqual(response.result["engine_result"], "tesSUCCESS")

account_info2 = await client.request(
AccountInfo(account=DESTINATION.classic_address)
)
final_balance = int(account_info2.result["account_data"]["Balance"])
self.assertEqual(
final_balance, initial_balance + int(amount) - int(BRIDGE.signature_reward)
)
Loading