@@ -14,8 +14,9 @@ use crate::{
1414pub struct AccountSelector {
1515 owners : HashSet < [ u8 ; 32 ] > ,
1616 pubkeys : HashSet < [ u8 ; 32 ] > ,
17+ mints : HashSet < Pubkey > ,
1718 startup : Option < bool > ,
18- token_addresses : Option < HashSet < Pubkey > > ,
19+ token_reg : Option < HashSet < Pubkey > > ,
1920}
2021
2122impl AccountSelector {
@@ -24,6 +25,7 @@ impl AccountSelector {
2425 owners,
2526 all_tokens,
2627 pubkeys,
28+ mints,
2729 startup,
2830 } = config;
2931
@@ -39,23 +41,37 @@ impl AccountSelector {
3941 . collect :: < Result < _ , _ > > ( )
4042 . context ( "Failed to parse account pubkeys" ) ?;
4143
42- Ok ( Self {
44+ let mints = mints
45+ . into_iter ( )
46+ . map ( |s| s. parse ( ) )
47+ . collect :: < Result < _ , _ > > ( )
48+ . context ( "Failed to parse token account mint addresses" ) ?;
49+
50+ let mut ret = Self {
4351 owners,
4452 pubkeys,
53+ mints,
4554 startup,
46- token_addresses : if all_tokens {
55+ token_reg : if all_tokens {
4756 None
4857 } else {
4958 Some ( HashSet :: new ( ) )
5059 } ,
51- } )
60+ } ;
61+
62+ // Don't screen tokens if we're never going to return them
63+ if !ret. owners . contains ( TOKEN_KEY . as_ref ( ) ) {
64+ ret. token_reg = None ;
65+ }
66+
67+ Ok ( ret)
5268 }
5369
5470 /// Lazy-load the token addresses. Fails if token addresses are not wanted
5571 /// or if they have already been loaded.
56- pub fn init_tokens ( & mut self , addrs : HashSet < Pubkey > ) {
57- assert ! ( self . token_addresses . as_ref( ) . unwrap( ) . is_empty( ) ) ;
58- self . token_addresses = Some ( addrs) ;
72+ pub fn init_token_registry ( & mut self , addrs : HashSet < Pubkey > ) {
73+ assert ! ( self . token_reg . as_ref( ) . unwrap( ) . is_empty( ) ) ;
74+ self . token_reg = Some ( addrs) ;
5975 }
6076
6177 #[ inline]
@@ -64,30 +80,44 @@ impl AccountSelector {
6480 }
6581
6682 #[ inline]
67- pub fn screen_tokens ( & self ) -> bool {
68- self . token_addresses . is_some ( )
83+ pub fn screen_token_registry ( & self ) -> bool {
84+ self . token_reg . is_some ( )
6985 }
7086
7187 #[ inline]
7288 pub fn is_selected ( & self , acct : & ReplicaAccountInfo , is_startup : bool ) -> bool {
7389 let ReplicaAccountInfo { owner, data, .. } = * acct;
7490
75- if self . startup . map_or ( false , |s| is_startup != s)
76- || !( self . owners . contains ( owner) || self . pubkeys . contains ( acct. pubkey ) )
77- {
91+ if self . startup . map_or ( false , |s| is_startup != s) {
7892 return false ;
7993 }
8094
81- if owner == TOKEN_KEY . as_ref ( ) && data . len ( ) == TokenAccount :: get_packed_len ( ) {
82- if let Some ( ref addrs ) = self . token_addresses {
83- let token_account = TokenAccount :: unpack_from_slice ( data ) ;
95+ if self . pubkeys . contains ( acct . pubkey ) {
96+ return true ;
97+ }
8498
85- if let Ok ( token_account) = token_account {
86- if token_account. amount > 1 || addrs. contains ( & token_account. mint ) {
87- return false ;
88- }
89- }
90- }
99+ let token = if ( self . token_reg . is_some ( ) || !self . mints . is_empty ( ) )
100+ && owner == TOKEN_KEY . as_ref ( )
101+ && data. len ( ) == TokenAccount :: get_packed_len ( )
102+ {
103+ TokenAccount :: unpack_from_slice ( data) . ok ( )
104+ } else {
105+ None
106+ } ;
107+
108+ if token. map_or ( false , |t| self . mints . contains ( & t. mint ) ) {
109+ return true ;
110+ }
111+
112+ if !self . owners . contains ( owner) {
113+ return false ;
114+ }
115+
116+ if token
117+ . zip ( self . token_reg . as_ref ( ) )
118+ . map_or ( false , |( t, r) | t. amount > 1 || r. contains ( & t. mint ) )
119+ {
120+ return false ;
91121 }
92122
93123 true
@@ -113,10 +143,17 @@ impl InstructionSelector {
113143 . collect :: < Result < _ , _ > > ( )
114144 . context ( "Failed to parse instruction program keys" ) ?;
115145
116- Ok ( Self {
146+ let mut ret = Self {
117147 programs,
118148 screen_tokens : !all_token_calls,
119- } )
149+ } ;
150+
151+ // Don't screen token calls if we're never going to return them
152+ if !ret. programs . contains ( & TOKEN_KEY ) {
153+ ret. screen_tokens = false ;
154+ }
155+
156+ Ok ( ret)
120157 }
121158
122159 #[ inline]
0 commit comments