Skip to content

Commit

Permalink
EvalDeltas should be done.
Browse files Browse the repository at this point in the history
Not tested enough, especially on handling shared account modifications.
  • Loading branch information
jannotti committed Jan 22, 2024
1 parent 3ed1d11 commit c3cf4d1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 50 deletions.
54 changes: 14 additions & 40 deletions algosdk/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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()]
)
+ "}"
)
2 changes: 1 addition & 1 deletion algosdk/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def algo_msgp_decode(enc):

def cook(raw):
stop = {b"gd", b"ld", b"lg"}
safe = {b"type"}
safe = {b"type", b"gen"}

if isinstance(raw, dict):
cooked = {}
Expand Down
78 changes: 71 additions & 7 deletions algosdk/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,9 @@ def __eq__(self, other):
and self.authorizing_address == other.authorizing_address
)

def __str__(self):
return ostr(self)


class SignedTxnWithAD:
def __init__(self, stxn: SignedTransaction, apply_data):
Expand All @@ -2188,7 +2191,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)


Expand All @@ -2214,26 +2217,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):
t = v[b"at"]
if t == 1:
return v[b"bs"]
elif t == 2:
return v[b"ui"]
elif t == 3:
return None
exit(1)

def __str__(self):
return ostr(self)


class MultisigTransaction:
Expand Down Expand Up @@ -3404,3 +3458,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()]
)
+ "}"
)
4 changes: 2 additions & 2 deletions algosdk/v2client/algod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c3cf4d1

Please sign in to comment.