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