def TwoHundredThirtyFiveTest(rpc: RPC) -> None: blockchain: Blockchain = Blockchain() dataFilter: SpamFilter = SpamFilter(5) edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #Mine one Block to the node. blsPrivKey: PrivateKey = PrivateKey( bytes.fromhex(rpc.call("personal", "getMiner"))) blsPubKey: bytes = blsPrivKey.toPublicKey().serialize() #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) rpc.call("merit", "publishBlock", [template["id"], block.serialize().hex()]) #Send Meros a Data and receive its Verification to make sure it's verifying Transactions in the first place. data: Data = Data(bytes(32), edPubKey.to_bytes()) data.sign(edPrivKey) data.beat(dataFilter) rpc.meros.liveConnect(blockchain.blocks[0].header.hash) if rpc.meros.liveTransaction(data) != rpc.meros.live.recv(): raise TestError("Meros didn't send back the Data.") if MessageType(rpc.meros.live.recv()[0]) != MessageType.SignedVerification: raise TestError("Meros didn't send us its SignedVerification.") #Close our connection and mine 8 Blocks so its Merit is locked. rpc.meros.live.connection.close() for _ in range(8): block = Block( BlockHeader(0, blockchain.blocks[-1].header.hash, bytes(32), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200, 0), BlockBody()) #Reusing its key is fine as mining doesn't count as participation. block.mine(blsPrivKey, blockchain.difficulty()) blockchain.add(block) #Sleep 30 seconds to make sure Meros noted we disconnected, and then reconnect. sleep(30) rpc.meros.liveConnect(blockchain.blocks[0].header.hash) rpc.meros.syncConnect(blockchain.blocks[0].header.hash) #Sync the Blocks. for b in range(8): header: bytes = rpc.meros.liveBlockHeader(blockchain.blocks[b + 2].header) if rpc.meros.sync.recv() != (MessageType.BlockBodyRequest.toByte() + blockchain.blocks[b + 2].header.hash): raise TestError("Meros didn't request the BlockBody.") rpc.meros.blockBody(blockchain.blocks[b + 2]) if rpc.meros.live.recv() != header: raise TestError("Meros didn't send back the header.") if MessageType( rpc.meros.live.recv()[0]) != MessageType.SignedVerification: raise TestError("Meros didn't verify this Block's data.") #Verify its Merit is locked. #Theoretically, all code after this check is unecessary. #Meros verifies a Block's Data after updating its State. #Therefore, if the above last Block had its Data verified, this issue should be closed. #That said, the timing is a bit too tight for comfort. #Better safe than sorry. Hence why the code after this check exists. if rpc.call("merit", "getMerit", [0])["status"] != "Locked": raise TestError("Merit wasn't locked when it was supposed to be.") #Send it a Transaction and make sure Meros verifies it, despite having its Merit locked. data = Data(data.hash, edPubKey.to_bytes()) data.sign(edPrivKey) data.beat(dataFilter) if rpc.meros.liveTransaction(data) != rpc.meros.live.recv(): raise TestError("Meros didn't send back the Data.") if MessageType(rpc.meros.live.recv()[0]) != MessageType.SignedVerification: raise TestError("Meros didn't send us its SignedVerification.")
verifs: List[Dict[str, Any]] = [] for _ in range(2): data.sign(privKey) data.beat(spamFilter) datas.append(data.toJSON()) verif: SignedVerification = SignedVerification(data.hash) verif.sign(0, PrivateKey(0)) verifs.append(verif.toSignedJSON()) data = Data(data.hash, bytes(2)) privKey = ed25519.SigningKey(b'\1' * 32) pubKey = privKey.get_verifying_key() data = Data(bytes(32), pubKey.to_bytes()) for i in range(5): data.sign(privKey) data.beat(spamFilter) datas.append(data.toJSON()) verif: SignedVerification = SignedVerification(data.hash) verif.sign(0, PrivateKey(0)) verifs.append(verif.toSignedJSON()) if i < 2: data = Data(data.hash, bytes(4)) else: data = Data(data.hash, bytes(8)) proto.add(0, [ VerificationPacket(bytes.fromhex(data["hash"]), [0]) for data in datas[2:] ])
def StringBasedTypesTest( rpc: RPC ) -> None: edPrivKey: Ristretto.SigningKey = Ristretto.SigningKey(b'\0' * 32) edPubKey: bytes = edPrivKey.get_verifying_key() blsPubKey: PublicKey = PrivateKey(0).toPublicKey() #hex. #Test 0x-prefixed and no-0x both work without issue. data: Data = Data(bytes(32), edPubKey) data.sign(edPrivKey) rpc.call("transactions", "publishTransactionWithoutWork", {"type": "Data", "transaction": "0x" + data.serialize()[:-4].hex()}) data = Data(data.hash, b"abc") data.sign(edPrivKey) rpc.call("transactions", "publishTransactionWithoutWork", {"type": "Data", "transaction": data.serialize()[:-4].hex()}) #Test non-hex data is properly handled. try: rpc.call("transactions", "publishTransactionWithoutWork", {"type": "Data", "transaction": "az"}) raise TestError() except Exception as e: if str(e) != "-32602 Invalid params.": raise TestError("Meros accepted non-hex data for a hex argument.") #Hash. rpc.call("transactions", "getTransaction", {"hash": data.hash.hex()}) rpc.call("transactions", "getTransaction", {"hash": "0x" + data.hash.hex()}) #Also call the upper form, supplementing the above hex tests (as Hash routes through hex). rpc.call("transactions", "getTransaction", {"hash": data.hash.hex().upper()}) rpc.call("transactions", "getTransaction", {"hash": "0x" + data.hash.hex().upper()}) #Improper length. try: rpc.call("transactions", "getTransaction", {"hash": data.hash.hex().upper()[:-2]}) raise TestError() except Exception as e: if str(e) != "-32602 Invalid params.": raise TestError("Meros accepted a hex string with an improper length as a Hash.") #BLSPublicKey. try: rpc.call("merit", "getNickname", {"key": blsPubKey.serialize().hex()}) raise TestError() except Exception as e: if str(e) != "-2 Key doesn't have a nickname assigned.": raise TestError("Meros didn't accept a valid BLSPublicKey.") try: rpc.call("merit", "getNickname", {"key": blsPubKey.serialize().hex()[:-2]}) raise TestError() except Exception as e: if str(e) != "-32602 Invalid params.": raise TestError("Meros accepted a hex string with an improper length as a BLSPublicKey.") #Missing flags. try: rpc.call("merit", "getNickname", {"key": "0" + blsPubKey.serialize().hex()[1]}) raise TestError() except Exception as e: if str(e) != "-32602 Invalid params.": raise TestError("Meros accepted an invalid BLSPublicKey as a BLSPublicKey.") #EdPublicKey. rpc.call("personal", "setAccount", {"key": edPubKey.hex(), "chainCode": bytes(32).hex()}) try: rpc.call("personal", "setAccount", {"key": edPubKey[:-2].hex(), "chainCode": bytes(32).hex()}) raise TestError() except Exception as e: if str(e) != "-32602 Invalid params.": raise TestError("Meros accepted a hex string with an improper length as an EdPublicKey.")
block: Block = Block( BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32), blsPubKey.serialize(), blockchain.blocks[-1].header.time + 1200), BlockBody()) block.mine(blsPrivKey, blockchain.difficulty()) blockchain.add(block) data = Data(b"", b"") svs: List[SignedVerification] = [] packets: List[VerificationPacket] = [] for d in range(0, 3): if d == 0: data = Data(bytes(32), edPubKey.to_bytes()) else: data = Data(data.hash, edPubKey.to_bytes()) data.sign(edPrivKey) data.beat(dataFilter) transactions.add(data) svs.append(SignedVerification(data.hash)) svs[-1].sign(0, blsPrivKey) packets.append(VerificationPacket(data.hash, [0])) block = Block( BlockHeader(0, blockchain.last(), BlockHeader.createContents(packets), 1, bytes(4), BlockHeader.createSketchCheck(bytes(4), packets), 0, blockchain.blocks[-1].header.time + 1200), BlockBody( packets, [], Signature.aggregate( [svs[0].signature, svs[1].signature, svs[2].signature])))