-
Notifications
You must be signed in to change notification settings - Fork 2.1k
optimize get_spends RPC #19958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arvidn
wants to merge
1
commit into
main
Choose a base branch
from
rpc-optimizations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
optimize get_spends RPC #19958
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
from chia.consensus.get_block_generator import get_block_generator | ||
from chia.consensus.pos_quality import UI_ACTUAL_SPACE_CONSTANT_FACTOR | ||
from chia.full_node.fee_estimator_interface import FeeEstimatorInterface | ||
from chia.full_node.full_block_utils import GeneratorBlockInfo, get_height_and_tx_status_from_block | ||
from chia.full_node.full_node import FullNode | ||
from chia.protocols.outbound_message import NodeType | ||
from chia.rpc.rpc_server import Endpoint, EndpointResult | ||
|
@@ -481,15 +482,15 @@ async def get_block_spends(self, request: dict[str, Any]) -> EndpointResult: | |
if "header_hash" not in request: | ||
raise ValueError("No header_hash in request") | ||
header_hash = bytes32.from_hexstr(request["header_hash"]) | ||
full_block: Optional[FullBlock] = await self.service.block_store.get_full_block(header_hash) | ||
if full_block is None: | ||
block_info: Optional[GeneratorBlockInfo] = await self.service.block_store.get_block_info(header_hash) | ||
if block_info is None: | ||
raise ValueError(f"Block {header_hash.hex()} not found") | ||
|
||
block_generator = await get_block_generator(self.service.blockchain.lookup_block_generators, full_block) | ||
block_generator = await get_block_generator(self.service.blockchain.lookup_block_generators, block_info) | ||
if block_generator is None: # if block is not a transaction block. | ||
return {"block_spends": []} | ||
|
||
flags = get_flags_for_height_and_constants(full_block.height, self.service.constants) | ||
flags = get_flags_for_height_and_constants(block_info.height, self.service.constants) | ||
spends = await asyncio.get_running_loop().run_in_executor( | ||
self.executor, | ||
get_spends_for_trusted_block, | ||
|
@@ -505,15 +506,15 @@ async def get_block_spends_with_conditions(self, request: dict[str, Any]) -> End | |
if "header_hash" not in request: | ||
raise ValueError("No header_hash in request") | ||
header_hash = bytes32.from_hexstr(request["header_hash"]) | ||
full_block: Optional[FullBlock] = await self.service.block_store.get_full_block(header_hash) | ||
if full_block is None: | ||
block_info: Optional[GeneratorBlockInfo] = await self.service.block_store.get_block_info(header_hash) | ||
if block_info is None: | ||
raise ValueError(f"Block {header_hash.hex()} not found") | ||
|
||
block_generator = await get_block_generator(self.service.blockchain.lookup_block_generators, full_block) | ||
block_generator = await get_block_generator(self.service.blockchain.lookup_block_generators, block_info) | ||
if block_generator is None: # if block is not a transaction block. | ||
return {"block_spends_with_conditions": []} | ||
|
||
flags = get_flags_for_height_and_constants(full_block.height, self.service.constants) | ||
flags = get_flags_for_height_and_constants(block_info.height, self.service.constants) | ||
spends_with_conditions = await asyncio.get_running_loop().run_in_executor( | ||
self.executor, | ||
get_spends_for_trusted_block_with_conditions, | ||
|
@@ -792,7 +793,9 @@ async def get_puzzle_and_solution(self, request: dict[str, Any]) -> EndpointResu | |
assert block_generator is not None | ||
|
||
try: | ||
puzzle, solution = get_puzzle_and_solution_for_coin( | ||
puzzle, solution = await asyncio.get_running_loop().run_in_executor( | ||
self.executor, | ||
get_puzzle_and_solution_for_coin, | ||
block_generator.program, | ||
block_generator.generator_refs, | ||
self.service.constants.MAX_BLOCK_COST_CLVM, | ||
|
@@ -807,16 +810,18 @@ async def get_additions_and_removals(self, request: dict[str, Any]) -> EndpointR | |
if "header_hash" not in request: | ||
raise ValueError("No header_hash in request") | ||
header_hash = bytes32.from_hexstr(request["header_hash"]) | ||
|
||
block: Optional[FullBlock] = await self.service.block_store.get_full_block(header_hash) | ||
if block is None: | ||
block_bytes: Optional[bytes] = await self.service.block_store.get_full_block_bytes(header_hash) | ||
if block_bytes is None: | ||
raise ValueError(f"Block {header_hash.hex()} not found") | ||
|
||
block_view = memoryview(block_bytes) | ||
height, _is_tx_block = get_height_and_tx_status_from_block(block_view) | ||
|
||
Comment on lines
-810
to
+819
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to avoid this extra complexity by simply augmenting |
||
async with self.service.blockchain.priority_mutex.acquire(priority=BlockchainMutexPriority.low): | ||
if self.service.blockchain.height_to_hash(block.height) != header_hash: | ||
if self.service.blockchain.height_to_hash(height) != header_hash: | ||
raise ValueError(f"Block at {header_hash.hex()} is no longer in the blockchain (it's in a fork)") | ||
additions: list[CoinRecord] = await self.service.coin_store.get_coins_added_at_height(block.height) | ||
removals: list[CoinRecord] = await self.service.coin_store.get_coins_removed_at_height(block.height) | ||
additions: list[CoinRecord] = await self.service.coin_store.get_coins_added_at_height(height) | ||
removals: list[CoinRecord] = await self.service.coin_store.get_coins_removed_at_height(height) | ||
|
||
return { | ||
"additions": [coin_record_dict_backwards_compat(cr.to_json_dict()) for cr in additions], | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid this added complexity by receiving
height
inblock_info_from_block
if we must (to keep returning this augmentedGeneratorBlockInfo
) instead of computing it. The caller has it already.