Skip to content

Commit 1b2941c

Browse files
committed
[release/1.4.10] cmd, core, eth, miner, params, tests: finalize the DAO fork
(cherry picked from commit 2c2e389)
1 parent b8c0883 commit 1b2941c

File tree

12 files changed

+7934
-829
lines changed

12 files changed

+7934
-829
lines changed

cmd/geth/dao_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var daoGenesisForkBlock = big.NewInt(314)
8383
// Tests that the DAO hard-fork number and the nodes support/opposition is correctly
8484
// set in the database after various initialization procedures and invocations.
8585
func TestDAODefaultMainnet(t *testing.T) {
86-
testDAOForkBlockNewChain(t, false, "", [][2]bool{{false, false}}, params.MainNetDAOForkBlock, false)
86+
testDAOForkBlockNewChain(t, false, "", [][2]bool{{false, false}}, params.MainNetDAOForkBlock, true)
8787
}
8888
func TestDAOSupportMainnet(t *testing.T) {
8989
testDAOForkBlockNewChain(t, false, "", [][2]bool{{true, false}}, params.MainNetDAOForkBlock, true)
@@ -98,7 +98,7 @@ func TestDAOSwitchToOpposeMainnet(t *testing.T) {
9898
testDAOForkBlockNewChain(t, false, "", [][2]bool{{true, false}, {false, true}}, params.MainNetDAOForkBlock, false)
9999
}
100100
func TestDAODefaultTestnet(t *testing.T) {
101-
testDAOForkBlockNewChain(t, true, "", [][2]bool{{false, false}}, params.TestNetDAOForkBlock, false)
101+
testDAOForkBlockNewChain(t, true, "", [][2]bool{{false, false}}, params.TestNetDAOForkBlock, true)
102102
}
103103
func TestDAOSupportTestnet(t *testing.T) {
104104
testDAOForkBlockNewChain(t, true, "", [][2]bool{{true, false}}, params.TestNetDAOForkBlock, true)
@@ -116,7 +116,7 @@ func TestDAOInitOldPrivnet(t *testing.T) {
116116
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{}, nil, false)
117117
}
118118
func TestDAODefaultOldPrivnet(t *testing.T) {
119-
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, false}}, params.MainNetDAOForkBlock, false)
119+
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, false}}, params.MainNetDAOForkBlock, true)
120120
}
121121
func TestDAOSupportOldPrivnet(t *testing.T) {
122122
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{true, false}}, params.MainNetDAOForkBlock, true)

cmd/utils/flags.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,17 +807,18 @@ func MustMakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainC
807807
// Set any missing fields due to them being unset or system upgrade
808808
if config.HomesteadBlock == nil {
809809
if ctx.GlobalBool(TestNetFlag.Name) {
810-
config.HomesteadBlock = new(big.Int).Set(params.TestNetHomesteadBlock)
810+
config.HomesteadBlock = params.TestNetHomesteadBlock
811811
} else {
812-
config.HomesteadBlock = new(big.Int).Set(params.MainNetHomesteadBlock)
812+
config.HomesteadBlock = params.MainNetHomesteadBlock
813813
}
814814
}
815815
if config.DAOForkBlock == nil {
816816
if ctx.GlobalBool(TestNetFlag.Name) {
817-
config.DAOForkBlock = new(big.Int).Set(params.TestNetDAOForkBlock)
817+
config.DAOForkBlock = params.TestNetDAOForkBlock
818818
} else {
819-
config.DAOForkBlock = new(big.Int).Set(params.MainNetDAOForkBlock)
819+
config.DAOForkBlock = params.MainNetDAOForkBlock
820820
}
821+
config.DAOForkSupport = true
821822
}
822823
// Force override any existing configs if explicitly requested
823824
switch {

core/block_validator.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package core
1818

1919
import (
20-
"bytes"
2120
"fmt"
2221
"math/big"
2322
"time"
@@ -249,33 +248,7 @@ func ValidateHeader(config *ChainConfig, pow pow.PoW, header *types.Header, pare
249248
}
250249
}
251250
// If all checks passed, validate the extra-data field for hard forks
252-
return ValidateHeaderExtraData(config, header)
253-
}
254-
255-
// ValidateHeaderExtraData validates the extra-data field of a block header to
256-
// ensure it conforms to hard-fork rules.
257-
func ValidateHeaderExtraData(config *ChainConfig, header *types.Header) error {
258-
// DAO hard-fork extension to the header validity: a) if the node is no-fork,
259-
// do not accept blocks in the [fork, fork+10) range with the fork specific
260-
// extra-data set; b) if the node is pro-fork, require blocks in the specific
261-
// range to have the unique extra-data set.
262-
if daoBlock := config.DAOForkBlock; daoBlock != nil {
263-
// Check whether the block is among the fork extra-override range
264-
limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
265-
if daoBlock.Cmp(header.Number) <= 0 && header.Number.Cmp(limit) < 0 {
266-
// Depending whether we support or oppose the fork, verrift the extra-data contents
267-
if config.DAOForkSupport {
268-
if bytes.Compare(header.Extra, params.DAOForkBlockExtra) != 0 {
269-
return ValidationError("DAO pro-fork bad block extra-data: 0x%x", header.Extra)
270-
}
271-
} else {
272-
if bytes.Compare(header.Extra, params.DAOForkBlockExtra) == 0 {
273-
return ValidationError("DAO no-fork bad block extra-data: 0x%x", header.Extra)
274-
}
275-
}
276-
}
277-
}
278-
return nil
251+
return ValidateDAOHeaderExtraData(config, header)
279252
}
280253

281254
// CalcDifficulty is the difficulty adjustment algorithm. It returns

core/dao.go

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

core/state_processor.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/ethereum/go-ethereum/crypto"
2626
"github.com/ethereum/go-ethereum/logger"
2727
"github.com/ethereum/go-ethereum/logger/glog"
28-
"github.com/ethereum/go-ethereum/params"
2928
)
3029

3130
var (
@@ -134,19 +133,3 @@ func AccumulateRewards(statedb *state.StateDB, header *types.Header, uncles []*t
134133
}
135134
statedb.AddBalance(header.Coinbase, reward)
136135
}
137-
138-
// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
139-
// rules, transferring all balances of a set of DAO accounts to a single refund
140-
// contract.
141-
func ApplyDAOHardFork(statedb *state.StateDB) {
142-
// Retrieve the contract to refund balances into
143-
refund := statedb.GetOrNewStateObject(params.DAORefundContract)
144-
145-
// Move every DAO account and extra-balance account funds into the refund contract
146-
for _, addr := range params.DAODrainList {
147-
if account := statedb.GetStateObject(addr); account != nil {
148-
refund.AddBalance(account.Balance())
149-
account.SetBalance(new(big.Int))
150-
}
151-
}
152-
}

eth/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
511511
}
512512
// If we're seemingly on the same chain, disable the drop timer
513513
if verifyDAO {
514-
glog.V(logger.Info).Infof("%v: seems to be on the same side of the DAO fork", p)
514+
glog.V(logger.Debug).Infof("%v: seems to be on the same side of the DAO fork", p)
515515
p.forkDrop.Stop()
516516
p.forkDrop = nil
517517
return nil
@@ -527,11 +527,11 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
527527
p.forkDrop = nil
528528

529529
// Validate the header and either drop the peer or continue
530-
if err := core.ValidateHeaderExtraData(pm.chainconfig, headers[0]); err != nil {
531-
glog.V(logger.Info).Infof("%v: verified to be on the other side of the DAO fork, dropping", p)
530+
if err := core.ValidateDAOHeaderExtraData(pm.chainconfig, headers[0]); err != nil {
531+
glog.V(logger.Debug).Infof("%v: verified to be on the other side of the DAO fork, dropping", p)
532532
return err
533533
}
534-
glog.V(logger.Info).Infof("%v: verified to be on the same side of the DAO fork", p)
534+
glog.V(logger.Debug).Infof("%v: verified to be on the same side of the DAO fork", p)
535535
}
536536
// Irrelevant of the fork checks, send the header to the fetcher just in case
537537
headers = pm.fetcher.FilterHeaders(headers, time.Now())

miner/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func (self *worker) commitNewWork() {
474474
if daoBlock := self.config.DAOForkBlock; daoBlock != nil {
475475
// Check whether the block is among the fork extra-override range
476476
limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
477-
if daoBlock.Cmp(header.Number) <= 0 && header.Number.Cmp(limit) < 0 {
477+
if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
478478
// Depending whether we support or oppose the fork, override differently
479479
if self.config.DAOForkSupport {
480480
header.Extra = common.CopyBytes(params.DAOForkBlockExtra)

0 commit comments

Comments
 (0)