consensus.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. )
  39. // Various error messages to mark blocks invalid. These should be private to
  40. // prevent engine specific errors from being referenced in the remainder of the
  41. // codebase, inherently breaking if the engine is swapped out. Please put common
  42. // error types into the consensus package.
  43. var (
  44. errLargeBlockTime = errors.New("timestamp too big")
  45. errZeroBlockTime = errors.New("timestamp equals parent's")
  46. errTooManyUncles = errors.New("too many uncles")
  47. errDuplicateUncle = errors.New("duplicate uncle")
  48. errUncleIsAncestor = errors.New("uncle is ancestor")
  49. errDanglingUncle = errors.New("uncle's parent is not ancestor")
  50. errNonceOutOfRange = errors.New("nonce out of range")
  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.fakeFull {
  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.fakeFull || 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.fakeFull {
  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().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 := CalcDifficulty(chain.Config(), 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. if header.GasLimit.Cmp(math.MaxBig63) > 0 {
  231. return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, math.MaxBig63)
  232. }
  233. // Verify that the gasUsed is <= gasLimit
  234. if header.GasUsed.Cmp(header.GasLimit) > 0 {
  235. return fmt.Errorf("invalid gasUsed: have %v, gasLimit %v", header.GasUsed, header.GasLimit)
  236. }
  237. // Verify that the gas limit remains within allowed bounds
  238. diff := new(big.Int).Set(parent.GasLimit)
  239. diff = diff.Sub(diff, header.GasLimit)
  240. diff.Abs(diff)
  241. limit := new(big.Int).Set(parent.GasLimit)
  242. limit = limit.Div(limit, params.GasLimitBoundDivisor)
  243. if diff.Cmp(limit) >= 0 || header.GasLimit.Cmp(params.MinGasLimit) < 0 {
  244. return fmt.Errorf("invalid gas limit: have %v, want %v += %v", header.GasLimit, parent.GasLimit, limit)
  245. }
  246. // Verify that the block number is parent's +1
  247. if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
  248. return consensus.ErrInvalidNumber
  249. }
  250. // Verify the engine specific seal securing the block
  251. if seal {
  252. if err := ethash.VerifySeal(chain, header); err != nil {
  253. return err
  254. }
  255. }
  256. // If all checks passed, validate any special fields for hard forks
  257. if err := misc.VerifyDAOHeaderExtraData(chain.Config(), header); err != nil {
  258. return err
  259. }
  260. if err := misc.VerifyForkHashes(chain.Config(), header, uncle); err != nil {
  261. return err
  262. }
  263. return nil
  264. }
  265. // CalcDifficulty is the difficulty adjustment algorithm. It returns
  266. // the difficulty that a new block should have when created at time
  267. // given the parent block's time and difficulty.
  268. // TODO (karalabe): Move the chain maker into this package and make this private!
  269. func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
  270. next := new(big.Int).Add(parent.Number, big1)
  271. switch {
  272. case config.IsByzantium(next):
  273. return calcDifficultyByzantium(time, parent)
  274. case config.IsHomestead(next):
  275. return calcDifficultyHomestead(time, parent)
  276. default:
  277. return calcDifficultyFrontier(time, parent)
  278. }
  279. }
  280. // Some weird constants to avoid constant memory allocs for them.
  281. var (
  282. expDiffPeriod = big.NewInt(100000)
  283. big1 = big.NewInt(1)
  284. big2 = big.NewInt(2)
  285. big9 = big.NewInt(9)
  286. big10 = big.NewInt(10)
  287. bigMinus99 = big.NewInt(-99)
  288. big2999999 = big.NewInt(2999999)
  289. )
  290. // calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
  291. // the difficulty that a new block should have when created at time given the
  292. // parent block's time and difficulty. The calculation uses the Byzantium rules.
  293. func calcDifficultyByzantium(time uint64, parent *types.Header) *big.Int {
  294. // https://github.com/ethereum/EIPs/issues/100.
  295. // algorithm:
  296. // diff = (parent_diff +
  297. // (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
  298. // ) + 2^(periodCount - 2)
  299. bigTime := new(big.Int).SetUint64(time)
  300. bigParentTime := new(big.Int).Set(parent.Time)
  301. // holds intermediate values to make the algo easier to read & audit
  302. x := new(big.Int)
  303. y := new(big.Int)
  304. // (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
  305. x.Sub(bigTime, bigParentTime)
  306. x.Div(x, big9)
  307. if parent.UncleHash == types.EmptyUncleHash {
  308. x.Sub(big1, x)
  309. } else {
  310. x.Sub(big2, x)
  311. }
  312. // max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
  313. if x.Cmp(bigMinus99) < 0 {
  314. x.Set(bigMinus99)
  315. }
  316. // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
  317. y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
  318. x.Mul(y, x)
  319. x.Add(parent.Difficulty, x)
  320. // minimum difficulty can ever be (before exponential factor)
  321. if x.Cmp(params.MinimumDifficulty) < 0 {
  322. x.Set(params.MinimumDifficulty)
  323. }
  324. // calculate a fake block numer for the ice-age delay:
  325. // https://github.com/ethereum/EIPs/pull/669
  326. // fake_block_number = min(0, block.number - 3_000_000
  327. fakeBlockNumber := new(big.Int)
  328. if parent.Number.Cmp(big2999999) >= 0 {
  329. fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, big2999999) // Note, parent is 1 less than the actual block number
  330. }
  331. // for the exponential factor
  332. periodCount := fakeBlockNumber
  333. periodCount.Div(periodCount, expDiffPeriod)
  334. // the exponential factor, commonly referred to as "the bomb"
  335. // diff = diff + 2^(periodCount - 2)
  336. if periodCount.Cmp(big1) > 0 {
  337. y.Sub(periodCount, big2)
  338. y.Exp(big2, y, nil)
  339. x.Add(x, y)
  340. }
  341. return x
  342. }
  343. // calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
  344. // the difficulty that a new block should have when created at time given the
  345. // parent block's time and difficulty. The calculation uses the Homestead rules.
  346. func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
  347. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.mediawiki
  348. // algorithm:
  349. // diff = (parent_diff +
  350. // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
  351. // ) + 2^(periodCount - 2)
  352. bigTime := new(big.Int).SetUint64(time)
  353. bigParentTime := new(big.Int).Set(parent.Time)
  354. // holds intermediate values to make the algo easier to read & audit
  355. x := new(big.Int)
  356. y := new(big.Int)
  357. // 1 - (block_timestamp - parent_timestamp) // 10
  358. x.Sub(bigTime, bigParentTime)
  359. x.Div(x, big10)
  360. x.Sub(big1, x)
  361. // max(1 - (block_timestamp - parent_timestamp) // 10, -99)
  362. if x.Cmp(bigMinus99) < 0 {
  363. x.Set(bigMinus99)
  364. }
  365. // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
  366. y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
  367. x.Mul(y, x)
  368. x.Add(parent.Difficulty, x)
  369. // minimum difficulty can ever be (before exponential factor)
  370. if x.Cmp(params.MinimumDifficulty) < 0 {
  371. x.Set(params.MinimumDifficulty)
  372. }
  373. // for the exponential factor
  374. periodCount := new(big.Int).Add(parent.Number, big1)
  375. periodCount.Div(periodCount, expDiffPeriod)
  376. // the exponential factor, commonly referred to as "the bomb"
  377. // diff = diff + 2^(periodCount - 2)
  378. if periodCount.Cmp(big1) > 0 {
  379. y.Sub(periodCount, big2)
  380. y.Exp(big2, y, nil)
  381. x.Add(x, y)
  382. }
  383. return x
  384. }
  385. // calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the
  386. // difficulty that a new block should have when created at time given the parent
  387. // block's time and difficulty. The calculation uses the Frontier rules.
  388. func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
  389. diff := new(big.Int)
  390. adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor)
  391. bigTime := new(big.Int)
  392. bigParentTime := new(big.Int)
  393. bigTime.SetUint64(time)
  394. bigParentTime.Set(parent.Time)
  395. if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
  396. diff.Add(parent.Difficulty, adjust)
  397. } else {
  398. diff.Sub(parent.Difficulty, adjust)
  399. }
  400. if diff.Cmp(params.MinimumDifficulty) < 0 {
  401. diff.Set(params.MinimumDifficulty)
  402. }
  403. periodCount := new(big.Int).Add(parent.Number, big1)
  404. periodCount.Div(periodCount, expDiffPeriod)
  405. if periodCount.Cmp(big1) > 0 {
  406. // diff = diff + 2^(periodCount - 2)
  407. expDiff := periodCount.Sub(periodCount, big2)
  408. expDiff.Exp(big2, expDiff, nil)
  409. diff.Add(diff, expDiff)
  410. diff = math.BigMax(diff, params.MinimumDifficulty)
  411. }
  412. return diff
  413. }
  414. // VerifySeal implements consensus.Engine, checking whether the given block satisfies
  415. // the PoW difficulty requirements.
  416. func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
  417. // If we're running a fake PoW, accept any seal as valid
  418. if ethash.fakeMode {
  419. time.Sleep(ethash.fakeDelay)
  420. if ethash.fakeFail == header.Number.Uint64() {
  421. return errInvalidPoW
  422. }
  423. return nil
  424. }
  425. // If we're running a shared PoW, delegate verification to it
  426. if ethash.shared != nil {
  427. return ethash.shared.VerifySeal(chain, header)
  428. }
  429. // Sanity check that the block number is below the lookup table size (60M blocks)
  430. number := header.Number.Uint64()
  431. if number/epochLength >= uint64(len(cacheSizes)) {
  432. // Go < 1.7 cannot calculate new cache/dataset sizes (no fast prime check)
  433. return errNonceOutOfRange
  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. cache := ethash.cache(number)
  441. size := datasetSize(number)
  442. if ethash.tester {
  443. size = 32 * 1024
  444. }
  445. digest, result := hashimotoLight(size, cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64())
  446. if !bytes.Equal(header.MixDigest[:], digest) {
  447. return errInvalidMixDigest
  448. }
  449. target := new(big.Int).Div(maxUint256, header.Difficulty)
  450. if new(big.Int).SetBytes(result).Cmp(target) > 0 {
  451. return errInvalidPoW
  452. }
  453. return nil
  454. }
  455. // Prepare implements consensus.Engine, initializing the difficulty field of a
  456. // header to conform to the ethash protocol. The changes are done inline.
  457. func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error {
  458. parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
  459. if parent == nil {
  460. return consensus.ErrUnknownAncestor
  461. }
  462. header.Difficulty = CalcDifficulty(chain.Config(), header.Time.Uint64(), parent)
  463. return nil
  464. }
  465. // Finalize implements consensus.Engine, accumulating the block and uncle rewards,
  466. // setting the final state and assembling the block.
  467. 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) {
  468. // Accumulate any block and uncle rewards and commit the final state root
  469. AccumulateRewards(chain.Config(), state, header, uncles)
  470. header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
  471. // Header seems complete, assemble into a block and return
  472. return types.NewBlock(header, txs, uncles, receipts), nil
  473. }
  474. // Some weird constants to avoid constant memory allocs for them.
  475. var (
  476. big8 = big.NewInt(8)
  477. big32 = big.NewInt(32)
  478. )
  479. // AccumulateRewards credits the coinbase of the given block with the mining
  480. // reward. The total reward consists of the static block reward and rewards for
  481. // included uncles. The coinbase of each uncle block is also rewarded.
  482. // TODO (karalabe): Move the chain maker into this package and make this private!
  483. func AccumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
  484. // Select the correct block reward based on chain progression
  485. blockReward := frontierBlockReward
  486. if config.IsByzantium(header.Number) {
  487. blockReward = byzantiumBlockReward
  488. }
  489. // Accumulate the rewards for the miner and any included uncles
  490. reward := new(big.Int).Set(blockReward)
  491. r := new(big.Int)
  492. for _, uncle := range uncles {
  493. r.Add(uncle.Number, big8)
  494. r.Sub(r, header.Number)
  495. r.Mul(r, blockReward)
  496. r.Div(r, big8)
  497. state.AddBalance(uncle.Coinbase, r)
  498. r.Div(blockReward, big32)
  499. reward.Add(reward, r)
  500. }
  501. state.AddBalance(header.Coinbase, reward)
  502. }