def getMerkle( self, holderArg: blspy.PublicKey, start: int, end: int = -1 ) -> bytes: holder: bytes = holderArg.serialize() merkle: List[bytes] = [] end += 1 if end == 0: end = len(self.holders[holder]) for e in range(start, end): elem: Element = self.holders[holder][e] if isinstance(elem, Verification): merkle.append( blake2b( elem.prefix + Verification.serialize(Verification.fromElement(elem)), digest_size=48 ).digest() ) elif isinstance(elem, MeritRemoval): if merkle: raise Exception("Creating a merkle with a MeritRemoval despite the merkle already having elements.") merkle.append( blake2b( elem.prefix + MeritRemoval.serialize(MeritRemoval.fromElement(elem)), digest_size=48 ).digest() ) if e != end - 1: raise Exception("Creating a merkle with a MeritRemoval despite the merkle having more elements after this.") else: raise Exception("MeritHolder has an unsupported Element type: " + type(elem).__name__) if not merkle: return b'\0' * 48 while len(merkle) != 1: if len(merkle) % 2 == 1: merkle.append(merkle[-1]) for m in range(0, len(merkle), 2): merkle[m // 2] = blake2b( merkle[m] + merkle[m + 1], digest_size=48 ).digest() merkle = merkle[: len(merkle) // 2] return merkle[0]
def fromJSON(json: Dict[str, Any]) -> Any: e1: Element = Element() if json["elements"][0]["descendant"] == "Verification": e1 = Verification.fromJSON(json["elements"][0]) e2: Element = Element() if json["elements"][1]["descendant"] == "Verification": e2 = Verification.fromJSON(json["elements"][1]) result: MeritRemoval = MeritRemoval(json["partial"], e1, e2) result.nonce = json["nonce"] return result
def updateTXs(self, holder: bytes, tips: Dict[bytes, int]) -> None: #If the holder doesn't have an entry, provide one. if holder not in self.allTips: self.allTips[holder] = -1 #Iterate over every new Element. for e in range(self.allTips[holder] + 1, tips[holder] + 1): elem: Element = self.consensus.holders[holder][e] #If it was a Verification, track the mentionmd transaction. if isinstance(elem, Verification): if Verification.fromElement(elem).hash not in self.synced: self.txs[Verification.fromElement(elem).hash] = True
def fromJSON( sendDiff: bytes, dataDiff: bytes, json: Dict[str, List[Dict[str, Any]]] ) -> Any: result = Consensus(sendDiff, dataDiff) for mh in json: for elem in json[mh]: if "signed" in elem: if elem["descendant"] == "Verification": result.add(SignedVerification.fromJSON(elem)) elif elem["descendant"] == "MeritRemoval": if elem["partial"]: result.add(PartiallySignedMeritRemoval.fromJSON(elem)) else: result.add(SignedMeritRemoval.fromJSON(elem)) else: raise Exception("JSON has an unsupported Element type: " + elem["descendant"]) else: if elem["descendant"] == "Verification": result.add(Verification.fromJSON(elem)) elif elem["descendant"] == "MeritRemoval": result.add(MeritRemoval.fromJSON(elem)) else: raise Exception("JSON has an unsupported Element type: " + elem["descendant"]) return result
def add(self, transactions: Transactions, consensus: Consensus, state: State, block: Block) -> List[Mint]: #Construct the new Epoch. epoch: Dict[bytes, List[bytes]] = {} for record in block.body.records: mh: bytes = record[0].serialize() start = 0 if mh in self.tips: start = self.tips[mh] self.tips[mh] = record[1] for e in range(start, record[1] + 1): if isinstance(consensus.holders[mh][e], Verification): tx: bytes = Verification.fromElement( consensus.holders[mh][e]).hash if not tx in epoch: epoch[tx] = [] epoch[tx].append(mh) #Move TXs belonging to an old Epoch to said Epoch. txs: List[bytes] = list(epoch.keys()) for tx in txs: for e in range(5): if tx in self.epochs[e]: self.epochs[e][tx] += epoch[tx] del epoch[tx] #Grab the oldest Epoch. self.epochs.append(epoch) epoch = self.epochs[0] del self.epochs[0] return self.score(transactions, state, epoch)
def fromJSON(keys: Dict[bytes, int], jsonArg: Dict[str, Any]) -> Any: json: Dict[str, Any] = dict(jsonArg) json["elements"] = list(json["elements"]) json["elements"][0] = dict(json["elements"][0]) json["elements"][1] = dict(json["elements"][1]) json["elements"][0]["holder"] = json["holder"] json["elements"][1]["holder"] = json["holder"] e1: Element = Verification(bytes(32), 0) if json["elements"][0]["descendant"] == "Verification": e1 = Verification.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "VerificationPacket": json["elements"][0]["holders"] = list( json["elements"][0]["holders"]) for h in range(len(json["elements"][0]["holders"])): json["elements"][0]["holders"][h] = keys[bytes.fromhex( json["elements"][0]["holders"][h])] e1 = VerificationPacket.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "SendDifficulty": e1 = SendDifficulty.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "DataDifficulty": e1 = DataDifficulty.fromJSON(json["elements"][0]) else: raise Exception( "Unknown Element used to construct a MeritRemoval.") e2: Element = Verification(bytes(32), 0) if json["elements"][1]["descendant"] == "Verification": e2 = Verification.fromJSON(json["elements"][1]) elif json["elements"][1]["descendant"] == "VerificationPacket": json["elements"][1]["holders"] = list( json["elements"][1]["holders"]) for h in range(len(json["elements"][1]["holders"])): json["elements"][1]["holders"][h] = keys[bytes.fromhex( json["elements"][1]["holders"][h])] e2 = VerificationPacket.fromJSON(json["elements"][1]) elif json["elements"][1]["descendant"] == "SendDifficulty": e2 = SendDifficulty.fromJSON(json["elements"][1]) elif json["elements"][1]["descendant"] == "DataDifficulty": e2 = DataDifficulty.fromJSON(json["elements"][1]) else: raise Exception( "Unknown Element used to construct a MeritRemoval.") return MeritRemoval(e1, e2, json["partial"])
def fromSignedJSON(keys: Dict[bytes, int], jsonArg: Dict[str, Any]) -> Any: json: Dict[str, Any] = dict(jsonArg) json["elements"] = list(json["elements"]) json["elements"][0] = dict(json["elements"][0]) json["elements"][1] = dict(json["elements"][1]) json["elements"][0]["holder"] = json["holder"] json["elements"][1]["holder"] = json["holder"] e1: Union[SignedVerification, SignedVerificationPacket, SignedSendDifficulty, SignedDataDifficulty] = SignedVerification(bytes(32), 0) if json["elements"][0]["descendant"] == "Verification": e1 = Verification.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "VerificationPacket": json["elements"][0]["holders"] = list( json["elements"][0]["holders"]) for h in range(len(json["elements"][0]["holders"])): json["elements"][0]["holders"][h] = keys[bytes.fromhex( json["elements"][0]["holders"][h])] e1 = VerificationPacket.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "SendDifficulty": e1 = SendDifficulty.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "DataDifficulty": e1 = DataDifficulty.fromJSON(json["elements"][0]) e2: Union[SignedVerification, SignedVerificationPacket, SignedSendDifficulty, SignedDataDifficulty] = SignedVerification(bytes(32), 0) if json["elements"][1]["descendant"] == "Verification": e2 = SignedVerification.fromSignedJSON(json["elements"][1]) elif json["elements"][1]["descendant"] == "VerificationPacket": json["elements"][1]["holders"] = list( json["elements"][1]["holders"]) for h in range(len(json["elements"][1]["holders"])): json["elements"][1]["holders"][h] = keys[bytes.fromhex( json["elements"][1]["holders"][h])] e2 = SignedVerificationPacket.fromSignedJSON(json["elements"][1]) elif json["elements"][1]["descendant"] == "SendDifficulty": e2 = SignedSendDifficulty.fromSignedJSON(json["h"][1]) elif json["elements"][1]["descendant"] == "DataDifficulty": e2 = SignedDataDifficulty.fromSignedJSON(json["elements"][1]) return PartialMeritRemoval(e1, e2)
def fromJSON(json: Dict[str, Any]) -> Any: e1: Element = Element() if json["elements"][0]["descendant"] == "Verification": e1 = Verification.fromJSON(json["elements"][0]) else: raise Exception( "MeritRemoval constructed from an unsupported type of Element: " + json["elements"][0]["descendant"]) se2: SignedElement = SignedElement() if json["elements"][1]["descendant"] == "Verification": se2 = SignedVerification.fromJSON(json["elements"][1]) else: raise Exception( "MeritRemoval constructed from an unsupported type of Element: " + json["elements"][1]["descendant"]) result: PartiallySignedMeritRemoval = PartiallySignedMeritRemoval( e1, se2) result.nonce = json["nonce"] return result
def fromSignedJSON(jsonArg: Dict[str, Any]) -> Any: json: Dict[str, Any] = dict(jsonArg) json["elements"] = list(json["elements"]) json["elements"][0] = dict(json["elements"][0]) json["elements"][1] = dict(json["elements"][1]) json["elements"][0]["holder"] = json["holder"] json["elements"][1]["holder"] = json["holder"] e1: Union[SignedVerification, SignedMeritRemovalVerificationPacket, SignedSendDifficulty, SignedDataDifficulty] = SignedVerification(bytes(32), 0) if json["elements"][0]["descendant"] == "Verification": e1 = Verification.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "VerificationPacket": e1 = SignedMeritRemovalVerificationPacket.fromJSON( json["elements"][0]) elif json["elements"][0]["descendant"] == "SendDifficulty": e1 = SendDifficulty.fromJSON(json["elements"][0]) elif json["elements"][0]["descendant"] == "DataDifficulty": e1 = DataDifficulty.fromJSON(json["elements"][0]) e2: Union[SignedVerification, SignedMeritRemovalVerificationPacket, SignedSendDifficulty, SignedDataDifficulty] = SignedVerification(bytes(32), 0) if json["elements"][1]["descendant"] == "Verification": e2 = SignedVerification.fromSignedJSON(json["elements"][1]) elif json["elements"][1]["descendant"] == "VerificationPacket": e2 = SignedMeritRemovalVerificationPacket.fromSignedJSON( json["elements"][1]) elif json["elements"][1]["descendant"] == "SendDifficulty": e2 = SignedSendDifficulty.fromSignedJSON(json["elements"][1]) elif json["elements"][1]["descendant"] == "DataDifficulty": e2 = SignedDataDifficulty.fromSignedJSON(json["elements"][1]) return PartialMeritRemoval(e1, e2, json["holder"])
blsPrivKey1: blspy.PrivateKey = blspy.PrivateKey.from_seed(b'\0') blsPubKey1: blspy.PublicKey = blsPrivKey1.get_public_key() blsPrivKey2: blspy.PrivateKey = blspy.PrivateKey.from_seed(b'\1') blsPubKey2: blspy.PublicKey = blsPrivKey2.get_public_key() #Give the second key pair Merit. block: Block = Block( BlockHeader(13, blockchain.last(), int(time())), BlockBody([], [(blsPubKey2, 100)]) ) block.mine(blockchain.difficulty()) blockchain.add(block) print("Generated Competing Block " + str(block.header.nonce) + ".") #Grab the claim hash. claim: bytes = Verification.fromElement(consensus.holders[blsPubKey1.serialize()][1]).hash #Create two competing Sends. send1: Send = Send( [(claim, 0)], [( edPubKey1.to_bytes(), Claim.fromTransaction(transactions.txs[claim]).amount )] ) send1.sign(edPrivKey1) send1.beat(consensus.sendFilter) send1.verified = True transactions.add(send1) send2: Send = Send(