Skip to content

Commit

Permalink
reordering code before reworking forming transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Jan 14, 2025
1 parent e34a181 commit 5773c17
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions src/main/scala/org/ergoplatform/mining/CandidateGenerator.scala
Original file line number Diff line number Diff line change
@@ -460,8 +460,7 @@ object CandidateGenerator extends ScorexLogging {

// Make progress in time since last block.
// If no progress is made, then, by consensus rules, the block will be rejected.
val timestamp =
Math.max(System.currentTimeMillis(), bestHeaderOpt.map(_.timestamp + 1).getOrElse(0L))
val timestamp = Math.max(System.currentTimeMillis(), bestHeaderOpt.map(_.timestamp + 1).getOrElse(0L))

// Calculate required difficulty for the new block, the same diff for subblock
val nBits: Long = if (continueInputBlock) {
@@ -480,9 +479,15 @@ object CandidateGenerator extends ScorexLogging {
val updInterlinks = popowAlgos.updateInterlinks(bestHeaderOpt, bestExtensionOpt)
val interlinksExtension = popowAlgos.interlinksToExtension(updInterlinks)

val votingSettings = ergoSettings.chainSettings.voting
// todo: cache votes and version for a header, do not recalculate it each block
/*
* Calculate extension candidate without input-block specific fields, votes, and block version
*/

val (preExtensionCandidate, votes: Array[Byte], version: Byte) = bestHeaderOpt
.map { header =>
val votingSettings = ergoSettings.chainSettings.voting

val newHeight = header.height + 1
val currentParams = stateContext.currentParameters
val voteForSoftFork = forkOrdered(ergoSettings, currentParams, header)
@@ -519,33 +524,9 @@ object CandidateGenerator extends ScorexLogging {
(interlinksExtension, Array(0: Byte, 0: Byte, 0: Byte), Header.InitialVersion)
)

// digest (Merkle tree root) of new first-class transactions since last input-block
val inputBlockTransactionsDigest = (InputBlockTransactionsDigestKey, Array.emptyByteArray) // todo: real bytes

// digest (Merkle tree root) of new first-class transactions since last input-block
val previousInputBlocksTransactions = (PreviousInputBlockTransactionsDigestKey, Array.emptyByteArray) // todo: real bytes

// reference to a last seen input block
val prevInputBlockId = parentInputBlockIdOpt.map { prevInputBlockId =>
(PrevInputBlockIdKey, prevInputBlockId)
}.toSeq

val inputBlockFields = ExtensionCandidate(
prevInputBlockId ++ Seq(inputBlockTransactionsDigest, previousInputBlocksTransactions)
)

val extensionCandidate = preExtensionCandidate ++ inputBlockFields

val upcomingContext = state.stateContext.upcoming(
minerPk.value,
timestamp,
nBits,
votes,
proposedUpdate,
version
)

val emissionTxs = emissionTxOpt.toSeq
/*
* Forming transactions to get included
*/

// todo: could be removed after 5.0, but we still slowly decreasing it for starters
// we allow for some gap, to avoid possible problems when different interpreter version can estimate cost
@@ -558,23 +539,55 @@ object CandidateGenerator extends ScorexLogging {
500000
}

val upcomingContext = state.stateContext.upcoming(
minerPk.value,
timestamp,
nBits,
votes,
proposedUpdate,
version
)

val transactionCandidates = emissionTxOpt.toSeq ++ prioritizedTransactions ++ poolTxs.map(_.transaction)

val (txs, toEliminate) = collectTxs(
minerPk,
state.stateContext.currentParameters.maxBlockCost - safeGap,
state.stateContext.currentParameters.maxBlockSize,
state,
upcomingContext,
emissionTxs ++ prioritizedTransactions ++ poolTxs.map(_.transaction)
transactionCandidates
)

val eliminateTransactions = EliminateTransactions(toEliminate)

if (txs.isEmpty) {
throw new IllegalArgumentException(
s"Proofs for 0 txs cannot be generated : emissionTxs: ${emissionTxs.size}, priorityTxs: ${prioritizedTransactions.size}, poolTxs: ${poolTxs.size}"
s"Proofs for 0 txs cannot be generated : emissionTx: ${emissionTxOpt.isDefined}, priorityTxs: ${prioritizedTransactions.size}, poolTxs: ${poolTxs.size}"
)
}

/*
* Put input block related fields into extension section of block candidate
*/

// digest (Merkle tree root) of new first-class transactions since last input-block
val inputBlockTransactionsDigest = (InputBlockTransactionsDigestKey, Array.emptyByteArray) // todo: real bytes

// digest (Merkle tree root) of new first-class transactions since last input-block
val previousInputBlocksTransactions = (PreviousInputBlockTransactionsDigestKey, Array.emptyByteArray) // todo: real bytes

// reference to a last seen input block
val prevInputBlockId = parentInputBlockIdOpt.map { prevInputBlockId =>
(PrevInputBlockIdKey, prevInputBlockId)
}.toSeq

val inputBlockFields = ExtensionCandidate(
prevInputBlockId ++ Seq(inputBlockTransactionsDigest, previousInputBlocksTransactions)
)

val extensionCandidate = preExtensionCandidate ++ inputBlockFields

def deriveWorkMessage(block: CandidateBlock) = {
ergoSettings.chainSettings.powScheme.deriveExternalCandidate(
block,

0 comments on commit 5773c17

Please sign in to comment.