Пример #1
0
 def toJSON(self) -> Dict[str, List[Dict[str, Any]]]:
     result: Dict[str, List[Dict[str, Any]]] = {}
     for holder in self.holders:
         result[holder.hex().upper()] = []
         for elem in self.holders[holder]:
             if hasattr(elem, "toSignedJSON"):
                 result[holder.hex().upper()].append(
                     SignedElement.fromElement(elem).toSignedJSON())
             else:
                 result[holder.hex().upper()].append(elem.toJSON())
     return result
Пример #2
0
    def fromJSON(json: Dict[str, Any]) -> Any:
        se1: SignedElement = SignedElement()
        if json["elements"][0]["descendant"] == "Verification":
            se1 = SignedVerification.fromJSON(json["elements"][0])
        else:
            raise Exception(
                "MeritRemoval constructed from an unsupported type of Element: "
                + json["elements"][0]["descendant"])

        se2: SignedElement = SignedElement()
        if json["elements"][1]["descendant"] == "Verification":
            se2 = SignedVerification.fromJSON(json["elements"][1])
        else:
            raise Exception(
                "MeritRemoval constructed from an unsupported type of Element: "
                + json["elements"][1]["descendant"])

        result: SignedMeritRemoval = SignedMeritRemoval(se1, se2)
        result.nonce = json["nonce"]
        return result
Пример #3
0
    def signedElement(self, elem: Element) -> bytes:
        res: bytes = bytes()
        if isinstance(elem, SignedVerification):
            res = MessageType.SignedVerification.toByte()
        elif isinstance(elem, PartiallySignedMeritRemoval):
            res = MessageType.SignedMeritRemoval.toByte()
        else:
            raise Exception(
                "Unsupported Element passed to Meros.signedElement.")
        res += SignedElement.fromElement(elem).signedSerialize()

        self.send(res)
        return res
Пример #4
0
    def getAggregate(self, records: List[Tuple[blspy.PublicKey, int,
                                               int]]) -> bytes:
        signatures: List[blspy.Signature] = []

        for record in records:
            holder: bytes = record[0].serialize()
            start: int = record[1]
            end: int = record[2]
            if end == -1:
                end = len(self.holders[holder])
            else:
                end += 1

            for e in range(start, end):
                signatures.append(
                    SignedElement.fromElement(
                        self.holders[holder][e]).blsSignature)

        result: bytes = blspy.Signature.aggregate(signatures).serialize()
        return result
Пример #5
0
    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)
    if d < 3:
        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 first 3 Elements.
block: Block = Block(
    BlockHeader(2, blockchain.last(), int(time()),
                consensus.getAggregate([(pubKey, 0, 2)])),
    BlockBody([(pubKey, 2, consensus.getMerkle(pubKey, 0, 2))]))
#Mine it.
block.mine(blockchain.difficulty)

#Add it.
blockchain.add(block)
print("Generated Pending Actions Block " + str(block.header.nonce) + ".")

#Generate a Block with the MeritRemoval.
Пример #6
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] = {
Пример #7
0
#Load a Multiple Block and load their MeritRemoval.
snFile: IO[Any] = open(
    "python_tests/Vectors/Consensus/MeritRemoval/SameNonce.json", "r")
snVectors: Dict[str, Any] = json.loads(snFile.read())

blockchain.add(Block.fromJSON(snVectors["blockchain"][0]))

mr1: SignedMeritRemoval = SignedMeritRemoval.fromJSON(snVectors["removal"])

snFile.close()

#Create a second MeritRemoval.
sv: SignedVerification = SignedVerification(b'\1' * 48)
sv.sign(privKey, 0)
mr2: SignedMeritRemoval = SignedMeritRemoval(mr1.se1,
                                             SignedElement.fromElement(sv))

#Add the second MeritRemoval to Consensus.
consensus.add(mr2)

#Generate a Block with the second MeritRemoval.
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 Multiple Block " + str(block.header.nonce) + ".")