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 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 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]
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(