-
Notifications
You must be signed in to change notification settings - Fork 141
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
New Feature: Adding methods to use the simulate endpoint #420
Merged
Merged
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
7bebd3f
Adding methods to use the simulate endpoint
barnjamin 8ad7d0c
fix call to sim raw, add response format of msgpack
barnjamin 601abe5
Merge branch 'develop' into simulate-endpoint
barnjamin a388e37
adding simulate to atc
barnjamin 26e63e4
lint
barnjamin 17a1b56
update names
barnjamin 661e08c
passing msgpack in both response_format and format
barnjamin c6105af
tmp
barnjamin ffb8e64
Remove simulate response, dont bail on would_succeed false
barnjamin e9398f4
grab the right field out of the txn results
barnjamin 26e7265
docstrings
barnjamin 9dd2db6
provide more information since we get it back from simulate anyway
barnjamin c2f98f8
adding cucumber tests
barnjamin 179c67d
try to make sandbox run against experimental-api branch
barnjamin 7fbbb26
revert env var change
barnjamin 882052e
remove extra kwarg
barnjamin 5382250
add new test paths, remove json, add docstring
barnjamin 5148785
Replace direct method access with method we've already got
barnjamin 16f0275
adding check for group number
barnjamin 8f4a4df
adding tests for empty signer simulation
barnjamin 7d28c75
fix return value for atc simulation
barnjamin 53155b8
fix return value for atc simulation
barnjamin ffef864
Merge pull request #1 from barnjamin/simulate-empty-signer
barnjamin 63a4b6a
Update .test-env
algochoi d56aa55
typed return val
bbroder-algo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -479,6 +479,55 @@ def submit(self, client: algod.AlgodClient) -> List[str]: | |
self.status = AtomicTransactionComposerStatus.SUBMITTED | ||
return self.tx_ids | ||
|
||
def simulate( | ||
self, client: algod.AlgodClient | ||
) -> "SimulateAtomicTransactionResponse": | ||
barnjamin marked this conversation as resolved.
Show resolved
Hide resolved
barnjamin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if self.status <= AtomicTransactionComposerStatus.SUBMITTED: | ||
self.gather_signatures() | ||
barnjamin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
raise error.AtomicTransactionComposerError( | ||
"AtomicTransactionComposerStatus must be submitted or " | ||
"lower to simulate a group" | ||
) | ||
|
||
simulation_result: Dict[str, Any] = client.simulate_transactions( | ||
self.signed_txns, response_format="json" | ||
barnjamin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
# Only take the first group in the simulate response | ||
txn_group: Dict[str, Any] = simulation_result["txn-groups"][0] | ||
|
||
# Parse out abi results | ||
txn_results = [t["txn-result"] for t in txn_group["txn-results"]] | ||
method_results = self.parse_response(txn_results) | ||
|
||
# build up data structure with fields we'd want | ||
sim_results = [] | ||
for idx, result in enumerate(method_results): | ||
sim_txn: Dict[str, Any] = txn_group["txn-results"][idx] | ||
|
||
sim_results.append( | ||
SimulateABIResult( | ||
tx_id=result.tx_id, | ||
raw_value=result.raw_value, | ||
return_value=result.return_value, | ||
decode_error=result.decode_error, | ||
tx_info=result.tx_info, | ||
method=self.method_dict[idx], | ||
missing_signature=sim_txn.get("missing-signature", False), | ||
) | ||
) | ||
|
||
return SimulateAtomicTransactionResponse( | ||
version=simulation_result.get("version", 0), | ||
would_succeed=simulation_result.get("would-succeed", False), | ||
failure_message=txn_group.get("failure-message", ""), | ||
failed_at=txn_group.get("failed-at"), | ||
simulate_response=simulation_result, | ||
tx_ids=self.tx_ids, | ||
results=sim_results, | ||
) | ||
|
||
def execute( | ||
self, client: algod.AlgodClient, wait_rounds: int | ||
) -> "AtomicTransactionResponse": | ||
|
@@ -517,20 +566,33 @@ def execute( | |
self.status = AtomicTransactionComposerStatus.COMMITTED | ||
|
||
confirmed_round = resp["confirmed-round"] | ||
method_results = [] | ||
|
||
for i, tx_id in enumerate(self.tx_ids): | ||
tx_results = [ | ||
client.pending_transaction_info(tx_id) for tx_id in self.tx_ids | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. caveat/nit: I don't think we need to get the txn info for every transaction, just the ones with method calls ( |
||
] | ||
|
||
method_results = self.parse_response(tx_results) | ||
|
||
return AtomicTransactionResponse( | ||
confirmed_round=confirmed_round, | ||
tx_ids=self.tx_ids, | ||
results=method_results, | ||
) | ||
|
||
def parse_response(self, txns: List[Dict[str, Any]]) -> List["ABIResult"]: | ||
|
||
method_results = [] | ||
for i, tx_info in enumerate(txns): | ||
tx_id = self.tx_ids[i] | ||
raw_value: Optional[bytes] = None | ||
return_value = None | ||
decode_error = None | ||
tx_info: Optional[Any] = None | ||
|
||
if i not in self.method_dict: | ||
continue | ||
|
||
# Parse log for ABI method return value | ||
try: | ||
tx_info = client.pending_transaction_info(tx_id) | ||
if self.method_dict[i].returns.type == abi.Returns.VOID: | ||
method_results.append( | ||
ABIResult( | ||
|
@@ -569,21 +631,18 @@ def execute( | |
except Exception as e: | ||
decode_error = e | ||
|
||
abi_result = ABIResult( | ||
tx_id=tx_id, | ||
raw_value=cast(bytes, raw_value), | ||
return_value=return_value, | ||
decode_error=decode_error, | ||
tx_info=cast(Any, tx_info), | ||
method=self.method_dict[i], | ||
method_results.append( | ||
ABIResult( | ||
tx_id=tx_id, | ||
raw_value=cast(bytes, raw_value), | ||
return_value=return_value, | ||
decode_error=decode_error, | ||
tx_info=tx_info, | ||
method=self.method_dict[i], | ||
) | ||
) | ||
method_results.append(abi_result) | ||
|
||
return AtomicTransactionResponse( | ||
confirmed_round=confirmed_round, | ||
tx_ids=self.tx_ids, | ||
results=method_results, | ||
) | ||
return method_results | ||
|
||
|
||
class TransactionSigner(ABC): | ||
|
@@ -740,3 +799,43 @@ def __init__( | |
self.confirmed_round = confirmed_round | ||
self.tx_ids = tx_ids | ||
self.abi_results = results | ||
|
||
|
||
class SimulateABIResult(ABIResult): | ||
def __init__( | ||
self, | ||
tx_id: str, | ||
raw_value: bytes, | ||
return_value: Any, | ||
decode_error: Optional[Exception], | ||
tx_info: dict, | ||
method: abi.Method, | ||
missing_signature: bool, | ||
) -> None: | ||
self.tx_id = tx_id | ||
self.raw_value = raw_value | ||
self.return_value = return_value | ||
self.decode_error = decode_error | ||
self.tx_info = tx_info | ||
self.method = method | ||
self.missing_signature = missing_signature | ||
|
||
|
||
class SimulateAtomicTransactionResponse: | ||
def __init__( | ||
self, | ||
version: int, | ||
would_succeed: bool, | ||
failure_message: str, | ||
failed_at: Optional[List[int]], | ||
simulate_response: Dict[str, Any], | ||
tx_ids: List[str], | ||
results: List[SimulateABIResult], | ||
) -> None: | ||
self.version = version | ||
self.would_succeed = would_succeed | ||
self.failure_message = failure_message | ||
self.failed_at = failed_at | ||
self.simulate_response = simulate_response | ||
self.tx_ids = tx_ids | ||
self.abi_results = results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
@rekey_v1 | ||
@send | ||
@send.keyregtxn | ||
@simulate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remember to change this back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i looked at the trap ray
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#445