11package org .tron .consensus .dpos ;
22
33import static org .tron .consensus .base .Constant .BLOCK_PRODUCED_INTERVAL ;
4+ import static org .tron .consensus .base .Constant .BLOCK_PRODUCE_TIMEOUT_PERCENT ;
45
56import com .google .protobuf .ByteString ;
6- import java .util .Arrays ;
77import lombok .Setter ;
88import lombok .extern .slf4j .Slf4j ;
99import org .joda .time .DateTime ;
1515import org .tron .common .utils .ByteArray ;
1616import org .tron .common .utils .Sha256Hash ;
1717import org .tron .consensus .ConsensusDelegate ;
18- import org .tron .consensus .base .Param .Miner ;
1918import org .tron .consensus .base .State ;
20- import org .tron .core .capsule .AccountCapsule ;
2119import org .tron .protos .Protocol .Block ;
2220import org .tron .protos .Protocol .BlockHeader ;
2321
@@ -43,10 +41,11 @@ public class DposTask {
4341
4442 public void init () {
4543
44+ if (!dposService .isEnable () || StringUtils .isEmpty (dposService .getMiners ())) {
45+ return ;
46+ }
47+
4648 Runnable runnable = () -> {
47- if (!dposService .isEnable () || StringUtils .isEmpty (dposService .getMiners ())) {
48- return ;
49- }
5049 while (isRunning ) {
5150 try {
5251 if (dposService .isNeedSyncCheck ()) {
@@ -73,7 +72,9 @@ public void init() {
7372
7473 public void stop () {
7574 isRunning = false ;
76- produceThread .interrupt ();
75+ if (produceThread != null ) {
76+ produceThread .interrupt ();
77+ }
7778 logger .info ("DPoS service stopped." );
7879 }
7980
@@ -91,18 +92,20 @@ private State produceBlock() {
9192 return State .NOT_TIME_YET ;
9293 }
9394
94- final ByteString scheduledWitness = dposSlot .getScheduledWitness (slot );
95- state = stateManager .getState (scheduledWitness );
95+ ByteString pWitness = dposSlot .getScheduledWitness (slot );
96+ state = stateManager .getState (pWitness );
9697 if (!State .OK .equals (state )) {
9798 return state ;
9899 }
99100
100- Block block = dposService .getBlockHandle ().produce ();
101+ long pTime = dposSlot .getTime (slot );
102+ long timeout = pTime + BLOCK_PRODUCED_INTERVAL / 2 * BLOCK_PRODUCE_TIMEOUT_PERCENT / 100 ;
103+ Block block = dposService .getBlockHandle ().produce (timeout );
101104 if (block == null ) {
102105 return State .PRODUCE_BLOCK_FAILED ;
103106 }
104107
105- Block sBlock = getSignedBlock (block , scheduledWitness , slot );
108+ Block sBlock = getSignedBlock (block , pWitness , pTime );
106109
107110 stateManager .setCurrentBlock (sBlock );
108111
@@ -120,11 +123,11 @@ private State produceBlock() {
120123 return State .OK ;
121124 }
122125
123- public Block getSignedBlock (Block block , ByteString witness , long slot ) {
126+ public Block getSignedBlock (Block block , ByteString witness , long time ) {
124127 BlockHeader .raw raw = block .getBlockHeader ().getRawData ().toBuilder ()
125128 .setParentHash (ByteString .copyFrom (consensusDelegate .getLatestBlockHeaderHash ().getBytes ()))
126129 .setNumber (consensusDelegate .getLatestBlockHeaderNumber () + 1 )
127- .setTimestamp (dposSlot . getTime ( slot ) )
130+ .setTimestamp (time )
128131 .setWitnessAddress (witness )
129132 .build ();
130133
0 commit comments