def test_chain_current_file_number() -> None: chain = Chain() assert chain.current_file_number == 0 chain.set_current_file_number(chain.current_file_number + 1) assert chain.current_file_number == 1 chain.set_current_file_number(chain.current_file_number + 1) assert chain.current_file_number == 2 chain.set_current_file_number(9999999) assert chain.current_file_number == 9999999
def test_chain_height(): chain = Chain() assert chain.tip == 0 chain.set_tip(chain.tip + 1) assert chain.tip == 1 chain.set_tip(chain.tip + 1) assert chain.tip == 2 chain.set_tip(9999999) assert chain.tip == 9999999
def test_get_blk_file_size() -> None: chain = Chain() Config.MAX_FILE_SIZE = 1000 # Override the max file size for the test path = f"{Config.BLOCKS_DIR}testblock" f = open(path, "a") f.write("".join("x" for _ in range(100))) f.close() assert get_blk_file_size("testblock") == 100
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_chain_current_file_number_increase_when_file_surpases_the_max_size_allowed() -> None: chain = Chain() Config.MAX_FILE_SIZE = 1000 # Override the max file size for the test current_file_number = chain.current_file_number current_file = get_current_blk_file(current_file_number) assert current_file_number == 0 path = f"{Config.BLOCKS_DIR}{current_file}" content_length = Config.MAX_FILE_SIZE + 100 # Surpass f = open(path, "a") f.write("".join("x" for _ in range(content_length))) f.close() actual_file_number = chain.current_file_number assert actual_file_number == 1
def test_accounts() -> None: chain = Chain() assert chain.get_account("LVx7PJDKumHEKtWbZVwg9MxLJmZMf7bdGx") == None chain.set_account("LVx7PJDKumHEKtWbZVwg9MxLJmZMf7bdGx", {"balance": 100}) assert chain.get_account("LVx7PJDKumHEKtWbZVwg9MxLJmZMf7bdGx") == { "balance": 100 } chain.set_account( "LVx7PJDKumHEKtWbZVwg9MxLJmZMf7bdGx", {"balance": 100, "committed": 5000}, ) assert chain.get_account("LVx7PJDKumHEKtWbZVwg9MxLJmZMf7bdGx") == { "balance": 100, "committed": 5000, } chain.set_account( "LXs5GsdMxWtCcXZ4RhZHdKpLs7c71q18eM", {"balance": 0, "committed": 10000}, ) assert chain.get_account("LXs5GsdMxWtCcXZ4RhZHdKpLs7c71q18eM") == { "balance": 0, "committed": 10000, }
def test_block_file_numbers() -> None: chain = Chain() assert chain.get_block_file_number(100) == None chain.set_block_file_number(100, 1) assert chain.get_block_file_number(100) == 1 chain.set_block_file_number(100, 10) chain.set_block_file_number(653020, 250) assert chain.get_block_file_number(100) == 10 assert chain.get_block_file_number(653020) == 250
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