miner.hoon 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /= dk /apps/dumbnet/lib/types
  2. /= sp /common/stark/prover
  3. /= dumb-transact /common/tx-engine
  4. /= * /common/zoon
  5. ::
  6. :: everything to do with mining and mining state
  7. ::
  8. |_ [m=mining-state:dk =blockchain-constants:dumb-transact]
  9. +* t ~(. dumb-transact blockchain-constants)
  10. +| %admin
  11. :: +set-mining: set .mining
  12. ++ set-mining
  13. |= mine=?
  14. ^- mining-state:dk
  15. m(mining mine)
  16. ::
  17. :: +set-pubkey: set .pubkey
  18. ++ set-pubkeys
  19. |= pks=(list lock:t)
  20. ^- mining-state:dk
  21. =. pubkeys.m
  22. (~(gas z-in *(z-set lock:t)) pks)
  23. m
  24. ::
  25. :: +set-shares set .shares
  26. ++ set-shares
  27. |= shr=(list [lock:t @])
  28. =. shares.m
  29. (~(gas z-by *(z-map lock:t @)) shr)
  30. m
  31. ::
  32. +| %candidate-block
  33. ++ set-pow
  34. |= prf=proof:sp
  35. ^- mining-state:dk
  36. m(pow.candidate-block (some prf))
  37. ::
  38. ++ set-digest
  39. ^- mining-state:dk
  40. m(digest.candidate-block (compute-digest:page:t candidate-block.m))
  41. ::
  42. :: +update-timestamp: updates timestamp on candidate block if needed
  43. ::
  44. :: this should be run every time we get a poke.
  45. ++ update-timestamp
  46. |= now=@da
  47. ^- mining-state:dk
  48. ?: |(=(*page:t candidate-block.m) !mining.m)
  49. :: not mining or no candidate block is set so no need to update timestamp
  50. m
  51. ?: %+ gte timestamp.candidate-block.m
  52. (time-in-secs:page:t (sub now update-candidate-timestamp-interval:t))
  53. :: has not been ~m2, so leave timestamp alone
  54. m
  55. =. timestamp.candidate-block.m (time-in-secs:page:t now)
  56. =/ print-var
  57. %- trip
  58. ^- @t
  59. %^ cat 3
  60. 'candidate block timestamp updated: '
  61. (scot %$ timestamp.candidate-block.m)
  62. ~> %slog.[0 [%leaf print-var]]
  63. m
  64. ::
  65. :: +heard-new-tx: potentially changes candidate block in reaction to a raw-tx
  66. ++ heard-new-tx
  67. |= raw=raw-tx:t
  68. ^- mining-state:dk
  69. ?. mining.m m :: if we're not mining, do nothing
  70. ::
  71. :: if the mining pubkey is not set, do nothing
  72. ?: =(*(z-set lock:t) pubkeys.m) m
  73. :: check to see if block is valid with tx - this checks whether the inputs
  74. :: exist, whether the new size will exceed block size, and whether timelocks
  75. :: are valid
  76. =/ tx=(unit tx:t) (mole |.((new:tx:t raw height.candidate-block.m)))
  77. ?~ tx
  78. :: invalid tx. we don't emit a %liar effect from this because it might
  79. :: just not be valid for this particular block
  80. m
  81. =/ new-acc=(unit tx-acc:t)
  82. (process:tx-acc:t candidate-acc.m u.tx height.candidate-block.m)
  83. ?~ new-acc
  84. ::~& >>> """
  85. :: tx {(trip (to-b58:hash:t id.raw))} cannot be added to candidate
  86. :: block.
  87. :: """
  88. m
  89. :: we can add tx to candidate-block
  90. =. tx-ids.candidate-block.m
  91. (~(put z-in tx-ids.candidate-block.m) id.raw)
  92. =/ old-fees=coins:t fees.candidate-acc.m
  93. =. candidate-acc.m u.new-acc
  94. =/ new-fees=coins:t fees.candidate-acc.m
  95. :: check if new-fees != old-fees to determine if split should be recalculated.
  96. :: since we don't have replace-by-fee
  97. ?: =(new-fees old-fees)
  98. :: fees are equal so no need to recalculate split
  99. m
  100. :: fees are unequal. for this miner, fees are only ever monotonically
  101. :: incremented and so this assertion should never fail.
  102. ?> (gth new-fees old-fees)
  103. =/ fee-diff=coins:t (sub new-fees old-fees)
  104. :: compute old emission+fees
  105. =/ old-assets=coins:t
  106. %+ roll ~(val z-by coinbase.candidate-block.m)
  107. |= [c=coins:t sum=coins:t]
  108. (add c sum)
  109. =/ new-assets=coins:t (add old-assets fee-diff)
  110. =. coinbase.candidate-block.m
  111. (new:coinbase-split:t new-assets shares.m)
  112. m
  113. ::
  114. :: +heard-new-block: refreshes the candidate block to be mined in reaction to a new block
  115. ::
  116. :: when we hear a new heaviest block, we need to update the candidate we're attempting
  117. :: to mine. that means we should update the parent and page number of the block, and carry
  118. :: over any transactions we had previously been attempting to include that werent
  119. :: included in the most recent block.
  120. ++ heard-new-block
  121. |= [c=consensus-state:dk p=pending-state:dk now=@da]
  122. ^- mining-state:dk
  123. ?. mining.m m :: if we're not mining, do nothing
  124. ::
  125. :: do a sanity check that we have a heaviest block, and that the heaviest block
  126. :: is not the parent of our current candidate block
  127. ?~ heaviest-block.c
  128. :: genesis block has its own codepath, which is why this conditional does not attempt
  129. :: to generate the genesis block
  130. ~> %slog.[0 leaf+"attempted to generate new candidate block when we have no genesis block"]
  131. m
  132. ?: =(u.heaviest-block.c parent.candidate-block.m)
  133. ~> %slog.[0 leaf+"heaviest block unchanged, do not generate new candidate block"]
  134. m
  135. ?: =(*(z-set lock:t) pubkeys.m)
  136. ~> %slog.[0 leaf+"no pubkey(s) set so no new candidate block will be generated"]
  137. m
  138. =/ print-var
  139. %- trip
  140. ^- @t
  141. %^ cat 3
  142. 'generating new candidate block with parent: '
  143. (to-b58:hash:t u.heaviest-block.c)
  144. ~> %slog.[0 [%leaf print-var]]
  145. =. candidate-block.m
  146. %- new:page:t
  147. :* (to-page:local-page:t (~(got z-by blocks.c) u.heaviest-block.c))
  148. now
  149. (~(got z-by targets.c) u.heaviest-block.c)
  150. shares.m
  151. ==
  152. =. candidate-acc.m
  153. (new:tx-acc:t (~(get z-by balance.c) u.heaviest-block.c))
  154. ::
  155. :: roll over the pending txs and try to include them in the new candidate block
  156. %+ roll ~(val z-by raw-txs.p)
  157. |= [raw=raw-tx:t min=_m]
  158. (heard-new-tx raw)
  159. --