def block2() -> Block: block = Block( version=1, prev_block_hash=Config.COINBASE_TX_ID, timestamp=1_501_821_412, bits=24, nonce=10_126_761, ) block.txns = [coinbase2, tx1, tx2, tx3, tx4, tx5] return block
def test_block_save(): coinbase_transaction_1 = Transaction( chain=1, nonce=1, fee=0, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block1 = Block( version=1, height=0, miner=WALLET_1["address"], prev_block_hash="0" * 64, timestamp=1_623_168_442, bits=b"\x1d\x0f\xff\xff", nonce=12308683, txns=[coinbase_transaction_1], ) print(block1.json()) coinbase_transaction_2 = Transaction( chain=1, nonce=2, fee=0, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block2 = Block( version=1, height=1, miner=WALLET_1["address"], prev_block_hash=block1.id, timestamp=1_623_168_442, bits=b"\x1d\x0f\xff\xff", nonce=12308683, txns=[coinbase_transaction_2], ) print(block2.json()) chain = Chain() assert chain.tip == 0 block1.save() assert chain.tip == 0 block2.save() assert chain.tip == 1 assert False
def test_block_select_transactions(): redis_client = redis.Redis(host=Config.REDIS_HOST, port=Config.REDIS_PORT, db=Config.REDIS_DB) redis_client.flushdb() transactions = add_test_transactions() assert len(redis_client.keys()) == 5 block1 = Block( version=1, prev_block_hash=0, timestamp=1_623_168_442, signature="0", txns=[], ) block1.select_transactions() txns_list = [ { "id": transactions[0].id, "signature": transactions[0].unlock_sig.hex(), }, { "id": transactions[1].id, "signature": transactions[1].unlock_sig.hex(), }, { "id": transactions[2].id, "signature": transactions[2].unlock_sig.hex(), }, { "id": transactions[3].id, "signature": transactions[3].unlock_sig.hex(), }, { "id": transactions[4].id, "signature": transactions[4].unlock_sig.hex(), }, ] newlist = sorted(txns_list, key=itemgetter("id")) assert block1.txns[0].id == newlist[0]["id"] assert block1.txns[1].id == newlist[1]["id"] assert block1.txns[2].id == newlist[2]["id"] assert block1.txns[3].id == newlist[3]["id"] assert block1.txns[4].id == newlist[4]["id"]
def test_validate(): coinbase_transaction_1 = Transaction( chain=1, nonce=1, fee=0, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block1 = Block( version=1, height=0, prev_block_hash="0" * 64, timestamp=1_623_168_442, bits=Config.STARTING_DIFFICULTY, nonce=0, txns=[coinbase_transaction_1], ) assert block1.validate() is False proof_of_work(block=block1, starting_at=4334955) assert block1.nonce == 4334956 assert block1.validate() is True transactions = add_test_transactions() coinbase_transaction_2 = Transaction( chain=1, nonce=2, fee=0, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block2 = Block( version=1, height=1, prev_block_hash=block1.id, timestamp=1_623_208_442, bits=Config.STARTING_DIFFICULTY, nonce=0, txns=[coinbase_transaction_2, *transactions], ) proof_of_work(block=block2, starting_at=1162892) assert block2.nonce == 1162893
def test_block_serialize__with_one_transaction(blockchain): coinbase_transacion = Transaction( chain=1, nonce=8763, fee=100, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block1 = Block( version=1, height=0, miner=WALLET_1["address"], prev_block_hash="0" * 64, timestamp=1_623_168_442, bits=b"\x1f\x00\xff\xff", nonce=0, txns=[coinbase_transacion], ) block2 = Block().deserialize(block1.serialize()) assert block1.id == block2.id assert block1.json() == block2.json()
def test_block_deserialize(block1, block2): # type: ignore block = Block() block.deserialize( "ba77d89f010000000000000000000000000000000000000000000000000000000000" "000000000000c7f83f3706612f372096e82b6333c7e51b8bf3cf2aa6abdf348bce7e" "ce21017218000000e4f98359a9859a00000006010001000000000000000000000000" "0000000000000000000000000000000000000000ffffffff011000000000100f2052" "a010000003476a915008d3f74c5f4b7b9d3193a950986a9a16decb4155088ac01000" "14ac727f587761a17f5a3b63de589959989c655b579d261361ebaa287954440a5000" "000000100000000001d0090000000000003476a915002fb168b47d7cb54b07c7a4c4" "f7c005811f0a775d88ac0100014ac727f587761a17f5a3b63de589959989c655b579" "d261361ebaa287954440a500000000010000000000180969800000000003476a9150" "0d00b433df737fd3da4b2551a356d556206f2e60788ac0100014ac727f587761a17f" "5a3b63de589959989c655b579d261361ebaa287954440a5000000000100000000001" "00c2eb0b000000003476a915002a35a40188d3ca15da2cb5ac43c5f3add8c64b1688" "ac0100014ac727f587761a17f5a3b63de589959989c655b579d261361ebaa2879544" "40a500000000010000000000130c80700000000003476a91500785211d22e8c5c6ba" "d7410b9baf99dac866a447f88ac0100014ac727f587761a17f5a3b63de589959989c" "655b579d261361ebaa287954440a501000000010000000000130c807000000000034" "76a9150044d27de0e2f8e87f26beaf3cc727f1a642a09af388ac" ) assert block.version == block2.version assert block.timestamp == block2.timestamp assert block.bits == block2.bits assert block.nonce == block2.nonce assert block.prev_block_hash == block2.prev_block_hash assert [tx.id for tx in block.txns] == [tx.id for tx in block2.txns] assert block.id == block2.id block_small = Block() block_small.deserialize( "ba77d89f010000000000000000000000000000000000000000000000000000000000" "0000000000008823303f8041cc303e52ca1c7d0533bffe09f63c51a86137f6221945" "5f38951818000000e4f98359a9859a00000001010001000000000000000000000000" "0000000000000000000000000000000000000000ffffffff0100000000003005ed0b" "2000000003476a91500ff7ff008512170c57980f447dc24f5c52e2df03c88ac002f6" "859000000003476a915003ea6275ba0c6860ce74727f72617883099961a4388ac006" "5cd1d000000003476a91500785211d22e8c5c6bad7410b9baf99dac866a447f88ac" ) assert block_small.version == block1.version assert block_small.timestamp == block1.timestamp assert block_small.bits == block1.bits assert block_small.nonce == block1.nonce assert block_small.prev_block_hash == block1.prev_block_hash assert [tx.id for tx in block_small.txns] == [tx.id for tx in block1.txns] assert block_small.id == block1.id
def test_block_serialize__with_multiple_transaction(): coinbase_transaction_1 = Transaction( chain=1, nonce=1, fee=0, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block1 = Block( version=1, height=0, miner=WALLET_1["address"], prev_block_hash="0" * 64, timestamp=1_623_168_442, bits=b"\x1f\x00\xff\xff", nonce=0, txns=[coinbase_transaction_1], ) transactions = add_test_transactions() coinbase_transaction_2 = Transaction( chain=1, nonce=2, fee=0, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block2 = Block( version=1, height=1, miner=WALLET_1["address"], prev_block_hash=block1.id, timestamp=1_623_208_442, bits=b"\x1f\x00\xff\xff", nonce=0, txns=[coinbase_transaction_2, *transactions], ) block3 = Block().deserialize(block2.serialize()) assert block2.id == block3.id assert block2.json() == block3.json()
def init_blockchain(): """ Initialize blockchain with genesis block """ coinbase_transacion = Transaction( chain=1, nonce=8763, fee=100, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=None, ) block1 = Block( version=1, height=0, prev_block_hash="0" * 64, timestamp=1_623_168_442, bits=b"\1f\x00\xff\xff", nonce=0, txns=[coinbase_transacion], )
from luracoin.helpers import bits_to_target from luracoin.blocks import Block from luracoin.transactions import Transaction from tests.helpers import add_test_transactions from luracoin.config import Config from luracoin.pow import proof_of_work coinbase_transaction_1 = Transaction( chain=1, nonce=1, fee=0, value=50000, to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP", unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE, ) block1 = Block( version=1, height=0, prev_block_hash="0" * 64, timestamp=1_623_168_442, bits=b"\x1d\x0f\xff\xff", nonce=0, txns=[coinbase_transaction_1], ) # Nonce: 12308683 proof_of_work(block1, 12_308_683) print(block1.json())
def test_full_blockchain(): START_TIMESTAMP = 1639159886 chain = Chain() assert chain.tip == 0 block1 = Block( version=1, height=0, prev_block_hash="0" * 64, miner=WALLET_1["address"], timestamp=START_TIMESTAMP, bits=Config.STARTING_DIFFICULTY, nonce=156369, txns=[], ) print("\nBlock 1 \n" + json.dumps(block1.json(), indent=4)) block1.save() # Check chain Height is updated assert chain.tip == 0 # Check block file number is updated assert chain.get_block_file_number(0) == 0 # Check that the block file contains one block assert len(Block.get_blocks_from_file(0)) == 1 # Check that the block retrieved from the files is the same assert Block.get(0).id == block1.id assert Block.get(0).serialize() == block1.serialize() # TODO: Check miner balance # TODO: Check file content # TODO: Check stacking # Transaction to stack 15 Luracoins transaction_1 = Transaction( chain=1, nonce=1, fee=0, value=Config.LURASHIS_PER_COIN * 15, to_address=Config.STAKING_ADDRESS, ) transaction_1.sign(unhexlify(WALLET_1["private_key"])) assert transaction_1.validate() == True block2 = Block( version=1, height=1, prev_block_hash=block1.id, miner=WALLET_1["address"], timestamp=START_TIMESTAMP + (3*60), # 3 minutes bits=Config.STARTING_DIFFICULTY, nonce=4358788, txns=[transaction_1], ) print("\nBlock 2 \n" + json.dumps(block2.json(), indent=4)) block2.save() # Check chain Height is updated assert chain.tip == 1 # Check block file number is updated assert chain.get_block_file_number(0) == 0 # Check that the block file contains two blocks assert len(Block.get_blocks_from_file(0)) == 2 # Check that the block retrieved from the files is the same assert Block.get(1).id == block2.id assert Block.get(1).serialize() == block2.serialize() # TODO: Check miner balance # TODO: Check file content # TODO: Check stacking assert False