def fromJSON( json: Dict[str, Any] ) -> Any: return Block( BlockHeader.fromJSON(json["header"]), BlockBody.fromJSON(json) )
def __init__( self ) -> None: self.genesis: bytes = b"MEROS_DEVELOPER_NETWORK".rjust(32, b'\0') self.upcomingKey: bytes = self.genesis setRandomXKey(self.upcomingKey) self.blockTime: int = 60 self.difficulties: List[int] = [100] self.blocks: List[Block] = [ Block( BlockHeader( 0, self.genesis, bytes(32), 0, bytes(4), bytes(32), PublicKey().serialize(), 0 ), BlockBody() ) ] self.keys: Dict[bytes, int] = {}
def fromJSON( keys: Dict[bytes, int], json: Dict[str, Any] ) -> Any: return Block( BlockHeader.fromJSON(json["header"]), BlockBody.fromJSON(keys, json) )
#Blake2b standard function. from hashlib import blake2b #JSON standard lib. import json #Blockchain. blockchain: Blockchain = Blockchain() #Miner Private Key. privKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) #Create the Block. block: Block = Block( BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32), privKey.toPublicKey().serialize(), blockchain.blocks[-1].header.time + 1200), BlockBody()) #Generate blocks. for i in range(1, 26): #Mine the Block. block.mine(privKey, blockchain.difficulty()) #Add it locally. blockchain.add(block) print("Generated Blank Block " + str(i) + ".") #Create the next Block. block = Block( BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200), BlockBody())
from hashlib import blake2b #JSON standard lib. import json #Blockchain. blockchain: Blockchain = Blockchain() #BLS Keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) 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 Same Nonce Block " + str(len(blockchain.blocks)) + ".") #Create a DataDifficulty. dataDiff: SignedDataDifficulty = SignedDataDifficulty(3, 0) dataDiff.sign(0, blsPrivKey) #Create a conflicting DataDifficulty with the same nonce. dataDiffConflicting = SignedDataDifficulty(1, 0) dataDiffConflicting.sign(0, blsPrivKey)
import json #Blockchains. main: Blockchain = Blockchain() alt: Blockchain = Blockchain() #Miner Private Keys. privKeys: List[PrivateKey] = [ PrivateKey(blake2b(b'\0', digest_size=32).digest()), PrivateKey(blake2b(b'\1', digest_size=32).digest()) ] #Create the Block to the first miner. block: Block = Block( BlockHeader(0, main.last(), bytes(32), 1, bytes(4), bytes(32), privKeys[0].toPublicKey().serialize(), main.blocks[-1].header.time + 1200), BlockBody()) block.mine(privKeys[0], main.difficulty()) main.add(block) alt.add(block) print("Generated Reorganizations Depth One Block 1.") #Create the Block to the second miner. block = Block( BlockHeader(0, main.last(), bytes(32), 1, bytes(4), bytes(32), privKeys[1].toPublicKey().serialize(), main.blocks[-1].header.time + 1200), BlockBody()) block.mine(privKeys[1], alt.difficulty()) main.add(block) alt.add(block) print("Generated Reorganizations Depth One Block 2.")
blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #SpamFilter. spamFilter: SpamFilter = SpamFilter(bytes.fromhex("CC" * 32)) #Blockchain. blockchain: Blockchain = Blockchain() #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 Verify Competing Block " + str(len(blockchain.blocks)) + ".") #Create the initial Data and two competing Datas. datas: List[Data] = [Data(bytes(32), edPubKey.to_bytes())]
#BLS Private Key. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) #Create a Data for the VerificationPacket. data: Data = Data(bytes(32), edPubKey.to_bytes()) data.sign(edPrivKey) data.beat(dataFilter) transactions.add(data) #Generate the VerificationPacket Block. block = Block( BlockHeader( 0, blockchain.last(), BlockHeader.createContents([VerificationPacket(data.hash, [1])]), 1, bytes(4), BlockHeader.createSketchCheck(bytes(4), [VerificationPacket(data.hash, [1])]), blsPrivKey.toPublicKey().serialize(), blockchain.blocks[-1].header.time + 1200), BlockBody([VerificationPacket(data.hash, [1])], [], blsPrivKey.sign(b""))) #Mine it. block.mine(blsPrivKey, blockchain.difficulty()) #Add it to the vectors. blocks.append(block.toJSON()) print("Generated Hundred Six Block Elements VerificationPacket Block.") #Generate the SendDifficulty Block. elements: List[Element] = [] elements.append(SendDifficulty(0, 0, 1)) block = Block(
cmFile.close() #Ed25519 keys. edPrivKey1: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey1: ed25519.VerifyingKey = edPrivKey1.get_verifying_key() edPubKey2: ed25519.VerifyingKey = ed25519.SigningKey(b'\1' * 32).get_verifying_key() #BLS keys. blsPrivKey1: blspy.PrivateKey = blspy.PrivateKey.from_seed(b'\0') blsPubKey1: blspy.PublicKey = blsPrivKey1.get_public_key() blsPrivKey2: blspy.PrivateKey = blspy.PrivateKey.from_seed(b'\1') blsPubKey2: blspy.PublicKey = blsPrivKey2.get_public_key() #Give the second key pair Merit. block: Block = Block( BlockHeader(13, blockchain.last(), int(time())), BlockBody([], [(blsPubKey2, 100)]) ) block.mine(blockchain.difficulty()) blockchain.add(block) print("Generated Competing Block " + str(block.header.nonce) + ".") #Grab the claim hash. claim: bytes = Verification.fromElement(consensus.holders[blsPubKey1.serialize()][1]).hash #Create two competing Sends. send1: Send = Send( [(claim, 0)], [( edPubKey1.to_bytes(), Claim.fromTransaction(transactions.txs[claim]).amount
firstVerif: SignedVerification = SignedVerification(first.hash) firstVerif.sign(0, blsPrivKey) secondVerif: SignedVerification = SignedVerification(second.hash) secondVerif.sign(0, blsPrivKey) packets: List[VerificationPacket] = [ VerificationPacket(first.hash, [0]), VerificationPacket(second.hash, [0]), ] #Generate another 6 Blocks. #Next block should have the packets. block: Block = Block( BlockHeader(0, merit.blockchain.last(), BlockHeader.createContents(packets), 1, bytes(4), BlockHeader.createSketchCheck(bytes(4), packets), 0, merit.blockchain.blocks[-1].header.time + 1200), BlockBody( packets, [], Signature.aggregate([firstVerif.signature, secondVerif.signature]))) for _ in range(6): #Mine it. block.mine(blsPrivKey, merit.blockchain.difficulty()) #Add it. merit.add(block) print("Generated Competing Finalized Block " + str(len(merit.blockchain.blocks) - 1) + ".") #Create the next Block. block = Block(
blocks: List[Dict[str, Any]] = json.loads(bbFile.read()) main: Blockchain = Blockchain.fromJSON(blocks) #Only add the first 15 to the alt chain. alt: Blockchain = Blockchain() for b in range(15): alt.add(Block.fromJSON(blocks[b])) #Generate an alternative fifteen Blocks. for i in range(1, 15): #Create the next Block. block = Block( BlockHeader( 0, alt.last(), bytes(32), 1, bytes(4), bytes(32), 0, alt.blocks[-1].header.time + 1201 #Use a slightly different time to ensure a different hash. ), BlockBody() ) #Mine the Block. block.mine(privKey, alt.difficulty()) #Add it locally. alt.add(block) print("Generated Longer Chain, More Work Block " + str(i) + ".") vectors: IO[Any] = open("PythonTests/Vectors/Merit/Reorganizations/LongerChainMoreWork.json", "w")
#BLS Keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #SpamFilter. spamFilter: SpamFilter = SpamFilter(5) #Blockchains. packetedChain: Blockchain = Blockchain() reorderedChain: Blockchain = Blockchain() #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:
#Time standard function. from time import time #JSON standard lib. import json #Blockchain. blockchain: Blockchain = Blockchain( b"MEROS_DEVELOPER_NETWORK", 60, int("FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 16) ) #Generate blocks. for i in range(1, 26): #Create the Block. block: Block = Block( BlockHeader(i, blockchain.last(), int(time())), BlockBody() ) block.mine(blockchain.difficulty()) #Add it locally. blockchain.add(block) print("Generated Blank Block " + str(i) + ".") vectors: IO[Any] = open("PythonTests/Vectors/Merit/BlankBlocks.json", "w") vectors.write(json.dumps(blockchain.toJSON())) vectors.close()
#BLS Keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #Create a DataDifficulty. dataDiffs: List[SignedDataDifficulty] = [ SignedDataDifficulty(3, 0), SignedDataDifficulty(1, 1) ] for dataDiff in dataDiffs: dataDiff.sign(0, blsPrivKey) #Generate a Block containing the DataDifficulty. block = Block( BlockHeader(0, blockchain.last(), BlockHeader.createContents([], [dataDiffs[0]]), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200), BlockBody([], [dataDiffs[0]], dataDiffs[0].signature)) #Mine it. block.mine(blsPrivKey, blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated DataDifficulty Block " + str(len(blockchain.blocks)) + ".") #Mine 24 more Blocks until there's a vote. for _ in range(24): block = Block( BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32), 0, blockchain.blocks[-1].header.time + 1200), BlockBody()) block.mine(blsPrivKey, blockchain.difficulty())
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)) consensus.add(removal) #Generate a Block with the Verifications. block: Block = Block( BlockHeader(2, blockchain.last(), int(time()), consensus.getAggregate([(pubKey, 0, 5)])), BlockBody([(pubKey, 5, consensus.getMerkle(pubKey, 0, 5))])) #Mine it. block.mine(blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Pending Actions Block " + str(block.header.nonce) + ".") #Generate 4 more Blocks. for i in range(3, 7): block = Block(BlockHeader(i, blockchain.last(), int(time())), BlockBody()) #Mine it. block.mine(blockchain.difficulty()) #Add it.
def blockHeader(self, header: BlockHeader) -> bytes: res: bytes = (MessageType.BlockHeader.toByte() + header.serialize()) self.send(res) return res
main: Blockchain = Blockchain.fromJSON(blocks) #Only add the first 15 to the alt chain. alt: Blockchain = Blockchain() for b in range(15): alt.add(Block.fromJSON(blocks[b])) #Generate an alternative five Blocks. for i in range(1, 5): #Create the next Block. block = Block( BlockHeader( 0, alt.last(), bytes(32), 1, bytes(4), bytes(32), 0, alt.blocks[-1].header.time + 1 #Use a much shorter time to acquire more work. ), BlockBody()) #Mine the Block. block.mine(privKey, alt.difficulty()) #Add it locally. alt.add(block) print("Generated Shorter Chain, More Work Block " + str(i) + ".") vectors: IO[Any] = open(
edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS keys. blsPrivKeys: List[PrivateKey] = [ PrivateKey(blake2b(b'\0', digest_size=32).digest()), PrivateKey(blake2b(b'\1', digest_size=32).digest()) ] blsPubKeys: List[PublicKey] = [ blsPrivKeys[0].toPublicKey(), blsPrivKeys[1].toPublicKey() ] #Give the first key Merit. block: Block = Block( BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32), blsPubKeys[0].serialize(), blockchain.blocks[-1].header.time + 1200), BlockBody()) #Mine it. block.mine(blsPrivKeys[0], blockchain.difficulty()) #Add it. blockchain.add(block) print("Generated Hundred Forty Two Block " + str(len(blockchain.blocks)) + ".") #Give the second key Merit. block: Block = Block( BlockHeader(0, blockchain.last(), bytes(32), 1, bytes(4), bytes(32), blsPubKeys[1].serialize(), blockchain.blocks[-1].header.time + 1200), BlockBody()) block.mine(blsPrivKeys[1], blockchain.difficulty())
#Create the Data. data: Data = Data(edPubKey.to_bytes().rjust(48, b'\0'), bytes()) data.sign(edPrivKey) data.beat(consensus.dataFilter) data.verified = True transactions.add(data) #Verify it. verif: SignedVerification = SignedVerification(data.hash) verif.sign(blsPrivKey, 0) consensus.add(verif) #Generate another 6 Blocks. #Next block should have a record. block: Block = Block( BlockHeader(6, merit.blockchain.last(), int(time()), consensus.getAggregate([(blsPubKey, 0, -1)])), BlockBody([(blsPubKey, 0, consensus.getMerkle(blsPubKey, 0))])) for i in range(7, 13): #Mine it. block.mine(merit.blockchain.difficulty()) #Add it. merit.add(transactions, consensus, block) print("Generated Claimed Mint Block " + str(block.header.nonce) + ".") #Create the next Block. block = Block(BlockHeader(i, merit.blockchain.last(), int(time())), BlockBody()) #Claim the new Mint. claim: Claim = Claim([merit.mints[0].hash], edPubKey.to_bytes())
for h in order[0]: for s in order[0][h]: if s not in packets: packets[s] = VerificationPacket(sends[s].hash, []) packets[s].holders.append(h) verif: SignedVerification = SignedVerification(sends[s].hash) verif.sign(h, blsPrivKeys[h]) toAggregate.append(verif.signature) block: Block = Block( BlockHeader( 0, blockchain.last(), BlockHeader.createContents([], list(packets.values())), 1, bytes(4), BlockHeader.createSketchCheck(bytes(4), list(packets.values())), order[1], blockchain.blocks[-1].header.time + 1200 ), BlockBody(list(packets.values()), [], Signature.aggregate(toAggregate)) ) miner: Union[bytes, int] = order[1] if isinstance(miner, bytes): for k in range(len(blsPubKeys)): if miner == blsPubKeys[k].serialize(): block.mine(blsPrivKeys[k], blockchain.difficulty()) break else: block.mine(blsPrivKeys[miner], blockchain.difficulty())
edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #BLS Keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #SpamFilter. spamFilter: SpamFilter = SpamFilter(5) #Blockchains. e1Chain: Blockchain = Blockchain() e2Chain: Blockchain = Blockchain() #Generate a Block granting the holder Merit. block = Block( BlockHeader(0, e1Chain.last(), bytes(32), 1, bytes(4), bytes(32), blsPubKey.serialize(), e1Chain.blocks[-1].header.time + 1200), BlockBody()) #Mine it. block.mine(blsPrivKey, e1Chain.difficulty()) #Add it. e1Chain.add(block) e2Chain.add(block) print("Generated Hundred Thirty Three Block 1/2 " + str(len(e1Chain.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: 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, bytes(4), BlockHeader.createSketchCheck(bytes(4), [VerificationPacket(verif.hash, [0])]), 0, merit.blockchain.blocks[-1].header.time + 1200), BlockBody([VerificationPacket(verif.hash, [0])], [], verif.signature)) for _ in range(6): #Mine it. block.mine(blsPrivKey, merit.blockchain.difficulty()) #Add it. merit.add(block) print("Generated Invalid Competing Block " + str(len(merit.blockchain.blocks) - 1) + ".") #Create the next Block. block = Block(
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()), 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) + ".") result: Dict[str, Any] = { "blockchain": blockchain.toJSON(), "data": snVectors["data"], "removal1": removal1.toSignedJSON(), "removal2": removal2.toSignedJSON() } vectors: IO[Any] = open(
[(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, blockchain.last(), BlockHeader.createContents([packet]), 1, bytes(4), BlockHeader.createSketchCheck(bytes(4), [packet]), 0, blockchain.blocks[-1].header.time + 1200), BlockBody([packet], [], sv.signature)) #Mine the Block. block.mine(blsPrivKey, blockchain.difficulty()) #Add the Block. blockchain.add(block) print("Generated Same Input Block " + str(len(blockchain.blocks) - 1) + ".") #Save the vector. result: Dict[str, Any] = { "blockchain": blockchain.toJSON(), "transactions": transactions.toJSON() }
#BLS Keys. blsPrivKey: PrivateKey = PrivateKey(blake2b(b'\0', digest_size=32).digest()) blsPubKey: PublicKey = blsPrivKey.toPublicKey() #Ed25519 keys. edPrivKey: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32) edPubKey: ed25519.VerifyingKey = edPrivKey.get_verifying_key() #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 Same Element Block " + str(len(blockchain.blocks)) + ".") #Create a SendDifficulty. sendDiff: SignedSendDifficulty = SignedSendDifficulty(4, 0)