-
Notifications
You must be signed in to change notification settings - Fork 225
Remove /gov/read
and /gov/query
endpoints
#2442
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c7bb1d7
Remove call to /gov/read for reconfiguration
2047a5f
Service
614e8e8
Retrieve specific public entry from ledger
2c9dcef
Fix JS
7449149
WIP JWT
3465a95
Merge remote-tracking branch 'upstream/main' into remove_gov_read_1
93eda50
Works!
8e0a81e
Remove usage of /gov/query
dfe348c
Almost done
3b0b4c5
Cleanup
de00196
Merge branch 'main' into remove_gov_read_1
jumaffre 854298d
Cross ts and dot is
82a2e88
Merge branch 'remove_gov_read_1' of github.com:jumaffre/CCF into remo…
2a73429
Format
ee60cff
Constitution is a ServiceMap
fec8c4a
Oops
2b09d45
Ooops
776177f
Fixes
f848bda
Update python/ccf/ledger.py
jumaffre 9fd0c86
Update python/ledger_tutorial.py
jumaffre e2611f7
Merge branch 'main' into remove_gov_read_1
jumaffre e396713
Fix e2e test
f1fa42d
Merge branch 'remove_gov_read_1' of github.com:jumaffre/CCF into remo…
4b63c33
Merge branch 'main' into remove_gov_read_1
jumaffre bff027f
Format
a37b6ca
Merge branch 'remove_gov_read_1' of github.com:jumaffre/CCF into remo…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
import struct | ||
import os | ||
|
||
from typing import BinaryIO, NamedTuple, Optional | ||
from typing import BinaryIO, NamedTuple, Optional, Tuple, Dict | ||
|
||
import json | ||
import base64 | ||
|
@@ -31,6 +31,9 @@ | |
SIGNATURE_TX_TABLE_NAME = "public:ccf.internal.signatures" | ||
NODES_TABLE_NAME = "public:ccf.gov.nodes.info" | ||
|
||
# Key used by CCF to record single-key tables | ||
WELL_KNOWN_SINGLETON_TABLE_KEY = bytes(bytearray(8)) | ||
|
||
|
||
def to_uint_32(buffer): | ||
return struct.unpack("@I", buffer)[0] | ||
|
@@ -420,8 +423,7 @@ def __next__(self): | |
self._ledger_validator.add_transaction(self) | ||
|
||
return self | ||
except Exception as exception: | ||
LOG.exception(f"Encountered exception: {exception}") | ||
except: | ||
raise | ||
|
||
|
||
|
@@ -466,10 +468,14 @@ class Ledger: | |
_current_chunk: LedgerChunk | ||
_ledger_validator: LedgerValidator | ||
|
||
def _reset_iterators(self): | ||
self._fileindex = -1 | ||
# Initialize LedgerValidator instance which will be passed to LedgerChunks. | ||
self._ledger_validator = LedgerValidator() | ||
|
||
def __init__(self, directory: str): | ||
|
||
self._filenames = [] | ||
self._fileindex = -1 | ||
|
||
ledgers = os.listdir(directory) | ||
# Sorts the list based off the first number after ledger_ so that | ||
|
@@ -485,8 +491,7 @@ def __init__(self, directory: str): | |
if os.path.isfile(os.path.join(directory, chunk)): | ||
self._filenames.append(os.path.join(directory, chunk)) | ||
|
||
# Initialize LedgerValidator instance which will be passed to LedgerChunks. | ||
self._ledger_validator = LedgerValidator() | ||
self._reset_iterators() | ||
|
||
def __next__(self) -> LedgerChunk: | ||
self._fileindex += 1 | ||
|
@@ -513,6 +518,68 @@ def last_verified_txid(self): | |
self._ledger_validator.last_verified_seqno, | ||
) | ||
|
||
def get_transaction(self, seqno: int) -> Transaction: | ||
""" | ||
Returns the :py:class:`ccf.Ledger.Transaction` recorded in the ledger at the given sequence number | ||
|
||
Note that the transaction returned may not yet be verified by a | ||
signature transaction nor committed by the service. | ||
|
||
:param int seqno: Sequence number of the transaction to fetch | ||
|
||
:return: :py:class:`ccf.Ledger.Transaction` | ||
""" | ||
if seqno < 1: | ||
raise ValueError("Ledger first seqno is 1") | ||
|
||
self._reset_iterators() | ||
|
||
transaction = None | ||
try: | ||
for chunk in self: | ||
for tx in chunk: | ||
public_transaction = tx.get_public_domain() | ||
if public_transaction.get_seqno() == seqno: | ||
return tx | ||
finally: | ||
self._reset_iterators() | ||
|
||
if transaction is None: | ||
raise UnknownTransaction( | ||
f"Transaction at seqno {seqno} does not exist in ledger" | ||
) | ||
return transaction | ||
|
||
def get_latest_public_state(self) -> Tuple[dict, int]: | ||
""" | ||
Returns the current public state of the service. | ||
|
||
Note that the public state returned may not yet be verified by a | ||
signature transaction nor committed by the service. | ||
|
||
:return: Tuple[Dict, int]: Tuple containing a dictionnary of public tables and their values and the seqno of the state read from the ledger. | ||
jumaffre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
self._reset_iterators() | ||
|
||
public_tables: Dict[str, Dict] = {} | ||
latest_seqno = 0 | ||
for chunk in self: | ||
for tx in chunk: | ||
latest_seqno = tx.get_public_domain().get_seqno() | ||
for table_name, records in tx.get_public_domain().get_tables().items(): | ||
if table_name in public_tables: | ||
public_tables[table_name].update(records) | ||
# Remove deleted keys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the latest state does not include keys deleted keys (value of |
||
public_tables[table_name] = { | ||
k: v | ||
for k, v in public_tables[table_name].items() | ||
if v is not None | ||
} | ||
else: | ||
public_tables[table_name] = records | ||
|
||
return public_tables, latest_seqno | ||
|
||
|
||
class InvalidRootException(Exception): | ||
"""MerkleTree root doesn't match with the root reported in the signature's table""" | ||
|
@@ -528,3 +595,7 @@ class CommitIdRangeException(Exception): | |
|
||
class UntrustedNodeException(Exception): | ||
"""The signing node wasn't part of the network""" | ||
|
||
|
||
class UnknownTransaction(Exception): | ||
"""The transaction at seqno does not exist in ledger""" |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-critical: This iteration is going to be very slow. Could we rely on the filenames here to scan to the relevant chunk, and parse it in isolation? And even within that, could we skip over other transactions rather than parsing them? Not needed for this PR or current tests, but will likely bite us if we start using this
ledger
util in more places/on real service ledgers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I should have mentioned it in the comment. I wanted the fetching to still verify the integrity of the ledger and we currently have no way to do that without starting from the very beginning of the ledger. I'll create a separate ticket and add a comment in the docstring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eddyashton See first item in #2453 for follow-up improvements.