Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
#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)
reorderedChain.add(block)
print("Generated Hundred Twenty Three Packet Block 1/2 " +
      str(len(packetedChain.blocks)) + ".")

#Create the initial Data and two competing Datas.
datas: List[Data] = [Data(bytes(32), edPubKey.to_bytes())]
datas.append(Data(datas[0].hash, b"Initial Data."))
datas.append(Data(datas[0].hash, b"Second Data."))
for data in datas:
    data.sign(edPrivKey)
    data.beat(spamFilter)

#Create Verifications for all 3.
verifs: List[SignedVerification] = []
for data in datas:
    verifs.append(SignedVerification(data.hash, 0))
    verifs[-1].sign(0, blsPrivKey)

#Create a MeritRemoval out of the conflicting Verifications.
mr: SignedMeritRemoval = SignedMeritRemoval(verifs[1], verifs[2])
Ejemplo n.º 4
0
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())
data.sign(edPrivKey)
data.beat(dataFilter)
transactions.add(data)

#Verify it.
verif: SignedVerification = SignedVerification(data.hash)
verif.sign(0, blsPrivKey)

#Generate another 6 Blocks.
#Next block should have a packet.
block: Block = Block(
    BlockHeader(
        0, merit.blockchain.last(),
        BlockHeader.createContents([],
                                   [VerificationPacket(verif.hash, [0])]), 1,
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
blsPubKey: PublicKey = blsPrivKey.toPublicKey()

#Generate a Block granting the holder Merit.
block = Block(
    BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32),
                blsPubKey.serialize(),
                blockchain.blocks[-1].header.time + 1200), BlockBody())
#Mine it.
block.mine(blsPrivKey, blockchain.difficulty())

#Add it.
blockchain.add(block)
print("Generated Invalid Competing Block " + str(len(blockchain.blocks)) + ".")

#Create a Data using a bogus input.
data: Data = Data(bytes.fromhex("11" * 32), bytes(1))
transactions.add(data)

#Create a competing Data using the same input.
competingData: Data = Data(bytes.fromhex("11" * 32), bytes(2))
transactions.add(competingData)

#Verify the Datas.
verif: SignedVerification = SignedVerification(data.hash)
verif.sign(0, blsPrivKey)

competingVerif: SignedVerification = SignedVerification(competingData.hash)
competingVerif.sign(0, blsPrivKey)

#Create a MeritRemoval out of the conflicting Verifications.
mr: SignedMeritRemoval = SignedMeritRemoval(verif, competingVerif)
Ejemplo n.º 7
0
dataFilter: SpamFilter = SpamFilter(5)

#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 1 Blank Block.
for i in range(1):
    merit.add(Block.fromJSON(blankBlocks[i]))

#Create the Data and a successor.
first: Data = Data(bytes(32), edPubKey.to_bytes())
first.sign(edPrivKey)
first.beat(dataFilter)
transactions.add(first)

second: Data = Data(first.hash, bytes(1))
second.sign(edPrivKey)
second.beat(dataFilter)
transactions.add(second)

#Verify them.
firstVerif: SignedVerification = SignedVerification(first.hash)
firstVerif.sign(0, blsPrivKey)

secondVerif: SignedVerification = SignedVerification(second.hash)
secondVerif.sign(0, blsPrivKey)
Ejemplo n.º 8
0
#Ed25519 keys.
edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32)
edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key()

#BLS Keys.
privKey: blspy.PrivateKey = blspy.PrivateKey.from_seed(b'\0')
pubKey: blspy.PublicKey = privKey.get_public_key()

#Add a single Block to create Merit.
bbFile: IO[Any] = open("PythonTests/Vectors/Merit/BlankBlocks.json", "r")
blocks: List[Dict[str, Any]] = json.loads(bbFile.read())
blockchain.add(Block.fromJSON(blocks[0]))
bbFile.close()

#Create a Data with an invalid signature.
data: Data = Data(edPubKey.to_bytes().rjust(48, b'\0'), bytes())
data.signature = edPrivKey.sign(b"INVALID")
data.beat(consensus.dataFilter)

#Create a Verification.
sv: SignedVerification = SignedVerification(data.hash)
sv.sign(privKey, 0)
consensus.add(sv)

#Generate another Block.
block: Block = Block(
    BlockHeader(
        2,
        blockchain.last(),
        int(time()),
        consensus.getAggregate([(pubKey, 0, -1)])
Ejemplo n.º 9
0
#Ed25519 keys.
edKeys: List[ed25519.SigningKey] = []
for i in range(6):
    edKeys.append(ed25519.SigningKey(i.to_bytes(1, "big") * 32))

#Add a single Block to create Merit.
bbFile: IO[Any] = open("PythonTests/Vectors/Merit/BlankBlocks.json", "r")
blocks: List[Dict[str, Any]] = json.loads(bbFile.read())
blockchain.add(Block.fromJSON(blocks[0]))
bbFile.close()

#Create a Data per key.
datas: List[Data] = []
for edPrivKey in edKeys:
    datas.append(
        Data(edPrivKey.get_verifying_key().to_bytes().rjust(48, b'\0'),
             bytes()))
    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)
    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))
Ejemplo n.º 10
0
blsPubKey: PublicKey = blsPrivKey.toPublicKey()

#Generate a Block granting the holder Merit.
block = Block(
    BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32),
                blsPubKey.serialize(),
                blockchain.blocks[-1].header.time + 1200), BlockBody())
#Mine it.
block.mine(blsPrivKey, blockchain.difficulty())

#Add it.
blockchain.add(block)
print("Generated Prune Unaddable Block " + str(len(blockchain.blocks)) + ".")

#Create the original Data.
datas: List[Data] = [Data(bytes(32), edPubKey.to_bytes())]
datas[0].sign(edPrivKey)
datas[0].beat(dataFilter)

#Verify it.
verifs: List[SignedVerification] = [SignedVerification(datas[0].hash)]
verifs[0].sign(0, blsPrivKey)

#Create two competing Datas yet only verify the first.
for d in range(2):
    datas.append(Data(datas[0].hash, d.to_bytes(1, "big")))
    datas[1 + d].sign(edPrivKey)
    datas[1 + d].beat(dataFilter)

verifs.append(SignedVerification(datas[1].hash))
verifs[1].sign(0, blsPrivKey)
Ejemplo n.º 11
0
#Add a 5th Block to another verifier.
block: Block = Block(
    BlockHeader(0, merit.blockchain.last(), bytes(32), 1, bytes(4), bytes(32),
                blsPubKeys[1].serialize(),
                merit.blockchain.blocks[-1].header.time + 1200), BlockBody())
#Mine it.
block.mine(blsPrivKeys[1], merit.blockchain.difficulty())

#Add it.
merit.add(block)
print("Generated Aggregated Claim Block " +
      str(len(merit.blockchain.blocks) - 1) + ".")

#Create the Datas.
datas: List[Data] = [Data(bytes(32), edPubKey.to_bytes())]
datas.append(Data(datas[-1].hash, bytes(1)))
datas.append(Data(datas[-1].hash, bytes(1)))
datas.append(Data(datas[-1].hash, bytes(1)))
for data in datas:
    data.sign(edPrivKey)
    data.beat(dataFilter)
    transactions.add(data)

#Verify them.
verifs: List[List[SignedVerification]] = []
for data in datas:
    verifs.append(
        [SignedVerification(data.hash),
         SignedVerification(data.hash)])
    for v in range(2):
Ejemplo n.º 12
0
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.")