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

Block: Add objects to represent the return value from block_info #531

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
176 changes: 176 additions & 0 deletions algosdk/block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
from algosdk import encoding, transaction


class BlockInfo:
def __init__(self, block, certificate):
self.block = block
self.certificate = certificate
pass

@staticmethod
def undictify(d):
return BlockInfo(
block=Block.undictify(d.get("block")),
certificate=Cert.undictify(d.get("cert")),
)

def __str__(self):
return ostr(self)


class Block:
def __init__(
self,
round,
branch,
seed,
commit,
sha256commit,
timestamp,
genesis_id,
genesis_hash,
fee_sink,
rewards_pool,
rewards_level,
rewards_rate,
rewards_residue,
rewards_calculation_round,
upgrade_propose,
upgrade_delay,
upgrade_approve,
current_protocol,
next_protocol,
next_protocol_approvals,
next_protocol_vote_before,
next_protocol_switch_on,
counter,
payset,
):
self.round = round
self.branch = branch
self.seed = seed
self.commit = commit
self.sha256commit = sha256commit
self.timestamp = timestamp
self.genesis_id = genesis_id
self.genesis_hash = genesis_hash
self.fee_sink = fee_sink
self.rewards_pool = rewards_pool
self.rewards_level = rewards_level
self.rewards_rate = rewards_rate
self.rewards_residue = rewards_residue
self.rewards_calculation_round = rewards_calculation_round
self.upgrade_propose = upgrade_propose
self.upgrade_delay = upgrade_delay
self.upgrade_approve = upgrade_approve
self.current_protocol = current_protocol
self.next_protocol = next_protocol
self.next_protocol_approvals = next_protocol_approvals
self.next_protocol_vote_before = next_protocol_vote_before
self.next_protocol_switch_on = next_protocol_switch_on
self.counter = counter
self.payset = payset

@staticmethod
def undictify(d):
stxns = []
gi = d.get("gen")
gh = d.get("gh")
for stib in d.get("txns", []):
stxn = stib.copy()
stxn["txn"] = stxn["txn"].copy()
if stib.get("hgi", False):
stxn["txn"]["gi"] = gi
# Unconditionally put the genesis hash into the txn. This
# is not strictly correct for very early transactions
# (v15) on testnet. They could have been submitted
# without genhash.
stxn["txn"]["gh"] = gh
stxns.append(transaction.SignedTxnWithAD.undictify(stxn))
return Block(
round=d.get("rnd", 0),
branch=d.get("prev"),
seed=d.get("seed"),
commit=d.get("txn"),
sha256commit=d.get("txn256"),
timestamp=d.get("ts", 0),
genesis_id=d.get("gen"),
genesis_hash=d.get("gh"),
fee_sink=encoding.encode_address(d.get("fees")),
rewards_pool=encoding.encode_address(d.get("rwd")),
rewards_level=d.get("earn", 0),
rewards_rate=d.get("rate", 0),
rewards_residue=d.get("frac", 0),
rewards_calculation_round=d.get("rwcalr", 0),
upgrade_propose=d.get("upgradeprop"),
upgrade_delay=d.get("upgradedelay", 0),
upgrade_approve=d.get("upgradeyes", False),
current_protocol=d.get("proto"),
next_protocol=d.get("nextproto"),
next_protocol_approvals=d.get("nextyes", 0),
next_protocol_vote_before=d.get("nextbefore", 0),
next_protocol_switch_on=d.get("nextswitch", 0),
counter=d.get("tc", 0),
payset=stxns,
)

def __str__(self):
return ostr(self)


class Cert:
def __init__(
self, round, period, step, proposal, votes, equivocation_votes
):
self.round = round
self.period = period
self.step = step
self.proposal = proposal
self.votes = votes
self.equivocation_votes = equivocation_votes

@staticmethod
def undictify(d):
return Cert(
round=d.get("rnd", 0),
period=d.get("per", 0),
step=d.get("step", 0),
proposal=ProposalValue.undictify(d.get("prop")),
votes=[],
equivocation_votes=[],
)

def __str__(self):
return ostr(self)


class ProposalValue:
def __init__(
self, original_period, original_proposer, block_digest, encoding_digest
):
self.original_period = original_period
self.original_proposer = original_proposer
self.block_digest = block_digest
self.encoding_digest = encoding_digest

@staticmethod
def undictify(d):
return ProposalValue(
original_period=d.get("oper", 0),
original_proposer=encoding.encode_address(d.get("oprop")),
block_digest=d.get("dig"),
encoding_digest=d.get("encdig"),
)

def __str__(self):
return ostr(self)


def ostr(o):
return (
"{"
+ ", ".join(
[str(key) + ": " + str(value) for key, value in o.__dict__.items()]
)
+ "}"
)
89 changes: 63 additions & 26 deletions algosdk/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import msgpack
from Cryptodome.Hash import SHA512

from algosdk import auction, constants, error, transaction
from algosdk import auction, block, constants, error, transaction


def msgpack_encode(obj):
Expand Down Expand Up @@ -68,31 +68,68 @@ def msgpack_decode(enc):
"""
decoded = enc
if not isinstance(enc, dict):
decoded = msgpack.unpackb(base64.b64decode(enc), raw=False)
if "type" in decoded:
return transaction.Transaction.undictify(decoded)
if "l" in decoded:
return transaction.LogicSig.undictify(decoded)
if "msig" in decoded:
return transaction.MultisigTransaction.undictify(decoded)
if "lsig" in decoded:
if "txn" in decoded:
return transaction.LogicSigTransaction.undictify(decoded)
return transaction.LogicSigAccount.undictify(decoded)
if "sig" in decoded:
return transaction.SignedTransaction.undictify(decoded)
if "txn" in decoded:
return transaction.Transaction.undictify(decoded["txn"])
if "subsig" in decoded:
return transaction.Multisig.undictify(decoded)
if "txlist" in decoded:
return transaction.TxGroup.undictify(decoded)
if "t" in decoded:
return auction.NoteField.undictify(decoded)
if "bid" in decoded:
return auction.SignedBid.undictify(decoded)
if "auc" in decoded:
return auction.Bid.undictify(decoded)
decoded = algo_msgp_decode(base64.b64decode(enc))
return undictify(decoded)


def algo_msgp_decode(enc):
"""Performs msgpack decoding on an Algorand object. Extra care is
taken so that some internal fields that are marked as strings are
decoded without utf-8 processing, because they aren't utf-8. Yet,
we want most string like values to become Python str types for
simplicity.

"""
raw = msgpack.unpackb(enc, raw=True, strict_map_key=False)
return cook(raw)


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

if isinstance(raw, dict):
cooked = {}
for key, value in raw.items():
v = value if key in stop else cook(value)
v = v.decode() if key in safe else v
if type(key) is bytes:
cooked[key.decode()] = v
else:
cooked[key] = v
return cooked
if isinstance(raw, list):
return [cook(item) for item in raw]
return raw


def undictify(d):
if "type" in d:
return transaction.Transaction.undictify(d)
if "l" in d:
return transaction.LogicSig.undictify(d)
if "msig" in d:
return transaction.MultisigTransaction.undictify(d)
if "lsig" in d:
if "txn" in d:
return transaction.LogicSigTransaction.undictify(d)
return transaction.LogicSigAccount.undictify(d)
if "sig" in d:
return transaction.SignedTransaction.undictify(d)
if "txn" in d:
return transaction.Transaction.undictify(d["txn"])
if "subsig" in d:
return transaction.Multisig.undictify(d)
if "txlist" in d:
return transaction.TxGroup.undictify(d)
if "t" in d:
return auction.NoteField.undictify(d)
if "bid" in d:
return auction.SignedBid.undictify(d)
if "auc" in d:
return auction.Bid.undictify(d)
if "block" in d:
return block.BlockInfo.undictify(d)


def is_valid_address(addr):
Expand Down
Loading
Loading