Skip to content

Commit ae58c01

Browse files
authored
Update sample code and add poller section
1 parent 11fadf0 commit ae58c01

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

articles/confidential-ledger/quickstart-python.md

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ network_identity = identity_client.get_ledger_identity(
166166

167167
ledger_tls_cert_file_name = "networkcert.pem"
168168
with open(ledger_tls_cert_file_name, "w") as cert_file:
169-
cert_file.write(network_identity.ledger_tls_certificate)
169+
cert_file.write(network_identity['ledgerTlsCertificate'])
170170
```
171171

172172
Now we can use the network certificate, along with the ledger URL and our credentials, to create a confidential ledger client.
@@ -179,12 +179,10 @@ ledger_client = ConfidentialLedgerClient(
179179
)
180180
```
181181

182-
We are prepared to write to the ledger. We will do so using the `create_ledger_entry` function.
182+
We are prepared to write to the ledger. We will do so using the `create_ledger_entry` function.
183183

184184
```python
185-
sample_entry = {
186-
"contents": "Hello world!"
187-
}
185+
sample_entry = {"contents": "Hello world!"}
188186
append_result = ledger_client.create_ledger_entry(entry=sample_entry)
189187
print(append_result['transactionId'])
190188
```
@@ -196,7 +194,14 @@ latest_entry = ledger_client.get_ledger_entry(transaction_id=append_result['tran
196194
print(f"Current entry (transaction id = {latest_entry['transactionId']}) in collection {latest_entry['collectionId']}: {latest_entry['contents']}")
197195
```
198196

199-
The print function will return "Hello world!", as that is the message in the ledger that that corresponds to the transaction ID.
197+
If you would simply like the latest transaction that was committed to the ledger, you can use the `get_current_ledger_entry` function.
198+
199+
```python
200+
latest_entry = ledger_client.get_current_ledger_entry()
201+
print(f"Current entry (transaction id = {latest_entry['transactionId']}) in collection {latest_entry['collectionId']}: {latest_entry['contents']}")
202+
```
203+
204+
The print function will return "Hello world!", as that is the message in the ledger that that corresponds to the transaction ID and is the latest transaction.
200205

201206
## Full sample code
202207

@@ -286,33 +291,33 @@ ledger_client = ConfidentialLedgerClient(
286291
)
287292

288293
# Write to the ledger
289-
sample_entry = {
290-
"contents": "Hello world!"
291-
}
292-
append_result = ledger_client.create_ledger_entry(entry=sample_entry)
293-
print(append_result['transactionId'])
294-
295-
# Wait until transaction is committed on the ledger
296-
while True:
297-
commit_result = ledger_client.get_transaction_status(append_result['transactionId'])
298-
print(commit_result['state'])
299-
if (commit_result['state'] == "Committed"):
300-
break
301-
time.sleep(1)
302-
303-
# Wait until entry is read from enclave and validated
304-
while True:
305-
latest_entry = ledger_client.get_ledger_entry(transaction_id=append_result['transactionId'])
306-
print(latest_entry['state'])
307-
if (latest_entry['state'] == "Ready"):
308-
break
309-
time.sleep(1)
294+
sample_entry = {"contents": "Hello world!"}
295+
ledger_client.create_ledger_entry(entry=sample_entry)
310296

311297
# Read from the ledger
312-
latest_entry = latest_entry['entry']
298+
latest_entry = ledger_client.get_current_ledger_entry()
313299
print(f"Current entry (transaction id = {latest_entry['transactionId']}) in collection {latest_entry['collectionId']}: {latest_entry['contents']}")
314300
```
315301

302+
## Pollers
303+
304+
If you would like to wait for your write transaction to be committed to your ledger you can use the `begin_create_ledger_entry` function. This will return a poller to wait until the your entry is durably committed.
305+
306+
```python
307+
sample_entry = {"contents": "Hello world!"}
308+
ledger_entry_poller = ledger_client.begin_create_ledger_entry(
309+
entry=sample_entry
310+
)
311+
ledger_entry_result = ledger_entry_poller.result()
312+
```
313+
314+
Querying an older ledger entry requires the ledger to read the entry from disk and validate it. You can use the `begin_get_ledger_entry` function to create a poller that will wait until the queried entry is in a ready state to view.
315+
316+
```python
317+
get_entry_poller = ledger_client.begin_get_ledger_entry(transaction_id=ledger_entry_result['transactionId'])
318+
entry = get_entry_poller.result()
319+
```
320+
316321
## Clean up resources
317322

318323
Other Azure confidential ledger articles can build upon this quickstart. If you plan to continue on to work with subsequent quickstarts and tutorials, you may wish to leave these resources in place.

0 commit comments

Comments
 (0)