@@ -14,8 +14,8 @@ use script::Script;
14
14
use staking;
15
15
16
16
use {
17
- AccountMap , BestIndex , BlockHeaderFor , CertCache , DepositCache , HashsForNumber , Module ,
18
- NumberForHash , Params , ParamsInfo , Trait , TxProposal ,
17
+ BestIndex , BlockHeaderFor , CertCache , DepositCache , HashsForNumber , Module , NumberForHash ,
18
+ Params , ParamsInfo , Trait , TxProposal ,
19
19
} ;
20
20
21
21
use tx:: { Proposal , RollBack , TxStorage } ;
@@ -165,6 +165,20 @@ impl<T: Trait> Chain<T> {
165
165
best_number + 1
166
166
} ,
167
167
} ;
168
+
169
+ <NumberForHash < T > >:: insert ( new_best_header. hash . clone ( ) , new_best_header. number ) ;
170
+ runtime_io:: print ( "------------" ) ;
171
+ runtime_io:: print ( new_best_header. hash . to_vec ( ) . as_slice ( ) ) ;
172
+ <HashsForNumber < T > >:: mutate ( new_best_header. number , |v| {
173
+ let h = new_best_header. hash . clone ( ) ;
174
+ if v. contains ( & h) == false {
175
+ v. push ( h) ;
176
+ }
177
+ } ) ;
178
+ // best chain choose finish
179
+ <BestIndex < T > >:: put ( new_best_header. clone ( ) ) ;
180
+
181
+ // deposit/withdraw handle start
168
182
let symbol: Symbol = Module :: < T > :: SYMBOL . to_vec ( ) ;
169
183
let irr_block = <IrrBlock < T > >:: get ( ) ;
170
184
// Deposit
@@ -174,9 +188,10 @@ impl<T: Trait> Chain<T> {
174
188
for ( account_id, amount, tx_hash, block_hash) in vec {
175
189
match <NumberForHash < T > >:: get ( block_hash. clone ( ) ) {
176
190
Some ( height) => {
177
- if new_best_header. number > height + irr_block {
191
+ if new_best_header. number >= height + irr_block {
178
192
runtime_io:: print ( "-----------financial_records deposit" ) ;
179
- <financial_records:: Module < T > >:: deposit (
193
+ // TODO handle err
194
+ let _ = <financial_records:: Module < T > >:: deposit (
180
195
& account_id,
181
196
& symbol,
182
197
As :: sa ( amount) ,
@@ -195,75 +210,86 @@ impl<T: Trait> Chain<T> {
195
210
}
196
211
197
212
// Withdraw
198
- let candidate = <TxProposal < T > >:: get ( ) ;
199
- if candidate. is_some ( ) {
200
- let tx = candidate. unwrap ( ) ;
201
- match <NumberForHash < T > >:: get ( tx. block_hash ) {
202
- Some ( height) => {
203
- if new_best_header. number > height + irr_block {
204
- runtime_io:: print ( "----new_best_header.number-----" ) ;
205
- let txid = tx. tx . hash ( ) ;
206
- for output in tx. tx . outputs . iter ( ) {
207
- let script: Script = output. clone ( ) . script_pubkey . into ( ) ;
208
- let script_address =
209
- script. extract_destinations ( ) . unwrap_or ( Vec :: new ( ) ) ;
210
- let network_id = <NetworkId < T > >:: get ( ) ;
211
- let network = if network_id == 1 {
212
- keys:: Network :: Testnet
213
- } else {
214
- keys:: Network :: Mainnet
215
- } ;
216
- let address = keys:: Address {
217
- kind : script_address[ 0 ] . kind ,
218
- network,
219
- hash : script_address[ 0 ] . hash . clone ( ) ,
220
- } ;
221
- let account_id = <AddressMap < T > >:: get ( address) ;
222
- if account_id. is_some ( ) {
223
- <financial_records:: Module < T > >:: withdrawal_finish (
224
- & account_id. unwrap ( ) ,
225
- & symbol,
226
- Some ( txid. as_ref ( ) . to_vec ( ) ) ,
227
- ) ;
228
- }
229
- }
230
- let vec = <financial_records:: Module < T > >:: get_withdraw_cache ( & symbol) ;
231
- if vec. is_some ( ) {
232
- let mut address_vec = Vec :: new ( ) ;
233
- for ( account_id, balance) in vec. unwrap ( ) {
234
- let address = <AccountMap < T > >:: get ( account_id) ;
235
- if address. is_some ( ) {
236
- address_vec. push ( ( address. unwrap ( ) , balance. as_ ( ) as u64 ) ) ;
237
- }
238
- }
239
- let btc_fee = <BtcFee < T > >:: get ( ) ;
240
- if let Err ( e) = <Proposal < T > >:: create_proposal ( address_vec, btc_fee) {
241
- return Err ( ChainErr :: OtherErr ( e) ) ;
213
+ let len = Module :: < T > :: tx_proposal_len ( ) ;
214
+ // get last proposal
215
+ if len > 0 {
216
+ let mut candidate = Module :: < T > :: tx_proposal ( len - 1 ) . unwrap ( ) ;
217
+ // candidate: CandidateTx
218
+ if candidate. confirmed == false {
219
+ match <NumberForHash < T > >:: get ( & candidate. block_hash ) {
220
+ Some ( height) => {
221
+ if new_best_header. number >= height + irr_block {
222
+ runtime_io:: print ( "----new_best_header.number-----" ) ;
223
+ let txid = candidate. tx . hash ( ) ;
224
+ /*for output in candidate.tx.outputs.iter() {
225
+ let script: Script = output.clone().script_pubkey.into();
226
+ let script_address =
227
+ script.extract_destinations().unwrap_or(Vec::new());
228
+ let network_id = <NetworkId<T>>::get();
229
+ let network = if network_id == 1 {
230
+ keys::Network::Testnet
231
+ } else {
232
+ keys::Network::Mainnet
233
+ };
234
+ let address = keys::Address {
235
+ kind: script_address[0].kind,
236
+ network,
237
+ hash: script_address[0].hash.clone(),
238
+ };*/
239
+ //let account_id = <AddressMap<T>>::get(address);
240
+ //if account_id.is_some() {
241
+ for ( account_id, _) in candidate. outs . clone ( ) {
242
+ // TODO handle err
243
+ let _ = <financial_records:: Module < T > >:: withdrawal_finish (
244
+ & account_id,
245
+ & symbol,
246
+ Some ( txid. as_ref ( ) . to_vec ( ) ) ,
247
+ ) ;
248
+ }
249
+ //}
250
+ candidate. confirmed = true ;
251
+ // mark this tx withdraw finish!
252
+ TxProposal :: < T > :: insert ( len - 1 , candidate) ;
242
253
}
243
- } else {
244
- <TxProposal < T > >:: kill ( ) ;
245
254
}
255
+ None => { }
246
256
}
247
257
}
248
- None => { }
258
+ }
259
+
260
+ // case 0: 当刚启动时Candidate lenth = 0 时
261
+ // case 1: 所有提现交易都是正常逻辑执行,会confirmed.
262
+ // case 2: 非正常逻辑提现,candidate.unexpect 会在handle_input时设置,
263
+ // 标记该链上这笔proposal由于BTC 托管人没有按着正常逻辑签名广播, 该proposal可能永远不会confirmed.
264
+ // 所以开始重新创建proposal.
265
+ if len == 0 {
266
+ // no withdraw cache would return None
267
+ if let Some ( indexs) = financial_records:: Module :: < T > :: withdrawal_cache_indexs ( & symbol) {
268
+ let btc_fee = <BtcFee < T > >:: get ( ) ;
269
+ if let Err ( e) = <Proposal < T > >:: create_proposal ( indexs, btc_fee) {
270
+ return Err ( ChainErr :: OtherErr ( e) ) ;
271
+ }
272
+ }
273
+
249
274
}
250
- } else {
251
- let vec = <financial_records:: Module < T > >:: get_withdraw_cache ( & symbol) ;
252
- if vec. is_some ( ) {
253
- runtime_io:: print ( "-----------first withdraw" ) ;
254
- let mut address_vec = Vec :: new ( ) ;
255
- for ( account_id, balance) in vec. unwrap ( ) {
256
- let address = <AccountMap < T > >:: get ( account_id) ;
257
- if address. is_some ( ) {
258
- address_vec. push ( ( address. unwrap ( ) , balance. as_ ( ) as u64 ) ) ;
259
- }
260
- }
261
- let btc_fee = <BtcFee < T > >:: get ( ) ;
262
- if let Err ( e) = <Proposal < T > >:: create_proposal ( address_vec, btc_fee) {
263
- return Err ( ChainErr :: OtherErr ( e) ) ;
264
- }
275
+ if len > 0 {
276
+ let candidate = Module :: < T > :: tx_proposal ( len - 1 ) . unwrap ( ) ;
277
+ if candidate. confirmed || candidate. unexpect {
278
+ // no withdraw cache would return None
279
+ if let Some ( indexs) = financial_records:: Module :: < T > :: withdrawal_cache_indexs ( & symbol) {
280
+ let btc_fee = <BtcFee < T > >:: get ( ) ;
281
+ if let Err ( e) = <Proposal < T > >:: create_proposal ( indexs, btc_fee) {
282
+ return Err ( ChainErr :: OtherErr ( e) ) ;
283
+ }
284
+ }
285
+ }
265
286
}
266
- }
287
+ // let candidate = <TxProposal<T>>::get();
288
+ // if candidate.is_some() {
289
+ // let tx = candidate.unwrap();
290
+ // } else {
291
+ //
292
+ // }
267
293
// SendCert
268
294
if let Some ( cert_info) = <CertCache < T > >:: take ( ) {
269
295
runtime_io:: print ( "------CertCache take" ) ;
@@ -272,17 +298,6 @@ impl<T: Trait> Chain<T> {
272
298
}
273
299
}
274
300
275
- <NumberForHash < T > >:: insert ( new_best_header. hash . clone ( ) , new_best_header. number ) ;
276
- runtime_io:: print ( "------------" ) ;
277
- runtime_io:: print ( new_best_header. hash . to_vec ( ) . as_slice ( ) ) ;
278
- <HashsForNumber < T > >:: mutate ( new_best_header. number , |v| {
279
- let h = new_best_header. hash . clone ( ) ;
280
- if v. contains ( & h) == false {
281
- v. push ( h) ;
282
- }
283
- } ) ;
284
-
285
- <BestIndex < T > >:: put ( new_best_header) ;
286
301
Ok ( ( ) )
287
302
}
288
303
/// Rollbacks single best block
0 commit comments