From 6f183f7eee895e0635baf029b0d268223db79c8b Mon Sep 17 00:00:00 2001 From: Mariano Sorgente <3069354+mariano54@users.noreply.github.com> Date: Tue, 15 Sep 2020 20:06:43 +0900 Subject: [PATCH] Improve wallet sync performance --- electron-react/package-lock.json | 2 +- electron-react/src/pages/Farmer.js | 2 ++ src/full_node/full_node.py | 8 ++------ src/wallet/wallet_puzzle_store.py | 18 ++++++++++++++++++ src/wallet/wallet_state_manager.py | 27 ++++++++++++++++----------- tests/wallet/test_puzzle_store.py | 12 ++++++++++++ 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/electron-react/package-lock.json b/electron-react/package-lock.json index 7090c6c0..bd16b1ef 100644 --- a/electron-react/package-lock.json +++ b/electron-react/package-lock.json @@ -5262,7 +5262,7 @@ } }, "electron-osx-sign": { - "version": "github:electron/electron-osx-sign#877226b5198f2ed9661129ea56a1ea048c4c55ae", + "version": "github:electron/electron-osx-sign#6ba45b2deec3f4f3629010645f92e6506df133ee", "from": "github:electron/electron-osx-sign#master", "dev": true, "requires": { diff --git a/electron-react/src/pages/Farmer.js b/electron-react/src/pages/Farmer.js index 438adc76..92f3f880 100644 --- a/electron-react/src/pages/Farmer.js +++ b/electron-react/src/pages/Farmer.js @@ -613,6 +613,7 @@ const Farmer = props => { const classes = props.classes; const checkRewards = useCallback(async () => { + console.log("Checking rewards"); let totalChia = BigInt(0); let biggestHeight = 0; for (let wallet of wallets) { @@ -624,6 +625,7 @@ const Farmer = props => { if (tx.additions.length < 1) { continue; } + console.log("Checking tx", tx); // Height here is filled into the whole 256 bits (32 bytes) of the parent let hexHeight = arr_to_hex( big_int_to_array(BigInt(tx.confirmed_at_index), 32) diff --git a/src/full_node/full_node.py b/src/full_node/full_node.py index 69a3f62a..3ebca652 100644 --- a/src/full_node/full_node.py +++ b/src/full_node/full_node.py @@ -55,7 +55,7 @@ from src.util.hash import std_hash from src.util.ints import uint32, uint64, uint128 from src.util.merkle_set import MerkleSet from src.util.path import mkdir, path_from_root -from src.types.peer_info import PeerInfo, TimestampedPeerInfo +from src.types.peer_info import PeerInfo OutboundMessageGenerator = AsyncGenerator[OutboundMessage, None] @@ -1725,11 +1725,7 @@ class FullNode: ) -> OutboundMessageGenerator: if self.global_connections is None: return - tmp_connected_peers = self.global_connections.get_full_node_peerinfos() - connected_peers = [ - TimestampedPeerInfo(peer.host, peer.port, uint64(0)) - for peer in tmp_connected_peers - ] + connected_peers = self.global_connections.get_full_node_peerinfos() unconnected_peers = self.global_connections.peers.get_peers( recent_threshold=24 * 60 * 60 ) diff --git a/src/wallet/wallet_puzzle_store.py b/src/wallet/wallet_puzzle_store.py index 029574cc..09f5487a 100644 --- a/src/wallet/wallet_puzzle_store.py +++ b/src/wallet/wallet_puzzle_store.py @@ -177,6 +177,24 @@ class WalletPuzzleStore: return row is not None + async def one_of_puzzle_hashes_exists(self, puzzle_hashes: List[bytes32]) -> bool: + """ + Checks if one of the passed puzzle_hashes is present in the db. + """ + if len(puzzle_hashes) < 1: + return False + puzzle_hashes_db = tuple([ph.hex() for ph in puzzle_hashes]) + formatted_str = ( + f"SELECT * from derivation_paths WHERE puzzle_hash in " + f'({"?," * (len(puzzle_hashes_db) - 1)}?) LIMIT 1' + ) + cursor = await self.db_connection.execute(formatted_str, puzzle_hashes_db) + + row = await cursor.fetchone() + await cursor.close() + + return row is not None + async def index_for_pubkey(self, pubkey: G1Element) -> Optional[uint32]: """ Returns derivation paths for the given pubkey. diff --git a/src/wallet/wallet_state_manager.py b/src/wallet/wallet_state_manager.py index 7ad3ff94..2dd1e3a3 100644 --- a/src/wallet/wallet_state_manager.py +++ b/src/wallet/wallet_state_manager.py @@ -790,17 +790,22 @@ class WalletStateManager: self.block_records[block.header_hash] = block await self.wallet_store.add_block_record(block, False) - async with self.puzzle_store.lock: - for addition in block.additions: - record = await self.puzzle_store.get_derivation_record_for_puzzle_hash( - addition.puzzle_hash.hex() - ) - if record is None: - continue - index = record.index - await self.puzzle_store.set_used_up_to(index) - - await self.create_more_puzzle_hashes() + # If one of these new additions is ours, generate more puzzle hashes + phs: List[bytes32] = [addition.puzzle_hash for addition in block.additions] + block_includes_our_tx: bool = await self.puzzle_store.one_of_puzzle_hashes_exists( + phs + ) + if block_includes_our_tx: + async with self.puzzle_store.lock: + for addition in block.additions: + record = await self.puzzle_store.get_derivation_record_for_puzzle_hash( + addition.puzzle_hash.hex() + ) + if record is None: + continue + index = record.index + await self.puzzle_store.set_used_up_to(index) + await self.create_more_puzzle_hashes() # Genesis case if self.lca is None: diff --git a/tests/wallet/test_puzzle_store.py b/tests/wallet/test_puzzle_store.py index 6a3c8c68..ee6eb4dd 100644 --- a/tests/wallet/test_puzzle_store.py +++ b/tests/wallet/test_puzzle_store.py @@ -67,6 +67,18 @@ class TestPuzzleStore: await db.add_derivation_paths(derivation_recs) assert await db.puzzle_hash_exists(derivation_recs[0].puzzle_hash) is True + + phs_1 = [derivation_recs[0].puzzle_hash] + phs_2 = [32 * bytes([1]), derivation_recs[0].puzzle_hash] + phs_3 = [derivation_recs[0].puzzle_hash, 32 * bytes([1])] + phs_4 = [32 * bytes([1]), 32 * bytes([2])] + phs_5 = [] + assert await db.one_of_puzzle_hashes_exists(phs_1) is True + assert await db.one_of_puzzle_hashes_exists(phs_2) is True + assert await db.one_of_puzzle_hashes_exists(phs_3) is True + assert await db.one_of_puzzle_hashes_exists(phs_4) is False + assert await db.one_of_puzzle_hashes_exists(phs_5) is False + assert await db.index_for_pubkey(derivation_recs[4].pubkey) == 2 assert await db.index_for_puzzle_hash(derivation_recs[2].puzzle_hash) == 1 assert await db.wallet_info_for_puzzle_hash(