@@ -1470,16 +1470,17 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
14701470 }
14711471 }
14721472
1473- TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule (blockCapsule );
1474-
14751473 Set <String > accountSet = new HashSet <>();
14761474 AtomicInteger shieldedTransCounts = new AtomicInteger (0 );
1475+ List <TransactionCapsule > toBePacked = new ArrayList <>();
1476+ long currentSize = blockCapsule .getInstance ().getSerializedSize ();
1477+ boolean isSort = Args .getInstance ().isOpenTransactionSort ();
14771478 while (pendingTransactions .size () > 0 || rePushTransactions .size () > 0 ) {
14781479 boolean fromPending = false ;
14791480 TransactionCapsule trx ;
14801481 if (pendingTransactions .size () > 0 ) {
14811482 trx = pendingTransactions .peek ();
1482- if (Args . getInstance (). isOpenTransactionSort () ) {
1483+ if (isSort ) {
14831484 TransactionCapsule trxRepush = rePushTransactions .peek ();
14841485 if (trxRepush == null || trx .getOrder () >= trxRepush .getOrder ()) {
14851486 fromPending = true ;
@@ -1516,13 +1517,14 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15161517 }
15171518
15181519 // check the block size
1519- if ((blockCapsule . getInstance (). getSerializedSize () + trx .getSerializedSize () + 3 )
1520+ if ((currentSize + trx .getSerializedSize () + 3 )
15201521 > ChainConstant .BLOCK_SIZE ) {
15211522 postponedTrxCount ++;
1522- continue ;
1523+ continue ; // try pack more small trx
15231524 }
15241525 //shielded transaction
1525- if (isShieldedTransaction (trx .getInstance ())
1526+ Transaction transaction = trx .getInstance ();
1527+ if (isShieldedTransaction (transaction )
15261528 && shieldedTransCounts .incrementAndGet () > SHIELDED_TRANS_IN_BLOCK_COUNTS ) {
15271529 continue ;
15281530 }
@@ -1532,7 +1534,7 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15321534 if (accountSet .contains (ownerAddress )) {
15331535 continue ;
15341536 } else {
1535- if (isMultiSignTransaction (trx . getInstance () )) {
1537+ if (isMultiSignTransaction (transaction )) {
15361538 accountSet .add (ownerAddress );
15371539 }
15381540 }
@@ -1542,27 +1544,25 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15421544 // apply transaction
15431545 try (ISession tmpSession = revokingStore .buildSession ()) {
15441546 accountStateCallBack .preExeTrans ();
1545- TransactionInfo result = processTransaction (trx , blockCapsule );
1547+ processTransaction (trx , blockCapsule );
15461548 accountStateCallBack .exeTransFinish ();
15471549 tmpSession .merge ();
1548- blockCapsule .addTransaction (trx );
1549- if (Objects .nonNull (result )) {
1550- transactionRetCapsule .addTransactionInfo (result );
1551- }
1550+ toBePacked .add (trx );
1551+ currentSize += trx .getSerializedSize () + 2 ; // proto tag num is 1 , so tag size is 2
15521552 } catch (Exception e ) {
15531553 logger .error ("Process trx {} failed when generating block {}, {}." , trx .getTransactionId (),
15541554 blockCapsule .getNum (), e .getMessage ());
15551555 }
15561556 }
1557-
1557+ blockCapsule . addAllTransactions ( toBePacked );
15581558 accountStateCallBack .executeGenerateFinish ();
15591559
15601560 session .reset ();
15611561
1562- logger .info ("Generate block {} success, trxs: {}, pendingCount: {}, rePushCount: {},"
1563- + " postponedCount: {}. " ,
1562+ logger .info ("Generate block {} success, trxs:{}, pendingCount: {}, rePushCount: {},"
1563+ + " postponedCount: {}, blockSize: {} B " ,
15641564 blockCapsule .getNum (), blockCapsule .getTransactions ().size (),
1565- pendingTransactions .size (), rePushTransactions .size (), postponedTrxCount );
1565+ pendingTransactions .size (), rePushTransactions .size (), postponedTrxCount , currentSize );
15661566
15671567 blockCapsule .setMerkleRoot ();
15681568 blockCapsule .sign (miner .getPrivateKey ());
@@ -1654,18 +1654,21 @@ private void processBlock(BlockCapsule block, List<TransactionCapsule> txs)
16541654 try {
16551655 merkleContainer .resetCurrentMerkleTree ();
16561656 accountStateCallBack .preExecute (block );
1657+ List <TransactionInfo > results = new ArrayList <>();
1658+ long num = block .getNum ();
16571659 for (TransactionCapsule transactionCapsule : block .getTransactions ()) {
1658- transactionCapsule .setBlockNum (block . getNum () );
1660+ transactionCapsule .setBlockNum (num );
16591661 if (block .generatedByMyself ) {
16601662 transactionCapsule .setVerified (true );
16611663 }
16621664 accountStateCallBack .preExeTrans ();
16631665 TransactionInfo result = processTransaction (transactionCapsule , block );
16641666 accountStateCallBack .exeTransFinish ();
16651667 if (Objects .nonNull (result )) {
1666- transactionRetCapsule . addTransactionInfo (result );
1668+ results . add (result );
16671669 }
16681670 }
1671+ transactionRetCapsule .addAllTransactionInfos (results );
16691672 accountStateCallBack .executePushFinish ();
16701673 } finally {
16711674 accountStateCallBack .exceptionFinish ();
0 commit comments