github.com/dim4egster/coreth@v0.10.2/consensus/misc/dao.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2016 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package misc
    28  
    29  import (
    30  	"bytes"
    31  	"errors"
    32  	"math/big"
    33  
    34  	"github.com/dim4egster/coreth/core/state"
    35  	"github.com/dim4egster/coreth/core/types"
    36  	"github.com/dim4egster/coreth/params"
    37  )
    38  
    39  var (
    40  	// ErrBadProDAOExtra is returned if a header doesn't support the DAO fork on a
    41  	// pro-fork client.
    42  	ErrBadProDAOExtra = errors.New("bad DAO pro-fork extra-data")
    43  
    44  	// ErrBadNoDAOExtra is returned if a header does support the DAO fork on a no-
    45  	// fork client.
    46  	ErrBadNoDAOExtra = errors.New("bad DAO no-fork extra-data")
    47  )
    48  
    49  // VerifyDAOHeaderExtraData validates the extra-data field of a block header to
    50  // ensure it conforms to DAO hard-fork rules.
    51  //
    52  // DAO hard-fork extension to the header validity:
    53  //   a) if the node is no-fork, do not accept blocks in the [fork, fork+10) range
    54  //      with the fork specific extra-data set
    55  //   b) if the node is pro-fork, require blocks in the specific range to have the
    56  //      unique extra-data set.
    57  func VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header) error {
    58  	// Short circuit validation if the node doesn't care about the DAO fork
    59  	if config.DAOForkBlock == nil {
    60  		return nil
    61  	}
    62  	// Make sure the block is within the fork's modified extra-data range
    63  	limit := new(big.Int).Add(config.DAOForkBlock, params.DAOForkExtraRange)
    64  	if header.Number.Cmp(config.DAOForkBlock) < 0 || header.Number.Cmp(limit) >= 0 {
    65  		return nil
    66  	}
    67  	// Depending on whether we support or oppose the fork, validate the extra-data contents
    68  	if config.DAOForkSupport {
    69  		if !bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
    70  			return ErrBadProDAOExtra
    71  		}
    72  	} else {
    73  		if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
    74  			return ErrBadNoDAOExtra
    75  		}
    76  	}
    77  	// All ok, header has the same extra-data we expect
    78  	return nil
    79  }
    80  
    81  // ApplyDAOHardFork modifies the state database according to the DAO hard-fork
    82  // rules, transferring all balances of a set of DAO accounts to a single refund
    83  // contract.
    84  func ApplyDAOHardFork(statedb *state.StateDB) {
    85  	// Retrieve the contract to refund balances into
    86  	if !statedb.Exist(params.DAORefundContract) {
    87  		statedb.CreateAccount(params.DAORefundContract)
    88  	}
    89  
    90  	// Move every DAO account and extra-balance account funds into the refund contract
    91  	for _, addr := range params.DAODrainList() {
    92  		statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr))
    93  		statedb.SetBalance(addr, new(big.Int))
    94  	}
    95  }