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