From 7e6f3d723afcba1846b30da6658476c19d38f8b8 Mon Sep 17 00:00:00 2001 From: John Jannotti Date: Sun, 21 Jan 2024 13:45:23 -0500 Subject: [PATCH] EvalDeltas should be done. Not tested enough, especially on handling shared account modifications. --- algosdk/block.py | 54 ++++++++-------------------- algosdk/transaction.py | 75 +++++++++++++++++++++++++++++++++++---- algosdk/v2client/algod.py | 4 +-- 3 files changed, 84 insertions(+), 49 deletions(-) diff --git a/algosdk/block.py b/algosdk/block.py index 2230c4d2..eea3b871 100644 --- a/algosdk/block.py +++ b/algosdk/block.py @@ -15,16 +15,7 @@ def undictify(d): ) def __str__(self): - return ( - "{" - + ", ".join( - [ - str(key) + ": " + str(value) - for key, value in self.__dict__.items() - ] - ) - + "}" - ) + return ostr(self) class Block: @@ -124,16 +115,7 @@ def undictify(d): ) def __str__(self): - return ( - "{" - + ", ".join( - [ - str(key) + ": " + str(value) - for key, value in self.__dict__.items() - ] - ) - + "}" - ) + return ostr(self) class Cert: @@ -159,16 +141,7 @@ def undictify(d): ) def __str__(self): - return ( - "{" - + ", ".join( - [ - str(key) + ": " + str(value) - for key, value in self.__dict__.items() - ] - ) - + "}" - ) + return ostr(self) class ProposalValue: @@ -184,19 +157,20 @@ def __init__( def undictify(d): return ProposalValue( original_period=d.get("oper", 0), - original_proposer=d.get("oprop"), + original_proposer=encoding.encode_address(d.get("oprop")), block_digest=d.get("dig"), encoding_digest=d.get("encdig"), ) def __str__(self): - return ( - "{" - + ", ".join( - [ - str(key) + ": " + str(value) - for key, value in self.__dict__.items() - ] - ) - + "}" + return ostr(self) + + +def ostr(o): + return ( + "{" + + ", ".join( + [str(key) + ": " + str(value) for key, value in o.__dict__.items()] ) + + "}" + ) diff --git a/algosdk/transaction.py b/algosdk/transaction.py index 40947695..3970f8d7 100644 --- a/algosdk/transaction.py +++ b/algosdk/transaction.py @@ -2188,7 +2188,7 @@ def __init__(self, stxn: SignedTransaction, apply_data): @staticmethod def undictify(d): stxn = SignedTransaction.undictify(d) - ad = ApplyData.undictify(d) + ad = ApplyData.undictify(d, stxn.transaction) return SignedTxnWithAD(stxn, ad) @@ -2214,26 +2214,77 @@ def __init__( self.application_id = application_id @staticmethod - def undictify(d): + def undictify(d, txn): return ApplyData( d.get("ca", 0), d.get("aca", 0), d.get("rs", 0), d.get("rr", 0), d.get("rc", 0), - EvalDelta.undictify(d.get("dt", {})), + EvalDelta.undictify(d.get("dt"), txn), d.get("caid", 0), d.get("apid", 0), ) + def __str__(self): + return ostr(self) + class EvalDelta: - def __init__(self, gd): - self.global_delta = gd + def __init__(self, globals, locals, shared, logs, inners): + self.global_delta = globals + self.local_deltas = locals + self.shared_accounts = shared + self.logs = logs + self.inner_transactions = inners + + @classmethod + def undictify(cls, d, txn): + if d is None: + return None + locals = { + i: EvalDelta.values(sd) for (i, sd) in d.get("ld", {}).items() + } + shared = d.get("sa", []) + if txn is not None: + locals = { + cls.resolve(i, txn, shared): sd for (i, sd) in locals.items() + } + return EvalDelta( + EvalDelta.values(d.get("gd", {})), + locals, + shared, + d.get("logs", []), + [SignedTxnWithAD.undictify(i) for i in d.get("itxn", [])], + ) @staticmethod - def undictify(d): - return EvalDelta(d.get("gd")) + def resolve(i, txn, shared): + if i == 0: + return txn.sender + i -= 1 + if i < len(txn.accounts): + return txn.accounts[i] + i -= len(txn.accounts) + return shared[i] + + @classmethod + def values(cls, d): + return {k: cls.value(v) for (k, v) in d.items()} + + @classmethod + def value(cls, v): + match v[b"at"]: + case 1: + return v[b"bs"] + case 2: + return v[b"ui"] + case 3: + return None + exit(1) + + def __str__(self): + return ostr(self) class MultisigTransaction: @@ -3404,3 +3455,13 @@ def decode_programs(app): app["params"]["clear-state-program"] ) return app + + +def ostr(o): + return ( + "{" + + ", ".join( + [str(key) + ": " + str(value) for key, value in o.__dict__.items()] + ) + + "}" + ) diff --git a/algosdk/v2client/algod.py b/algosdk/v2client/algod.py index ea34c1bc..cc11aca3 100644 --- a/algosdk/v2client/algod.py +++ b/algosdk/v2client/algod.py @@ -17,7 +17,7 @@ from urllib import parse from urllib.request import Request, urlopen -from algosdk import constants, encoding, error, transaction, util +from algosdk import block, constants, encoding, error, transaction, util from algosdk.v2client import models AlgodResponseType = Union[Dict[str, Any], bytes] @@ -288,7 +288,7 @@ def block( round: int | str, **kwargs: Any, ) -> "block.BlockInfo": - msgp = self.block_info(round, "msgpack") + msgp = self.block_info(int(round), "msgpack") d = encoding.algo_msgp_decode(msgp) return encoding.undictify(d)