diff options
Diffstat (limited to 'eth')
-rw-r--r-- | eth/backend.go | 78 | ||||
-rw-r--r-- | eth/downloader/downloader.go | 42 | ||||
-rw-r--r-- | eth/downloader/downloader_test.go | 84 | ||||
-rw-r--r-- | eth/downloader/events.go | 10 | ||||
-rw-r--r-- | eth/downloader/peer.go | 10 | ||||
-rw-r--r-- | eth/downloader/queue.go | 10 | ||||
-rw-r--r-- | eth/fetcher/fetcher.go | 10 | ||||
-rw-r--r-- | eth/fetcher/fetcher_test.go | 13 | ||||
-rw-r--r-- | eth/fetcher/metrics.go | 10 | ||||
-rw-r--r-- | eth/gasprice.go | 10 | ||||
-rw-r--r-- | eth/handler.go | 11 | ||||
-rw-r--r-- | eth/metrics.go | 10 | ||||
-rw-r--r-- | eth/peer.go | 10 | ||||
-rw-r--r-- | eth/protocol.go | 12 | ||||
-rw-r--r-- | eth/protocol_test.go | 19 | ||||
-rw-r--r-- | eth/sync.go | 12 |
16 files changed, 204 insertions, 147 deletions
diff --git a/eth/backend.go b/eth/backend.go index 9c661ad54..4795000e0 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -1,18 +1,18 @@ // Copyright 2014 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // Package eth implements the Ethereum protocol. package eth @@ -45,6 +45,7 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/nat" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/whisper" ) @@ -75,9 +76,13 @@ type Config struct { Name string NetworkId int GenesisNonce int + GenesisFile string + GenesisBlock *types.Block // used by block tests + Olympic bool BlockChainVersion int SkipBcVersionCheck bool // e.g. blockchain export + DatabaseCache int DataDir string LogFile string @@ -259,7 +264,7 @@ func New(config *Config) (*Ethereum, error) { newdb := config.NewDB if newdb == nil { - newdb = func(path string) (common.Database, error) { return ethdb.NewLDBDatabase(path) } + newdb = func(path string) (common.Database, error) { return ethdb.NewLDBDatabase(path, config.DatabaseCache) } } blockDb, err := newdb(filepath.Join(config.DataDir, "blockchain")) if err != nil { @@ -283,22 +288,34 @@ func New(config *Config) (*Ethereum, error) { db.Meter("eth/db/extra/") } nodeDb := filepath.Join(config.DataDir, "nodes") + glog.V(logger.Info).Infof("Protocol Versions: %v, Network Id: %v", ProtocolVersions, config.NetworkId) - // Perform database sanity checks - /* - // The databases were previously tied to protocol versions. Currently we - // are moving away from this decision as approaching Frontier. The below - // check was left in for now but should eventually be just dropped. - - d, _ := blockDb.Get([]byte("ProtocolVersion")) - protov := int(common.NewValue(d).Uint()) - if protov != config.ProtocolVersion && protov != 0 { - path := filepath.Join(config.DataDir, "blockchain") - return nil, fmt.Errorf("Database version mismatch. Protocol(%d / %d). `rm -rf %s`", protov, config.ProtocolVersion, path) + if len(config.GenesisFile) > 0 { + fr, err := os.Open(config.GenesisFile) + if err != nil { + return nil, err } - saveProtocolVersion(blockDb, config.ProtocolVersion) - */ - glog.V(logger.Info).Infof("Protocol Versions: %v, Network Id: %v", ProtocolVersions, config.NetworkId) + + block, err := core.WriteGenesisBlock(stateDb, blockDb, fr) + if err != nil { + return nil, err + } + glog.V(logger.Info).Infof("Successfully wrote genesis block. New genesis hash = %x\n", block.Hash()) + } + + if config.Olympic { + _, err := core.WriteTestNetGenesisBlock(stateDb, blockDb, 42) + if err != nil { + return nil, err + } + glog.V(logger.Error).Infoln("Starting Olympic network") + } + + // This is for testing only. + if config.GenesisBlock != nil { + core.WriteBlock(blockDb, config.GenesisBlock) + core.WriteHead(blockDb, config.GenesisBlock) + } if !config.SkipBcVersionCheck { b, _ := blockDb.Get([]byte("BlockchainVersion")) @@ -344,9 +361,13 @@ func New(config *Config) (*Ethereum, error) { } else { eth.pow = ethash.New() } - genesis := core.GenesisBlock(uint64(config.GenesisNonce), stateDb) - eth.chainManager, err = core.NewChainManager(genesis, blockDb, stateDb, extraDb, eth.pow, eth.EventMux()) + //genesis := core.GenesisBlock(uint64(config.GenesisNonce), stateDb) + eth.chainManager, err = core.NewChainManager(blockDb, stateDb, extraDb, eth.pow, eth.EventMux()) if err != nil { + if err == core.ErrNoGenesis { + return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`) + } + return nil, err } eth.txPool = core.NewTxPool(eth.EventMux(), eth.chainManager.State, eth.chainManager.GasLimit) @@ -357,6 +378,13 @@ func New(config *Config) (*Ethereum, error) { eth.miner = miner.New(eth, eth.EventMux(), eth.pow) eth.miner.SetGasPrice(config.GasPrice) + + extra := config.Name + if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() { + extra = extra[:params.MaximumExtraDataSize.Uint64()] + } + eth.miner.SetExtra([]byte(extra)) + if config.Shh { eth.whisper = whisper.New() eth.shhVersionId = int(eth.whisper.Version()) @@ -470,7 +498,11 @@ func (s *Ethereum) StartMining(threads int) error { func (s *Ethereum) Etherbase() (eb common.Address, err error) { eb = s.etherbase if (eb == common.Address{}) { - err = fmt.Errorf("etherbase address must be explicitly specified") + addr, e := s.AccountManager().AddressByIndex(0) + if e != nil { + err = fmt.Errorf("etherbase address must be explicitly specified") + } + eb = common.HexToAddress(addr) } return } diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 7cf83ada3..e3e22a784 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // Package downloader contains the manual full chain synchronisation. package downloader @@ -21,6 +21,7 @@ import ( "bytes" "errors" "math" + "math/big" "math/rand" "sync" "sync/atomic" @@ -232,10 +233,10 @@ func (d *Downloader) UnregisterPeer(id string) error { // Synchronise tries to sync up our local block chain with a remote peer, both // adding various sanity checks as well as wrapping it with various log entries. -func (d *Downloader) Synchronise(id string, head common.Hash) { - glog.V(logger.Detail).Infof("Attempting synchronisation: %v, 0x%x", id, head) +func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int) { + glog.V(logger.Detail).Infof("Attempting synchronisation: %v, head 0x%x, TD %v", id, head[:4], td) - switch err := d.synchronise(id, head); err { + switch err := d.synchronise(id, head, td); err { case nil: glog.V(logger.Detail).Infof("Synchronisation completed") @@ -257,7 +258,7 @@ func (d *Downloader) Synchronise(id string, head common.Hash) { // synchronise will select the peer and use it for synchronising. If an empty string is given // it will use the best peer possible and synchronize if it's TD is higher than our own. If any of the // checks fail an error will be returned. This method is synchronous -func (d *Downloader) synchronise(id string, hash common.Hash) error { +func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int) error { // Mock out the synchonisation if testing if d.synchroniseMock != nil { return d.synchroniseMock(id, hash) @@ -295,7 +296,7 @@ func (d *Downloader) synchronise(id string, hash common.Hash) error { if p == nil { return errUnknownPeer } - return d.syncWithPeer(p, hash) + return d.syncWithPeer(p, hash, td) } // Has checks if the downloader knows about a particular hash, meaning that its @@ -306,7 +307,7 @@ func (d *Downloader) Has(hash common.Hash) bool { // syncWithPeer starts a block synchronization based on the hash chain from the // specified peer and head hash. -func (d *Downloader) syncWithPeer(p *peer, hash common.Hash) (err error) { +func (d *Downloader) syncWithPeer(p *peer, hash common.Hash, td *big.Int) (err error) { d.mux.Post(StartEvent{}) defer func() { // reset on error @@ -335,7 +336,7 @@ func (d *Downloader) syncWithPeer(p *peer, hash common.Hash) (err error) { return err } errc := make(chan error, 2) - go func() { errc <- d.fetchHashes(p, number+1) }() + go func() { errc <- d.fetchHashes(p, td, number+1) }() go func() { errc <- d.fetchBlocks(number + 1) }() // If any fetcher fails, cancel the other @@ -788,7 +789,7 @@ func (d *Downloader) findAncestor(p *peer) (uint64, error) { // fetchHashes keeps retrieving hashes from the requested number, until no more // are returned, potentially throttling on the way. -func (d *Downloader) fetchHashes(p *peer, from uint64) error { +func (d *Downloader) fetchHashes(p *peer, td *big.Int, from uint64) error { glog.V(logger.Debug).Infof("%v: downloading hashes from #%d", p, from) // Create a timeout timer, and the associated hash fetcher @@ -827,8 +828,19 @@ func (d *Downloader) fetchHashes(p *peer, from uint64) error { case d.processCh <- false: case <-d.cancelCh: } - // Error out if no hashes were retrieved at all - if !gotHashes { + // If no hashes were retrieved at all, the peer violated it's TD promise that it had a + // better chain compared to ours. The only exception is if it's promised blocks were + // already imported by other means (e.g. fecher): + // + // R <remote peer>, L <local node>: Both at block 10 + // R: Mine block 11, and propagate it to L + // L: Queue block 11 for import + // L: Notice that R's head and TD increased compared to ours, start sync + // L: Import of block 11 finishes + // L: Sync begins, and finds common ancestor at 11 + // L: Request new hashes up from 11 (R's TD was higher, it must have something) + // R: Nothing to give + if !gotHashes && td.Cmp(d.headBlock().Td) > 0 { return errStallingPeer } return nil diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index cf1d0c2fb..61fc7827b 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package downloader @@ -97,8 +97,18 @@ func newTester() *downloadTester { } // sync starts synchronizing with a remote peer, blocking until it completes. -func (dl *downloadTester) sync(id string) error { - err := dl.downloader.synchronise(id, dl.peerHashes[id][0]) +func (dl *downloadTester) sync(id string, td *big.Int) error { + hash := dl.peerHashes[id][0] + + // If no particular TD was requested, load from the peer's blockchain + if td == nil { + td = big.NewInt(1) + if block, ok := dl.peerBlocks[id][hash]; ok { + td = block.Td + } + } + err := dl.downloader.synchronise(id, hash, td) + for { // If the queue is empty and processing stopped, break hashes, blocks := dl.downloader.queue.Size() @@ -261,7 +271,7 @@ func TestSynchronisation60(t *testing.T) { tester.newPeer("peer", eth60, hashes, blocks) // Synchronise with the peer and make sure all blocks were retrieved - if err := tester.sync("peer"); err != nil { + if err := tester.sync("peer", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if imported := len(tester.ownBlocks); imported != targetBlocks+1 { @@ -281,7 +291,7 @@ func TestCanonicalSynchronisation61(t *testing.T) { tester.newPeer("peer", eth61, hashes, blocks) // Synchronise with the peer and make sure all blocks were retrieved - if err := tester.sync("peer"); err != nil { + if err := tester.sync("peer", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if imported := len(tester.ownBlocks); imported != targetBlocks+1 { @@ -312,7 +322,7 @@ func testThrottling(t *testing.T, protocol int) { // Start a synchronisation concurrently errc := make(chan error) go func() { - errc <- tester.sync("peer") + errc <- tester.sync("peer", nil) }() // Iteratively take some blocks, always checking the retrieval count for len(tester.ownBlocks) < targetBlocks+1 { @@ -361,14 +371,14 @@ func TestForkedSynchronisation61(t *testing.T) { tester.newPeer("fork B", eth61, hashesB, blocksB) // Synchronise with the peer and make sure all blocks were retrieved - if err := tester.sync("fork A"); err != nil { + if err := tester.sync("fork A", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if imported := len(tester.ownBlocks); imported != common+fork+1 { t.Fatalf("synchronised block mismatch: have %v, want %v", imported, common+fork+1) } // Synchronise with the second peer and make sure that fork is pulled too - if err := tester.sync("fork B"); err != nil { + if err := tester.sync("fork B", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if imported := len(tester.ownBlocks); imported != common+2*fork+1 { @@ -411,7 +421,7 @@ func testCancel(t *testing.T, protocol int) { t.Errorf("block or hash count mismatch: %d hashes, %d blocks, want 0", hashCount, blockCount) } // Synchronise with the peer, but cancel afterwards - if err := tester.sync("peer"); err != nil { + if err := tester.sync("peer", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } tester.downloader.cancel() @@ -438,14 +448,14 @@ func testMultiSynchronisation(t *testing.T, protocol int) { } // Synchronise with the middle peer and make sure half of the blocks were retrieved id := fmt.Sprintf("peer #%d", targetPeers/2) - if err := tester.sync(id); err != nil { + if err := tester.sync(id, nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if imported := len(tester.ownBlocks); imported != len(tester.peerHashes[id]) { t.Fatalf("synchronised block mismatch: have %v, want %v", imported, len(tester.peerHashes[id])) } // Synchronise with the best peer and make sure everything is retrieved - if err := tester.sync("peer #0"); err != nil { + if err := tester.sync("peer #0", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if imported := len(tester.ownBlocks); imported != targetBlocks+1 { @@ -469,7 +479,7 @@ func TestSlowSynchronisation60(t *testing.T) { // Try to sync with the peers (pull hashes from fast) start := time.Now() - if err := tester.sync("fast"); err != nil { + if err := tester.sync("fast", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if imported := len(tester.ownBlocks); imported != targetBlocks+1 { @@ -497,14 +507,14 @@ func TestNonExistingParentAttack60(t *testing.T) { tester.newPeer("attack", eth60, hashes, blocks) // Try and sync with the malicious node and check that it fails - if err := tester.sync("attack"); err == nil { + if err := tester.sync("attack", nil); err == nil { t.Fatalf("block synchronization succeeded") } if tester.hasBlock(hashes[0]) { t.Fatalf("tester accepted unknown-parent block: %v", blocks[hashes[0]]) } // Try to synchronize with the valid chain and make sure it succeeds - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if !tester.hasBlock(tester.peerHashes["valid"][0]) { @@ -525,7 +535,7 @@ func TestRepeatingHashAttack60(t *testing.T) { // TODO: Is this thing valid?? // Try and sync with the malicious node errc := make(chan error) go func() { - errc <- tester.sync("attack") + errc <- tester.sync("attack", nil) }() // Make sure that syncing returns and does so with a failure select { @@ -537,7 +547,7 @@ func TestRepeatingHashAttack60(t *testing.T) { // TODO: Is this thing valid?? } } // Ensure that a valid chain can still pass sync - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } } @@ -555,11 +565,11 @@ func TestNonExistingBlockAttack60(t *testing.T) { tester.newPeer("attack", eth60, hashes, blocks) // Try and sync with the malicious node and check that it fails - if err := tester.sync("attack"); err != errPeersUnavailable { + if err := tester.sync("attack", nil); err != errPeersUnavailable { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errPeersUnavailable) } // Ensure that a valid chain can still pass sync - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } } @@ -583,11 +593,11 @@ func TestInvalidHashOrderAttack60(t *testing.T) { tester.newPeer("attack", eth60, hashes, blocks) // Try and sync with the malicious node and check that it fails - if err := tester.sync("attack"); err != errInvalidChain { + if err := tester.sync("attack", nil); err != errInvalidChain { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errInvalidChain) } // Ensure that a valid chain can still pass sync - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } } @@ -611,11 +621,11 @@ func TestMadeupHashChainAttack60(t *testing.T) { tester.newPeer("attack", eth60, randomHashes, nil) // Try and sync with the malicious node and check that it fails - if err := tester.sync("attack"); err != errCrossCheckFailed { + if err := tester.sync("attack", nil); err != errCrossCheckFailed { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errCrossCheckFailed) } // Ensure that a valid chain can still pass sync - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } } @@ -636,7 +646,7 @@ func TestMadeupHashChainDrippingAttack60(t *testing.T) { // Try and sync with the attacker, one hash at a time tester.maxHashFetch = 1 tester.newPeer("attack", eth60, randomHashes, nil) - if err := tester.sync("attack"); err != errStallingPeer { + if err := tester.sync("attack", nil); err != errStallingPeer { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errStallingPeer) } } @@ -659,7 +669,7 @@ func TestMadeupBlockChainAttack60(t *testing.T) { // Try and sync with the malicious node and check that it fails tester := newTester() tester.newPeer("attack", eth60, gapped, blocks) - if err := tester.sync("attack"); err != errCrossCheckFailed { + if err := tester.sync("attack", nil); err != errCrossCheckFailed { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errCrossCheckFailed) } // Ensure that a valid chain can still pass sync @@ -667,7 +677,7 @@ func TestMadeupBlockChainAttack60(t *testing.T) { crossCheckCycle = defaultCrossCheckCycle tester.newPeer("valid", eth60, hashes, blocks) - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } } @@ -690,7 +700,7 @@ func TestBannedChainStarvationAttack60(t *testing.T) { // the head of the invalid chain is blocked too. for banned := tester.downloader.banned.Size(); ; { // Try to sync with the attacker, check hash chain failure - if err := tester.sync("attack"); err != errInvalidChain { + if err := tester.sync("attack", nil); err != errInvalidChain { if tester.downloader.banned.Has(forkHashes[0]) && err == errBannedHead { break } @@ -711,7 +721,7 @@ func TestBannedChainStarvationAttack60(t *testing.T) { t.Fatalf("banned attacker registered: %v", peer) } // Ensure that a valid chain can still pass sync - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } } @@ -743,7 +753,7 @@ func TestBannedChainMemoryExhaustionAttack60(t *testing.T) { // the head of the invalid chain is blocked too. for { // Try to sync with the attacker, check hash chain failure - if err := tester.sync("attack"); err != errInvalidChain { + if err := tester.sync("attack", nil); err != errInvalidChain { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errInvalidChain) } // Short circuit if the entire chain was banned. @@ -754,7 +764,7 @@ func TestBannedChainMemoryExhaustionAttack60(t *testing.T) { if bans := tester.downloader.banned.Size(); bans > maxBannedHashes { t.Fatalf("ban cap exceeded: have %v, want max %v", bans, maxBannedHashes) } - for hash, _ := range core.BadHashes { + for hash := range core.BadHashes { if !tester.downloader.banned.Has(hash) { t.Fatalf("hard coded ban evacuated: %x", hash) } @@ -764,7 +774,7 @@ func TestBannedChainMemoryExhaustionAttack60(t *testing.T) { MaxBlockFetch = defaultMaxBlockFetch maxBannedHashes = defaultMaxBannedHashes - if err := tester.sync("valid"); err != nil { + if err := tester.sync("valid", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } } @@ -790,7 +800,7 @@ func TestOverlappingDeliveryAttack60(t *testing.T) { return rawGetBlocks(append(request, hashes[0])) } // Test that synchronisation can complete, check for import success - if err := tester.sync("attack"); err != nil { + if err := tester.sync("attack", nil); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } start := time.Now() @@ -807,7 +817,7 @@ func TestOverlappingDeliveryAttack60(t *testing.T) { func TestHighTDStarvationAttack61(t *testing.T) { tester := newTester() tester.newPeer("attack", eth61, []common.Hash{genesis.Hash()}, nil) - if err := tester.sync("attack"); err != errStallingPeer { + if err := tester.sync("attack", big.NewInt(1000000)); err != errStallingPeer { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errStallingPeer) } } @@ -849,7 +859,7 @@ func TestHashAttackerDropping(t *testing.T) { // Simulate a synchronisation and check the required result tester.downloader.synchroniseMock = func(string, common.Hash) error { return tt.result } - tester.downloader.Synchronise(id, genesis.Hash()) + tester.downloader.Synchronise(id, genesis.Hash(), big.NewInt(1000)) if _, ok := tester.peerHashes[id]; !ok != tt.drop { t.Errorf("test %d: peer drop mismatch for %v: have %v, want %v", i, tt.result, !ok, tt.drop) } diff --git a/eth/downloader/events.go b/eth/downloader/events.go index e5c62e121..64905b8f2 100644 --- a/eth/downloader/events.go +++ b/eth/downloader/events.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package downloader diff --git a/eth/downloader/peer.go b/eth/downloader/peer.go index 89b40d1ac..4273b9168 100644 --- a/eth/downloader/peer.go +++ b/eth/downloader/peer.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // Contains the active peer-set of the downloader, maintaining both failures // as well as reputation metrics to prioritize the block retrievals. diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index a758410a5..96e08e144 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // Contains the block download scheduler to collect download tasks and schedule // them in an ordered, and throttled way. diff --git a/eth/fetcher/fetcher.go b/eth/fetcher/fetcher.go index 376cf6f6f..55b6c5c1c 100644 --- a/eth/fetcher/fetcher.go +++ b/eth/fetcher/fetcher.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // Package fetcher contains the block announcement based synchonisation. package fetcher diff --git a/eth/fetcher/fetcher_test.go b/eth/fetcher/fetcher_test.go index 5050cb742..ecbb3f868 100644 --- a/eth/fetcher/fetcher_test.go +++ b/eth/fetcher/fetcher_test.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package fetcher @@ -28,12 +28,13 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/params" ) var ( testdb, _ = ethdb.NewMemDatabase() genesis = core.GenesisBlockForTesting(testdb, common.Address{}, big.NewInt(0)) - unknownBlock = types.NewBlock(&types.Header{}, nil, nil, nil) + unknownBlock = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, nil, nil, nil) ) // makeChain creates a chain of n blocks starting at and including parent. diff --git a/eth/fetcher/metrics.go b/eth/fetcher/metrics.go index 93f328bc9..76cc49226 100644 --- a/eth/fetcher/metrics.go +++ b/eth/fetcher/metrics.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // Contains the metrics collected by the fetcher. diff --git a/eth/gasprice.go b/eth/gasprice.go index fbb7fec3f..031098e9a 100644 --- a/eth/gasprice.go +++ b/eth/gasprice.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package eth diff --git a/eth/handler.go b/eth/handler.go index 50e2ac99b..2bd369901 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package eth @@ -95,6 +95,7 @@ func NewProtocolManager(networkId int, mux *event.TypeMux, txpool txPool, pow po newPeerCh: make(chan *peer, 1), txsyncCh: make(chan *txsync), quitSync: make(chan struct{}), + netId: networkId, } // Initiate a sub-protocol for every implemented version we can handle manager.SubProtocols = make([]p2p.Protocol, len(ProtocolVersions)) diff --git a/eth/metrics.go b/eth/metrics.go index 132172cb5..625b90b67 100644 --- a/eth/metrics.go +++ b/eth/metrics.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package eth diff --git a/eth/peer.go b/eth/peer.go index d70ad4c94..ade1f37ea 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package eth diff --git a/eth/protocol.go b/eth/protocol.go index 704a637e2..b8c9b50d0 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -1,18 +1,18 @@ // Copyright 2014 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package eth @@ -30,7 +30,7 @@ var ProtocolVersions = []uint{61, 60} var ProtocolLengths = []uint64{9, 8} const ( - NetworkId = 0 + NetworkId = 1 ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message ) diff --git a/eth/protocol_test.go b/eth/protocol_test.go index 686380b40..a24d98f69 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -1,18 +1,18 @@ // Copyright 2014 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package eth @@ -60,7 +60,7 @@ func TestStatusMsgErrors(t *testing.T) { }, { code: StatusMsg, data: statusData{uint32(ProtocolVersions[0]), 999, td, currentBlock, genesis}, - wantError: errResp(ErrNetworkIdMismatch, "999 (!= 0)"), + wantError: errResp(ErrNetworkIdMismatch, "999 (!= 1)"), }, { code: StatusMsg, data: statusData{uint32(ProtocolVersions[0]), NetworkId, td, currentBlock, common.Hash{3}}, @@ -178,12 +178,13 @@ type testPeer struct { } func newProtocolManagerForTesting(txAdded chan<- []*types.Transaction) *ProtocolManager { + db, _ := ethdb.NewMemDatabase() + core.WriteTestNetGenesisBlock(db, db, 0) var ( em = new(event.TypeMux) - db, _ = ethdb.NewMemDatabase() - chain, _ = core.NewChainManager(core.GenesisBlock(0, db), db, db, db, core.FakePow{}, em) + chain, _ = core.NewChainManager(db, db, db, core.FakePow{}, em) txpool = &fakeTxPool{added: txAdded} - pm = NewProtocolManager(0, em, txpool, core.FakePow{}, chain) + pm = NewProtocolManager(NetworkId, em, txpool, core.FakePow{}, chain) ) pm.Start() return pm diff --git a/eth/sync.go b/eth/sync.go index 11e229af6..b4dea4b0f 100644 --- a/eth/sync.go +++ b/eth/sync.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // -// go-ethereum is distributed in the hope that it will be useful, +// The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. package eth @@ -164,5 +164,5 @@ func (pm *ProtocolManager) synchronise(peer *peer) { return } // Otherwise try to sync with the downloader - pm.downloader.Synchronise(peer.id, peer.Head()) + pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td()) } |