consensus.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package ethash
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "math/big"
  22. "runtime"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/common/math"
  26. "github.com/ethereum/go-ethereum/consensus"
  27. "github.com/ethereum/go-ethereum/consensus/misc"
  28. "github.com/ethereum/go-ethereum/core/state"
  29. "github.com/ethereum/go-ethereum/core/types"
  30. "github.com/ethereum/go-ethereum/params"
  31. set "gopkg.in/fatih/set.v0"
  32. )
  33. // Ethash proof-of-work protocol constants.
  34. var (
  35. FrontierBlockReward *big.Int = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
  36. ByzantiumBlockReward *big.Int = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
  37. maxUncles = 2 // Maximum number of uncles allowed in a single block
  38. allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
  39. )
  40. // Various error messages to mark blocks invalid. These should be private to
  41. // prevent engine specific errors from being referenced in the remainder of the
  42. // codebase, inherently breaking if the engine is swapped out. Please put common
  43. // error types into the consensus package.
  44. var (
  45. errLargeBlockTime = errors.New("timestamp too big")
  46. errZeroBlockTime = errors.New("timestamp equals parent's")
  47. errTooManyUncles = errors.New("too many uncles")
  48. errDuplicateUncle = errors.New("duplicate uncle")
  49. errUncleIsAncestor = errors.New("uncle is ancestor")
  50. errDanglingUncle = errors.New("uncle's parent is not ancestor")
  51. errInvalidDifficulty = errors.New("non-positive difficulty")
  52. errInvalidMixDigest = errors.New("invalid mix digest")
  53. errInvalidPoW = errors.New("invalid proof-of-work")
  54. )
  55. // Author implements consensus.Engine, returning the header's coinbase as the
  56. // proof-of-work verified author of the block.
  57. func (ethash *Ethash) Author(header *types.Header) (common.Address, error) {
  58. return header.Coinbase, nil
  59. }
  60. // VerifyHeader checks whether a header conforms to the consensus rules of the
  61. // stock Ethereum ethash engine.
  62. func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
  63. // If we're running a full engine faking, accept any input as valid
  64. if ethash.config.PowMode == ModeFullFake {
  65. return nil
  66. }
  67. // Short circuit if the header is known, or it's parent not
  68. number := header.Number.Uint64()
  69. if chain.GetHeader(header.Hash(), number) != nil {
  70. return nil
  71. }
  72. parent := chain.GetHeader(header.ParentHash, number-1)
  73. if parent == nil {
  74. return consensus.ErrUnknownAncestor
  75. }
  76. // Sanity checks passed, do a proper verification
  77. return ethash.verifyHeader(chain, header, parent, false, seal)
  78. }
  79. // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
  80. // concurrently. The method returns a quit channel to abort the operations and
  81. // a results channel to retrieve the async verifications.
  82. func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
  83. // If we're running a full engine faking, accept any input as valid
  84. if ethash.config.PowMode == ModeFullFake || len(headers) == 0 {
  85. abort, results := make(chan struct{}), make(chan error, len(headers))
  86. for i := 0; i < len(headers); i++ {
  87. results <- nil
  88. }
  89. return abort, results
  90. }
  91. // Spawn as many workers as allowed threads
  92. workers := runtime.GOMAXPROCS(0)
  93. if len(headers) < workers {
  94. workers = len(headers)
  95. }
  96. // Create a task channel and spawn the verifiers
  97. var (
  98. inputs = make(chan int)
  99. done = make(chan int, workers)
  100. errors = make([]error, len(headers))
  101. abort = make(chan struct{})
  102. )
  103. for i := 0; i < workers; i++ {
  104. go func() {
  105. for index := range inputs {
  106. errors[index] = ethash.verifyHeaderWorker(chain, headers, seals, index)
  107. done <- index
  108. }
  109. }()
  110. }
  111. errorsOut := make(chan error, len(headers))
  112. go func() {
  113. defer close(inputs)
  114. var (
  115. in, out = 0, 0
  116. checked = make([]bool, len(headers))
  117. inputs = inputs
  118. )
  119. for {
  120. select {
  121. case inputs <- in:
  122. if in++; in == len(headers) {
  123. // Reached end of headers. Stop sending to workers.
  124. inputs = nil
  125. }
  126. case index := <-done:
  127. for checked[index] = true; checked[out]; out++ {
  128. errorsOut <- errors[out]
  129. if out == len(headers)-1 {
  130. return
  131. }
  132. }
  133. case <-abort:
  134. return
  135. }
  136. }
  137. }()
  138. return abort, errorsOut
  139. }
  140. func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, headers []*types.Header, seals []bool, index int) error {
  141. var parent *types.Header
  142. if index == 0 {
  143. parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
  144. } else if headers[index-1].Hash() == headers[index].ParentHash {
  145. parent = headers[index-1]
  146. }
  147. if parent == nil {
  148. return consensus.ErrUnknownAncestor
  149. }
  150. if chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()) != nil {
  151. return nil // known block
  152. }
  153. return ethash.verifyHeader(chain, headers[index], parent, false, seals[index])
  154. }
  155. // VerifyUncles verifies that the given block's uncles conform to the consensus
  156. // rules of the stock Ethereum ethash engine.
  157. func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
  158. // If we're running a full engine faking, accept any input as valid
  159. if ethash.config.PowMode == ModeFullFake {
  160. return nil
  161. }
  162. // Verify that there are at most 2 uncles included in this block
  163. if len(block.Uncles()) > maxUncles {
  164. return errTooManyUncles
  165. }
  166. // Gather the set of past uncles and ancestors
  167. uncles, ancestors := set.New(), make(map[common.Hash]*types.Header)
  168. number, parent := block.NumberU64()-1, block.ParentHash()
  169. for i := 0; i < 7; i++ {
  170. ancestor := chain.GetBlock(parent, number)
  171. if ancestor == nil {
  172. break
  173. }
  174. ancestors[ancestor.Hash()] = ancestor.Header()
  175. for _, uncle := range ancestor.Uncles() {
  176. uncles.Add(uncle.Hash())
  177. }
  178. parent, number = ancestor.ParentHash(), number-1
  179. }
  180. ancestors[block.Hash()] = block.Header()
  181. uncles.Add(block.Hash())
  182. // Verify each of the uncles that it's recent, but not an ancestor
  183. for _, uncle := range block.Uncles() {
  184. // Make sure every uncle is rewarded only once
  185. hash := uncle.Hash()
  186. if uncles.Has(hash) {
  187. return errDuplicateUncle
  188. }
  189. uncles.Add(hash)
  190. // Make sure the uncle has a valid ancestry
  191. if ancestors[hash] != nil {
  192. return errUncleIsAncestor
  193. }
  194. if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() {
  195. return errDanglingUncle
  196. }
  197. if err := ethash.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
  198. return err
  199. }
  200. }
  201. return nil
  202. }
  203. // verifyHeader checks whether a header conforms to the consensus rules of the
  204. // stock Ethereum ethash engine.
  205. // See YP section 4.3.4. "Block Header Validity"
  206. func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *types.Header, uncle bool, seal bool) error {
  207. // Ensure that the header's extra-data section is of a reasonable size
  208. if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
  209. return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
  210. }
  211. // Verify the header's timestamp
  212. if uncle {
  213. if header.Time.Cmp(math.MaxBig256) > 0 {
  214. return errLargeBlockTime
  215. }
  216. } else {
  217. if header.Time.Cmp(big.NewInt(time.Now().Add(allowedFutureBlockTime).Unix())) > 0 {
  218. return consensus.ErrFutureBlock
  219. }
  220. }
  221. if header.Time.Cmp(parent.Time) <= 0 {
  222. return errZeroBlockTime
  223. }
  224. // Verify the block's difficulty based in it's timestamp and parent's difficulty
  225. expected := ethash.CalcDifficulty(chain, header.Time.Uint64(), parent)
  226. if expected.Cmp(header.Difficulty) != 0 {
  227. return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, expected)
  228. }
  229. // Verify that the gas limit is <= 2^63-1
  230. cap := uint64(0x7fffffffffffffff)
  231. if header.GasLimit > cap {
  232. return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, cap)
  233. }
  234. // Verify that the gasUsed is <= gasLimit
  235. if header.GasUsed > header.GasLimit {
  236. return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
  237. }
  238. // Verify that the gas limit remains within allowed bounds
  239. diff := int64(parent.GasLimit) - int64(header.GasLimit)
  240. if diff < 0 {
  241. diff *= -1
  242. }
  243. limit := parent.GasLimit / params.GasLimitBoundDivisor
  244. if uint64(diff) >= limit || header.GasLimit < params.MinGasLimit {
  245. return fmt.Errorf("invalid gas limit: have %d, want %d += %d", header.GasLimit, parent.GasLimit, limit)
  246. }
  247. // Verify that the block number is parent's +1
  248. if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
  249. return consensus.ErrInvalidNumber
  250. }
  251. // Verify the engine specific seal securing the block
  252. if seal {
  253. if err := ethash.VerifySeal(chain, header); err != nil {
  254. return err
  255. }
  256. }
  257. // If all checks passed, validate any special fields for hard forks
  258. if err := misc.VerifyDAOHeaderExtraData(chain.Config(), header); err != nil {
  259. return err
  260. }
  261. if err := misc.VerifyForkHashes(chain.Config(), header, uncle); err != nil {
  262. return err
  263. }
  264. return nil
  265. }
  266. // CalcDifficulty is the difficulty adjustment algorithm. It returns
  267. // the difficulty that a new block should have when created at time
  268. // given the parent block's time and difficulty.
  269. func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
  270. return CalcDifficulty(chain.Config(), time, parent)
  271. }
  272. // CalcDifficulty is the difficulty adjustment algorithm. It returns
  273. // the difficulty that a new block should have when created at time
  274. // given the parent block's time and difficulty.
  275. func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
  276. next := new(big.Int).Add(parent.Number, big1)
  277. switch {
  278. case config.IsByzantium(next):
  279. return calcDifficultyByzantium(time, parent)
  280. case config.IsHomestead(next):
  281. return calcDifficultyHomestead(time, parent)
  282. default:
  283. return calcDifficultyFrontier(time, parent)
  284. }
  285. }
  286. // Some weird constants to avoid constant memory allocs for them.
  287. var (
  288. expDiffPeriod = big.NewInt(100000)
  289. big1 = big.NewInt(1)
  290. big2 = big.NewInt(2)
  291. big9 = big.NewInt(9)
  292. big10 = big.NewInt(10)
  293. bigMinus99 = big.NewInt(-99)
  294. big2999999 = big.NewInt(2999999)
  295. )
  296. // calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
  297. // the difficulty that a new block should have when created at time given the
  298. // parent block's time and difficulty. The calculation uses the Byzantium rules.
  299. func calcDifficultyByzantium(time uint64, parent *types.Header) *big.Int {
  300. // https://github.com/ethereum/EIPs/issues/100.
  301. // algorithm:
  302. // diff = (parent_diff +
  303. // (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
  304. // ) + 2^(periodCount - 2)
  305. bigTime := new(big.Int).SetUint64(time)
  306. bigParentTime := new(big.Int).Set(parent.Time)
  307. // holds intermediate values to make the algo easier to read & audit
  308. x := new(big.Int)
  309. y := new(big.Int)
  310. // (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
  311. x.Sub(bigTime, bigParentTime)
  312. x.Div(x, big9)
  313. if parent.UncleHash == types.EmptyUncleHash {
  314. x.Sub(big1, x)
  315. } else {
  316. x.Sub(big2, x)
  317. }
  318. // max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
  319. if x.Cmp(bigMinus99) < 0 {
  320. x.Set(bigMinus99)
  321. }
  322. // parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
  323. y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
  324. x.Mul(y, x)
  325. x.Add(parent.Difficulty, x)
  326. // minimum difficulty can ever be (before exponential factor)
  327. if x.Cmp(params.MinimumDifficulty) < 0 {
  328. x.Set(params.MinimumDifficulty)
  329. }
  330. // calculate a fake block number for the ice-age delay:
  331. // https://github.com/ethereum/EIPs/pull/669
  332. // fake_block_number = min(0, block.number - 3_000_000
  333. fakeBlockNumber := new(big.Int)
  334. if parent.Number.Cmp(big2999999) >= 0 {
  335. fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, big2999999) // Note, parent is 1 less than the actual block number
  336. }
  337. // for the exponential factor
  338. periodCount := fakeBlockNumber
  339. periodCount.Div(periodCount, expDiffPeriod)
  340. // the exponential factor, commonly referred to as "the bomb"
  341. // diff = diff + 2^(periodCount - 2)
  342. if periodCount.Cmp(big1) > 0 {
  343. y.Sub(periodCount, big2)
  344. y.Exp(big2, y, nil)
  345. x.Add(x, y)
  346. }
  347. return x
  348. }
  349. // calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
  350. // the difficulty that a new block should have when created at time given the
  351. // parent block's time and difficulty. The calculation uses the Homestead rules.
  352. func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
  353. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
  354. // algorithm:
  355. // diff = (parent_diff +
  356. // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
  357. // ) + 2^(periodCount - 2)
  358. bigTime := new(big.Int).SetUint64(time)
  359. bigParentTime := new(big.Int).Set(parent.Time)
  360. // holds intermediate values to make the algo easier to read & audit
  361. x := new(big.Int)
  362. y := new(big.Int)
  363. // 1 - (block_timestamp - parent_timestamp) // 10
  364. x.Sub(bigTime, bigParentTime)
  365. x.Div(x, big10)
  366. x.Sub(big1, x)
  367. // max(1 - (block_timestamp - parent_timestamp) // 10, -99)
  368. if x.Cmp(bigMinus99) < 0 {
  369. x.Set(bigMinus99)
  370. }
  371. // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
  372. y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
  373. x.Mul(y, x)
  374. x.Add(parent.Difficulty, x)
  375. // minimum difficulty can ever be (before exponential factor)
  376. if x.Cmp(params.MinimumDifficulty) < 0 {
  377. x.Set(params.MinimumDifficulty)
  378. }
  379. // for the exponential factor
  380. periodCount := new(big.Int).Add(parent.Number, big1)
  381. periodCount.Div(periodCount, expDiffPeriod)
  382. // the exponential factor, commonly referred to as "the bomb"
  383. // diff = diff + 2^(periodCount - 2)
  384. if periodCount.Cmp(big1) > 0 {
  385. y.Sub(periodCount, big2)
  386. y.Exp(big2, y, nil)
  387. x.Add(x, y)
  388. }
  389. return x
  390. }
  391. // calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the
  392. // difficulty that a new block should have when created at time given the parent
  393. // block's time and difficulty. The calculation uses the Frontier rules.
  394. func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
  395. diff := new(big.Int)
  396. adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor)
  397. bigTime := new(big.Int)
  398. bigParentTime := new(big.Int)
  399. bigTime.SetUint64(time)
  400. bigParentTime.Set(parent.Time)
  401. if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
  402. diff.Add(parent.Difficulty, adjust)
  403. } else {
  404. diff.Sub(parent.Difficulty, adjust)
  405. }
  406. if diff.Cmp(params.MinimumDifficulty) < 0 {
  407. diff.Set(params.MinimumDifficulty)
  408. }
  409. periodCount := new(big.Int).Add(parent.Number, big1)
  410. periodCount.Div(periodCount, expDiffPeriod)
  411. if periodCount.Cmp(big1) > 0 {
  412. // diff = diff + 2^(periodCount - 2)
  413. expDiff := periodCount.Sub(periodCount, big2)
  414. expDiff.Exp(big2, expDiff, nil)
  415. diff.Add(diff, expDiff)
  416. diff = math.BigMax(diff, params.MinimumDifficulty)
  417. }
  418. return diff
  419. }
  420. // VerifySeal implements consensus.Engine, checking whether the given block satisfies
  421. // the PoW difficulty requirements.
  422. func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
  423. // If we're running a fake PoW, accept any seal as valid
  424. if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
  425. time.Sleep(ethash.fakeDelay)
  426. if ethash.fakeFail == header.Number.Uint64() {
  427. return errInvalidPoW
  428. }
  429. return nil
  430. }
  431. // If we're running a shared PoW, delegate verification to it
  432. if ethash.shared != nil {
  433. return ethash.shared.VerifySeal(chain, header)
  434. }
  435. // Ensure that we have a valid difficulty for the block
  436. if header.Difficulty.Sign() <= 0 {
  437. return errInvalidDifficulty
  438. }
  439. // Recompute the digest and PoW value and verify against the header
  440. number := header.Number.Uint64()
  441. cache := ethash.cache(number)
  442. size := datasetSize(number)
  443. if ethash.config.PowMode == ModeTest {
  444. size = 32 * 1024
  445. }
  446. digest, result := hashimotoLight(size, cache.cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64())
  447. // Caches are unmapped in a finalizer. Ensure that the cache stays live
  448. // until after the call to hashimotoLight so it's not unmapped while being used.
  449. runtime.KeepAlive(cache)
  450. if !bytes.Equal(header.MixDigest[:], digest) {
  451. return errInvalidMixDigest
  452. }
  453. target := new(big.Int).Div(maxUint256, header.Difficulty)
  454. if new(big.Int).SetBytes(result).Cmp(target) > 0 {
  455. return errInvalidPoW
  456. }
  457. return nil
  458. }
  459. // Prepare implements consensus.Engine, initializing the difficulty field of a
  460. // header to conform to the ethash protocol. The changes are done inline.
  461. func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error {
  462. parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
  463. if parent == nil {
  464. return consensus.ErrUnknownAncestor
  465. }
  466. header.Difficulty = ethash.CalcDifficulty(chain, header.Time.Uint64(), parent)
  467. return nil
  468. }
  469. // Finalize implements consensus.Engine, accumulating the block and uncle rewards,
  470. // setting the final state and assembling the block.
  471. func (ethash *Ethash) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
  472. // Accumulate any block and uncle rewards and commit the final state root
  473. accumulateRewards(chain.Config(), state, header, uncles)
  474. header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
  475. // Header seems complete, assemble into a block and return
  476. return types.NewBlock(header, txs, uncles, receipts), nil
  477. }
  478. // Some weird constants to avoid constant memory allocs for them.
  479. var (
  480. big8 = big.NewInt(8)
  481. big32 = big.NewInt(32)
  482. )
  483. // AccumulateRewards credits the coinbase of the given block with the mining
  484. // reward. The total reward consists of the static block reward and rewards for
  485. // included uncles. The coinbase of each uncle block is also rewarded.
  486. func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
  487. // Select the correct block reward based on chain progression
  488. blockReward := FrontierBlockReward
  489. if config.IsByzantium(header.Number) {
  490. blockReward = ByzantiumBlockReward
  491. }
  492. // Accumulate the rewards for the miner and any included uncles
  493. reward := new(big.Int).Set(blockReward)
  494. r := new(big.Int)
  495. for _, uncle := range uncles {
  496. r.Add(uncle.Number, big8)
  497. r.Sub(r, header.Number)
  498. r.Mul(r, blockReward)
  499. r.Div(r, big8)
  500. state.AddBalance(uncle.Coinbase, r)
  501. r.Div(blockReward, big32)
  502. reward.Add(reward, r)
  503. }
  504. state.AddBalance(header.Coinbase, reward)
  505. }