@@ -190,47 +190,48 @@ private BlockProductionCondition tryProduceBlock() throws InterruptedException {
190190 return BlockProductionCondition .LOW_PARTICIPATION ;
191191 }
192192
193- long slot = controller .getSlotAtTime (now );
194- logger .debug ("Slot:" + slot );
195-
196- if (slot == 0 ) {
197- logger .info ("Not time yet,now:{},headBlockTime:{},headBlockNumber:{},headBlockId:{}" ,
198- new DateTime (now ),
199- new DateTime (
200- this .tronApp .getDbManager ().getDynamicPropertiesStore ()
201- .getLatestBlockHeaderTimestamp ()),
202- this .tronApp .getDbManager ().getDynamicPropertiesStore ().getLatestBlockHeaderNumber (),
203- this .tronApp .getDbManager ().getDynamicPropertiesStore ().getLatestBlockHeaderHash ());
204- return BlockProductionCondition .NOT_TIME_YET ;
205- }
206-
207- if (now < controller .getManager ().getDynamicPropertiesStore ().getLatestBlockHeaderTimestamp ()) {
208- logger .warn ("have a timestamp:{} less than or equal to the previous block:{}" ,
209- new DateTime (now ), new DateTime (
210- this .tronApp .getDbManager ().getDynamicPropertiesStore ()
211- .getLatestBlockHeaderTimestamp ()));
212- return BlockProductionCondition .EXCEPTION_PRODUCING_BLOCK ;
213- }
214-
215193 if (!controller .activeWitnessesContain (this .getLocalWitnessStateMap ().keySet ())) {
216194 logger .info ("Unelected. Elected Witnesses: {}" ,
217195 StringUtil .getAddressStringList (controller .getActiveWitnesses ()));
218196 return BlockProductionCondition .UNELECTED ;
219197 }
220198
221- final ByteString scheduledWitness = controller .getScheduledWitness (slot );
222-
223- if (!this .getLocalWitnessStateMap ().containsKey (scheduledWitness )) {
224- logger .info ("It's not my turn, ScheduledWitness[{}],slot[{}],abSlot[{}]," ,
225- ByteArray .toHexString (scheduledWitness .toByteArray ()), slot ,
226- controller .getAbSlotAtTime (now ));
227- return NOT_MY_TURN ;
228- }
199+ try {
229200
230- BlockCapsule block ;
201+ BlockCapsule block ;
231202
232- try {
233203 synchronized (tronApp .getDbManager ()) {
204+ long slot = controller .getSlotAtTime (now );
205+ logger .debug ("Slot:" + slot );
206+ if (slot == 0 ) {
207+ logger .info ("Not time yet,now:{},headBlockTime:{},headBlockNumber:{},headBlockId:{}" ,
208+ new DateTime (now ),
209+ new DateTime (
210+ this .tronApp .getDbManager ().getDynamicPropertiesStore ()
211+ .getLatestBlockHeaderTimestamp ()),
212+ this .tronApp .getDbManager ().getDynamicPropertiesStore ().getLatestBlockHeaderNumber (),
213+ this .tronApp .getDbManager ().getDynamicPropertiesStore ().getLatestBlockHeaderHash ());
214+ return BlockProductionCondition .NOT_TIME_YET ;
215+ }
216+
217+ if (now < controller .getManager ().getDynamicPropertiesStore ()
218+ .getLatestBlockHeaderTimestamp ()) {
219+ logger .warn ("have a timestamp:{} less than or equal to the previous block:{}" ,
220+ new DateTime (now ), new DateTime (
221+ this .tronApp .getDbManager ().getDynamicPropertiesStore ()
222+ .getLatestBlockHeaderTimestamp ()));
223+ return BlockProductionCondition .EXCEPTION_PRODUCING_BLOCK ;
224+ }
225+
226+ final ByteString scheduledWitness = controller .getScheduledWitness (slot );
227+
228+ if (!this .getLocalWitnessStateMap ().containsKey (scheduledWitness )) {
229+ logger .info ("It's not my turn, ScheduledWitness[{}],slot[{}],abSlot[{}]," ,
230+ ByteArray .toHexString (scheduledWitness .toByteArray ()), slot ,
231+ controller .getAbSlotAtTime (now ));
232+ return NOT_MY_TURN ;
233+ }
234+
234235 long scheduledTime = controller .getSlotTime (slot );
235236
236237 if (scheduledTime - now > PRODUCE_TIME_OUT ) {
@@ -247,22 +248,23 @@ private BlockProductionCondition tryProduceBlock() throws InterruptedException {
247248
248249 block = generateBlock (scheduledTime , scheduledWitness ,
249250 controller .lastHeadBlockIsMaintenance ());
251+ }
250252
251- if (block == null ) {
252- logger .warn ("exception when generate block" );
253- return BlockProductionCondition .EXCEPTION_PRODUCING_BLOCK ;
254- }
253+ if (block == null ) {
254+ logger .warn ("exception when generate block" );
255+ return BlockProductionCondition .EXCEPTION_PRODUCING_BLOCK ;
256+ }
255257
256- int blockProducedTimeOut = Args .getInstance ().getBlockProducedTimeOut ();
258+ int blockProducedTimeOut = Args .getInstance ().getBlockProducedTimeOut ();
257259
258- if ( DateTime . now (). getMillis () - now
259- > ChainConstant .BLOCK_PRODUCED_INTERVAL * blockProducedTimeOut / 100 ) {
260- logger . warn ( "Task timeout ( > {}ms),startTime:{},endTime:{}" ,
261- ChainConstant . BLOCK_PRODUCED_INTERVAL * blockProducedTimeOut / 100 ,
262- new DateTime (now ), DateTime . now ());
263- tronApp . getDbManager (). eraseBlock ( );
264- return BlockProductionCondition . TIME_OUT ;
265- }
260+ long timeout = Math
261+ . min ( ChainConstant .BLOCK_PRODUCED_INTERVAL * blockProducedTimeOut / 100 + 500 ,
262+ ChainConstant . BLOCK_PRODUCED_INTERVAL );
263+ if ( DateTime . now (). getMillis () - now > timeout ) {
264+ logger . warn ( "Task timeout ( > {}ms),startTime:{},endTime:{}" , timeout , new DateTime (now ),
265+ DateTime . now () );
266+ tronApp . getDbManager (). eraseBlock () ;
267+ return BlockProductionCondition . TIME_OUT ;
266268 }
267269
268270 logger .info (
@@ -271,6 +273,7 @@ private BlockProductionCondition tryProduceBlock() throws InterruptedException {
271273 block .getTransactions ().size (),
272274 new DateTime (block .getTimeStamp ()),
273275 block .getParentHash ());
276+
274277 broadcastBlock (block );
275278
276279 return BlockProductionCondition .PRODUCED ;
0 commit comments