@@ -1434,7 +1434,6 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1434
1434
MetricKeys .Histogram .BLOCK_GENERATE_LATENCY , address );
1435
1435
Metrics .histogramObserve (MetricKeys .Histogram .MINER_LATENCY ,
1436
1436
(System .currentTimeMillis () - blockTime ) / Metrics .MILLISECONDS_PER_SECOND , address );
1437
- long postponedTrxCount = 0 ;
1438
1437
logger .info ("Generate block {} begin" , chainBaseManager .getHeadBlockNum () + 1 );
1439
1438
1440
1439
BlockCapsule blockCapsule = new BlockCapsule (chainBaseManager .getHeadBlockNum () + 1 ,
@@ -1456,16 +1455,17 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1456
1455
}
1457
1456
}
1458
1457
1459
- TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule (blockCapsule );
1460
-
1461
1458
Set <String > accountSet = new HashSet <>();
1462
1459
AtomicInteger shieldedTransCounts = new AtomicInteger (0 );
1460
+ List <TransactionCapsule > toBePacked = new ArrayList <>();
1461
+ long currentSize = blockCapsule .getInstance ().getSerializedSize ();
1462
+ boolean isSort = Args .getInstance ().isOpenTransactionSort ();
1463
1463
while (pendingTransactions .size () > 0 || rePushTransactions .size () > 0 ) {
1464
1464
boolean fromPending = false ;
1465
1465
TransactionCapsule trx ;
1466
1466
if (pendingTransactions .size () > 0 ) {
1467
1467
trx = pendingTransactions .peek ();
1468
- if (Args . getInstance (). isOpenTransactionSort () ) {
1468
+ if (isSort ) {
1469
1469
TransactionCapsule trxRepush = rePushTransactions .peek ();
1470
1470
if (trxRepush == null || trx .getOrder () >= trxRepush .getOrder ()) {
1471
1471
fromPending = true ;
@@ -1501,24 +1501,23 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1501
1501
}
1502
1502
1503
1503
// check the block size
1504
- if ((blockCapsule .getInstance ().getSerializedSize () + trx .getSerializedSize () + 3 )
1505
- > ChainConstant .BLOCK_SIZE ) {
1506
- postponedTrxCount ++;
1507
- continue ;
1504
+ if ((currentSize += trx .getSerializedSize () + 2 ) > ChainConstant .BLOCK_SIZE ) {
1505
+ break ;
1508
1506
}
1509
1507
//shielded transaction
1510
- if (isShieldedTransaction (trx .getInstance ())
1508
+ Transaction transaction = trx .getInstance ();
1509
+ if (isShieldedTransaction (transaction )
1511
1510
&& shieldedTransCounts .incrementAndGet () > SHIELDED_TRANS_IN_BLOCK_COUNTS ) {
1512
1511
continue ;
1513
1512
}
1514
1513
//multi sign transaction
1515
- Contract contract = trx . getInstance () .getRawData ().getContract (0 );
1514
+ Contract contract = transaction .getRawData ().getContract (0 );
1516
1515
byte [] owner = TransactionCapsule .getOwner (contract );
1517
1516
String ownerAddress = ByteArray .toHexString (owner );
1518
1517
if (accountSet .contains (ownerAddress )) {
1519
1518
continue ;
1520
1519
} else {
1521
- if (isMultiSignTransaction (trx . getInstance () )) {
1520
+ if (isMultiSignTransaction (transaction )) {
1522
1521
accountSet .add (ownerAddress );
1523
1522
}
1524
1523
}
@@ -1528,27 +1527,24 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
1528
1527
// apply transaction
1529
1528
try (ISession tmpSession = revokingStore .buildSession ()) {
1530
1529
accountStateCallBack .preExeTrans ();
1531
- TransactionInfo result = processTransaction (trx , blockCapsule );
1530
+ processTransaction (trx , blockCapsule );
1532
1531
accountStateCallBack .exeTransFinish ();
1533
1532
tmpSession .merge ();
1534
- blockCapsule .addTransaction (trx );
1535
- if (Objects .nonNull (result )) {
1536
- transactionRetCapsule .addTransactionInfo (result );
1537
- }
1533
+ toBePacked .add (trx );
1538
1534
} catch (Exception e ) {
1539
1535
logger .error ("Process trx {} failed when generating block: {}" , trx .getTransactionId (),
1540
1536
e .getMessage ());
1541
1537
}
1542
1538
}
1543
-
1539
+ blockCapsule . addAllTransactions ( toBePacked );
1544
1540
accountStateCallBack .executeGenerateFinish ();
1545
1541
1546
1542
session .reset ();
1547
1543
1548
1544
logger .info ("Generate block {} success, trxs:{}, pendingCount: {}, rePushCount: {},"
1549
- + " postponedCount : {}" ,
1545
+ + " blockSize : {} B " ,
1550
1546
blockCapsule .getNum (), blockCapsule .getTransactions ().size (),
1551
- pendingTransactions .size (), rePushTransactions .size (), postponedTrxCount );
1547
+ pendingTransactions .size (), rePushTransactions .size (), currentSize );
1552
1548
1553
1549
blockCapsule .setMerkleRoot ();
1554
1550
blockCapsule .sign (miner .getPrivateKey ());
@@ -1641,18 +1637,21 @@ private void processBlock(BlockCapsule block, List<TransactionCapsule> txs)
1641
1637
try {
1642
1638
merkleContainer .resetCurrentMerkleTree ();
1643
1639
accountStateCallBack .preExecute (block );
1640
+ List <TransactionInfo > results = new ArrayList <>();
1641
+ long num = block .getNum ();
1644
1642
for (TransactionCapsule transactionCapsule : block .getTransactions ()) {
1645
- transactionCapsule .setBlockNum (block . getNum () );
1643
+ transactionCapsule .setBlockNum (num );
1646
1644
if (block .generatedByMyself ) {
1647
1645
transactionCapsule .setVerified (true );
1648
1646
}
1649
1647
accountStateCallBack .preExeTrans ();
1650
1648
TransactionInfo result = processTransaction (transactionCapsule , block );
1651
1649
accountStateCallBack .exeTransFinish ();
1652
1650
if (Objects .nonNull (result )) {
1653
- transactionRetCapsule . addTransactionInfo (result );
1651
+ results . add (result );
1654
1652
}
1655
1653
}
1654
+ transactionRetCapsule .addAllTransactionInfos (results );
1656
1655
accountStateCallBack .executePushFinish ();
1657
1656
} finally {
1658
1657
accountStateCallBack .exceptionFinish ();
0 commit comments