@@ -1470,16 +1470,17 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1470
1470
}
1471
1471
}
1472
1472
1473
- TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule (blockCapsule );
1474
-
1475
1473
Set <String > accountSet = new HashSet <>();
1476
1474
AtomicInteger shieldedTransCounts = new AtomicInteger (0 );
1475
+ List <TransactionCapsule > toBePacked = new ArrayList <>();
1476
+ long currentSize = blockCapsule .getInstance ().getSerializedSize ();
1477
+ boolean isSort = Args .getInstance ().isOpenTransactionSort ();
1477
1478
while (pendingTransactions .size () > 0 || rePushTransactions .size () > 0 ) {
1478
1479
boolean fromPending = false ;
1479
1480
TransactionCapsule trx ;
1480
1481
if (pendingTransactions .size () > 0 ) {
1481
1482
trx = pendingTransactions .peek ();
1482
- if (Args . getInstance (). isOpenTransactionSort () ) {
1483
+ if (isSort ) {
1483
1484
TransactionCapsule trxRepush = rePushTransactions .peek ();
1484
1485
if (trxRepush == null || trx .getOrder () >= trxRepush .getOrder ()) {
1485
1486
fromPending = true ;
@@ -1516,13 +1517,14 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1516
1517
}
1517
1518
1518
1519
// check the block size
1519
- if ((blockCapsule . getInstance (). getSerializedSize () + trx .getSerializedSize () + 3 )
1520
+ if ((currentSize + trx .getSerializedSize () + 3 )
1520
1521
> ChainConstant .BLOCK_SIZE ) {
1521
1522
postponedTrxCount ++;
1522
- continue ;
1523
+ continue ; // try pack more small trx
1523
1524
}
1524
1525
//shielded transaction
1525
- if (isShieldedTransaction (trx .getInstance ())
1526
+ Transaction transaction = trx .getInstance ();
1527
+ if (isShieldedTransaction (transaction )
1526
1528
&& shieldedTransCounts .incrementAndGet () > SHIELDED_TRANS_IN_BLOCK_COUNTS ) {
1527
1529
continue ;
1528
1530
}
@@ -1532,7 +1534,7 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1532
1534
if (accountSet .contains (ownerAddress )) {
1533
1535
continue ;
1534
1536
} else {
1535
- if (isMultiSignTransaction (trx . getInstance () )) {
1537
+ if (isMultiSignTransaction (transaction )) {
1536
1538
accountSet .add (ownerAddress );
1537
1539
}
1538
1540
}
@@ -1542,27 +1544,25 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1542
1544
// apply transaction
1543
1545
try (ISession tmpSession = revokingStore .buildSession ()) {
1544
1546
accountStateCallBack .preExeTrans ();
1545
- TransactionInfo result = processTransaction (trx , blockCapsule );
1547
+ processTransaction (trx , blockCapsule );
1546
1548
accountStateCallBack .exeTransFinish ();
1547
1549
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
1552
1552
} catch (Exception e ) {
1553
1553
logger .error ("Process trx {} failed when generating block {}, {}." , trx .getTransactionId (),
1554
1554
blockCapsule .getNum (), e .getMessage ());
1555
1555
}
1556
1556
}
1557
-
1557
+ blockCapsule . addAllTransactions ( toBePacked );
1558
1558
accountStateCallBack .executeGenerateFinish ();
1559
1559
1560
1560
session .reset ();
1561
1561
1562
- logger .info ("Generate block {} success, trxs: {}, pendingCount: {}, rePushCount: {},"
1563
- + " postponedCount: {}. " ,
1562
+ logger .info ("Generate block {} success, trxs:{}, pendingCount: {}, rePushCount: {},"
1563
+ + " postponedCount: {}, blockSize: {} B " ,
1564
1564
blockCapsule .getNum (), blockCapsule .getTransactions ().size (),
1565
- pendingTransactions .size (), rePushTransactions .size (), postponedTrxCount );
1565
+ pendingTransactions .size (), rePushTransactions .size (), postponedTrxCount , currentSize );
1566
1566
1567
1567
blockCapsule .setMerkleRoot ();
1568
1568
blockCapsule .sign (miner .getPrivateKey ());
@@ -1654,18 +1654,21 @@ private void processBlock(BlockCapsule block, List<TransactionCapsule> txs)
1654
1654
try {
1655
1655
merkleContainer .resetCurrentMerkleTree ();
1656
1656
accountStateCallBack .preExecute (block );
1657
+ List <TransactionInfo > results = new ArrayList <>();
1658
+ long num = block .getNum ();
1657
1659
for (TransactionCapsule transactionCapsule : block .getTransactions ()) {
1658
- transactionCapsule .setBlockNum (block . getNum () );
1660
+ transactionCapsule .setBlockNum (num );
1659
1661
if (block .generatedByMyself ) {
1660
1662
transactionCapsule .setVerified (true );
1661
1663
}
1662
1664
accountStateCallBack .preExeTrans ();
1663
1665
TransactionInfo result = processTransaction (transactionCapsule , block );
1664
1666
accountStateCallBack .exeTransFinish ();
1665
1667
if (Objects .nonNull (result )) {
1666
- transactionRetCapsule . addTransactionInfo (result );
1668
+ results . add (result );
1667
1669
}
1668
1670
}
1671
+ transactionRetCapsule .addAllTransactionInfos (results );
1669
1672
accountStateCallBack .executePushFinish ();
1670
1673
} finally {
1671
1674
accountStateCallBack .exceptionFinish ();
0 commit comments