@@ -45,15 +45,15 @@ class MicroBlockMinerImpl(
45
45
account : KeyPair ,
46
46
accumulatedBlock : Block ,
47
47
restTotalConstraint : MiningConstraint ,
48
- lastMicroBlock : Long
48
+ prevMicroBlockTs : Long
49
49
): Task [Unit ] =
50
- generateOneMicroBlockTask(account, accumulatedBlock, restTotalConstraint, lastMicroBlock )
50
+ generateOneMicroBlockTask(account, accumulatedBlock, restTotalConstraint, prevMicroBlockTs )
51
51
.flatMap {
52
52
case res @ Success (newBlock, newConstraint) =>
53
53
Task .defer(generateMicroBlockSequence(account, newBlock, newConstraint, res.nanoTime))
54
54
case Retry =>
55
55
Task
56
- .defer(generateMicroBlockSequence(account, accumulatedBlock, restTotalConstraint, lastMicroBlock ))
56
+ .defer(generateMicroBlockSequence(account, accumulatedBlock, restTotalConstraint, prevMicroBlockTs ))
57
57
.delayExecution(1 second)
58
58
case Stop =>
59
59
setDebugState(MinerDebugInfo .MiningBlocks )
@@ -65,7 +65,7 @@ class MicroBlockMinerImpl(
65
65
account : KeyPair ,
66
66
accumulatedBlock : Block ,
67
67
restTotalConstraint : MiningConstraint ,
68
- lastMicroBlock : Long
68
+ prevMicroBlockTs : Long
69
69
): Task [MicroBlockMiningResult ] = {
70
70
val packTask = Task .cancelable[(Option [Seq [Transaction ]], MiningConstraint , Option [ByteStr ])] { cb =>
71
71
@ volatile var cancelled = false
@@ -93,8 +93,8 @@ class MicroBlockMinerImpl(
93
93
)
94
94
)
95
95
)
96
- log.trace(s " Finished pack for ${accumulatedBlock.id()}" )
97
96
val updatedTotalConstraint = updatedMdConstraint.head
97
+ log.trace(s " Finished pack for ${accumulatedBlock.id()}, updated total constraint: $updatedTotalConstraint" )
98
98
cb.onSuccess((unconfirmed, updatedTotalConstraint, stateHash))
99
99
}
100
100
Task .eval {
@@ -104,24 +104,25 @@ class MicroBlockMinerImpl(
104
104
105
105
packTask.flatMap {
106
106
case (Some (unconfirmed), updatedTotalConstraint, stateHash) if unconfirmed.nonEmpty =>
107
- val delay = {
108
- val delay = System .nanoTime() - lastMicroBlock
109
- val requiredDelay = settings.microBlockInterval.toNanos
110
- if (delay >= requiredDelay) Duration .Zero else (requiredDelay - delay).nanos
111
- }
112
-
113
107
for {
114
- _ <- Task .now(if (delay > Duration .Zero ) log.trace(s " Sleeping ${delay.toMillis} ms before applying microBlock " ))
115
- _ <- Task .sleep(delay)
116
- r <-
117
- if (blockchainUpdater.lastBlockId.forall(_ == accumulatedBlock.id())) {
118
- log.trace(s " Generating microBlock for ${account.toAddress}, constraints: $updatedTotalConstraint" )
119
- appendAndBroadcastMicroBlock(account, accumulatedBlock, unconfirmed, updatedTotalConstraint, stateHash)
120
- } else {
121
- log.trace(s " Stopping generating microBlock for ${account.toAddress}, new key block was appended " )
122
- Task (Stop )
123
- }
124
- } yield r
108
+ blocks <- forgeBlocks(account, accumulatedBlock, unconfirmed, stateHash)
109
+ .leftWiden[Throwable ]
110
+ .liftTo[Task ]
111
+ (signedBlock, microBlock) = blocks
112
+ delay = {
113
+ val delay = System .nanoTime() - prevMicroBlockTs
114
+ val requiredDelay = settings.microBlockInterval.toNanos
115
+ if (delay >= requiredDelay) Duration .Zero else (requiredDelay - delay).nanos
116
+ }
117
+ _ <-
118
+ if (delay > Duration .Zero ) {
119
+ log.trace(s " Sleeping ${delay.toMillis} ms before applying microBlock " )
120
+ Task .sleep(delay)
121
+ } else Task .unit
122
+ _ <- appendMicroBlock(microBlock, account)
123
+ } yield
124
+ if (updatedTotalConstraint.isFull) Stop
125
+ else Success (signedBlock, updatedTotalConstraint)
125
126
126
127
case (_, updatedTotalConstraint, _) =>
127
128
if (updatedTotalConstraint.isFull) {
@@ -139,39 +140,27 @@ class MicroBlockMinerImpl(
139
140
}
140
141
}
141
142
142
- private def appendAndBroadcastMicroBlock (
143
- account : KeyPair ,
144
- block : Block ,
145
- transactions : Seq [Transaction ],
146
- constraint : MiningConstraint ,
147
- stateHash : Option [BlockId ]
148
- ): Task [MicroBlockMiningResult ] =
149
- for {
150
- (signedBlock, microBlock) <- forgeBlocks(account, block, transactions, stateHash).leftWiden[Throwable ].liftTo[Task ]
151
- blockId <- appendMicroBlock(microBlock)
152
- _ = BlockStats .mined(microBlock, blockId)
153
- _ <- broadcastMicroBlock(account, microBlock, blockId)
154
- } yield
155
- if (constraint.isFull) Stop
156
- else Success (signedBlock, constraint)
157
-
158
- private def broadcastMicroBlock (account : KeyPair , microBlock : MicroBlock , blockId : BlockId ): Task [Unit ] =
159
- Task (if (allChannels != null ) allChannels.broadcast(MicroBlockInv (account, blockId, microBlock.reference)))
160
-
161
- private def appendMicroBlock (microBlock : MicroBlock ): Task [BlockId ] =
162
- MicroblockAppender (blockchainUpdater, utx, appenderScheduler)(microBlock, None )
163
- .flatMap {
164
- case Left (err) => Task .raiseError(MicroBlockAppendError (microBlock, err))
165
- case Right (v) => Task .now(v)
166
- }
143
+ private def appendMicroBlock (microBlock : MicroBlock , account : KeyPair ): Task [BlockId ] =
144
+ MicroblockAppender (blockchainUpdater, utx, appenderScheduler)(microBlock, None ).flatMap {
145
+ case Left (err) => Task .raiseError(MicroBlockAppendError (microBlock, err))
146
+ case Right (blockId) =>
147
+ Task .evalAsync {
148
+ BlockStats .mined(microBlock, blockId)
149
+ if (allChannels != null ) {
150
+ allChannels.broadcast(MicroBlockInv (account, blockId, microBlock.reference))
151
+ }
152
+ blockId
153
+ }
154
+ }.uncancelable
167
155
168
156
private def forgeBlocks (
169
157
account : KeyPair ,
170
158
accumulatedBlock : Block ,
171
- unconfirmed : Seq [Transaction ],
159
+ packedTxs : Seq [Transaction ],
172
160
stateHash : Option [ByteStr ]
173
161
): Either [MicroBlockMiningError , (Block , MicroBlock )] =
174
162
microBlockBuildTimeStats.measureSuccessful {
163
+ log.trace(s " Forging microBlock for ${account.toAddress}" )
175
164
for {
176
165
signedBlock <- Block
177
166
.buildAndSign(
@@ -180,7 +169,7 @@ class MicroBlockMinerImpl(
180
169
reference = accumulatedBlock.header.reference,
181
170
baseTarget = accumulatedBlock.header.baseTarget,
182
171
generationSignature = accumulatedBlock.header.generationSignature,
183
- txs = accumulatedBlock.transactionData ++ unconfirmed ,
172
+ txs = accumulatedBlock.transactionData ++ packedTxs ,
184
173
signer = account,
185
174
featureVotes = accumulatedBlock.header.featureVotes,
186
175
rewardVote = accumulatedBlock.header.rewardVote,
@@ -189,7 +178,7 @@ class MicroBlockMinerImpl(
189
178
)
190
179
.leftMap(BlockBuildError )
191
180
microBlock <- MicroBlock
192
- .buildAndSign(signedBlock.header.version, account, unconfirmed , accumulatedBlock.id(), signedBlock.signature, stateHash)
181
+ .buildAndSign(signedBlock.header.version, account, packedTxs , accumulatedBlock.id(), signedBlock.signature, stateHash)
193
182
.leftMap(MicroBlockBuildError )
194
183
} yield (signedBlock, microBlock)
195
184
}
0 commit comments