Exemplo n.º 1
0
    BlockBody(packet, [], verif.signature))
block.mine(blsPrivKey, merit.blockchain.difficulty())
merit.add(block)

#Generate another 5 Blocks to close the Epochs.
for _ in range(5):
    block = Block(
        BlockHeader(0, merit.blockchain.last(), bytes(32), 0, bytes(4),
                    bytes(32),
                    0, merit.blockchain.blocks[-1].header.time + 1200),
        BlockBody())
    block.mine(blsPrivKey, merit.blockchain.difficulty())
    merit.add(block)

#Create the Claim.
claim: Claim = Claim([(merit.mints[0], 0), (merit.mints[0], 0)],
                     edPubKey.to_bytes())
claim.sign(blsPrivKey)
transactions.add(claim)

#Archive it on the Blockchain.
verif: SignedVerification = SignedVerification(claim.hash)
verif.sign(0, blsPrivKey)

packet: List[VerificationPacket] = [VerificationPacket(claim.hash, [0])]
block = Block(
    BlockHeader(0, merit.blockchain.last(),
                BlockHeader.createContents(packet), 1, bytes(4),
                BlockHeader.createSketchCheck(bytes(4), packet), 0,
                merit.blockchain.blocks[-1].header.time + 1200),
    BlockBody(packet, [], verif.signature))
block.mine(blsPrivKey, merit.blockchain.difficulty())
Exemplo n.º 2
0
from e2e.Classes.Consensus.VerificationPacket import VerificationPacket
from e2e.Classes.Consensus.SpamFilter import SpamFilter

from e2e.Classes.Merit.Merit import Merit

from e2e.Vectors.Generation.PrototypeChain import PrototypeBlock, PrototypeChain

merit: Merit = PrototypeChain.withMint()
transactions: Transactions = Transactions()
sendFilter: SpamFilter = SpamFilter(3)

edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32)
edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key()

claim: Claim = Claim([(merit.mints[-1].hash, 0)], edPubKey.to_bytes())
claim.amount = merit.mints[-1].outputs[0][1]
claim.sign(PrivateKey(0))
transactions.add(claim)
merit.add(
    PrototypeBlock(merit.blockchain.blocks[-1].header.time + 1200,
                   packets=[VerificationPacket(claim.hash,
                                               [0])]).finish(0, merit))

#Create a Send spending it twice.
send: Send = Send(
    [(claim.hash, 0), (claim.hash, 0)],
    [(edPubKey.to_bytes(),
      Claim.fromTransaction(transactions.txs[claim.hash]).amount * 2)])
send.sign(edPrivKey)
send.beat(sendFilter)
def HundredSeventySevenTest(rpc: RPC) -> None:
    #Grab the keys.
    blsPrivKey: PrivateKey = PrivateKey(
        bytes.fromhex(rpc.call("personal", "getMiner")))
    blsPubKey: PublicKey = blsPrivKey.toPublicKey()

    #Faux Blockchain used to calculate the difficulty.
    blockchain: Blockchain = Blockchain()

    #Mine 8 Blocks.
    #The first creates the initial Data.
    #The next 6 pop it from the Epochs.
    #One more is to verify the next is popped as well.
    for b in range(0, 8):
        template: Dict[str, Any] = rpc.call("merit", "getBlockTemplate",
                                            [blsPubKey.serialize().hex()])
        template["header"] = bytes.fromhex(template["header"])

        header: BlockHeader = BlockHeader(
            0, template["header"][4:36], template["header"][36:68],
            int.from_bytes(template["header"][68:70], byteorder="little"),
            template["header"][70:74], template["header"][74:106], 0,
            int.from_bytes(template["header"][-4:], byteorder="little"))
        if b == 0:
            header.newMiner = True
            header.minerKey = blsPubKey.serialize()
        else:
            header.newMiner = False
            header.minerNick = 0

        if header.serializeHash()[:-4] != template["header"]:
            raise TestError("Failed to recreate the header.")

        header.mine(blsPrivKey, blockchain.difficulty())

        #Sleep for just over two thirty-second cycles to make sure we can connect to the node.
        sleep(65)
        rpc.meros.liveConnect(blockchain.blocks[-1].header.hash)

        blockchain.add(Block(header, BlockBody()))
        rpc.call("merit", "publishBlock", [
            template["id"],
            (template["header"] +
             header.proof.to_bytes(4, byteorder="little") + header.signature +
             bytes.fromhex(template["body"])).hex()
        ])

        if rpc.meros.live.recv() != rpc.meros.liveBlockHeader(header):
            raise TestError("Meros didn't broadcast the BlockHeader.")

        #If there's supposed to be a Mint, verify Meros claimed it.
        if b >= 6:
            #Artificially create the Mint since the Blockchain won't create one without a recreated BlockBody.
            #It's faster to create a faux Mint than to handle the BlockBodies.
            mint: Mint = Mint(header.hash, [(0, 50000)])

            claim: Claim = Claim([(mint.hash, 0)], bytes(32))
            claim.sign(blsPrivKey)
            if rpc.meros.live.recv()[0:-80] != (MessageType.Claim.toByte() +
                                                claim.serialize())[0:-80]:
                raise TestError("Meros didn't claim its Mint.")

            if MessageType(rpc.meros.live.recv()
                           [0]) != MessageType.SignedVerification:
                raise TestError("Meros didn't verify its Claim.")

        #Create the matching Data.
        data: Data = Data(blockchain.genesis, header.hash)
        #Make sure Meros broadcasts a valid Verification for it.
        verif: SignedVerification = SignedVerification(data.hash)
        verif.sign(0, blsPrivKey)
        if rpc.meros.live.recv() != rpc.meros.signedElement(verif):
            raise TestError("Meros didn't verify the Block's Data.")

        #Close the live connection so we don't have to worry about it being disconnected for inactivity.
        try:
            rpc.meros.live.connection.shutdown(socket.SHUT_RDWR)
            rpc.meros.live.connection.close()
        except OSError:
            pass
        sleep(3)
Exemplo n.º 4
0
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)],
  [(
    edPubKey.to_bytes(),
    Claim.fromTransaction(transactions.txs[claim]).amount * 2
  )]
)
send.sign(edPrivKey)
send.beat(sendFilter)
transactions.add(send)

#Create a Verification/VerificationPacket for the Send.
sv: SignedVerification = SignedVerification(send.hash)
sv.sign(0, blsPrivKey)
packet: VerificationPacket = VerificationPacket(send.hash, [0])

#Add a Block verifying it.
block: Block = Block(
  BlockHeader(
    0,