-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add objects to represent the return value from
block_info
There are a lot fields to still do (and the entire Cert object). This is a feeler to see if this will satisfy anyone.
- Loading branch information
Showing
6 changed files
with
261 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from algosdk import encoding, transaction | ||
|
||
|
||
class BlockInfo: | ||
def __init__(self, block, cert): | ||
self.block = block | ||
self.cert = cert | ||
pass | ||
|
||
@staticmethod | ||
def undictify(d): | ||
return BlockInfo( | ||
encoding.undictify(d.get("block")), | ||
encoding.undictify(d.get("cert")), | ||
) | ||
|
||
def __str__(self): | ||
return ( | ||
"{" | ||
+ ", ".join( | ||
[ | ||
str(key) + ": " + str(value) | ||
for key, value in self.__dict__.items() | ||
] | ||
) | ||
+ "}" | ||
) | ||
|
||
|
||
class Block: | ||
def __init__( | ||
self, | ||
round, | ||
branch, | ||
seed, | ||
commit, | ||
sha256commit, | ||
timestamp, | ||
genesis_id, | ||
genesis_hash, | ||
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.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"), | ||
counter=d.get("tc", 0), | ||
payset=stxns, | ||
) | ||
|
||
def __str__(self): | ||
return ( | ||
"{" | ||
+ ", ".join( | ||
[ | ||
str(key) + ": " + str(value) | ||
for key, value in self.__dict__.items() | ||
] | ||
) | ||
+ "}" | ||
) | ||
|
||
|
||
class Cert: | ||
def __init__(self): | ||
pass | ||
|
||
@staticmethod | ||
def undictify(d): | ||
return Cert() |
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
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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import algosdk.transaction as txn | ||
import algosdk.encoding as enc | ||
from utils import algod_env, get_algod_client | ||
|
||
algod = get_algod_client(*algod_env()) | ||
latest = algod.status().get("last-round") | ||
|
||
# Take an existing SDK return value, and turn it into a nice object | ||
bi = algod.block_info(latest, "msgpack") | ||
block = enc.undictify(enc.algo_msgp_decode(bi)) | ||
# walk the payset | ||
for txn in block.block.payset: | ||
print(txn.stxn.transaction.sender) | ||
|
||
|
||
# Or we can add new calls that coerce the msgp before giving it out | ||
for txn in algod.block(latest - 1).block.payset: | ||
print(txn.stxn.transaction.sender) |