def testBlockchain(b: int) -> None: #Data. data: Data = Data.fromJSON(vectors["data"]) #pylint: disable=no-member #MeritRemoval. removal: SignedMeritRemoval = SignedMeritRemoval.fromSignedJSON( vectors["removals"][b]) #Create and execute a Liver to send the MeritRemoval. def sendMeritRemoval() -> None: #Send the Data. if rpc.meros.liveTransaction(data) != rpc.meros.live.recv(): raise TestError("Meros didn't send back the Data.") rpc.meros.signedElement(removal) try: if len(rpc.meros.live.recv()) != 0: raise Exception() except TestError: raise SuccessError( "Meros rejected our MeritRemoval created from the same Element." ) except Exception: raise TestError( "Meros accepted our MeritRemoval created from the same Element." ) Liver(rpc, vectors["blockchain"], callbacks={ 1: sendMeritRemoval }).live()
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 InvalidCompetingTest( rpc: RPC ) -> None: file: IO[Any] = open("PythonTests/Vectors/Consensus/MeritRemoval/InvalidCompeting.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() keys: Dict[bytes, int] = { bytes.fromhex(vectors["blockchain"][0]["header"]["miner"]): 0 } #Transactions. transactions: Transactions = Transactions.fromJSON(vectors["transactions"]) #MeritRemoval. #pylint: disable=no-member removal: SignedMeritRemoval = SignedMeritRemoval.fromSignedJSON(keys, vectors["removal"]) #Create and execute a Liver to handle the MeritRemoval. def sendMeritRemoval() -> None: #Send and verify the MeritRemoval. removalBytes: bytes = rpc.meros.signedElement(removal) done: bool = False while True: try: msg: bytes = rpc.meros.recv() except TestError: raise TestError("Node disconnected us.") if MessageType(msg[0]) == MessageType.Syncing: rpc.meros.syncingAcknowledged() elif MessageType(msg[0]) == MessageType.TransactionRequest: rpc.meros.transaction(transactions.txs[msg[1 : 33]]) elif MessageType(msg[0]) == MessageType.SyncingOver: if done: break done = True if removalBytes != rpc.meros.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 11, 11, removal.holder, True) Liver( rpc, vectors["blockchain"], transactions, callbacks={ 11: sendMeritRemoval, 12: lambda: verifyMeritRemoval(rpc, 11, 11, removal.holder, False) } ).live() #Create and execute a Syncer to handle the MeritRemoval. Syncer(rpc, vectors["blockchain"], transactions).sync() verifyMeritRemoval(rpc, 11, 11, removal.holder, False)
def MultipleTest( rpc: RPC ) -> None: file: IO[Any] = open("PythonTests/Vectors/Consensus/MeritRemoval/Multiple.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #MeritRemovals. removals: List[SignedMeritRemoval] = [ SignedMeritRemoval.fromSignedJSON(vectors["removals"][0]), SignedMeritRemoval.fromSignedJSON(vectors["removals"][1]) ] #Send and verify the MeritRemoval. def sendMeritRemovals() -> None: removalBuf: bytes = rpc.meros.signedElement(removals[0]) if removalBuf != rpc.meros.live.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, removals[0].holder, True) rpc.meros.signedElement(removals[1]) if removalBuf != rpc.meros.live.recv(): raise TestError("Meros didn't send us the first Merit Removal.") verifyMeritRemoval(rpc, 1, 1, removals[0].holder, True) #Verify the holder has 0 Merit and is marked as malicious. def verifyFirstMeritRemoval() -> None: verifyMeritRemoval(rpc, 0, 0, removals[0].holder, True) #Create and execute a Liver to handle the Signed MeritRemovals. Liver( rpc, vectors["blockchain"], callbacks={ 1: sendMeritRemovals, 2: verifyFirstMeritRemoval, 3: lambda: verifyMeritRemoval(rpc, 0, 0, removals[0].holder, False) } ).live() #Create and execute a Syncer to handle a Signed MeritRemoval. Syncer(rpc, vectors["blockchain"]).sync() verifyMeritRemoval(rpc, 0, 0, removals[0].holder, False)
def SameNonceTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/SameNonce.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #MeritRemoval. #pylint: disable=no-member removal: SignedMeritRemoval = SignedMeritRemoval.fromSignedJSON( vectors["removal"]) #Create and execute a Liver to cause a Signed MeritRemoval. def sendElements() -> None: #Send the Elements. rpc.meros.signedElement(removal.se1) rpc.meros.signedElement(removal.se2) #Verify the first Element. if rpc.meros.live.recv() != (MessageType.SignedDataDifficulty.toByte() + removal.se1.signedSerialize()): raise TestError("Meros didn't send us the first Data Difficulty.") #Verify the MeritRemoval. if rpc.meros.live.recv() != (MessageType.SignedMeritRemoval.toByte() + removal.signedSerialize()): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, removal.holder, True) Liver(rpc, vectors["blockchain"], callbacks={ 1: sendElements, 2: lambda: verifyMeritRemoval(rpc, 1, 1, removal.holder, False) }).live() #Create and execute a Liver to handle a Signed MeritRemoval. def sendMeritRemoval() -> None: #Send and verify the MeritRemoval. if rpc.meros.signedElement(removal) != rpc.meros.live.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, removal.holder, True) Liver(rpc, vectors["blockchain"], callbacks={ 1: sendMeritRemoval, 2: lambda: verifyMeritRemoval(rpc, 1, 1, removal.holder, False) }).live() #Create and execute a Syncer to handle a Signed MeritRemoval. Syncer(rpc, vectors["blockchain"]).sync() verifyMeritRemoval(rpc, 1, 1, removal.holder, False)
def InvalidCompetingTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/InvalidCompeting.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #Transactions. transactions: Transactions = Transactions.fromJSON(vectors["transactions"]) #MeritRemoval. #pylint: disable=no-member removal: SignedMeritRemoval = SignedMeritRemoval.fromSignedJSON( vectors["removal"]) #Create and execute a Liver to handle the MeritRemoval. def sendMeritRemoval() -> None: #Send and verify the MeritRemoval. removalBytes: bytes = rpc.meros.signedElement(removal) sent: int = 0 while True: if sent == 2: break msg: bytes = rpc.meros.sync.recv() if MessageType(msg[0]) == MessageType.TransactionRequest: rpc.meros.syncTransaction(transactions.txs[msg[1:33]]) sent += 1 else: raise TestError("Unexpected message sent: " + msg.hex().upper()) if removalBytes != rpc.meros.live.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, removal.holder, True) #Verify the MeritRemoval and the Blockchain. def verify() -> None: verifyMeritRemoval(rpc, 1, 1, removal.holder, False) verifyBlockchain(rpc, Blockchain.fromJSON(vectors["blockchain"])) raise SuccessError( "MeritRemoval and Blockchain were properly handled.") Liver(rpc, vectors["blockchain"], transactions, callbacks={ 1: sendMeritRemoval, 2: verify }).live()
def HundredThirtyFiveTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/HundredThirtyFive.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #Datas. datas: List[Data] = [ Data.fromJSON(vectors["datas"][0]), Data.fromJSON(vectors["datas"][1]), Data.fromJSON(vectors["datas"][2]) ] #Transactions. transactions: Transactions = Transactions() for data in datas: transactions.add(data) #First MeritRemoval. mr: SignedMeritRemoval = SignedMeritRemoval.fromSignedJSON( vectors["removal"]) def sendMeritRemoval() -> None: #Send the Datas. for data in datas: if rpc.meros.liveTransaction(data) != rpc.meros.live.recv(): raise TestError("Meros didn't send us the Data.") #Send and verify the original MeritRemoval. if rpc.meros.signedElement(mr) != rpc.meros.live.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, mr.holder, True) Liver(rpc, vectors["blockchain"], transactions, callbacks={ 1: sendMeritRemoval }).live()
block.mine(blsPrivKey, blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Same Nonce Block " + str(len(blockchain.blocks)) + ".") #Create a DataDifficulty. dataDiff: SignedDataDifficulty = SignedDataDifficulty(3, 0) dataDiff.sign(0, blsPrivKey) #Create a conflicting DataDifficulty with the same nonce. dataDiffConflicting = SignedDataDifficulty(1, 0) dataDiffConflicting.sign(0, blsPrivKey) #Create a MeritRemoval out of the two of them. mr: SignedMeritRemoval = SignedMeritRemoval(dataDiff, dataDiffConflicting) #Generate a Block containing the MeritRemoval. block = Block( BlockHeader(0, blockchain.last(), BlockHeader.createContents([], [mr]), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200), BlockBody([], [mr], mr.signature)) #Mine it. block.mine(blsPrivKey, blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Same Nonce Block " + str(len(blockchain.blocks)) + ".") result: Dict[str, Any] = {
datas.append(Data(datas[0].hash, b"Initial Data.")) datas.append(Data(datas[0].hash, b"Second Data.")) for data in datas: data.sign(edPrivKey) data.beat(spamFilter) #Create Verifications for all 3. verifs: List[SignedVerification] = [] packets: List[VerificationPacket] = [] for data in datas: verifs.append(SignedVerification(data.hash, 0)) verifs[-1].sign(0, blsPrivKey) packets.append(VerificationPacket(data.hash, [0])) #Create a MeritRemoval out of the conflicting Verifications. mr: SignedMeritRemoval = SignedMeritRemoval(verifs[1], verifs[2]) #Generate a Block containing the MeritRemoval. block = Block( BlockHeader( 0, blockchain.last(), BlockHeader.createContents([], packets, [mr]), 1, bytes(4), BlockHeader.createSketchCheck(bytes(4), packets), 0, blockchain.blocks[-1].header.time + 1200 ), BlockBody( packets,
def testBlockchain(i: int) -> None: #First MeritRemoval. mr: SignedMeritRemoval = SignedMeritRemoval.fromSignedJSON( vectors["removals"][i]) def sendMeritRemoval() -> None: #Send the Datas. for data in datas: if rpc.meros.liveTransaction(data) != rpc.meros.live.recv(): raise TestError("Meros didn't send us the Data.") #Send and verify the MeritRemoval. if rpc.meros.signedElement(mr) != rpc.meros.live.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, mr.holder, True) def sendRepeatMeritRemoval() -> None: #Send the Block containing the modified Merit Removal. block: Block = Block.fromJSON(vectors["blockchains"][i][-1]) rpc.meros.liveBlockHeader(block.header) #Flag of if the Block's Body synced. blockBodySynced: bool = False #Handle sync requests. reqHash: bytes = bytes() while True: if blockBodySynced: #Sleep for a second so Meros handles the Block. sleep(1) #Try receiving from the Live socket, where Meros sends keep-alives. try: if len(rpc.meros.live.recv()) != 0: raise Exception() except TestError: #Verify the height is 3. #The genesis Block, the Block granting Merit, and the Block containing the MeritRemoval originally. try: if rpc.call("merit", "getHeight") != 3: raise Exception() except Exception: raise TestError( "Node added a Block containg a repeat MeritRemoval." ) #Since the node didn't add the Block, raise SuccessError. raise SuccessError( "Node didn't add a Block containing a repeat MeritRemoval." ) except Exception: raise TestError("Meros sent a keep-alive.") msg: bytes = rpc.meros.sync.recv() if MessageType(msg[0]) == MessageType.BlockBodyRequest: reqHash = msg[1:33] if reqHash != block.header.hash: raise TestError( "Meros asked for a Block Body that didn't belong to the Block we just sent it." ) #Send the BlockBody. blockBodySynced = True rpc.meros.blockBody(block) else: raise TestError("Unexpected message sent: " + msg.hex().upper()) Liver(rpc, vectors["blockchains"][i], transactions, callbacks={ 1: sendMeritRemoval, 2: sendRepeatMeritRemoval }).live()
def SameNonceTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/SameNonce.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #Blockchain blockchain: Blockchain = Blockchain.fromJSON( b"MEROS_DEVELOPER_NETWORK", 60, int( "FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 16), vectors["blockchain"]) #MeritRemoval. removal: SignedMeritRemoval = SignedMeritRemoval.fromJSON( vectors["removal"]) #Consensus. consensus: Consensus = Consensus( bytes.fromhex( "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ), bytes.fromhex( "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" )) consensus.add(removal) #Data. data: Data = Data.fromJSON(vectors["data"]) #Create and execute a Liver to cause a SameNonce MeritRemoval. def sendElements() -> None: #Send the Data/SignedVerifications. if rpc.meros.transaction(data) != rpc.meros.recv(): raise TestError("Unexpected message sent.") if rpc.meros.signedElement(removal.se1) != rpc.meros.recv(): raise TestError("Unexpected message sent.") rpc.meros.signedElement(removal.se2) #Verify the MeritRemoval. if rpc.meros.recv() != (MessageType.SignedMeritRemoval.toByte() + removal.signedSerialize()): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal, True) Liver(rpc, blockchain, consensus, callbacks={ 1: sendElements, 2: lambda: verifyMeritRemoval(rpc, 1, 100, removal, False) }).live() #Create and execute a Liver to handle a SameNonce MeritRemoval. def sendMeritRemoval() -> None: #Send and verify the MeritRemoval. if rpc.meros.signedElement(removal) != rpc.meros.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal, True) Liver(rpc, blockchain, consensus, callbacks={ 1: sendMeritRemoval, 2: lambda: verifyMeritRemoval(rpc, 1, 100, removal, False) }).live() #Create and execute a Syncer to handle a SameNonce MeritRemoval. Syncer(rpc, blockchain, consensus).sync() verifyMeritRemoval(rpc, 1, 100, removal, False)
#Add it. blockchain.add(block) print("Generated Hundred Twenty Three Swap Block " + str(len(blockchain.blocks)) + ".") #Create conflicting Data Difficulties. dataDiffs: List[SignedDataDifficulty] = [ SignedDataDifficulty(3, 0), SignedDataDifficulty(4, 0) ] dataDiffs[0].sign(0, blsPrivKey) dataDiffs[1].sign(0, blsPrivKey) #Create a MeritRemoval out of the conflicting Data Difficulties. mr: SignedMeritRemoval = SignedMeritRemoval(dataDiffs[0], dataDiffs[1]) #Generate a Block containing the MeritRemoval. block = Block( BlockHeader(0, blockchain.last(), BlockHeader.createContents([], [mr]), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200), BlockBody([], [mr], mr.signature)) #Mine it. block.mine(blsPrivKey, blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Hundred Twenty Three Swap Block " + str(len(blockchain.blocks)) + ".")
def MultipleTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/Multiple.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #Blockchain blockchain: Blockchain = Blockchain.fromJSON( b"MEROS_DEVELOPER_NETWORK", 60, int( "FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 16), vectors["blockchain"]) #MeritRemovals. removal1: SignedMeritRemoval = SignedMeritRemoval.fromJSON( vectors["removal1"]) removal2: SignedMeritRemoval = SignedMeritRemoval.fromJSON( vectors["removal2"]) #Data. data: Data = Data.fromJSON(vectors["data"]) #Create and execute a Liver to cause multiple MeritRemovals. def sendElements() -> None: #Send the Data. if rpc.meros.transaction(data) != rpc.meros.recv(): raise TestError("Unexpected message sent.") #Send the first SignedElement. if rpc.meros.signedElement(removal1.se1) != rpc.meros.recv(): raise TestError("Unexpected message sent.") #Send the second. rpc.meros.signedElement(removal1.se2) #Verify the first MeritRemoval. if rpc.meros.recv() != (MessageType.SignedMeritRemoval.toByte() + removal1.signedSerialize()): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal1, True) #Send the third SignedElement. rpc.meros.signedElement(removal2.se2) #Meros should treat the first created MeritRemoval as the default MeritRemoval. if rpc.meros.recv() != (MessageType.SignedMeritRemoval.toByte() + removal1.signedSerialize()): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal1, True) Liver(rpc, blockchain, callbacks={ 1: sendElements, 2: lambda: verifyMeritRemoval(rpc, 1, 100, removal2, False) }).live() #Create and execute a Liver to handle multiple MeritRemovals. def sendMeritRemovals() -> None: #Send and verify the first MeritRemoval. msg = rpc.meros.signedElement(removal1) if msg != rpc.meros.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal1, True) #Send the second MeritRemoval. rpc.meros.signedElement(removal2) #Meros should treat the first created MeritRemoval as the default. if msg != rpc.meros.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal1, True) Liver(rpc, blockchain, callbacks={ 1: sendMeritRemovals, 2: lambda: verifyMeritRemoval(rpc, 1, 100, removal2, False) }).live()
transactions.add(claim) #Verify the Claim. verif = SignedVerification(claim.hash) verif.sign(0, blsPrivKey) #Create a Send using the same input as the Claim. send: Send = Send([(merit.mints[0].hash, 0)], [(edPubKey.to_bytes(), 1)]) transactions.add(send) #Verify the Send. competingVerif: SignedVerification = SignedVerification(send.hash) competingVerif.sign(0, blsPrivKey) #Create a MeritRemoval out of the conflicting Verifications. mr: SignedMeritRemoval = SignedMeritRemoval(verif, competingVerif) #Generate a Block containing the MeritRemoval. block = Block( BlockHeader(0, merit.blockchain.last(), BlockHeader.createContents([], [], [mr]), 1, bytes(4), BlockHeader.createSketchCheck(bytes(4), []), 0, merit.blockchain.blocks[-1].header.time + 1200), BlockBody([], [mr], mr.signature)) #Mine it. block.mine(blsPrivKey, merit.blockchain.difficulty()) #Add it. merit.blockchain.add(block) print("Generated Invalid Competing Block " + str(len(merit.blockchain.blocks)) + ".")
def PendingActionsTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/PendingActions.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #Blockchain. blockchain: Blockchain = Blockchain.fromJSON( b"MEROS_DEVELOPER_NETWORK", 60, int( "FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 16), vectors["blockchain"]) #SignedVerifications. verifs: List[SignedVerification] = [] for verif in vectors["verifications"]: verifs.append(SignedVerification.fromJSON(verif)) #Removal. removal: SignedMeritRemoval = SignedMeritRemoval.fromJSON( vectors["removal"]) #Datas. datas: List[Data] = [] for data in vectors["datas"]: datas.append(Data.fromJSON(data)) #Send every Data/Verification. def sendDatasAndVerifications() -> None: #Send the Datas. for data in datas: if rpc.meros.transaction(data) != rpc.meros.recv(): raise TestError("Unexpected message sent.") #Send the Verifications. for verif in verifs: if rpc.meros.signedElement(verif) != rpc.meros.recv(): raise TestError("Unexpected message sent.") #Verify every Data has 100 Merit. for data in datas: if rpc.call("consensus", "getStatus", [data.hash.hex()])["merit"] != 100: raise TestError( "Meros didn't verify Transactions with received Verifications." ) #Cause a MeritRemoval. def causeMeritRemoval() -> None: #Send every Data/;Verification. sendDatasAndVerifications() #Send the problem Verification and verify the MeritRemoval. rpc.meros.signedElement(removal.se2) if rpc.meros.recv() != MessageType.SignedMeritRemoval.toByte( ) + removal.signedSerialize(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal, True) #Verify every Data has 0 Merit. for data in datas: if rpc.call("consensus", "getStatus", [data.hash.hex()])["merit"] != 0: raise TestError( "Meros didn't revert pending actions of a malicious MeritHolder." ) #Send a MeritRemoval. def sendMeritRemoval() -> None: #Send every Data/;Verification. sendDatasAndVerifications() #Send and verify the MeritRemoval. if rpc.meros.signedElement(removal) != rpc.meros.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 100, removal, True) #Verify every Data has 0 Merit. for data in datas: if rpc.call("consensus", "getStatus", [data.hash.hex()])["merit"] != 0: raise TestError( "Meros didn't revert pending actions of a malicious MeritHolder." ) #Check the Data's finalized with the proper amount of Merit and update the MeritRemoval. def check() -> None: #Verify the Datas have the Merit they should. for data in datas: if rpc.call("consensus", "getStatus", [data.hash.hex()])["merit"] != 0: raise TestError( "Meros didn't finalize with the reverted pending actions of a malicious MeritHolder." ) #Update the MeritRemoval's nonce. removal.nonce = 6 #Verify the MeritRemoval is now accessible with a nonce of 6. verifyMeritRemoval(rpc, 7, 0, removal, False) #Create and execute a Liver to cause a MeritRemoval. Liver(rpc, blockchain, callbacks={1: causeMeritRemoval, 7: check}).live() #Reset the MeritRemoval nonce. removal.nonce = 0 #Create and execute a Liver to handle a MeritRemoval. Liver(rpc, blockchain, callbacks={1: sendMeritRemoval, 7: check}).live()
Data(edPrivKey.get_verifying_key().to_bytes().rjust(48, b'\0'), bytes())) datas[-1].sign(edPrivKey) datas[-1].beat(consensus.dataFilter) #Create 1 Verification per Data. verifs: List[SignedVerification] = [] for d in range(len(datas)): verifs.append(SignedVerification(datas[d].hash)) verifs[-1].sign(privKey, d) consensus.add(verifs[-1]) #Create a MeritRemoval off the last one. sv: SignedVerification = SignedVerification(b'\0' * 48) sv.sign(privKey, 5) removal: SignedMeritRemoval = SignedMeritRemoval( SignedElement.fromElement(verifs[5]), SignedElement.fromElement(sv)) consensus.add(removal) #Generate a Block with the Verifications. block: Block = Block( BlockHeader(2, blockchain.last(), int(time()), consensus.getAggregate([(pubKey, 0, 5)])), BlockBody([(pubKey, 5, consensus.getMerkle(pubKey, 0, 5))])) #Mine it. block.mine(blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Pending Actions Block " + str(block.header.nonce) + ".") #Generate 4 more Blocks.
blockchain.add(Block.fromJSON(blocks[0])) bbFile.close() #Create a Data to verify. data: Data = Data(edPubKey.to_bytes().rjust(48, b'\0'), bytes()) data.sign(edPrivKey) data.beat(consensus.dataFilter) #Create two Verifications with the same nonce, yet for the different Datas. sv1: SignedVerification = SignedVerification(data.hash) sv1.sign(privKey, 0) sv2: SignedVerification = SignedVerification(b'\0' * 48) sv2.sign(privKey, 0) removal: SignedMeritRemoval = SignedMeritRemoval( SignedElement.fromElement(sv1), SignedElement.fromElement(sv2)) consensus.add(removal) #Generate another Block. block: Block = Block( BlockHeader(2, blockchain.last(), int(time()), consensus.getAggregate([(pubKey, 0, -1)])), BlockBody([(pubKey, 0, consensus.getMerkle(pubKey, 0))])) #Mine it. block.mine(blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Same Nonce Block " + str(block.header.nonce) + ".") result: Dict[str, Any] = {
PrivateKey(blake2b( b'\1', digest_size=32).digest()).toPublicKey().serialize() ], PrivateKey(blake2b(b'\1', digest_size=32).digest()).sign( verifs[1].signatureSerialize())), SignedMeritRemovalVerificationPacket( SignedVerificationPacket(verifs[1].hash), [ PrivateKey(blake2b( b'\1', digest_size=32).digest()).toPublicKey().serialize() ], PrivateKey(blake2b(b'\1', digest_size=32).digest()).sign( verifs[1].signatureSerialize())) ] #Create a MeritRemoval out of the conflicting Verifications. e1MR: SignedMeritRemoval = SignedMeritRemoval(verifs[1], packets[0], 0) e2MR: SignedMeritRemoval = SignedMeritRemoval(packets[1], verifs[2], 0) #Generate a Block containing the MeritRemoval for each chain. block = Block( BlockHeader(0, e1Chain.last(), BlockHeader.createContents([], [e1MR]), 1, bytes(4), bytes(32), 0, e1Chain.blocks[-1].header.time + 1200), BlockBody([], [e1MR], e1MR.signature)) block.mine(blsPrivKey, e1Chain.difficulty()) e1Chain.add(block) print("Generated Hundred Twenty Three Packet Block 1 " + str(len(e1Chain.blocks)) + ".") block = Block( BlockHeader(0, e2Chain.last(), BlockHeader.createContents([], [e2MR]), 1, bytes(4), bytes(32), 0, e2Chain.blocks[-1].header.time + 1200),
#Generate a Block containing the partial MeritRemoval. block = Block( BlockHeader(0, blockchain.last(), BlockHeader.createContents([], [partial]), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200), BlockBody([], [partial], partial.signature)) #Mine it. block.mine(blsPrivKey, blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Hundred Twenty Three Partial Block " + str(len(blockchain.blocks)) + ".") #Create a MeritRemoval which isn't partial. mr: SignedMeritRemoval = SignedMeritRemoval(dataDiffs[0], dataDiffs[1]) #Generate a Block containing the MeritRemoval. block = Block( BlockHeader(0, blockchain.last(), BlockHeader.createContents([], [mr]), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200), BlockBody([], [mr], mr.signature)) #Mine it. block.mine(blsPrivKey, blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Hundred Twenty Three Partial Block " + str(len(blockchain.blocks)) + ".")
def VerifyCompetingTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/VerifyCompeting.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() keys: Dict[bytes, int] = { bytes.fromhex(vectors["blockchain"][0]["header"]["miner"]): 0 } nicks: List[bytes] = [ bytes.fromhex(vectors["blockchain"][0]["header"]["miner"]) ] #Datas. datas: List[Data] = [ Data.fromJSON(vectors["datas"][0]), Data.fromJSON(vectors["datas"][1]), Data.fromJSON(vectors["datas"][2]) ] #Transactions. transactions: Transactions = Transactions() for data in datas: transactions.add(data) #Initial Data's Verification. verif: SignedVerification = SignedVerification.fromSignedJSON( vectors["verification"]) #MeritRemoval. #pylint: disable=no-member removal: SignedMeritRemoval = SignedMeritRemoval.fromSignedJSON( keys, vectors["removal"]) #Create and execute a Liver to cause a Signed MeritRemoval. def sendElements() -> None: #Send the Datas. for data in datas: if rpc.meros.transaction(data) != rpc.meros.recv(): raise TestError("Meros didn't send us the Data.") #Send the initial Data's verification. if rpc.meros.signedElement(verif) != rpc.meros.recv(): raise TestError("Meros didn't us the initial Data's Verification.") #Send the first Element. if rpc.meros.signedElement(removal.se1) != rpc.meros.recv(): raise TestError("Meros didn't send us the Verification.") #Trigger the MeritRemoval. rpc.meros.signedElement(removal.se2) if rpc.meros.recv() != (MessageType.SignedMeritRemoval.toByte() + removal.signedSerialize(nicks)): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, removal.holder, True) Liver(rpc, vectors["blockchain"], transactions, callbacks={ 1: sendElements, 2: lambda: verifyMeritRemoval(rpc, 1, 1, removal.holder, False) }).live() #Create and execute a Liver to handle a Signed MeritRemoval. def sendMeritRemoval() -> None: #Send the Datas. for data in datas: if rpc.meros.transaction(data) != rpc.meros.recv(): raise TestError("Meros didn't send us the Data.") #Send the initial Data's verification. if rpc.meros.signedElement(verif) != rpc.meros.recv(): raise TestError("Meros didn't us the initial Data's Verification.") #Send and verify the MeritRemoval. if rpc.meros.signedElement(removal) != rpc.meros.recv(): raise TestError("Meros didn't send us the Merit Removal.") verifyMeritRemoval(rpc, 1, 1, removal.holder, True) Liver(rpc, vectors["blockchain"], transactions, callbacks={ 1: sendMeritRemoval, 2: lambda: verifyMeritRemoval(rpc, 1, 1, removal.holder, False) }).live() #Create and execute a Syncer to handle a Signed MeritRemoval. Syncer(rpc, vectors["blockchain"], transactions).sync() verifyMeritRemoval(rpc, 1, 1, removal.holder, False)
int( "FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 16)) #BLS Keys. privKey: blspy.PrivateKey = blspy.PrivateKey.from_seed(b'\0') pubKey: blspy.PublicKey = privKey.get_public_key() #Load a Multiple Block and load their MeritRemoval. snFile: IO[Any] = open( "PythonTests/Vectors/Consensus/MeritRemoval/SameNonce.json", "r") snVectors: Dict[str, Any] = json.loads(snFile.read()) blockchain.add(Block.fromJSON(snVectors["blockchain"][0])) removal1: SignedMeritRemoval = SignedMeritRemoval.fromJSON( snVectors["removal"]) snFile.close() #Create a second MeritRemoval. sv: SignedVerification = SignedVerification(b'\1' * 48) sv.sign(privKey, 0) removal2: SignedMeritRemoval = SignedMeritRemoval( removal1.se1, SignedElement.fromElement(sv)) #Add the second MeritRemoval to Consensus. consensus.add(removal2) #Generate a Block with the second MeritRemoval. block: Block = Block( BlockHeader(2, blockchain.last(), int(time()),
#Create the initial Data and two competing Datas. datas: List[Data] = [Data(bytes(32), edPubKey.to_bytes())] datas.append(Data(datas[0].hash, b"Initial Data.")) datas.append(Data(datas[0].hash, b"Second Data.")) for data in datas: data.sign(edPrivKey) data.beat(spamFilter) #Create Verifications for all 3. verifs: List[SignedVerification] = [] for data in datas: verifs.append(SignedVerification(data.hash, 0)) verifs[-1].sign(0, blsPrivKey) #Create a MeritRemoval out of the conflicting Verifications. mr: SignedMeritRemoval = SignedMeritRemoval(verifs[1], verifs[2]) #Generate a Block containing the MeritRemoval. block = Block( BlockHeader(0, packetedChain.last(), BlockHeader.createContents([], [mr]), 1, bytes(4), bytes(32), 0, packetedChain.blocks[-1].header.time + 1200), BlockBody([], [mr], mr.signature)) #Mine it. block.mine(blsPrivKey, packetedChain.difficulty()) #Add it. packetedChain.add(block) print("Generated Hundred Twenty Three Packet Block 1 " + str(len(packetedChain.blocks)) + ".")
data.sign(edPrivKey) data.beat(SpamFilter(5)) #Create a Verification. verif: SignedVerification = SignedVerification(data.hash) verif.sign(0, blsPrivKey) #Create a MeritRemovalVerificationPacket verifying the same Transaction as the Verification. packet: SignedMeritRemovalVerificationPacket = SignedMeritRemovalVerificationPacket( SignedVerificationPacket(data.hash), [blsPubKey.serialize()], verif.signature ) #Create the three MeritRemovals. sendDiffMR: SignedMeritRemoval = SignedMeritRemoval(sendDiff, sendDiff) dataDiffMR: SignedMeritRemoval = SignedMeritRemoval(dataDiff, dataDiff) verifMR: SignedMeritRemoval = SignedMeritRemoval(verif, packet) result: Dict[str, Any] = { "blockchain": blockchain.toJSON(), "removals": [ sendDiffMR.toSignedJSON(), dataDiffMR.toSignedJSON(), verifMR.toSignedJSON() ], "data": data.toJSON() } vectors: IO[Any] = open("PythonTests/Vectors/Consensus/MeritRemoval/SameElement.json", "w") vectors.write(json.dumps(result)) vectors.close()