github.com/ethereum/go-ethereum@v1.14.3/consensus/misc/dao.go (about)

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