@@ -21,7 +21,7 @@ use stratum_common::roles_logic_sv2::{
21
21
utils:: Mutex ,
22
22
} ;
23
23
24
- use tracing:: { error, info, warn} ;
24
+ use tracing:: { debug , error, info, warn} ;
25
25
26
26
impl HandleMiningMessagesFromServerAsync for ChannelManager {
27
27
type Error = TproxyError ;
@@ -90,7 +90,6 @@ impl HandleMiningMessagesFromServerAsync for ChannelManager {
90
90
Some ( Arc :: new ( RwLock :: new ( extended_channel. clone ( ) ) ) ) ;
91
91
92
92
let upstream_extranonce_prefix: Extranonce = m. extranonce_prefix . clone ( ) . into ( ) ;
93
- info ! ( downstream_extranonce_len, "downstream_extranonce_len" ) ;
94
93
let translator_proxy_extranonce_prefix_len = proxy_extranonce_prefix_len (
95
94
m. extranonce_size . into ( ) ,
96
95
downstream_extranonce_len,
@@ -102,7 +101,8 @@ impl HandleMiningMessagesFromServerAsync for ChannelManager {
102
101
// that is used for rolling)
103
102
let range_0 = 0 ..extranonce_prefix. len ( ) ;
104
103
let range1 = range_0. end ..range_0. end + translator_proxy_extranonce_prefix_len;
105
- let range2 = range1. end ..m. extranonce_size as usize + extranonce_prefix. len ( ) ;
104
+ let range2 = range1. end ..range1. end + downstream_extranonce_len;
105
+ debug ! ( "\n \n range_0: {:?}, range1: {:?}, range2: {:?}\n \n " , range_0, range1, range2) ;
106
106
let extended_extranonce_factory = ExtendedExtranonce :: from_upstream_extranonce (
107
107
upstream_extranonce_prefix,
108
108
range_0,
@@ -121,44 +121,102 @@ impl HandleMiningMessagesFromServerAsync for ChannelManager {
121
121
. safe_lock ( |f| f. get_range2_len ( ) )
122
122
. expect ( "extranonce_prefix_factory mutex should not be poisoned" )
123
123
as u16 ;
124
- if downstream_extranonce_len <= new_extranonce_size as usize {
124
+ let new_extranonce_prefix = factory
125
+ . safe_lock ( |f| f. next_prefix_extended ( new_extranonce_size as usize ) )
126
+ . expect ( "extranonce_prefix_factory mutex should not be poisoned" )
127
+ . expect ( "next_prefix_extended should return a value for valid input" )
128
+ . into_b032 ( ) ;
129
+ let new_downstream_extended_channel = ExtendedChannel :: new (
130
+ m. channel_id ,
131
+ user_identity. clone ( ) ,
132
+ new_extranonce_prefix. clone ( ) . into_static ( ) . to_vec ( ) ,
133
+ target. clone ( ) . into ( ) ,
134
+ nominal_hashrate,
135
+ true ,
136
+ new_extranonce_size,
137
+ ) ;
138
+ channel_manager_data. extended_channels . insert (
139
+ m. channel_id ,
140
+ Arc :: new ( RwLock :: new ( new_downstream_extended_channel) ) ,
141
+ ) ;
142
+ let new_open_extended_mining_channel_success =
143
+ OpenExtendedMiningChannelSuccess {
144
+ request_id : m. request_id ,
145
+ channel_id : m. channel_id ,
146
+ extranonce_prefix : new_extranonce_prefix,
147
+ extranonce_size : new_extranonce_size,
148
+ target : m. target . clone ( ) ,
149
+ } ;
150
+ new_open_extended_mining_channel_success. into_static ( )
151
+ } else {
152
+ // Non-aggregated mode: check if we need to adjust extranonce size
153
+ if m. extranonce_size as usize != downstream_extranonce_len {
154
+ // We need to create an extranonce factory to ensure proper extranonce2_size
155
+ let upstream_extranonce_prefix: Extranonce = m. extranonce_prefix . clone ( ) . into ( ) ;
156
+ let translator_proxy_extranonce_prefix_len = proxy_extranonce_prefix_len (
157
+ m. extranonce_size . into ( ) ,
158
+ downstream_extranonce_len,
159
+ ) ;
160
+
161
+ // range 0 is the extranonce1 from upstream
162
+ // range 1 is the extranonce1 added by the tproxy
163
+ // range 2 is the extranonce2 used by the miner for rolling
164
+ let range_0 = 0 ..extranonce_prefix. len ( ) ;
165
+ let range1 = range_0. end ..range_0. end + translator_proxy_extranonce_prefix_len;
166
+ let range2 = range1. end ..range1. end + downstream_extranonce_len;
167
+ debug ! ( "\n \n range_0: {:?}, range1: {:?}, range2: {:?}\n \n " , range_0, range1, range2) ;
168
+ // Create the factory - this should succeed if configuration is valid
169
+ let extended_extranonce_factory = ExtendedExtranonce :: from_upstream_extranonce (
170
+ upstream_extranonce_prefix,
171
+ range_0,
172
+ range1,
173
+ range2,
174
+ )
175
+ . expect ( "Failed to create ExtendedExtranonce factory - likely extranonce size configuration issue" ) ;
176
+ // Store the factory for this specific channel
177
+ let factory = Arc :: new ( Mutex :: new ( extended_extranonce_factory) ) ;
125
178
let new_extranonce_prefix = factory
126
- . safe_lock ( |f| f. next_prefix_extended ( new_extranonce_size as usize ) )
127
- . expect ( "extranonce_prefix_factory mutex should not be poisoned " )
128
- . expect ( "next_prefix_extended should return a value for valid input " )
179
+ . safe_lock ( |f| f. next_prefix_extended ( downstream_extranonce_len ) )
180
+ . expect ( "Failed to access extranonce factory " )
181
+ . expect ( "Failed to generate extranonce prefix " )
129
182
. into_b032 ( ) ;
183
+ // Create channel with the configured extranonce size
130
184
let new_downstream_extended_channel = ExtendedChannel :: new (
131
185
m. channel_id ,
132
186
user_identity. clone ( ) ,
133
187
new_extranonce_prefix. clone ( ) . into_static ( ) . to_vec ( ) ,
134
188
target. clone ( ) . into ( ) ,
135
189
nominal_hashrate,
136
190
true ,
137
- new_extranonce_size ,
191
+ downstream_extranonce_len as u16 ,
138
192
) ;
139
193
channel_manager_data. extended_channels . insert (
140
194
m. channel_id ,
141
195
Arc :: new ( RwLock :: new ( new_downstream_extended_channel) ) ,
142
196
) ;
143
- let new_open_extended_mining_channel_success =
144
- OpenExtendedMiningChannelSuccess {
145
- request_id : m. request_id ,
146
- channel_id : m. channel_id ,
147
- extranonce_prefix : new_extranonce_prefix,
148
- extranonce_size : new_extranonce_size,
149
- target : m. target . clone ( ) ,
150
- } ;
151
- return new_open_extended_mining_channel_success. into_static ( ) ;
197
+ // Store factory for this channel (we'll need it for share processing)
198
+ if channel_manager_data. extranonce_factories . is_none ( ) {
199
+ channel_manager_data. extranonce_factories = Some ( std:: collections:: HashMap :: new ( ) ) ;
200
+ }
201
+ if let Some ( ref mut factories) = channel_manager_data. extranonce_factories {
202
+ factories. insert ( m. channel_id , factory) ;
203
+ }
204
+ let new_open_extended_mining_channel_success = OpenExtendedMiningChannelSuccess {
205
+ request_id : m. request_id ,
206
+ channel_id : m. channel_id ,
207
+ extranonce_prefix : new_extranonce_prefix,
208
+ extranonce_size : downstream_extranonce_len as u16 ,
209
+ target : m. target . clone ( ) ,
210
+ } ;
211
+ new_open_extended_mining_channel_success. into_static ( )
212
+ } else {
213
+ // Extranonce size matches, use as-is
214
+ channel_manager_data
215
+ . extended_channels
216
+ . insert ( m. channel_id , Arc :: new ( RwLock :: new ( extended_channel) ) ) ;
217
+ m. into_static ( )
152
218
}
153
219
}
154
-
155
- // If we are not in aggregated mode, we just insert the extended channel into the
156
- // map
157
- channel_manager_data
158
- . extended_channels
159
- . insert ( m. channel_id , Arc :: new ( RwLock :: new ( extended_channel) ) ) ;
160
-
161
- m. into_static ( )
162
220
} )
163
221
. map_err ( |e| {
164
222
error ! ( "Failed to lock channel manager data: {:?}" , e) ;
@@ -228,7 +286,7 @@ impl HandleMiningMessagesFromServerAsync for ChannelManager {
228
286
& mut self ,
229
287
m : SubmitSharesError < ' _ > ,
230
288
) -> Result < ( ) , Self :: Error > {
231
- warn ! ( "Received: {} ❌" , m. channel_id ) ;
289
+ warn ! ( "Received: {} ❌" , m) ;
232
290
Ok ( ( ) )
233
291
}
234
292
0 commit comments