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

Minimize use of transactions to advance rounds in #360 #365

Merged
merged 7 commits into from
Jul 28, 2022
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
6 changes: 3 additions & 3 deletions tests/steps/account_v2_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from algosdk import account, constants, encoding, logic
from algosdk.future import transaction
from behave import given, then, when
from tests.steps.other_v2_steps import dev_mode_wait_for_confirmation
import tests.steps.other_v2_steps # Imports MaybeString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch



def fund_account_address(
Expand All @@ -18,7 +18,7 @@ def fund_account_address(
)
signed_payment = context.wallet.sign_transaction(payment)
context.app_acl.send_transaction(signed_payment)
dev_mode_wait_for_confirmation(context, payment.get_txid(), 10)
transaction.wait_for_confirmation(context.app_acl, payment.get_txid(), 1)


@when(
Expand Down Expand Up @@ -453,7 +453,7 @@ def create_transient_and_fund(context, transient_fund_amount):
)
signed_payment = context.wallet.sign_transaction(payment)
context.app_acl.send_transaction(signed_payment)
dev_mode_wait_for_confirmation(context, payment.get_txid(), 10)
transaction.wait_for_confirmation(context.app_acl, payment.get_txid(), 1)


@then(
Expand Down
22 changes: 13 additions & 9 deletions tests/steps/application_v2_steps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import json
import re
import time

import pytest
from algosdk import abi, atomic_transaction_composer, encoding, mnemonic
Expand All @@ -13,9 +14,7 @@
from algosdk.future import transaction
from behave import given, step, then, when
from tests.steps.other_v2_steps import (
dev_mode_wait_for_confirmation,
read_program,
self_pay_transactions,
)


Expand Down Expand Up @@ -404,21 +403,26 @@ def remember_app_id(context):
context.app_ids.append(app_id)


def wait_for_algod_transaction_processing_to_complete():
"""
wait_for_algod_transaction_processing_to_complete is a Dev mode helper method that's a rough analog to `context.app_acl.status_after_block(last_round + 2)`.
* <p>
* Since Dev mode produces blocks on a per transaction basis, it's possible algod generates a block _before_ the corresponding SDK call to wait for a block. Without _any_ wait, it's possible the SDK looks for the transaction before algod completes processing. So, the method performs a local sleep to simulate waiting for a block.

"""
time.sleep(0.5)


@step("I wait for the transaction to be confirmed.")
def wait_for_app_txn_confirm(context):
sp = context.app_acl.suggested_params()
last_round = sp.first
# Send some transactions to advance block state in dev mode so we can
# check the status after block `last_round + 2`.
self_pay_transactions(context, 3)
context.app_acl.status_after_block(last_round + 2)
wait_for_algod_transaction_processing_to_complete()
if hasattr(context, "acl"):
assert "type" in context.acl.transaction_info(
context.transient_pk, context.app_txid
)
assert "type" in context.acl.transaction_by_id(context.app_txid)
else:
dev_mode_wait_for_confirmation(context, context.app_txid, 10)
transaction.wait_for_confirmation(context.app_acl, context.app_txid, 1)


@given("an application id {app_id}")
Expand Down
46 changes: 0 additions & 46 deletions tests/steps/other_v2_steps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import base64
import json
import os
import random
import unittest
import urllib
from datetime import datetime
Expand All @@ -10,7 +9,6 @@

import parse
from algosdk import (
account,
dryrun_results,
encoding,
error,
Expand Down Expand Up @@ -98,50 +96,6 @@ def read_program(context, path):
return read_program_binary(path)


DEV_ACCOUNT_INITIAL_MICROALGOS: int = 10_000_000
# Initialize a transient account in dev mode to make payment transactions.
def initialize_account(context, account):
payment = transaction.PaymentTxn(
sender=context.accounts[0],
sp=context.app_acl.suggested_params(),
receiver=account,
amt=DEV_ACCOUNT_INITIAL_MICROALGOS,
)
signed_payment = context.wallet.sign_transaction(payment)
context.app_acl.send_transaction(signed_payment)
# Wait and confirm that the payment succeeded.
transaction.wait_for_confirmation(context.app_acl, payment.get_txid(), 1)


# Send a self-payment transaction to itself to advance blocks in dev mode.
def self_pay_transactions(context, num_txns=1):
if not hasattr(context, "dev_pk"):
context.dev_sk, context.dev_pk = account.generate_account()
initialize_account(context, context.dev_pk)
sp = context.app_acl.suggested_params()
for _ in range(num_txns):
payment = transaction.PaymentTxn(
context.dev_pk,
sp,
context.dev_pk,
random.randint(1, int(DEV_ACCOUNT_INITIAL_MICROALGOS * 0.01)),
)
signed_payment = payment.sign(context.dev_sk)
context.app_acl.send_transaction(signed_payment)
# Wait and confirm that the payment succeeded.
# In dev mode, the transaction should be instantly confirmed in the block.
transaction.wait_for_confirmation(
context.app_acl, payment.get_txid(), 1
)


# To prevent excess waiting, send a zero payment transaction before
# the wait_for_confirmation function in dev mode.
def dev_mode_wait_for_confirmation(context, txid, rounds=1):
self_pay_transactions(context)
transaction.wait_for_confirmation(context.app_acl, txid, rounds)


@given("mock server recording request paths")
def setup_mockserver(context):
context.url = "http://127.0.0.1:" + str(context.path_server_port)
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def gen_key_kmd(context):
context.pk = context.wallet.generate_key()


@when("I generate a key using kmd for rekeying")
@when("I generate a key using kmd for rekeying and fund it")
def gen_rekey_kmd(context):
context.rekey = context.wallet.generate_key()
initialize_account(context, context.rekey)
Expand Down