def crypto_block(crypto_transaction, crypto_transaction_2): # TODO: Block is not from testnet and it has wrong data and signature # Change it once we're able to forge blocks with our code block = Block( height=2243161, id="10977713934532967004", id_hex=b"9858aca939b17a5c", number_of_transactions=2, payload_hash=( "3784b953afcf936bdffd43fdf005b5732b49c1fc6b11e195c364c20b2eb06282" ), payload_length=224, previous_block="3112633353705641986", previous_block_hex=b"2b324b8b33a85802", reward=200000000, timestamp=24760440, total_amount=3890300, total_fee=70000000, version=0, generator_public_key=( "020f5df4d2bc736d12ce43af5b1663885a893fade7ee5e62b3cc59315a63e6a325" ), block_signature=( "3045022100eee6c37b5e592e99811d588532726353592923f347c701d52912e6d583443e40022" "0277ffe38ad31e216ba0907c4738fed19b2071246b150c72c0a52bae4477ebe29" ), ) crypto_transaction.block_id = "7176646138626297930" crypto_transaction_2.block_id = "7176646138626297930" block.transactions = [crypto_transaction, crypto_transaction_2] return block
def test_apply_transaction_raises_if_cant_be_applied_to_sender_wallet(redis, mocker): manager = WalletManager() block = Block() transaction = TransferTransaction() transaction.type = TRANSACTION_TYPE_TRANSFER transaction.fee = 10000 transaction.amount = 430000 transaction.id = "hehe" transaction.sender_public_key = ( "020f5df4d2bc736d12ce43af5b1663885a893fade7ee5e62b3cc59315a63e6a325" ) transaction.recipient_id = "AZYnpgXS3x43nxqhT4q29sZScRwZeNKLpW" redis.set( "wallets:address:AThM5PNSKdU9pu1ydqQnzRWVeNCGr8HKof", Wallet( { "address": "AThM5PNSKdU9pu1ydqQnzRWVeNCGr8HKof", "public_key": ( "020f5df4d2bc736d12ce43af5b1663885a893fade7ee5e62b3cc59315a63e6a325" ), "balance": 5, } ).to_json(), ) with pytest.raises(Exception) as excinfo: manager.apply_transaction(transaction, block) assert str(excinfo.value) == ( "Can't apply transaction hehe from sender AThM5PNSKdU9pu1ydqQnzRWVeNCGr8HKof" )
def test_apply_block_updates_vote_balance_of_voted_delegate(redis, mocker): manager = WalletManager() apply_transaction_mock = mocker.patch( "chain.plugins.database.wallet_manager.WalletManager.apply_transaction" ) transaction_1 = TransferTransaction() transaction_2 = TransferTransaction() block = Block() block.height = 1337 block.reward = 2 block.transactions = [transaction_1, transaction_2] block.generator_public_key = ( "0316510c1409d3307d9f205cac58f1a871499c3ffea3878ddbbb48c821cfbc079a" ) redis.set( "wallets:address:AWoysqF1xm1LXYLQvmRDpfVNKzzaLVwPVM", json.dumps( { "address": "AWoysqF1xm1LXYLQvmRDpfVNKzzaLVwPVM", "public_key": ( "0316b3dc139c1a35927ecbdcb8d8b628ad06bd4f1869fe3ad0e23c8106678a460f" ), "vote_balance": 2000000, } ), ) redis.set( "wallets:address:ASt5oBHKDW8AeJe2Ybc1RucMLS7mRCiuRe", Wallet( { "address": "ASt5oBHKDW8AeJe2Ybc1RucMLS7mRCiuRe", "public_key": ( "0316510c1409d3307d9f205cac58f1a871499c3ffea3878ddbbb48c821cfbc079a" ), "balance": 0, "vote": ( "0316b3dc139c1a35927ecbdcb8d8b628ad06bd4f1869fe3ad0e23c8106678a460f" ), } ).to_json(), ) manager.apply_block(block) assert apply_transaction_mock.call_count == 2 delegate = Wallet( json.loads(redis.get("wallets:address:ASt5oBHKDW8AeJe2Ybc1RucMLS7mRCiuRe")) ) assert delegate.balance == 2 vote_wallet = Wallet( json.loads(redis.get("wallets:address:AWoysqF1xm1LXYLQvmRDpfVNKzzaLVwPVM")) ) assert vote_wallet.vote_balance == 2000002
def test_apply_transaction_force_apply_skips_can_be_applied_check(redis, mocker): manager = WalletManager() balances_mock = mocker.patch( "chain.plugins.database.wallet_manager.WalletManager._update_vote_balances" ) mocker.patch( "chain.plugins.database.wallet_manager.is_transaction_exception", return_value=True, ) block = Block() transaction = TransferTransaction() transaction.type = TRANSACTION_TYPE_TRANSFER transaction.fee = 10000 transaction.amount = 430000 transaction.id = "hehe" transaction.sender_public_key = ( "020f5df4d2bc736d12ce43af5b1663885a893fade7ee5e62b3cc59315a63e6a325" ) transaction.recipient_id = "AZYnpgXS3x43nxqhT4q29sZScRwZeNKLpW" redis.set( "wallets:address:AThM5PNSKdU9pu1ydqQnzRWVeNCGr8HKof", Wallet( { "address": "AThM5PNSKdU9pu1ydqQnzRWVeNCGr8HKof", "public_key": ( "020f5df4d2bc736d12ce43af5b1663885a893fade7ee5e62b3cc59315a63e6a325" ), "balance": 0, } ).to_json(), ) manager.apply_transaction(transaction, block) # updated sender wallet is saved back to redis sender = Wallet( json.loads(redis.get("wallets:address:AThM5PNSKdU9pu1ydqQnzRWVeNCGr8HKof")) ) assert sender.balance == -440000 # updated recipient wallet is saved back to redis recipient = Wallet( json.loads(redis.get("wallets:address:AZYnpgXS3x43nxqhT4q29sZScRwZeNKLpW")) ) assert recipient.balance == 430000 balances_mock.assert_called_once()
def test_apply_transaction_raises_if_voted_delegate_doesnt_exist(): """ Should raise exception if delegate already exists """ manager = WalletManager() public_key = "020f5df4d2bc736d12ce43af5b1663885a893fade7ee5e62b3cc59315a63e6a325" block = Block() transaction = TransferTransaction() transaction.type = TRANSACTION_TYPE_VOTE transaction.id = "hehe" transaction.asset["votes"] = ["+{}".format(public_key)] with pytest.raises(Exception) as excinfo: manager.apply_transaction(transaction, block) assert str( excinfo.value ) == "Can't apply transaction hehe: delegate {} does not exist".format(public_key)
def test_apply_block_raises_if_not_genesis_and_no_delegate(redis): manager = WalletManager() transaction_1 = TransferTransaction() transaction_2 = TransferTransaction() block = Block() block.height = 1337 block.reward = 2 block.transactions = [transaction_1, transaction_2] block.generator_public_key = ( "0316510c1409d3307d9f205cac58f1a871499c3ffea3878ddbbb48c821cfbc079a" ) with pytest.raises(Exception) as excinfo: manager.apply_block(block) assert str(excinfo.value) == ( "Could not find a delegate with public key: " "0316510c1409d3307d9f205cac58f1a871499c3ffea3878ddbbb48c821cfbc079a" )
def test_apply_transaction_raises_if_delegate_already_exists_for_delegate_reg(redis): """ Should raise exception if delegate already exists """ manager = WalletManager() block = Block() transaction = TransferTransaction() transaction.type = TRANSACTION_TYPE_DELEGATE_REGISTRATION transaction.id = "hehe" transaction.asset["delegate"] = {"username": "******"} manager = WalletManager() redis.set("wallets:username:harambe", "") with pytest.raises(Exception) as excinfo: manager.apply_transaction(transaction, block) assert "Can't apply transaction hehe: delegate name harambe already taken" == str( excinfo.value )
def test_apply_block_correctly_handles_genesis_block(redis, mocker): manager = WalletManager() apply_transaction_mock = mocker.patch( "chain.plugins.database.wallet_manager.WalletManager.apply_transaction" ) transaction_1 = TransferTransaction() transaction_2 = TransferTransaction() block = Block() block.height = 1 block.reward = 2 block.transactions = [transaction_1, transaction_2] block.generator_public_key = ( "0316510c1409d3307d9f205cac58f1a871499c3ffea3878ddbbb48c821cfbc079a" ) manager.apply_block(block) assert apply_transaction_mock.call_count == 2 delegate = Wallet( json.loads(redis.get("wallets:address:ASt5oBHKDW8AeJe2Ybc1RucMLS7mRCiuRe")) ) assert delegate.balance == 2