github.com/amazechain/amc@v0.1.3/internal/consensus/misc/dao.go (about)

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