Exemplo n.º 1
0
 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
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
    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()),