def __init__( self, sendDiff: bytes, dataDiff: bytes ) -> None: self.sendFilter = SpamFilter(sendDiff) self.dataFilter = SpamFilter(dataDiff) self.holders: Dict[bytes, List[Element]] = {}
def HundredFortySevenTest( rpc: RPC ) -> None: file: IO[Any] = open("PythonTests/Vectors/Transactions/ClaimedMint.json", "r") vectors: Dict[str, Any] = json.loads(file.read()) file.close() #Merit. merit: Merit = Merit.fromJSON(vectors["blockchain"]) #Transactions. transactions: Transactions = Transactions.fromJSON(vectors["transactions"]) #Ed25519 keys. privKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) pubKey: ed25519.VerifyingKey = privKey.get_verifying_key() #Grab the Claim hash, claim: bytes = merit.blockchain.blocks[-1].body.packets[0].hash #Create a Send which underflows. send: Send = Send( [(claim, 0)], [ (pubKey.to_bytes(), 18446744073709551231), (pubKey.to_bytes(), 385 + Claim.fromTransaction(transactions.txs[claim]).amount) ] ) send.sign(privKey) send.beat(SpamFilter(3)) #Custom function to send the last Block and verify it errors at the right place. def checkFail() -> None: #Send the Send. rpc.meros.liveTransaction(send) #Handle sync requests. while True: #Try receiving from the Live socket, where Meros sends keep-alives. try: if len(rpc.meros.live.recv()) != 0: raise Exception() except TestError: raise SuccessError("Node disconnected us after we sent an invalid Transaction.") except Exception: raise TestError("Meros sent a keep-alive.") #Create and execute a Liver. Liver(rpc, vectors["blockchain"], transactions, callbacks={12: checkFail}).live()
def DataTest(rpc: RPC) -> None: #Create the Spam Filter. spamFilter: SpamFilter = SpamFilter( bytes.fromhex( "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" )) #Create the Data. data: Data = Data(pubKey.to_bytes().rjust(48, b'\0'), b"Hello There! General Kenobi.") data.sign(privKey) data.beat(spamFilter) #Handshake with the node. rpc.meros.connect(254, 254, 0) #Send the Data. rpc.meros.transaction(data) #Verify the Data. verifyTransaction(rpc, data)
def DataTest(rpc: RPC) -> None: #Get the genesis hash. genesis: bytes = Blockchain().blocks[0].header.hash #Create the Spam Filter. spamFilter: SpamFilter = SpamFilter(5) #Create the Data. data: Data = Data(bytes(32), pubKey.to_bytes()) data.sign(privKey) data.beat(spamFilter) #Handshake with the node. rpc.meros.liveConnect(genesis) #Send the Data. rpc.meros.liveTransaction(data) #Sleep for 100 milliseconds. sleep(0.1) #Verify the Data. verifyTransaction(rpc, data)
from hashlib import blake2b #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"]) #Blockchain. blockchain: Blockchain = Blockchain.fromJSON(cmVectors["blockchain"]) cmFile.close() #SpamFilter. sendFilter: SpamFilter = SpamFilter(3) #Ed25519 keys. edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #Grab the Claim hash. claim: bytes = blockchain.blocks[-1].body.packets[0].hash #Create a Send spending it twice. send: Send = Send( [(claim, 0), (claim, 0)],
#Blake2b standard function. from hashlib import blake2b #JSON standard lib. import json #Ed25519 Keys. edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS Keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #SpamFilter. spamFilter: SpamFilter = SpamFilter(5) #Blockchains. packetedChain: Blockchain = Blockchain() reorderedChain: Blockchain = Blockchain() #Generate a Block granting the holder Merit. block = Block( BlockHeader(0, packetedChain.last(), bytes(32), 1, bytes(4), bytes(32), blsPubKey.serialize(), packetedChain.blocks[-1].header.time + 1200), BlockBody()) #Mine it. block.mine(blsPrivKey, packetedChain.difficulty()) #Add it. packetedChain.add(block)
#JSON standard lib. import json #Blank Blocks. bbFile: IO[Any] = open("PythonTests/Vectors/Merit/BlankBlocks.json", "r") blankBlocks: List[Dict[str, Any]] = json.loads(bbFile.read()) bbFile.close() #Transactions. transactions: Transactions = Transactions() #Merit. merit: Merit = Merit() #SpamFilter. dataFilter: SpamFilter = SpamFilter(bytes.fromhex("CC" * 32)) #Ed25519 keys. edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #Add 5 Blank Blocks. for i in range(5): merit.add(Block.fromJSON(merit.blockchain.keys, blankBlocks[i])) #Create the Data. data: Data = Data(bytes(32), edPubKey.to_bytes())
from hashlib import blake2b #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"]) #Blockchain. blockchain: Blockchain = Blockchain.fromJSON(cmVectors["blockchain"]) cmFile.close() #SpamFilter. sendFilter: SpamFilter = SpamFilter(bytes.fromhex("AA" * 32)) #Ed25519 keys. edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #Grab the Claim hash. claim: bytes = blockchain.blocks[-1].body.packets[0].hash #Create a Send spending it twice. send: Send = Send( [(claim, 0), (claim, 0)],
def EightyEightTest(rpc: RPC) -> None: #Ed25519 key. edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS key. blsPrivKey: PrivateKey = PrivateKey( blake2b(b'\0', digest_size=32).digest()) blsPubKey: str = blsPrivKey.toPublicKey().serialize().hex() #Blocks. file: IO[Any] = open("PythonTests/Vectors/Merit/BlankBlocks.json", "r") blocks: List[Dict[str, Any]] = json.loads(file.read()) file.close() #Merit. merit: Merit = Merit() #Spam Filter. dataFilter: SpamFilter = SpamFilter(bytes.fromhex("CC" * 32)) #Handshake with the node. rpc.meros.connect(254, 254, merit.blockchain.blocks[0].header.hash) #Send the first Block. block: Block = Block.fromJSON(merit.blockchain.keys, blocks[0]) merit.blockchain.add(block) rpc.meros.blockHeader(block.header) #Handle sync requests. reqHash: bytes = bytes() while True: msg: bytes = rpc.meros.recv() if MessageType(msg[0]) == MessageType.Syncing: rpc.meros.syncingAcknowledged() elif 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. rpc.meros.blockBody(merit.state.nicks, block) elif MessageType(msg[0]) == MessageType.SyncingOver: pass elif MessageType(msg[0]) == MessageType.BlockHeader: break else: raise TestError("Unexpected message sent: " + msg.hex().upper()) #Create two Datas. datas: List[Data] = [Data(bytes(32), edPubKey.to_bytes())] datas.append(Data(datas[0].hash, b"Hello there! General Kenobi.")) for data in datas: #Sign them and have them beat the spam filter. data.sign(edPrivKey) data.beat(dataFilter) #Transmit them. rpc.meros.transaction(data) #Verify both. verifs: List[SignedVerification] = [ SignedVerification(datas[0].hash), SignedVerification(datas[1].hash) ] for verif in verifs: verif.sign(0, blsPrivKey) #Only transmit the second. rpc.meros.signedElement(verifs[1]) sleep(0.5) #Verify the block template has no verifications. if bytes.fromhex( rpc.call("merit", "getBlockTemplate", [blsPubKey])["header"])[36:68] != bytes(32): raise TestError("Block template has Verification Packets.") #Transmit the first signed verification. rpc.meros.signedElement(verifs[0]) sleep(0.5) #Verify the block template has both verifications. template: Dict[str, Any] = rpc.call("merit", "getBlockTemplate", [blsPubKey]) template["header"] = bytes.fromhex(template["header"]) packets: List[VerificationPacket] = [ VerificationPacket(datas[0].hash, [0]), VerificationPacket(datas[1].hash, [0]) ] if template["header"][36:68] != BlockHeader.createContents( merit.state.nicks, packets): raise TestError( "Block template doesn't have both Verification Packets.") #Mine the Block. block = Block( BlockHeader( 0, block.header.hash, BlockHeader.createContents(merit.state.nicks, packets), 1, template["header"][-43:-39], BlockHeader.createSketchCheck(template["header"][-43:-39], packets), 0, int.from_bytes(template["header"][-4:], byteorder="big"), ), BlockBody( packets, [], Signature.aggregate([verifs[0].signature, verifs[1].signature]))) if block.header.serializeHash()[:-4] != template["header"]: raise TestError("Failed to recreate the header.") if block.body.serialize(merit.state.nicks, block.header.sketchSalt, len(packets)) != bytes.fromhex(template["body"]): raise TestError("Failed to recreate the body.") block.mine(blsPrivKey, merit.blockchain.difficulty()) merit.blockchain.add(block) #Publish it. rpc.call("merit", "publishBlock", [ template["id"], (template["header"] + block.header.proof.to_bytes(4, byteorder="big") + block.header.signature + block.body.serialize( merit.state.nicks, block.header.sketchSalt, len(packets))).hex() ]) #Verify the Blockchain. verifyBlockchain(rpc, merit.blockchain)
from hashlib import blake2b #JSON standard lib. import json #Blockchain. Solely used to get the genesis Block hash. blockchain: Blockchain = Blockchain() #Block vectors. blocks: List[Dict[str, Any]] = [] #Transactions. transactions: Transactions = Transactions() #Spam Filter. dataFilter: SpamFilter = SpamFilter(5) #Ed25519 keys. edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS Private Key. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) #Create a Data for the VerificationPacket. data: Data = Data(bytes(32), edPubKey.to_bytes()) data.sign(edPrivKey) data.beat(dataFilter) transactions.add(data) #Generate the VerificationPacket Block.
def HundredFiftyFiveTest(rpc: RPC) -> None: #Ed25519 keys. edPrivKeys: List[ed25519.SigningKey] = [ ed25519.SigningKey(b'\0' * 32), ed25519.SigningKey(b'\1' * 32) ] edPubKeys: List[ed25519.VerifyingKey] = [ edPrivKeys[0].get_verifying_key(), edPrivKeys[1].get_verifying_key() ] #BLS keys. blsPrivKey: PrivateKey = PrivateKey( bytes.fromhex(rpc.call("personal", "getMiner"))) blsPubKey: bytes = blsPrivKey.toPublicKey().serialize() #Blockchain. blockchain: Blockchain = Blockchain() #Spam Filter. dataFilter: SpamFilter = SpamFilter(5) #Handshake with the node. rpc.meros.liveConnect(blockchain.blocks[0].header.hash) rpc.meros.syncConnect(blockchain.blocks[0].header.hash) #Call getBlockTemplate just to get an ID. #Skips the need to write a sync loop for the BlockBody. template: Dict[str, Any] = rpc.call("merit", "getBlockTemplate", [blsPubKey.hex()]) #Mine a Block. block = Block( BlockHeader(0, blockchain.blocks[0].header.hash, bytes(32), 1, bytes(4), bytes(32), blsPubKey, blockchain.blocks[0].header.time + 1200, 0), BlockBody()) block.mine(blsPrivKey, blockchain.difficulty()) blockchain.add(block) #Publish it. rpc.call("merit", "publishBlock", [template["id"], block.serialize().hex()]) #Handle the fact Meros will now broadcast it to us. if MessageType(rpc.meros.live.recv()[0]) != MessageType.BlockHeader: raise TestError("Meros didn't broadcast the Block it just published.") #Create the Datas. datas: List[Data] = [ Data(bytes(32), edPubKeys[0].to_bytes()), Data(bytes(32), edPubKeys[1].to_bytes()) ] for d in range(len(datas)): #Sign, and mine the Data. datas[d].sign(edPrivKeys[d]) datas[d].beat(dataFilter) #Send the Data and verify Meros sends it back. if rpc.meros.liveTransaction(datas[d]) != rpc.meros.live.recv(): raise TestError("Meros didn't send back the Data.") #Verify Meros sends back a Verification. res: bytes = rpc.meros.live.recv() if MessageType(res[0]) != MessageType.SignedVerification: raise TestError("Meros didn't send a SignedVerification.") verif: SignedVerification = SignedVerification(datas[d].hash) verif.sign(0, blsPrivKey) if res[1:] != verif.signedSerialize(): raise TestError( "Meros didn't send the correct SignedVerification.")
#Add it. blockchain.add(block) print("Generated Same Element Block " + str(len(blockchain.blocks)) + ".") #Create a SendDifficulty. sendDiff: SignedSendDifficulty = SignedSendDifficulty(4, 0) sendDiff.sign(0, blsPrivKey) #Create a DataDifficulty. dataDiff: SignedDataDifficulty = SignedDataDifficulty(4, 0) dataDiff.sign(0, blsPrivKey) #Create a Data. data: Data = Data(bytes(32), edPubKey.to_bytes()) 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)