@@ -46,6 +46,7 @@ use std::cmp::min;
46
46
use std:: iter:: Extend ;
47
47
use std:: option:: Option :: { None , Some } ;
48
48
use std:: { collections:: HashMap , sync:: Arc } ;
49
+ use starcoin_types:: header:: DagHeader ;
49
50
50
51
51
52
pub struct ChainStatusWithBlock {
@@ -452,25 +453,25 @@ impl BlockChain {
452
453
let blues = block. uncles ( ) . expect ( "Blue blocks must exist" ) ;
453
454
let ( selected_parent, blues) = blues. split_at ( 1 ) ;
454
455
let selected_parent = selected_parent[ 0 ] . clone ( ) ;
455
- let block_info = self . storage . get_block_info ( selected_parent. id ( ) ) ?. expect ( "selected parent must executed" ) ;
456
+ let block_info_past = self . storage . get_block_info ( selected_parent. id ( ) ) ?. expect ( "selected parent must executed" ) ;
456
457
let header = block. header ( ) ;
457
-
458
458
let block_id = header. id ( ) ;
459
-
460
459
let block_metadata = block. to_metadata ( selected_parent. gas_used ( ) ) ;
461
- let mut transaction = vec ! [ Transaction :: BlockMetadata ( block_metadata) ] ;
460
+ let mut transactions = vec ! [ Transaction :: BlockMetadata ( block_metadata) ] ;
461
+ let mut total_difficulty = header. difficulty ( ) + block_info_past. total_difficulty ;
462
462
for blue in blues {
463
463
let blue_block = self . storage . get_block_by_hash ( blue. parent_hash ( ) ) ?. expect ( "block blue need exist" ) ;
464
- transaction. extend ( blue_block. transactions ( ) . iter ( ) . cloned ( ) . map ( Transaction :: UserTransaction ) )
464
+ transactions. extend ( blue_block. transactions ( ) . iter ( ) . cloned ( ) . map ( Transaction :: UserTransaction ) ) ;
465
+ total_difficulty += blue_block. header . difficulty ( ) ;
465
466
}
466
- transaction . extend (
467
+ transactions . extend (
467
468
block. transactions ( ) . iter ( ) . cloned ( ) . map ( Transaction :: UserTransaction ) ,
468
469
) ;
469
470
470
471
watch ( CHAIN_WATCH_NAME , "n21" ) ;
471
472
let executed_data = starcoin_executor:: block_execute (
472
473
& self . statedb ,
473
- transaction . clone ( ) ,
474
+ transactions . clone ( ) ,
474
475
self . epoch . block_gas_limit ( ) , //TODO: Fix me
475
476
self . vm_metrics . clone ( ) ,
476
477
) ?;
@@ -494,18 +495,18 @@ impl BlockChain {
494
495
495
496
verify_block ! (
496
497
VerifyBlockField :: State ,
497
- vec_transaction_info. len( ) == transaction . len( ) ,
498
+ vec_transaction_info. len( ) == transactions . len( ) ,
498
499
"invalid txn num in the block"
499
500
) ;
500
- let txn_accumulator= info_2_accumulator (
501
- block_info . txn_accumulator_info ,
502
- AccumulatorStoreType :: Transaction ,
503
- self . storage . as_ref ( ) ,
501
+ let txn_accumulator = info_2_accumulator (
502
+ block_info_past . txn_accumulator_info ,
503
+ AccumulatorStoreType :: Transaction ,
504
+ self . storage . as_ref ( ) ,
504
505
) ;
505
- let block_accumulator= info_2_accumulator (
506
- block_info . block_accumulator_info ,
507
- AccumulatorStoreType :: Block ,
508
- self . storage . as_ref ( ) ,
506
+ let block_accumulator = info_2_accumulator (
507
+ block_info_past . block_accumulator_info ,
508
+ AccumulatorStoreType :: Block ,
509
+ self . storage . as_ref ( ) ,
509
510
) ;
510
511
let transaction_global_index = txn_accumulator. num_leaves ( ) ;
511
512
@@ -534,8 +535,6 @@ impl BlockChain {
534
535
. flush ( )
535
536
. map_err ( |_err| BlockExecutorError :: BlockAccumulatorFlushErr ) ?;
536
537
537
- let pre_total_difficulty = block_info. total_difficulty ;
538
- let total_difficulty = pre_total_difficulty + header. difficulty ( ) ;
539
538
540
539
block_accumulator. append ( & [ block_id] ) ?;
541
540
block_accumulator. flush ( ) ?;
@@ -588,12 +587,12 @@ impl BlockChain {
588
587
. collect ( ) ,
589
588
) ?;
590
589
591
- let txn_id_vec = transaction
590
+ let txn_id_vec = transactions
592
591
. iter ( )
593
592
. map ( |user_txn| user_txn. id ( ) )
594
593
. collect :: < Vec < HashValue > > ( ) ;
595
594
// save transactions
596
- self . storage . save_transaction_batch ( transaction ) ?;
595
+ self . storage . save_transaction_batch ( transactions ) ?;
597
596
598
597
// save block's transactions
599
598
self . storage . save_block_transaction_ids ( block_id, txn_id_vec) ?;
@@ -602,7 +601,7 @@ impl BlockChain {
602
601
self . storage . save_block_info ( block_info. clone ( ) ) ?;
603
602
604
603
self . storage . save_table_infos ( txn_table_infos) ?;
605
-
604
+ self . dag . commit ( DagHeader :: new ( header . clone ( ) ) ) ? ;
606
605
watch ( CHAIN_WATCH_NAME , "n26" ) ;
607
606
Ok ( ExecutedBlock { block, block_info } )
608
607
}
@@ -767,9 +766,7 @@ impl BlockChain {
767
766
storage. save_block_txn_info_ids ( block_id, txn_info_ids) ?;
768
767
storage. commit_block ( block. clone ( ) ) ?;
769
768
storage. save_block_info ( block_info. clone ( ) ) ?;
770
-
771
769
storage. save_table_infos ( txn_table_infos) ?;
772
-
773
770
watch ( CHAIN_WATCH_NAME , "n26" ) ;
774
771
Ok ( ExecutedBlock { block, block_info } )
775
772
}
@@ -1333,8 +1330,49 @@ impl BlockChain {
1333
1330
}
1334
1331
1335
1332
fn connect_dag ( & mut self , executed_block : ExecutedBlock ) -> Result < ExecutedBlock > {
1336
- let ( block, block_info) = ( executed_block. block ( ) , executed_block. block_info ( ) ) ;
1333
+ let ( new_tip_block, _) = ( executed_block. block ( ) , executed_block. block_info ( ) ) ;
1334
+ let mut tips = self . status . dag_tips ( ) . expect ( "Tips should exist on dag" ) . clone ( ) ;
1335
+ let parents = executed_block. block . header . parents_hash ( ) . expect ( "Dag parents need exist" ) ;
1336
+ for hash in parents {
1337
+ tips. retain ( |x| * x != hash) ;
1338
+ }
1339
+ tips. push ( new_tip_block. id ( ) ) ;
1340
+
1341
+ let block_hash = {
1342
+ let ghost_of_tips = self . dag . ghostdata ( tips. as_slice ( ) ) ;
1343
+ ghost_of_tips. selected_parent
1344
+ } ;
1345
+ let ( block, block_info) = {
1346
+ let block = self . storage . get_block ( block_hash) ?. expect ( "Dag block should exist" ) ;
1347
+ let block_info = self . storage . get_block_info ( block_hash) ?. expect ( "Dag block info should exist" ) ;
1348
+ ( block, block_info)
1349
+ } ;
1350
+
1351
+
1352
+ let txn_accumulator_info = block_info. get_txn_accumulator_info ( ) ;
1353
+ let block_accumulator_info = block_info. get_block_accumulator_info ( ) ;
1354
+ let state_root = block. header ( ) . state_root ( ) ;
1337
1355
1356
+ self . txn_accumulator = info_2_accumulator (
1357
+ txn_accumulator_info. clone ( ) ,
1358
+ AccumulatorStoreType :: Transaction ,
1359
+ self . storage . as_ref ( ) ,
1360
+ ) ;
1361
+ self . block_accumulator = info_2_accumulator (
1362
+ block_accumulator_info. clone ( ) ,
1363
+ AccumulatorStoreType :: Block ,
1364
+ self . storage . as_ref ( ) ,
1365
+ ) ;
1366
+
1367
+ self . statedb = ChainStateDB :: new ( self . storage . clone ( ) . into_super_arc ( ) , Some ( state_root) ) ;
1368
+
1369
+ self . status = ChainStatusWithBlock {
1370
+ status : ChainStatus :: new ( block. header ( ) . clone ( ) , block_info. clone ( ) , Some ( tips) ) ,
1371
+ head : block. clone ( ) ,
1372
+ } ;
1373
+ if self . epoch . end_block_number ( ) == block. header ( ) . number ( ) {
1374
+ self . epoch = get_epoch_from_statedb ( & self . statedb ) ?;
1375
+ }
1338
1376
Ok ( executed_block)
1339
1377
}
1340
1378
}
0 commit comments