Skip to content

Commit 94ad694

Browse files
committed
Merge branch 'release/1.4'
2 parents fc0638f + 4c69536 commit 94ad694

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

eth/handler.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math"
2323
"math/big"
2424
"sync"
25+
"sync/atomic"
2526
"time"
2627

2728
"github.com/ethereum/go-ethereum/common"
@@ -58,7 +59,7 @@ type blockFetcherFn func([]common.Hash) error
5859
type ProtocolManager struct {
5960
networkId int
6061

61-
fastSync bool
62+
fastSync uint32
6263
txpool txPool
6364
blockchain *core.BlockChain
6465
chaindb ethdb.Database
@@ -87,15 +88,9 @@ type ProtocolManager struct {
8788
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
8889
// with the ethereum network.
8990
func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, mux *event.TypeMux, txpool txPool, pow pow.PoW, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
90-
// Figure out whether to allow fast sync or not
91-
if fastSync && blockchain.CurrentBlock().NumberU64() > 0 {
92-
glog.V(logger.Info).Infof("blockchain not empty, fast sync disabled")
93-
fastSync = false
94-
}
9591
// Create the protocol manager with the base fields
9692
manager := &ProtocolManager{
9793
networkId: networkId,
98-
fastSync: fastSync,
9994
eventMux: mux,
10095
txpool: txpool,
10196
blockchain: blockchain,
@@ -106,6 +101,14 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int,
106101
txsyncCh: make(chan *txsync),
107102
quitSync: make(chan struct{}),
108103
}
104+
// Figure out whether to allow fast sync or not
105+
if fastSync && blockchain.CurrentBlock().NumberU64() > 0 {
106+
glog.V(logger.Info).Infof("blockchain not empty, fast sync disabled")
107+
fastSync = false
108+
}
109+
if fastSync {
110+
manager.fastSync = uint32(1)
111+
}
109112
// Initiate a sub-protocol for every implemented version we can handle
110113
manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions))
111114
for i, version := range ProtocolVersions {
@@ -678,7 +681,11 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
678681
}
679682

680683
case msg.Code == TxMsg:
681-
// Transactions arrived, parse all of them and deliver to the pool
684+
// Transactions arrived, make sure we have a valid chain to handle them
685+
if atomic.LoadUint32(&pm.fastSync) == 1 {
686+
break
687+
}
688+
// Transactions can be processed, parse all of them and deliver to the pool
682689
var txs []*types.Transaction
683690
if err := msg.Decode(&txs); err != nil {
684691
return errResp(ErrDecode, "msg %v: %v", msg, err)

eth/sync.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package eth
1818

1919
import (
2020
"math/rand"
21+
"sync/atomic"
2122
"time"
2223

2324
"github.com/ethereum/go-ethereum/common"
@@ -167,18 +168,18 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
167168
}
168169
// Otherwise try to sync with the downloader
169170
mode := downloader.FullSync
170-
if pm.fastSync {
171+
if atomic.LoadUint32(&pm.fastSync) == 1 {
171172
mode = downloader.FastSync
172173
}
173174
if err := pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), mode); err != nil {
174175
return
175176
}
176177
// If fast sync was enabled, and we synced up, disable it
177-
if pm.fastSync {
178+
if atomic.LoadUint32(&pm.fastSync) == 1 {
178179
// Disable fast sync if we indeed have something in our chain
179180
if pm.blockchain.CurrentBlock().NumberU64() > 0 {
180181
glog.V(logger.Info).Infof("fast sync complete, auto disabling")
181-
pm.fastSync = false
182+
atomic.StoreUint32(&pm.fastSync, 0)
182183
}
183184
}
184185
}

eth/sync_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package eth
1818

1919
import (
20+
"sync/atomic"
2021
"testing"
2122
"time"
2223

@@ -29,12 +30,12 @@ import (
2930
func TestFastSyncDisabling(t *testing.T) {
3031
// Create a pristine protocol manager, check that fast sync is left enabled
3132
pmEmpty := newTestProtocolManagerMust(t, true, 0, nil, nil)
32-
if !pmEmpty.fastSync {
33+
if atomic.LoadUint32(&pmEmpty.fastSync) == 0 {
3334
t.Fatalf("fast sync disabled on pristine blockchain")
3435
}
3536
// Create a full protocol manager, check that fast sync gets disabled
3637
pmFull := newTestProtocolManagerMust(t, true, 1024, nil, nil)
37-
if pmFull.fastSync {
38+
if atomic.LoadUint32(&pmFull.fastSync) == 1 {
3839
t.Fatalf("fast sync not disabled on non-empty blockchain")
3940
}
4041
// Sync up the two peers
@@ -47,7 +48,7 @@ func TestFastSyncDisabling(t *testing.T) {
4748
pmEmpty.synchronise(pmEmpty.peers.BestPeer())
4849

4950
// Check that fast sync was disabled
50-
if pmEmpty.fastSync {
51+
if atomic.LoadUint32(&pmEmpty.fastSync) == 1 {
5152
t.Fatalf("fast sync not disabled after successful synchronisation")
5253
}
5354
}

0 commit comments

Comments
 (0)