|
12 | 12 | from eth.constants import (
|
13 | 13 | BLANK_ROOT_HASH,
|
14 | 14 | )
|
| 15 | +from eth.chains.base import ( |
| 16 | + MiningChain, |
| 17 | +) |
15 | 18 | from eth.db.chain import (
|
16 | 19 | ChainDB,
|
17 | 20 | )
|
18 | 21 | from eth.db.schema import SchemaV1
|
19 | 22 | from eth.exceptions import (
|
20 | 23 | HeaderNotFound,
|
21 | 24 | ParentNotFound,
|
| 25 | + ReceiptNotFound, |
22 | 26 | )
|
23 | 27 | from eth.rlp.headers import (
|
24 | 28 | BlockHeader,
|
25 | 29 | )
|
26 | 30 | from eth.tools.rlp import (
|
27 | 31 | assert_headers_eq,
|
28 | 32 | )
|
| 33 | +from eth._utils.address import ( |
| 34 | + force_bytes_to_address, |
| 35 | +) |
29 | 36 | from eth.vm.forks.frontier.blocks import (
|
30 | 37 | FrontierBlock,
|
31 | 38 | )
|
32 | 39 | from eth.vm.forks.homestead.blocks import (
|
33 | 40 | HomesteadBlock,
|
34 | 41 | )
|
35 | 42 |
|
| 43 | +from tests.core.helpers import ( |
| 44 | + new_transaction, |
| 45 | +) |
| 46 | + |
36 | 47 |
|
37 | 48 | A_ADDRESS = b"\xaa" * 20
|
38 | 49 | B_ADDRESS = b"\xbb" * 20
|
@@ -64,6 +75,14 @@ def block(request, header):
|
64 | 75 | return request.param(header)
|
65 | 76 |
|
66 | 77 |
|
| 78 | +@pytest.fixture |
| 79 | +def chain(chain_without_block_validation): |
| 80 | + if not isinstance(chain_without_block_validation, MiningChain): |
| 81 | + pytest.skip("these tests require a mining chain implementation") |
| 82 | + else: |
| 83 | + return chain_without_block_validation |
| 84 | + |
| 85 | + |
67 | 86 | def test_chaindb_add_block_number_to_hash_lookup(chaindb, block):
|
68 | 87 | block_number_to_hash_key = SchemaV1.make_block_number_to_hash_lookup_key(block.number)
|
69 | 88 | assert not chaindb.exists(block_number_to_hash_key)
|
@@ -129,3 +148,50 @@ def test_chaindb_get_canonical_block_hash(chaindb, block):
|
129 | 148 | chaindb.persist_block(block)
|
130 | 149 | block_hash = chaindb.get_canonical_block_hash(block.number)
|
131 | 150 | assert block_hash == block.hash
|
| 151 | + |
| 152 | + |
| 153 | +def test_chaindb_get_receipt_by_index( |
| 154 | + chain, |
| 155 | + funded_address, |
| 156 | + funded_address_private_key): |
| 157 | + NUMBER_BLOCKS_IN_CHAIN = 5 |
| 158 | + TRANSACTIONS_IN_BLOCK = 10 |
| 159 | + REQUIRED_BLOCK_NUMBER = 2 |
| 160 | + REQUIRED_RECEIPT_INDEX = 3 |
| 161 | + |
| 162 | + for block_number in range(NUMBER_BLOCKS_IN_CHAIN): |
| 163 | + for tx_index in range(TRANSACTIONS_IN_BLOCK): |
| 164 | + tx = new_transaction( |
| 165 | + chain.get_vm(), |
| 166 | + from_=funded_address, |
| 167 | + to=force_bytes_to_address(b'\x10\x10'), |
| 168 | + private_key=funded_address_private_key, |
| 169 | + ) |
| 170 | + new_block, tx_receipt, computation = chain.apply_transaction(tx) |
| 171 | + assert computation.is_success |
| 172 | + |
| 173 | + if (block_number + 1) == REQUIRED_BLOCK_NUMBER and tx_index == REQUIRED_RECEIPT_INDEX: |
| 174 | + actual_receipt = tx_receipt |
| 175 | + |
| 176 | + chain.mine_block() |
| 177 | + |
| 178 | + # Check that the receipt retrieved is indeed the actual one |
| 179 | + chaindb_retrieved_receipt = chain.chaindb.get_receipt_by_index( |
| 180 | + REQUIRED_BLOCK_NUMBER, |
| 181 | + REQUIRED_RECEIPT_INDEX, |
| 182 | + ) |
| 183 | + assert chaindb_retrieved_receipt == actual_receipt |
| 184 | + |
| 185 | + # Raise error if block number is not found |
| 186 | + with pytest.raises(ReceiptNotFound): |
| 187 | + chain.chaindb.get_receipt_by_index( |
| 188 | + NUMBER_BLOCKS_IN_CHAIN + 1, |
| 189 | + REQUIRED_RECEIPT_INDEX, |
| 190 | + ) |
| 191 | + |
| 192 | + # Raise error if receipt index is out of range |
| 193 | + with pytest.raises(ReceiptNotFound): |
| 194 | + chain.chaindb.get_receipt_by_index( |
| 195 | + NUMBER_BLOCKS_IN_CHAIN, |
| 196 | + TRANSACTIONS_IN_BLOCK + 1, |
| 197 | + ) |
0 commit comments