chia-blockchain/tests/full_node/test_mempool.py

675 lines
23 KiB
Python
Raw Normal View History

2020-01-30 04:28:53 +03:00
import asyncio
2020-02-01 03:28:16 +03:00
from time import time
2020-01-30 04:28:53 +03:00
import pytest
2020-01-31 23:44:04 +03:00
2020-01-30 04:28:53 +03:00
from src.server.outbound_message import OutboundMessage
from src.protocols import full_node_protocol
from src.types.condition_var_pair import ConditionVarPair
2020-02-03 08:59:00 +03:00
from src.types.condition_opcodes import ConditionOpcode
from src.types.spend_bundle import SpendBundle
2020-01-31 23:44:04 +03:00
from src.util.ints import uint64
2020-01-30 04:28:53 +03:00
from tests.setup_nodes import setup_two_nodes, test_constants, bt
2020-01-31 03:56:24 +03:00
from tests.wallet_tools import WalletTool
2020-01-30 04:28:53 +03:00
@pytest.fixture(scope="module")
def event_loop():
loop = asyncio.get_event_loop()
yield loop
class TestMempool:
@pytest.fixture(scope="function")
async def two_nodes(self):
2020-02-02 10:52:33 +03:00
async for _ in setup_two_nodes({"COINBASE_FREEZE_PERIOD": 0}):
yield _
@pytest.fixture(scope="function")
async def two_nodes_standard_freeze(self):
async for _ in setup_two_nodes({"COINBASE_FREEZE_PERIOD": 200}):
2020-01-30 04:28:53 +03:00
yield _
@pytest.mark.asyncio
async def test_basic_mempool(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-01-31 03:56:24 +03:00
wallet_a = WalletTool()
2020-01-30 04:28:53 +03:00
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
2020-02-01 03:28:16 +03:00
wallet_receiver = WalletTool()
2020-01-30 04:28:53 +03:00
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-01-30 04:28:53 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
2020-02-02 10:52:33 +03:00
block = blocks[1]
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(block)
):
2020-02-02 10:52:33 +03:00
pass
spend_bundle = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase
)
2020-03-30 12:03:03 +03:00
assert spend_bundle is not None
2020-02-14 21:03:56 +03:00
tx: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx):
2020-02-02 10:52:33 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function == "new_transaction"
2020-02-02 10:52:33 +03:00
2020-02-14 23:48:41 +03:00
sb = full_node_1.mempool_manager.get_spendbundle(spend_bundle.name())
2020-02-02 10:52:33 +03:00
assert sb is spend_bundle
@pytest.mark.asyncio
async def test_coinbase_freeze(self, two_nodes_standard_freeze):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-02 10:52:33 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-02-02 10:52:33 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes_standard_freeze
2020-01-30 04:28:53 +03:00
block = blocks[1]
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(block)
):
2020-02-02 10:52:33 +03:00
pass
spend_bundle = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase
)
assert spend_bundle is not None
2020-02-14 21:03:56 +03:00
tx: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle
)
2020-02-02 10:52:33 +03:00
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx):
2020-02-02 10:52:33 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function != "new_transaction"
2020-02-02 10:52:33 +03:00
2020-02-14 23:48:41 +03:00
sb = full_node_1.mempool_manager.get_spendbundle(spend_bundle.name())
2020-02-02 10:52:33 +03:00
assert sb is None
blocks = bt.get_consecutive_blocks(
test_constants, 200, [], 10, b"", coinbase_puzzlehash
)
2020-02-02 10:52:33 +03:00
for i in range(1, 201):
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(blocks[i])
):
2020-02-02 10:52:33 +03:00
pass
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx):
outbound_2: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound_2.message.function == "new_transaction"
2020-02-14 23:48:41 +03:00
sb = full_node_1.mempool_manager.get_spendbundle(spend_bundle.name())
2020-02-02 10:52:33 +03:00
assert sb is spend_bundle
2020-01-30 04:28:53 +03:00
@pytest.mark.asyncio
async def test_double_spend(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-01-31 03:56:24 +03:00
wallet_a = WalletTool()
2020-01-30 04:28:53 +03:00
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
2020-01-31 03:56:24 +03:00
wallet_receiver = WalletTool()
2020-01-30 04:28:53 +03:00
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-01-30 04:28:53 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(block)
):
2020-01-30 04:28:53 +03:00
pass
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase
)
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-01-30 04:28:53 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function == "new_transaction"
2020-01-30 04:28:53 +03:00
2020-01-31 03:56:24 +03:00
other_receiver = WalletTool()
spend_bundle2 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, other_receiver.get_new_puzzlehash(), block.header.data.coinbase
)
assert spend_bundle2 is not None
2020-02-14 21:03:56 +03:00
tx2: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle2
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx2):
2020-01-30 04:28:53 +03:00
pass
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
sb2 = full_node_1.mempool_manager.get_spendbundle(spend_bundle2.name())
2020-01-30 04:28:53 +03:00
assert sb1 == spend_bundle1
assert sb2 is None
2020-01-31 03:56:24 +03:00
@pytest.mark.asyncio
async def test_double_spend_with_higher_fee(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-01-31 03:56:24 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-01-31 03:56:24 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(block)
):
2020-01-31 03:56:24 +03:00
pass
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase
)
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-01-31 03:56:24 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function == "new_transaction"
2020-01-31 03:56:24 +03:00
spend_bundle2 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, fee=1
)
2020-01-31 03:56:24 +03:00
assert spend_bundle2 is not None
2020-02-14 21:03:56 +03:00
tx2: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle2
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx2):
2020-01-31 03:56:24 +03:00
pass
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
sb2 = full_node_1.mempool_manager.get_spendbundle(spend_bundle2.name())
2020-01-31 03:56:24 +03:00
assert sb1 is None
assert sb2 == spend_bundle2
2020-01-31 23:44:04 +03:00
@pytest.mark.asyncio
async def test_invalid_block_index(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-01-31 23:44:04 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-01-31 23:44:04 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(block)
):
2020-01-31 23:44:04 +03:00
pass
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_BLOCK_INDEX_EXCEEDS,
uint64(2).to_bytes(4, "big"),
None,
)
2020-01-31 23:44:04 +03:00
dic = {ConditionOpcode.ASSERT_BLOCK_INDEX_EXCEEDS: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-01-31 23:44:04 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-01-31 23:44:04 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function != "new_transaction"
2020-01-31 23:44:04 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-01-31 23:44:04 +03:00
assert sb1 is None
@pytest.mark.asyncio
async def test_correct_block_index(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-01-31 23:44:04 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-01-31 23:44:04 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(block)
):
2020-01-31 23:44:04 +03:00
pass
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_BLOCK_INDEX_EXCEEDS,
uint64(1).to_bytes(4, "big"),
None,
)
2020-01-31 23:44:04 +03:00
dic = {ConditionOpcode.ASSERT_BLOCK_INDEX_EXCEEDS: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-01-31 23:44:04 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-01-31 23:44:04 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function == "new_transaction"
2020-01-31 23:44:04 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-01-31 23:44:04 +03:00
assert sb1 is spend_bundle1
2020-02-01 01:19:35 +03:00
@pytest.mark.asyncio
async def test_invalid_block_age(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-01 01:19:35 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-02-01 01:19:35 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(block)
):
2020-02-01 01:19:35 +03:00
pass
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_BLOCK_AGE_EXCEEDS, uint64(5).to_bytes(4, "big"), None
)
2020-02-01 01:19:35 +03:00
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-02-01 01:19:35 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-02-01 01:19:35 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function != "new_transaction"
2020-02-01 01:19:35 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-02-01 01:19:35 +03:00
assert sb1 is None
@pytest.mark.asyncio
async def test_correct_block_age(self, two_nodes):
num_blocks = 4
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-02-01 01:19:35 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
for b in blocks:
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(b)
):
2020-02-01 01:19:35 +03:00
pass
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_BLOCK_AGE_EXCEEDS, uint64(3).to_bytes(4, "big"), None
)
2020-02-01 01:19:35 +03:00
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-02-01 01:19:35 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-02-01 01:19:35 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function == "new_transaction"
2020-02-01 01:19:35 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-02-01 01:19:35 +03:00
assert sb1 is spend_bundle1
2020-02-01 01:58:07 +03:00
@pytest.mark.asyncio
async def test_correct_my_id(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-01 01:58:07 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-02-01 01:58:07 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
for b in blocks:
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(b)
):
2020-02-01 01:58:07 +03:00
pass
cvp = ConditionVarPair(
2020-03-02 09:06:03 +03:00
ConditionOpcode.ASSERT_MY_COIN_ID, block.header.data.coinbase.name(), None
)
2020-02-01 01:58:07 +03:00
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-02-01 01:58:07 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-02-01 01:58:07 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function == "new_transaction"
2020-02-01 01:58:07 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-02-01 01:58:07 +03:00
assert sb1 is spend_bundle1
@pytest.mark.asyncio
async def test_invalid_my_id(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-01 01:58:07 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-02-01 01:58:07 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
for b in blocks:
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(b)
):
2020-02-01 01:58:07 +03:00
pass
cvp = ConditionVarPair(
2020-03-02 09:06:03 +03:00
ConditionOpcode.ASSERT_MY_COIN_ID,
blocks[2].header.data.coinbase.name(),
None,
)
2020-02-01 01:58:07 +03:00
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-02-01 01:58:07 +03:00
2020-03-30 12:03:03 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-02-01 01:58:07 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function != "new_transaction"
2020-02-01 01:58:07 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-02-01 01:58:07 +03:00
assert sb1 is None
2020-02-01 03:28:16 +03:00
@pytest.mark.asyncio
async def test_assert_time_exceeds(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-01 03:28:16 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-02-01 03:28:16 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
for b in blocks:
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(b)
):
2020-02-01 03:28:16 +03:00
pass
time_now = uint64(int(time() * 1000))
2020-02-01 03:28:16 +03:00
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_TIME_EXCEEDS, time_now.to_bytes(8, "big"), None
)
2020-02-01 03:28:16 +03:00
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-02-01 03:28:16 +03:00
2020-03-30 12:03:03 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-02-01 03:28:16 +03:00
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound.message.function == "new_transaction"
2020-02-01 03:28:16 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-02-01 03:28:16 +03:00
assert sb1 is spend_bundle1
@pytest.mark.asyncio
async def test_assert_time_exceeds_both_cases(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-01 03:28:16 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
2020-02-01 03:28:16 +03:00
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
for b in blocks:
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(b)
):
2020-02-01 03:28:16 +03:00
pass
time_now = uint64(int(time() * 1000))
2020-02-01 03:29:15 +03:00
time_now_plus_3 = time_now + 3000
2020-02-01 03:28:16 +03:00
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_TIME_EXCEEDS,
time_now_plus_3.to_bytes(8, "big"),
None,
)
2020-02-01 03:28:16 +03:00
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
2020-03-02 09:06:03 +03:00
1000, receiver_puzzlehash, block.header.data.coinbase, dic
)
2020-02-01 03:28:16 +03:00
assert spend_bundle1 is not None
2020-02-14 21:03:56 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx1):
2020-02-01 03:28:16 +03:00
outbound: OutboundMessage = _
2020-02-14 21:03:56 +03:00
assert outbound.message.function != "new_transaction"
2020-02-01 03:28:16 +03:00
# Sleep so that 3 sec passes
await asyncio.sleep(3)
2020-02-14 21:03:56 +03:00
tx2: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
2020-02-10 20:08:58 +03:00
spend_bundle1
)
2020-02-14 21:03:56 +03:00
async for _ in full_node_1.respond_transaction(tx2):
outbound_2: OutboundMessage = _
2020-02-01 03:28:16 +03:00
# Maybe transaction means that it's accepted in mempool
2020-02-14 21:03:56 +03:00
assert outbound_2.message.function == "new_transaction"
2020-02-01 03:28:16 +03:00
2020-02-14 23:48:41 +03:00
sb1 = full_node_1.mempool_manager.get_spendbundle(spend_bundle1.name())
2020-02-01 03:28:16 +03:00
assert sb1 is spend_bundle1
2020-02-28 00:44:24 +03:00
@pytest.mark.asyncio
async def test_correct_coin_consumed(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-28 00:44:24 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
block2 = blocks[2]
for b in blocks:
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(b)
2020-02-28 00:44:24 +03:00
):
pass
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_COIN_CONSUMED,
block2.header.data.coinbase.name(),
None,
2020-02-28 00:44:24 +03:00
)
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
1000, receiver_puzzlehash, block.header.data.coinbase, dic
2020-02-28 00:44:24 +03:00
)
spend_bundle2 = wallet_a.generate_signed_transaction(
1000, receiver_puzzlehash, block2.header.data.coinbase
2020-02-28 00:44:24 +03:00
)
bundle = SpendBundle.aggregate([spend_bundle1, spend_bundle2])
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
bundle
)
async for _ in full_node_1.respond_transaction(tx1):
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
assert outbound.message.function == "new_transaction"
mempool_bundle = full_node_1.mempool_manager.get_spendbundle(bundle.name())
assert mempool_bundle is bundle
@pytest.mark.asyncio
async def test_invalid_coin_consumed(self, two_nodes):
2020-02-28 01:29:01 +03:00
num_blocks = 2
2020-02-28 00:44:24 +03:00
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
test_constants, num_blocks, [], 10, b"", coinbase_puzzlehash
)
full_node_1, full_node_2, server_1, server_2 = two_nodes
block = blocks[1]
block2 = blocks[2]
for b in blocks:
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(b)
2020-02-28 00:44:24 +03:00
):
pass
cvp = ConditionVarPair(
ConditionOpcode.ASSERT_COIN_CONSUMED,
block2.header.data.coinbase.name(),
None,
2020-02-28 00:44:24 +03:00
)
dic = {cvp.opcode: [cvp]}
spend_bundle1 = wallet_a.generate_signed_transaction(
1000, receiver_puzzlehash, block.header.data.coinbase, dic
2020-02-28 00:44:24 +03:00
)
2020-03-30 12:03:03 +03:00
assert spend_bundle1 is not None
2020-02-28 00:44:24 +03:00
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(
spend_bundle1
)
async for _ in full_node_1.respond_transaction(tx1):
outbound: OutboundMessage = _
# Maybe transaction means that it's accepted in mempool
assert outbound.message.function == "new_transaction"
mempool_bundle = full_node_1.mempool_manager.get_spendbundle(
spend_bundle1.name()
)
2020-02-28 00:44:24 +03:00
assert mempool_bundle is None