@@ -240,7 +240,7 @@ async def get_confirmed_balance_for_wallet(self, wallet_id: int) -> uint64:
240240
241241 for record in record_list :
242242 amount = uint64 (amount + record .coin .amount )
243- self .log .info (f"amount is { amount } " )
243+ self .log .info (f"Confirmed balance amount is { amount } " )
244244 return uint64 (amount )
245245
246246 async def get_unconfirmed_balance (self , wallet_id ) -> uint64 :
@@ -487,6 +487,30 @@ async def receive_block(
487487 fast sync). If validation succeeds, block is adedd to DB. If it's a new TIP, transactions are
488488 reorged accordingly.
489489 """
490+ cb_and_fees_additions = []
491+ if header_block is not None :
492+ coinbase = header_block .header .data .coinbase
493+ fees_coin = header_block .header .data .fees_coin
494+ if await self .is_addition_relevant (coinbase ):
495+ cb_and_fees_additions .append (coinbase )
496+ if await self .is_addition_relevant (fees_coin ):
497+ cb_and_fees_additions .append (fees_coin )
498+ assert block .additions is not None
499+ if len (cb_and_fees_additions ) > 0 :
500+ block = BlockRecord (
501+ block .header_hash ,
502+ block .prev_header_hash ,
503+ block .height ,
504+ block .weight ,
505+ block .additions + cb_and_fees_additions ,
506+ block .removals ,
507+ block .total_iters ,
508+ block .new_challenge_hash ,
509+ )
510+
511+ assert block .additions is not None
512+ assert block .removals is not None
513+
490514 async with self .lock :
491515 if block .header_hash in self .block_records :
492516 return ReceiveBlockResult .ALREADY_HAVE_BLOCK
@@ -547,15 +571,18 @@ async def receive_block(
547571 for path_block in blocks_to_add :
548572 self .height_to_hash [path_block .height ] = path_block .header_hash
549573 await self .wallet_store .add_block_to_path (path_block .header_hash )
550- if header_block is not None :
551- coinbase = header_block .header .data .coinbase
552- fees_coin = header_block .header .data .fees_coin
553- if await self .is_addition_relevant (coinbase ):
554- await self .coin_added (coinbase , path_block .height , True )
555- if await self .is_addition_relevant (fees_coin ):
556- await self .coin_added (fees_coin , path_block .height , True )
574+ assert (
575+ path_block .additions is not None
576+ and path_block .removals is not None
577+ )
557578 for coin in path_block .additions :
558- await self .coin_added (coin , path_block .height , False )
579+ is_coinbase = (
580+ True
581+ if bytes32 ((path_block .height ).to_bytes (32 , "big" ))
582+ == coin .parent_coin_info
583+ else False
584+ )
585+ await self .coin_added (coin , path_block .height , is_coinbase )
559586 for coin_name in path_block .removals :
560587 await self .coin_removed (coin_name , path_block .height )
561588 self .lca = block .header_hash
@@ -810,7 +837,7 @@ def validate_select_proofs(
810837 self ,
811838 all_proof_hashes : List [Tuple [bytes32 , Optional [Tuple [uint64 , uint64 ]]]],
812839 heights : List [uint32 ],
813- cached_blocks : Dict [bytes32 , Tuple [BlockRecord , HeaderBlock ]],
840+ cached_blocks : Dict [bytes32 , Tuple [BlockRecord , HeaderBlock , Optional [ bytes ] ]],
814841 potential_header_hashes : Dict [uint32 , bytes32 ],
815842 ) -> bool :
816843 """
@@ -825,7 +852,7 @@ def validate_select_proofs(
825852 prev_height = uint32 (height - 1 )
826853 # Get previous header block
827854 prev_hh = potential_header_hashes [prev_height ]
828- _ , prev_header_block = cached_blocks [prev_hh ]
855+ _ , prev_header_block , _ = cached_blocks [prev_hh ]
829856
830857 # Validate proof hash of previous header block
831858 if (
@@ -868,7 +895,7 @@ def validate_select_proofs(
868895
869896 # Get header block
870897 hh = potential_header_hashes [height ]
871- _ , header_block = cached_blocks [hh ]
898+ _ , header_block , _ = cached_blocks [hh ]
872899
873900 # Validate challenge hash is == pospace challenge hash
874901 if challenge_hash != header_block .proof_of_space .challenge_hash :
@@ -966,21 +993,59 @@ def validate_select_proofs(
966993 return True
967994
968995 async def get_filter_additions_removals (
969- self , transactions_fitler : bytes
996+ self , new_block : BlockRecord , transactions_filter : bytes
970997 ) -> Tuple [List [bytes32 ], List [bytes32 ]]:
971998 """ Returns a list of our coin ids, and a list of puzzle_hashes that positively match with provided filter. """
972- tx_filter = PyBIP158 ([b for b in transactions_fitler ])
973- my_coin_records : Set [
999+ assert new_block .prev_header_hash in self .block_records
1000+
1001+ tx_filter = PyBIP158 ([b for b in transactions_filter ])
1002+
1003+ # Find fork point
1004+ fork_h : uint32 = self ._find_fork_point_in_chain (
1005+ self .block_records [self .lca ], new_block
1006+ )
1007+
1008+ # Get all unspent coins
1009+ my_coin_records_lca : Set [
9741010 WalletCoinRecord
975- ] = await self .wallet_store .get_coin_records_by_spent (False )
1011+ ] = await self .wallet_store .get_coin_records_by_spent (False , uint32 (fork_h + 1 ))
1012+
1013+ # Filter coins up to and including fork point
1014+ unspent_coin_names : Set [bytes32 ] = set ()
1015+ for coin in my_coin_records_lca :
1016+ if coin .confirmed_block_index <= fork_h :
1017+ unspent_coin_names .add (coin .name ())
1018+
1019+ # Get all blocks after fork point up to but not including this block
1020+ curr : BlockRecord = self .block_records [new_block .prev_header_hash ]
1021+ reorg_blocks : List [BlockRecord ] = []
1022+ while curr .height > fork_h :
1023+ reorg_blocks .append (curr )
1024+ curr = self .block_records [curr .prev_header_hash ]
1025+ reorg_blocks .reverse ()
1026+
1027+ # For each block, process additions to get all Coins, then process removals to get unspent coins
1028+ for reorg_block in reorg_blocks :
1029+ assert (
1030+ reorg_block .additions is not None and reorg_block .removals is not None
1031+ )
1032+ for addition in reorg_block .additions :
1033+ unspent_coin_names .add (addition .name ())
1034+ for removal in reorg_block .removals :
1035+ unspent_coin_names .remove (removal )
1036+
1037+ if new_block .additions is not None :
1038+ for addition in new_block .additions :
1039+ unspent_coin_names .add (addition .name ())
1040+
9761041 my_puzzle_hashes = await self .puzzle_store .get_all_puzzle_hashes ()
9771042
9781043 removals_of_interest : bytes32 = []
9791044 additions_of_interest : bytes32 = []
9801045
981- for record in my_coin_records :
982- if tx_filter .Match (bytearray (record . name () )):
983- removals_of_interest .append (record . name () )
1046+ for coin_name in unspent_coin_names :
1047+ if tx_filter .Match (bytearray (coin_name )):
1048+ removals_of_interest .append (coin_name )
9841049
9851050 for puzzle_hash in my_puzzle_hashes :
9861051 if tx_filter .Match (bytearray (puzzle_hash )):
0 commit comments