dao.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2016 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 misc
  17. import (
  18. "bytes"
  19. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/core/state"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/params"
  24. )
  25. var (
  26. // ErrBadProDAOExtra is returned if a header doesn't support the DAO fork on a
  27. // pro-fork client.
  28. ErrBadProDAOExtra = errors.New("bad DAO pro-fork extra-data")
  29. // ErrBadNoDAOExtra is returned if a header does support the DAO fork on a no-
  30. // fork client.
  31. ErrBadNoDAOExtra = errors.New("bad DAO no-fork extra-data")
  32. )
  33. // VerifyDAOHeaderExtraData validates the extra-data field of a block header to
  34. // ensure it conforms to DAO hard-fork rules.
  35. //
  36. // DAO hard-fork extension to the header validity:
  37. // a) if the node is no-fork, do not accept blocks in the [fork, fork+10) range
  38. // with the fork specific extra-data set
  39. // b) if the node is pro-fork, require blocks in the specific range to have the
  40. // unique extra-data set.
  41. func VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header) error {
  42. // Short circuit validation if the node doesn't care about the DAO fork
  43. if config.DAOForkBlock == nil {
  44. return nil
  45. }
  46. // Make sure the block is within the fork's modified extra-data range
  47. limit := new(big.Int).Add(config.DAOForkBlock, params.DAOForkExtraRange)
  48. if header.Number.Cmp(config.DAOForkBlock) < 0 || header.Number.Cmp(limit) >= 0 {
  49. return nil
  50. }
  51. // Depending on whether we support or oppose the fork, validate the extra-data contents
  52. if config.DAOForkSupport {
  53. if !bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
  54. return ErrBadProDAOExtra
  55. }
  56. } else {
  57. if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
  58. return ErrBadNoDAOExtra
  59. }
  60. }
  61. // All ok, header has the same extra-data we expect
  62. return nil
  63. }
  64. // ApplyDAOHardFork modifies the state database according to the DAO hard-fork
  65. // rules, transferring all balances of a set of DAO accounts to a single refund
  66. // contract.
  67. func ApplyDAOHardFork(statedb *state.StateDB) {
  68. // Retrieve the contract to refund balances into
  69. if !statedb.Exist(params.DAORefundContract) {
  70. statedb.CreateAccount(params.DAORefundContract)
  71. }
  72. // Move every DAO account and extra-balance account funds into the refund contract
  73. for _, addr := range params.DAODrainList() {
  74. statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr))
  75. statedb.SetBalance(addr, new(big.Int))
  76. }
  77. }