github.com/pfcoder/quorum@v2.0.3-0.20180501191142-d4a1b0958135+incompatible/consensus/ethash/consensus.go (about) 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 17 package ethash 18 19 import ( 20 "bytes" 21 "errors" 22 "fmt" 23 "math/big" 24 "runtime" 25 "time" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/common/math" 29 "github.com/ethereum/go-ethereum/consensus" 30 "github.com/ethereum/go-ethereum/consensus/misc" 31 "github.com/ethereum/go-ethereum/core/state" 32 "github.com/ethereum/go-ethereum/core/types" 33 "github.com/ethereum/go-ethereum/params" 34 set "gopkg.in/fatih/set.v0" 35 ) 36 37 // Ethash proof-of-work protocol constants. 38 var ( 39 frontierBlockReward *big.Int = big.NewInt(5e+18) // Block reward in wei for successfully mining a block 40 byzantiumBlockReward *big.Int = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium 41 maxUncles = 2 // Maximum number of uncles allowed in a single block 42 43 nanosecond2017Timestamp = mustParseRfc3339("2017-01-01T00:00:00+00:00").UnixNano() 44 ) 45 46 // Various error messages to mark blocks invalid. These should be private to 47 // prevent engine specific errors from being referenced in the remainder of the 48 // codebase, inherently breaking if the engine is swapped out. Please put common 49 // error types into the consensus package. 50 var ( 51 errLargeBlockTime = errors.New("timestamp too big") 52 errZeroBlockTime = errors.New("timestamp equals parent's") 53 errTooManyUncles = errors.New("too many uncles") 54 errDuplicateUncle = errors.New("duplicate uncle") 55 errUncleIsAncestor = errors.New("uncle is ancestor") 56 errDanglingUncle = errors.New("uncle's parent is not ancestor") 57 errNonceOutOfRange = errors.New("nonce out of range") 58 errInvalidDifficulty = errors.New("non-positive difficulty") 59 errInvalidMixDigest = errors.New("invalid mix digest") 60 errInvalidPoW = errors.New("invalid proof-of-work") 61 ) 62 63 func mustParseRfc3339(str string) time.Time { 64 time, err := time.Parse(time.RFC3339, str) 65 if err != nil { 66 panic("unexpected failure to parse rfc3339 timestamp: " + str) 67 } 68 return time 69 } 70 71 // Author implements consensus.Engine, returning the header's coinbase as the 72 // proof-of-work verified author of the block. 73 func (ethash *Ethash) Author(header *types.Header) (common.Address, error) { 74 return header.Coinbase, nil 75 } 76 77 // VerifyHeader checks whether a header conforms to the consensus rules of the 78 // stock Ethereum ethash engine. 79 func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { 80 // If we're running a full engine faking, accept any input as valid 81 if ethash.fakeFull { 82 return nil 83 } 84 // Short circuit if the header is known, or it's parent not 85 number := header.Number.Uint64() 86 if chain.GetHeader(header.Hash(), number) != nil { 87 return nil 88 } 89 parent := chain.GetHeader(header.ParentHash, number-1) 90 if parent == nil { 91 return consensus.ErrUnknownAncestor 92 } 93 // Sanity checks passed, do a proper verification 94 return ethash.verifyHeader(chain, header, parent, false, seal) 95 } 96 97 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers 98 // concurrently. The method returns a quit channel to abort the operations and 99 // a results channel to retrieve the async verifications. 100 func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { 101 // If we're running a full engine faking, accept any input as valid 102 if ethash.fakeFull || len(headers) == 0 { 103 abort, results := make(chan struct{}), make(chan error, len(headers)) 104 for i := 0; i < len(headers); i++ { 105 results <- nil 106 } 107 return abort, results 108 } 109 110 // Spawn as many workers as allowed threads 111 workers := runtime.GOMAXPROCS(0) 112 if len(headers) < workers { 113 workers = len(headers) 114 } 115 116 // Create a task channel and spawn the verifiers 117 var ( 118 inputs = make(chan int) 119 done = make(chan int, workers) 120 errors = make([]error, len(headers)) 121 abort = make(chan struct{}) 122 ) 123 for i := 0; i < workers; i++ { 124 go func() { 125 for index := range inputs { 126 errors[index] = ethash.verifyHeaderWorker(chain, headers, seals, index) 127 done <- index 128 } 129 }() 130 } 131 132 errorsOut := make(chan error, len(headers)) 133 go func() { 134 defer close(inputs) 135 var ( 136 in, out = 0, 0 137 checked = make([]bool, len(headers)) 138 inputs = inputs 139 ) 140 for { 141 select { 142 case inputs <- in: 143 if in++; in == len(headers) { 144 // Reached end of headers. Stop sending to workers. 145 inputs = nil 146 } 147 case index := <-done: 148 for checked[index] = true; checked[out]; out++ { 149 errorsOut <- errors[out] 150 if out == len(headers)-1 { 151 return 152 } 153 } 154 case <-abort: 155 return 156 } 157 } 158 }() 159 return abort, errorsOut 160 } 161 162 func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, headers []*types.Header, seals []bool, index int) error { 163 var parent *types.Header 164 if index == 0 { 165 parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1) 166 } else if headers[index-1].Hash() == headers[index].ParentHash { 167 parent = headers[index-1] 168 } 169 if parent == nil { 170 return consensus.ErrUnknownAncestor 171 } 172 if chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()) != nil { 173 return nil // known block 174 } 175 return ethash.verifyHeader(chain, headers[index], parent, false, seals[index]) 176 } 177 178 // VerifyUncles verifies that the given block's uncles conform to the consensus 179 // rules of the stock Ethereum ethash engine. 180 func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { 181 // If we're running a full engine faking, accept any input as valid 182 if ethash.fakeFull { 183 return nil 184 } 185 // Verify that there are at most 2 uncles included in this block 186 if len(block.Uncles()) > maxUncles { 187 return errTooManyUncles 188 } 189 // Gather the set of past uncles and ancestors 190 uncles, ancestors := set.New(), make(map[common.Hash]*types.Header) 191 192 number, parent := block.NumberU64()-1, block.ParentHash() 193 for i := 0; i < 7; i++ { 194 ancestor := chain.GetBlock(parent, number) 195 if ancestor == nil { 196 break 197 } 198 ancestors[ancestor.Hash()] = ancestor.Header() 199 for _, uncle := range ancestor.Uncles() { 200 uncles.Add(uncle.Hash()) 201 } 202 parent, number = ancestor.ParentHash(), number-1 203 } 204 ancestors[block.Hash()] = block.Header() 205 uncles.Add(block.Hash()) 206 207 // Verify each of the uncles that it's recent, but not an ancestor 208 for _, uncle := range block.Uncles() { 209 // Make sure every uncle is rewarded only once 210 hash := uncle.Hash() 211 if uncles.Has(hash) { 212 return errDuplicateUncle 213 } 214 uncles.Add(hash) 215 216 // Make sure the uncle has a valid ancestry 217 if ancestors[hash] != nil { 218 return errUncleIsAncestor 219 } 220 if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() { 221 return errDanglingUncle 222 } 223 if err := ethash.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, true); err != nil { 224 return err 225 } 226 } 227 return nil 228 } 229 230 // verifyHeader checks whether a header conforms to the consensus rules of the 231 // stock Ethereum ethash engine. 232 // See YP section 4.3.4. "Block Header Validity" 233 func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *types.Header, uncle bool, seal bool) error { 234 // Ensure that the header's extra-data section is of a reasonable size 235 maximumExtraDataSize := params.GetMaximumExtraDataSize(chain.Config().IsQuorum) 236 if uint64(len(header.Extra)) > maximumExtraDataSize { 237 return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), maximumExtraDataSize) 238 } 239 // Verify the header's timestamp 240 if uncle { 241 if header.Time.Cmp(math.MaxBig256) > 0 { 242 return errLargeBlockTime 243 } 244 } else if !chain.Config().IsQuorum { 245 if header.Time.Cmp(big.NewInt(time.Now().Unix())) > 0 { 246 return consensus.ErrFutureBlock 247 } 248 } else { 249 // We disable future checking if we're in --raft mode. This is crucial 250 // because block validation in the raft setting needs to be deterministic. 251 // There is no forking of the chain, and we need each node to only perform 252 // validation as a pure function of block contents with respect to the 253 // previous database state. 254 // 255 // NOTE: whereas we are currently checking whether the timestamp field has 256 // nanosecond semantics to detect --raft mode, we could also use a special 257 // "raft" sentinel in the Extra field, or pass a boolean for raftMode from 258 // all call sites of this function. 259 if raftMode := time.Now().UnixNano() > nanosecond2017Timestamp; !raftMode { 260 if header.Time.Cmp(big.NewInt(time.Now().Unix())) > 0 { 261 return consensus.ErrFutureBlock 262 } 263 } 264 } 265 if header.Time.Cmp(parent.Time) <= 0 { 266 return errZeroBlockTime 267 } 268 // Verify the block's difficulty based in it's timestamp and parent's difficulty 269 expected := CalcDifficulty(chain.Config(), header.Time.Uint64(), parent) 270 if expected.Cmp(header.Difficulty) != 0 { 271 return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, expected) 272 } 273 // Verify that the gas limit is <= 2^63-1 274 if header.GasLimit.Cmp(math.MaxBig63) > 0 { 275 return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, math.MaxBig63) 276 } 277 // Verify that the gasUsed is <= gasLimit 278 if header.GasUsed.Cmp(header.GasLimit) > 0 { 279 return fmt.Errorf("invalid gasUsed: have %v, gasLimit %v", header.GasUsed, header.GasLimit) 280 } 281 282 // Verify that the gas limit remains within allowed bounds 283 diff := new(big.Int).Set(parent.GasLimit) 284 diff = diff.Sub(diff, header.GasLimit) 285 diff.Abs(diff) 286 287 limit := new(big.Int).Set(parent.GasLimit) 288 limit = limit.Div(limit, params.GasLimitBoundDivisor) 289 290 if diff.Cmp(limit) >= 0 || header.GasLimit.Cmp(params.MinGasLimit) < 0 { 291 return fmt.Errorf("invalid gas limit: have %v, want %v += %v", header.GasLimit, parent.GasLimit, limit) 292 } 293 // Verify that the block number is parent's +1 294 if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 { 295 return consensus.ErrInvalidNumber 296 } 297 // Verify the engine specific seal securing the block 298 if seal { 299 if err := ethash.VerifySeal(chain, header); err != nil { 300 return err 301 } 302 } 303 // If all checks passed, validate any special fields for hard forks 304 if err := misc.VerifyDAOHeaderExtraData(chain.Config(), header); err != nil { 305 return err 306 } 307 if err := misc.VerifyForkHashes(chain.Config(), header, uncle); err != nil { 308 return err 309 } 310 return nil 311 } 312 313 // CalcDifficulty is the difficulty adjustment algorithm. It returns 314 // the difficulty that a new block should have when created at time 315 // given the parent block's time and difficulty. 316 // TODO (karalabe): Move the chain maker into this package and make this private! 317 func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int { 318 next := new(big.Int).Add(parent.Number, big1) 319 switch { 320 case config.IsByzantium(next): 321 return calcDifficultyByzantium(time, parent) 322 case config.IsHomestead(next): 323 return calcDifficultyHomestead(time, parent) 324 default: 325 return calcDifficultyFrontier(time, parent) 326 } 327 } 328 329 // Some weird constants to avoid constant memory allocs for them. 330 var ( 331 expDiffPeriod = big.NewInt(100000) 332 big1 = big.NewInt(1) 333 big2 = big.NewInt(2) 334 big9 = big.NewInt(9) 335 big10 = big.NewInt(10) 336 bigMinus99 = big.NewInt(-99) 337 big2999999 = big.NewInt(2999999) 338 ) 339 340 // calcDifficultyByzantium is the difficulty adjustment algorithm. It returns 341 // the difficulty that a new block should have when created at time given the 342 // parent block's time and difficulty. The calculation uses the Byzantium rules. 343 func calcDifficultyByzantium(time uint64, parent *types.Header) *big.Int { 344 // https://github.com/ethereum/EIPs/issues/100. 345 // algorithm: 346 // diff = (parent_diff + 347 // (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99)) 348 // ) + 2^(periodCount - 2) 349 350 bigTime := new(big.Int).SetUint64(time) 351 bigParentTime := new(big.Int).Set(parent.Time) 352 353 // holds intermediate values to make the algo easier to read & audit 354 x := new(big.Int) 355 y := new(big.Int) 356 357 // (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9 358 x.Sub(bigTime, bigParentTime) 359 x.Div(x, big9) 360 if parent.UncleHash == types.EmptyUncleHash { 361 x.Sub(big1, x) 362 } else { 363 x.Sub(big2, x) 364 } 365 // max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99) 366 if x.Cmp(bigMinus99) < 0 { 367 x.Set(bigMinus99) 368 } 369 // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) 370 y.Div(parent.Difficulty, params.DifficultyBoundDivisor) 371 x.Mul(y, x) 372 x.Add(parent.Difficulty, x) 373 374 // minimum difficulty can ever be (before exponential factor) 375 if x.Cmp(params.MinimumDifficulty) < 0 { 376 x.Set(params.MinimumDifficulty) 377 } 378 // calculate a fake block numer for the ice-age delay: 379 // https://github.com/ethereum/EIPs/pull/669 380 // fake_block_number = min(0, block.number - 3_000_000 381 fakeBlockNumber := new(big.Int) 382 if parent.Number.Cmp(big2999999) >= 0 { 383 fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, big2999999) // Note, parent is 1 less than the actual block number 384 } 385 // for the exponential factor 386 periodCount := fakeBlockNumber 387 periodCount.Div(periodCount, expDiffPeriod) 388 389 // the exponential factor, commonly referred to as "the bomb" 390 // diff = diff + 2^(periodCount - 2) 391 if periodCount.Cmp(big1) > 0 { 392 y.Sub(periodCount, big2) 393 y.Exp(big2, y, nil) 394 x.Add(x, y) 395 } 396 return x 397 } 398 399 // calcDifficultyHomestead is the difficulty adjustment algorithm. It returns 400 // the difficulty that a new block should have when created at time given the 401 // parent block's time and difficulty. The calculation uses the Homestead rules. 402 func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int { 403 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.mediawiki 404 // algorithm: 405 // diff = (parent_diff + 406 // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) 407 // ) + 2^(periodCount - 2) 408 409 bigTime := new(big.Int).SetUint64(time) 410 bigParentTime := new(big.Int).Set(parent.Time) 411 412 // holds intermediate values to make the algo easier to read & audit 413 x := new(big.Int) 414 y := new(big.Int) 415 416 // 1 - (block_timestamp - parent_timestamp) // 10 417 x.Sub(bigTime, bigParentTime) 418 x.Div(x, big10) 419 x.Sub(big1, x) 420 421 // max(1 - (block_timestamp - parent_timestamp) // 10, -99) 422 if x.Cmp(bigMinus99) < 0 { 423 x.Set(bigMinus99) 424 } 425 // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) 426 y.Div(parent.Difficulty, params.DifficultyBoundDivisor) 427 x.Mul(y, x) 428 x.Add(parent.Difficulty, x) 429 430 // minimum difficulty can ever be (before exponential factor) 431 if x.Cmp(params.MinimumDifficulty) < 0 { 432 x.Set(params.MinimumDifficulty) 433 } 434 // for the exponential factor 435 periodCount := new(big.Int).Add(parent.Number, big1) 436 periodCount.Div(periodCount, expDiffPeriod) 437 438 // the exponential factor, commonly referred to as "the bomb" 439 // diff = diff + 2^(periodCount - 2) 440 if periodCount.Cmp(big1) > 0 { 441 y.Sub(periodCount, big2) 442 y.Exp(big2, y, nil) 443 x.Add(x, y) 444 } 445 return x 446 } 447 448 // calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the 449 // difficulty that a new block should have when created at time given the parent 450 // block's time and difficulty. The calculation uses the Frontier rules. 451 func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int { 452 diff := new(big.Int) 453 adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor) 454 bigTime := new(big.Int) 455 bigParentTime := new(big.Int) 456 457 bigTime.SetUint64(time) 458 bigParentTime.Set(parent.Time) 459 460 if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 { 461 diff.Add(parent.Difficulty, adjust) 462 } else { 463 diff.Sub(parent.Difficulty, adjust) 464 } 465 if diff.Cmp(params.MinimumDifficulty) < 0 { 466 diff.Set(params.MinimumDifficulty) 467 } 468 469 periodCount := new(big.Int).Add(parent.Number, big1) 470 periodCount.Div(periodCount, expDiffPeriod) 471 if periodCount.Cmp(big1) > 0 { 472 // diff = diff + 2^(periodCount - 2) 473 expDiff := periodCount.Sub(periodCount, big2) 474 expDiff.Exp(big2, expDiff, nil) 475 diff.Add(diff, expDiff) 476 diff = math.BigMax(diff, params.MinimumDifficulty) 477 } 478 return diff 479 } 480 481 // VerifySeal implements consensus.Engine, checking whether the given block satisfies 482 // the PoW difficulty requirements. 483 func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Header) error { 484 isQuorum := chain != nil && chain.Config().IsQuorum 485 486 // If we're running a fake PoW, accept any seal as valid 487 if ethash.fakeMode { 488 time.Sleep(ethash.fakeDelay) 489 if ethash.fakeFail == header.Number.Uint64() { 490 return errInvalidPoW 491 } 492 return nil 493 } 494 // If we're running a shared PoW, delegate verification to it 495 if ethash.shared != nil { 496 return ethash.shared.VerifySeal(chain, header) 497 } 498 // Sanity check that the block number is below the lookup table size (60M blocks) 499 number := header.Number.Uint64() 500 if number/epochLength >= uint64(len(cacheSizes)) { 501 // Go < 1.7 cannot calculate new cache/dataset sizes (no fast prime check) 502 return errNonceOutOfRange 503 } 504 // Ensure that we have a valid difficulty for the block 505 if header.Difficulty.Sign() <= 0 { 506 return errInvalidDifficulty 507 } 508 // Recompute the digest and PoW value and verify against the header 509 cache := ethash.cache(number) 510 511 size := datasetSize(number) 512 if ethash.tester { 513 size = 32 * 1024 514 } 515 digest, result := hashimotoLight(size, cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64()) 516 if !isQuorum && !bytes.Equal(header.MixDigest[:], digest) { 517 return errInvalidMixDigest 518 } 519 target := new(big.Int).Div(maxUint256, header.Difficulty) 520 if new(big.Int).SetBytes(result).Cmp(target) > 0 { 521 if !isQuorum { 522 return errInvalidPoW 523 } 524 } 525 return nil 526 } 527 528 // Prepare implements consensus.Engine, initializing the difficulty field of a 529 // header to conform to the ethash protocol. The changes are done inline. 530 func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error { 531 parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) 532 if parent == nil { 533 return consensus.ErrUnknownAncestor 534 } 535 header.Difficulty = CalcDifficulty(chain.Config(), header.Time.Uint64(), parent) 536 537 return nil 538 } 539 540 // Finalize implements consensus.Engine, accumulating the block and uncle rewards, 541 // setting the final state and assembling the block. 542 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) { 543 // Accumulate any block and uncle rewards and commit the final state root 544 AccumulateRewards(chain.Config(), state, header, uncles) 545 header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) 546 547 // Header seems complete, assemble into a block and return 548 return types.NewBlock(header, txs, uncles, receipts), nil 549 } 550 551 // Some weird constants to avoid constant memory allocs for them. 552 var ( 553 big8 = big.NewInt(8) 554 big32 = big.NewInt(32) 555 ) 556 557 // AccumulateRewards credits the coinbase of the given block with the mining 558 // reward. The total reward consists of the static block reward and rewards for 559 // included uncles. The coinbase of each uncle block is also rewarded. 560 // TODO (karalabe): Move the chain maker into this package and make this private! 561 func AccumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) { 562 // Select the correct block reward based on chain progression 563 blockReward := frontierBlockReward 564 if config.IsByzantium(header.Number) { 565 blockReward = byzantiumBlockReward 566 } 567 // Accumulate the rewards for the miner and any included uncles 568 reward := new(big.Int).Set(blockReward) 569 r := new(big.Int) 570 for _, uncle := range uncles { 571 r.Add(uncle.Number, big8) 572 r.Sub(r, header.Number) 573 r.Mul(r, blockReward) 574 r.Div(r, big8) 575 state.AddBalance(uncle.Coinbase, r) 576 577 r.Div(blockReward, big32) 578 reward.Add(reward, r) 579 } 580 state.AddBalance(header.Coinbase, reward) 581 }