def VCompetingTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/Verification/Competing.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"]) #Consensus. consensus: Consensus = Consensus.fromJSON( bytes.fromhex( "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ), bytes.fromhex( "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" ), vectors["consensus"]) #Transactions. transactions: Transactions = Transactions.fromJSON(vectors["transactions"]) #Create and execute a Liver/Syncer. Liver(rpc, blockchain, consensus, transactions).live() Syncer(rpc, blockchain, consensus, transactions).sync()
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)
from PythonTests.Classes.Merit.Blockchain import Blockchain #BLS lib. import blspy #Time standard function. from time import time #JSON standard lib. import json #Consensus. consensus: Consensus = Consensus( bytes.fromhex( "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ), bytes.fromhex( "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" ), ) #Blockchain. blockchain: Blockchain = Blockchain( b"MEROS_DEVELOPER_NETWORK", 60, 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.
def PartialTest( rpc: RPC ) -> None: file: IO[Any] = open("PythonTests/Vectors/Consensus/MeritRemoval/Partial.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: PartiallySignedMeritRemoval = PartiallySignedMeritRemoval.fromJSON(vectors["removal"]) #Consensus. consensus: Consensus = Consensus( bytes.fromhex("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), bytes.fromhex("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC") ) consensus.add(removal.e1) consensus.add(removal) #Transactions. transactions: Transactions = Transactions() transactions.add(Data.fromJSON(vectors["data"])) #Create and execute a Liver to cause a Partial MeritRemoval. def sendElement() -> None: #Send the second Element. 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, 2, 200, removal, True) Liver( rpc, blockchain, consensus, transactions, callbacks={ 2: sendElement, 3: lambda: verifyMeritRemoval(rpc, 2, 200, removal, False) } ).live() #Create and execute a Liver to handle a Partial 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, 2, 200, removal, True) Liver( rpc, blockchain, consensus, transactions, callbacks={ 2: sendMeritRemoval, 3: lambda: verifyMeritRemoval(rpc, 2, 200, removal, False) } ).live() #Create and execute a Syncer to handle a Partial MeritRemoval. Syncer(rpc, blockchain, consensus, transactions).sync() verifyMeritRemoval(rpc, 2, 200, removal, False)
def VParsableTest(rpc: RPC) -> None: file: IO[Any] = open( "PythonTests/Vectors/Consensus/Verification/Parsable.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"]) #Consensus. consensus: Consensus = Consensus( bytes.fromhex( "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ), bytes.fromhex( "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" ), ) consensus.add(SignedVerification.fromJSON(vectors["verification"])) #Transactions. transactions: Transactions = Transactions() transactions.add(Data.fromJSON(vectors["data"])) #Handshake with the node. rpc.meros.connect(254, 254, 3) sentLast: bool = False reqHash: bytes = bytes() msg: bytes = bytes() height: int = 0 while True: try: msg = rpc.meros.recv() except TestError as e: if (not sentLast) or (str(e) != "Node disconnected us as a peer."): raise e break if MessageType(msg[0]) == MessageType.Syncing: rpc.meros.acknowledgeSyncing() elif MessageType(msg[0]) == MessageType.GetBlockHash: height = int.from_bytes(msg[1:5], "big") if height == 0: rpc.meros.blockHash(blockchain.last()) else: if height >= len(blockchain.blocks): raise TestError( "Meros asked for a Block Hash we do not have.") rpc.meros.blockHash(blockchain.blocks[height].header.hash) elif MessageType(msg[0]) == MessageType.BlockHeaderRequest: reqHash = msg[1:49] for block in blockchain.blocks: if block.header.hash == reqHash: rpc.meros.blockHeader(block.header) break if block.header.hash == blockchain.last(): raise TestError( "Meros asked for a Block Header we do not have.") elif MessageType(msg[0]) == MessageType.BlockBodyRequest: reqHash = msg[1:49] for block in blockchain.blocks: if block.header.hash == reqHash: rpc.meros.blockBody(block.body) break if block.header.hash == blockchain.last(): raise TestError( "Meros asked for a Block Body we do not have.") elif MessageType(msg[0]) == MessageType.ElementRequest: holder: bytes = msg[1:49] rpc.meros.element(consensus.holders[holder][int.from_bytes( msg[49:53], "big")]) elif MessageType(msg[0]) == MessageType.TransactionRequest: sentLast = True rpc.meros.transaction(transactions.txs[msg[1:49]]) elif MessageType(msg[0]) == MessageType.SyncingOver: pass else: raise TestError("Unexpected message sent: " + msg.hex().upper())
import blspy #Time standard function. from time import time #JSON standard lib. import json cmFile: IO[Any] = open("PythonTests/Vectors/Transactions/ClaimedMint.json", "r") cmVectors: Dict[str, Any] = json.loads(cmFile.read()) #Transactions. transactions: Transactions = Transactions.fromJSON(cmVectors["transactions"]) #Consensus. consensus: Consensus = Consensus.fromJSON( bytes.fromhex("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), bytes.fromhex("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"), cmVectors["consensus"] ) #Blockchain. blockchain: Blockchain = Blockchain.fromJSON( b"MEROS_DEVELOPER_NETWORK", 60, int("FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 16), cmVectors["blockchain"] ) cmFile.close() #Ed25519 keys. edPrivKey1: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey1: ed25519.VerifyingKey = edPrivKey1.get_verifying_key() edPubKey2: ed25519.VerifyingKey = ed25519.SigningKey(b'\1' * 32).get_verifying_key()