mirror of https://github.com/nucypher/nucypher.git
Remove storage of node's transcript bytes for each ritual - it isn't used
Allow caching of list of validators used in round 1 - allows potential reuse for round 2, and during threshold decryption if needed. Allow caching of node's decryption share for a completed ritual - allows for reuse for the same ritual. Use named constants as key values for entries in DKGStorage - just simpler.remotes/origin/v7.4.x
parent
6fe1a9462c
commit
f767865131
|
@ -1,45 +1,66 @@
|
|||
from collections import defaultdict
|
||||
from typing import Optional
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from hexbytes import HexBytes
|
||||
from nucypher_core.ferveo import AggregatedTranscript, Transcript
|
||||
from nucypher_core.ferveo import (
|
||||
AggregatedTranscript,
|
||||
DecryptionSharePrecomputed,
|
||||
DecryptionShareSimple,
|
||||
Validator,
|
||||
)
|
||||
|
||||
|
||||
class DKGStorage:
|
||||
"""A simple in-memory storage for DKG data"""
|
||||
|
||||
# round 1
|
||||
KEY_TRANSCRIPT_TXS = "transcript_tx_hashes"
|
||||
KEY_VALIDATORS = "validators"
|
||||
# round 2
|
||||
KEY_AGGREGATED_TXS = "aggregation_tx_hashes"
|
||||
KEY_AGGREGATED_TRANSCRIPTS = "aggregated_transcripts"
|
||||
# active rituals
|
||||
KEY_DECRYPTION_SHARE = "decryption_share"
|
||||
|
||||
def __init__(self):
|
||||
self.data = defaultdict(dict)
|
||||
|
||||
def store_transcript(self, ritual_id: int, transcript: Transcript) -> None:
|
||||
self.data["transcripts"][ritual_id] = bytes(transcript)
|
||||
|
||||
def get_transcript(self, ritual_id: int) -> Optional[Transcript]:
|
||||
data = self.data["transcripts"].get(ritual_id)
|
||||
if not data:
|
||||
return None
|
||||
transcript = Transcript.from_bytes(data)
|
||||
return transcript
|
||||
|
||||
#
|
||||
# DKG Round 1 - Transcripts
|
||||
#
|
||||
def store_transcript_txhash(self, ritual_id: int, txhash: HexBytes) -> None:
|
||||
self.data["transcript_tx_hashes"][ritual_id] = txhash
|
||||
self.data[self.KEY_TRANSCRIPT_TXS][ritual_id] = txhash
|
||||
|
||||
def clear_transcript_txhash(self, ritual_id: int, txhash: HexBytes) -> bool:
|
||||
if self.get_transcript_txhash(ritual_id) == txhash:
|
||||
del self.data["transcript_tx_hashes"][ritual_id]
|
||||
del self.data[self.KEY_TRANSCRIPT_TXS][ritual_id]
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_transcript_txhash(self, ritual_id: int) -> Optional[HexBytes]:
|
||||
return self.data["transcript_tx_hashes"].get(ritual_id)
|
||||
return self.data[self.KEY_TRANSCRIPT_TXS].get(ritual_id)
|
||||
|
||||
def store_aggregated_transcript(self, ritual_id: int, aggregated_transcript: AggregatedTranscript) -> None:
|
||||
self.data["aggregated_transcripts"][ritual_id] = bytes(aggregated_transcript)
|
||||
def store_validators(self, ritual_id: int, validators: List[Validator]) -> None:
|
||||
self.data[self.KEY_VALIDATORS][ritual_id] = list(validators)
|
||||
|
||||
def get_validators(self, ritual_id: int) -> Optional[List[Validator]]:
|
||||
validators = self.data[self.KEY_VALIDATORS].get(ritual_id)
|
||||
return list(validators)
|
||||
|
||||
#
|
||||
# DKG Round 2 - Aggregation
|
||||
#
|
||||
def store_aggregated_transcript(
|
||||
self, ritual_id: int, aggregated_transcript: AggregatedTranscript
|
||||
) -> None:
|
||||
self.data[self.KEY_AGGREGATED_TRANSCRIPTS][ritual_id] = bytes(
|
||||
aggregated_transcript
|
||||
)
|
||||
|
||||
def get_aggregated_transcript(
|
||||
self, ritual_id: int
|
||||
) -> Optional[AggregatedTranscript]:
|
||||
data = self.data["aggregated_transcripts"].get(ritual_id)
|
||||
data = self.data[self.KEY_AGGREGATED_TRANSCRIPTS].get(ritual_id)
|
||||
if not data:
|
||||
return None
|
||||
|
||||
|
@ -47,19 +68,28 @@ class DKGStorage:
|
|||
return aggregated_transcript
|
||||
|
||||
def store_aggregation_txhash(self, ritual_id: int, txhash: HexBytes) -> None:
|
||||
self.data["aggregation_tx_hashes"][ritual_id] = txhash
|
||||
self.data[self.KEY_AGGREGATED_TXS][ritual_id] = txhash
|
||||
|
||||
def clear_aggregated_txhash(self, ritual_id: int, txhash: HexBytes) -> bool:
|
||||
if self.get_aggregation_txhash(ritual_id) == txhash:
|
||||
del self.data["aggregation_tx_hashes"][ritual_id]
|
||||
del self.data[self.KEY_AGGREGATED_TXS][ritual_id]
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_aggregation_txhash(self, ritual_id: int) -> Optional[HexBytes]:
|
||||
return self.data["aggregation_tx_hashes"].get(ritual_id)
|
||||
return self.data[self.KEY_AGGREGATED_TXS].get(ritual_id)
|
||||
|
||||
def store_public_key(self, ritual_id: int, public_key: bytes) -> None:
|
||||
self.data["public_keys"][ritual_id] = public_key
|
||||
#
|
||||
# Completed DKG
|
||||
#
|
||||
def store_decryption_share(
|
||||
self,
|
||||
ritual_id: int,
|
||||
decryption_share: Union[DecryptionShareSimple, DecryptionSharePrecomputed],
|
||||
) -> None:
|
||||
self.data[self.KEY_DECRYPTION_SHARE][ritual_id] = decryption_share
|
||||
|
||||
def get_public_key(self, ritual_id: int) -> Optional[bytes]:
|
||||
return self.data["public_keys"].get(ritual_id)
|
||||
def get_decryption_share(
|
||||
self, ritual_id: int
|
||||
) -> Optional[Union[DecryptionShareSimple, DecryptionSharePrecomputed]]:
|
||||
self.data[self.KEY_DECRYPTION_SHARE].get(ritual_id)
|
||||
|
|
Loading…
Reference in New Issue