2020-09-10 20:27:42 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Package miner implements Ethereum block creation and mining.
|
|
|
|
package miner
|
|
|
|
|
|
|
|
import (
|
2022-01-24 10:19:52 +03:00
|
|
|
"errors"
|
2023-03-02 09:29:15 +03:00
|
|
|
"math/big"
|
2020-09-10 20:27:42 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-10-20 11:58:26 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/clique"
|
2020-09-10 20:27:42 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2022-10-24 16:13:55 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2023-06-16 15:29:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
|
2020-09-10 20:27:42 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2023-07-06 11:42:34 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-09-10 20:27:42 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-06-16 15:29:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-09-10 20:27:42 +03:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2024-02-13 16:49:53 +03:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
2020-09-10 20:27:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockBackend struct {
|
|
|
|
bc *core.BlockChain
|
2022-10-24 16:13:55 +03:00
|
|
|
txPool *txpool.TxPool
|
2020-09-10 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func NewMockBackend(bc *core.BlockChain, txPool *txpool.TxPool) *mockBackend {
|
2020-09-10 20:27:42 +03:00
|
|
|
return &mockBackend{
|
|
|
|
bc: bc,
|
|
|
|
txPool: txPool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockBackend) BlockChain() *core.BlockChain {
|
|
|
|
return m.bc
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func (m *mockBackend) TxPool() *txpool.TxPool {
|
2020-09-10 20:27:42 +03:00
|
|
|
return m.txPool
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:19:52 +03:00
|
|
|
func (m *mockBackend) StateAtBlock(block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (statedb *state.StateDB, err error) {
|
|
|
|
return nil, errors.New("not supported")
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:27:42 +03:00
|
|
|
type testBlockChain struct {
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 10:00:53 +03:00
|
|
|
root common.Hash
|
2023-06-16 15:29:40 +03:00
|
|
|
config *params.ChainConfig
|
2020-09-10 20:27:42 +03:00
|
|
|
statedb *state.StateDB
|
|
|
|
gasLimit uint64
|
|
|
|
chainHeadFeed *event.Feed
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
func (bc *testBlockChain) Config() *params.ChainConfig {
|
|
|
|
return bc.config
|
|
|
|
}
|
|
|
|
|
2023-03-02 09:29:15 +03:00
|
|
|
func (bc *testBlockChain) CurrentBlock() *types.Header {
|
|
|
|
return &types.Header{
|
|
|
|
Number: new(big.Int),
|
2020-09-10 20:27:42 +03:00
|
|
|
GasLimit: bc.gasLimit,
|
2023-03-02 09:29:15 +03:00
|
|
|
}
|
2020-09-10 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
|
2023-03-02 09:29:15 +03:00
|
|
|
return types.NewBlock(bc.CurrentBlock(), nil, nil, nil, trie.NewStackTrie(nil))
|
2020-09-10 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) {
|
|
|
|
return bc.statedb, nil
|
|
|
|
}
|
|
|
|
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 10:00:53 +03:00
|
|
|
func (bc *testBlockChain) HasState(root common.Hash) bool {
|
|
|
|
return bc.root == root
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:27:42 +03:00
|
|
|
func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
|
|
|
|
return bc.chainHeadFeed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiner(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2022-01-21 14:22:44 +03:00
|
|
|
miner, mux, cleanup := createMiner(t)
|
|
|
|
defer cleanup(false)
|
2023-01-20 19:26:01 +03:00
|
|
|
|
|
|
|
miner.Start()
|
2020-09-10 20:27:42 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
// Start the downloader
|
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
// Stop the downloader and wait for the update loop to run
|
|
|
|
mux.Post(downloader.DoneEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
miner: don't interrupt mining after successful sync (#21701)
* miner: exit loop when downloader Done or Failed
Following the logic of the comment at the method,
this fixes a regression introduced at 7cf56d6f064869cb62b1673f9ee437020c595391
, which would allow external parties to DoS with
blocks, preventing mining progress.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove ineff assign (lint)
Signed-off-by: meows <b5c6@protonmail.com>
* miner: update test re downloader events
Signed-off-by: meows <b5c6@protonmail.com>
* Revert "miner: remove ineff assign (lint)"
This reverts commit eaefcd34ab4862ebc936fb8a07578aa2744bc058.
* Revert "miner: exit loop when downloader Done or Failed"
This reverts commit 23abd34265aa246c38fc390bb72572ad6ae9fe3b.
* miner: add test showing imprecise TestMiner
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix waitForMiningState precision
This helper function would return an affirmation
on the first positive match on a desired bool.
This was imprecise; it return false positives
by not waiting initially for an 'updated' value.
This fix causes TestMiner_2 to fail, which is
expected.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove TestMiner_2 demonstrating broken test
This test demonstrated the imprecision of the test
helper function waitForMiningState. This function
has been fixed with 6d365c2851, and this test test
may now be removed.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix test regarding downloader event/mining expectations
See comment for logic.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: add test describing expectations for downloader/mining events
We expect that once the downloader emits a DoneEvent,
signaling a successful sync, that subsequent StartEvents
are not longer permitted to stop the miner.
This prevents a security vulnerability where forced syncs via
fake high blocks would stall mining operation.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: use 'canStop' state to fix downloader event handling
- Break downloader event handling into event
separating Done and Failed events. We need to
treat these cases differently since a DoneEvent
should prevent the miner from being stopped on
subsequent downloader Start events.
- Use canStop state to handle the one-off
case when a downloader first succeeds.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: improve comment wording
Signed-off-by: meows <b5c6@protonmail.com>
* miner: start mining on downloader events iff not already mining
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor miner update logic w/r/t downloader events
This makes mining pause/start logic regarding downloader
events more explicit. Instead of eternally handling downloader
events after the first done event, the subscription is closed
when downloader events are no longer actionable.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix handling downloader events on subcription closed
Signed-off-by: meows <b5c6@protonmail.com>
* miner: (lint:gosimple) use range over chan instead of for/select
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor update loop to remove race condition
The go routine handling the downloader events handling
vars in parallel with the parent routine, causing a
race condition.
This change, though ugly, remove the condition while
still allowing the downloader event subscription to be
closed when the miner has no further use for it (ie DoneEvent).
* miner: alternate fix for miner-flaw
Co-authored-by: meows <b5c6@protonmail.com>
2020-10-13 15:12:06 +03:00
|
|
|
|
|
|
|
// Subsequent downloader events after a successful DoneEvent should not cause the
|
|
|
|
// miner to start or stop. This prevents a security vulnerability
|
|
|
|
// that would allow entities to present fake high blocks that would
|
|
|
|
// stop mining operations by causing a downloader sync
|
|
|
|
// until it was discovered they were invalid, whereon mining would resume.
|
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
|
|
|
|
mux.Post(downloader.FailedEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMinerDownloaderFirstFails tests that mining is only
|
|
|
|
// permitted to run indefinitely once the downloader sees a DoneEvent (success).
|
|
|
|
// An initial FailedEvent should allow mining to stop on a subsequent
|
|
|
|
// downloader StartEvent.
|
|
|
|
func TestMinerDownloaderFirstFails(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2022-01-21 14:22:44 +03:00
|
|
|
miner, mux, cleanup := createMiner(t)
|
|
|
|
defer cleanup(false)
|
2023-01-20 19:26:01 +03:00
|
|
|
|
|
|
|
miner.Start()
|
miner: don't interrupt mining after successful sync (#21701)
* miner: exit loop when downloader Done or Failed
Following the logic of the comment at the method,
this fixes a regression introduced at 7cf56d6f064869cb62b1673f9ee437020c595391
, which would allow external parties to DoS with
blocks, preventing mining progress.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove ineff assign (lint)
Signed-off-by: meows <b5c6@protonmail.com>
* miner: update test re downloader events
Signed-off-by: meows <b5c6@protonmail.com>
* Revert "miner: remove ineff assign (lint)"
This reverts commit eaefcd34ab4862ebc936fb8a07578aa2744bc058.
* Revert "miner: exit loop when downloader Done or Failed"
This reverts commit 23abd34265aa246c38fc390bb72572ad6ae9fe3b.
* miner: add test showing imprecise TestMiner
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix waitForMiningState precision
This helper function would return an affirmation
on the first positive match on a desired bool.
This was imprecise; it return false positives
by not waiting initially for an 'updated' value.
This fix causes TestMiner_2 to fail, which is
expected.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove TestMiner_2 demonstrating broken test
This test demonstrated the imprecision of the test
helper function waitForMiningState. This function
has been fixed with 6d365c2851, and this test test
may now be removed.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix test regarding downloader event/mining expectations
See comment for logic.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: add test describing expectations for downloader/mining events
We expect that once the downloader emits a DoneEvent,
signaling a successful sync, that subsequent StartEvents
are not longer permitted to stop the miner.
This prevents a security vulnerability where forced syncs via
fake high blocks would stall mining operation.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: use 'canStop' state to fix downloader event handling
- Break downloader event handling into event
separating Done and Failed events. We need to
treat these cases differently since a DoneEvent
should prevent the miner from being stopped on
subsequent downloader Start events.
- Use canStop state to handle the one-off
case when a downloader first succeeds.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: improve comment wording
Signed-off-by: meows <b5c6@protonmail.com>
* miner: start mining on downloader events iff not already mining
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor miner update logic w/r/t downloader events
This makes mining pause/start logic regarding downloader
events more explicit. Instead of eternally handling downloader
events after the first done event, the subscription is closed
when downloader events are no longer actionable.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix handling downloader events on subcription closed
Signed-off-by: meows <b5c6@protonmail.com>
* miner: (lint:gosimple) use range over chan instead of for/select
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor update loop to remove race condition
The go routine handling the downloader events handling
vars in parallel with the parent routine, causing a
race condition.
This change, though ugly, remove the condition while
still allowing the downloader event subscription to be
closed when the miner has no further use for it (ie DoneEvent).
* miner: alternate fix for miner-flaw
Co-authored-by: meows <b5c6@protonmail.com>
2020-10-13 15:12:06 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
// Start the downloader
|
2020-09-10 20:27:42 +03:00
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, false)
|
miner: don't interrupt mining after successful sync (#21701)
* miner: exit loop when downloader Done or Failed
Following the logic of the comment at the method,
this fixes a regression introduced at 7cf56d6f064869cb62b1673f9ee437020c595391
, which would allow external parties to DoS with
blocks, preventing mining progress.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove ineff assign (lint)
Signed-off-by: meows <b5c6@protonmail.com>
* miner: update test re downloader events
Signed-off-by: meows <b5c6@protonmail.com>
* Revert "miner: remove ineff assign (lint)"
This reverts commit eaefcd34ab4862ebc936fb8a07578aa2744bc058.
* Revert "miner: exit loop when downloader Done or Failed"
This reverts commit 23abd34265aa246c38fc390bb72572ad6ae9fe3b.
* miner: add test showing imprecise TestMiner
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix waitForMiningState precision
This helper function would return an affirmation
on the first positive match on a desired bool.
This was imprecise; it return false positives
by not waiting initially for an 'updated' value.
This fix causes TestMiner_2 to fail, which is
expected.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove TestMiner_2 demonstrating broken test
This test demonstrated the imprecision of the test
helper function waitForMiningState. This function
has been fixed with 6d365c2851, and this test test
may now be removed.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix test regarding downloader event/mining expectations
See comment for logic.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: add test describing expectations for downloader/mining events
We expect that once the downloader emits a DoneEvent,
signaling a successful sync, that subsequent StartEvents
are not longer permitted to stop the miner.
This prevents a security vulnerability where forced syncs via
fake high blocks would stall mining operation.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: use 'canStop' state to fix downloader event handling
- Break downloader event handling into event
separating Done and Failed events. We need to
treat these cases differently since a DoneEvent
should prevent the miner from being stopped on
subsequent downloader Start events.
- Use canStop state to handle the one-off
case when a downloader first succeeds.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: improve comment wording
Signed-off-by: meows <b5c6@protonmail.com>
* miner: start mining on downloader events iff not already mining
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor miner update logic w/r/t downloader events
This makes mining pause/start logic regarding downloader
events more explicit. Instead of eternally handling downloader
events after the first done event, the subscription is closed
when downloader events are no longer actionable.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix handling downloader events on subcription closed
Signed-off-by: meows <b5c6@protonmail.com>
* miner: (lint:gosimple) use range over chan instead of for/select
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor update loop to remove race condition
The go routine handling the downloader events handling
vars in parallel with the parent routine, causing a
race condition.
This change, though ugly, remove the condition while
still allowing the downloader event subscription to be
closed when the miner has no further use for it (ie DoneEvent).
* miner: alternate fix for miner-flaw
Co-authored-by: meows <b5c6@protonmail.com>
2020-10-13 15:12:06 +03:00
|
|
|
|
2020-09-10 20:27:42 +03:00
|
|
|
// Stop the downloader and wait for the update loop to run
|
|
|
|
mux.Post(downloader.FailedEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
miner: don't interrupt mining after successful sync (#21701)
* miner: exit loop when downloader Done or Failed
Following the logic of the comment at the method,
this fixes a regression introduced at 7cf56d6f064869cb62b1673f9ee437020c595391
, which would allow external parties to DoS with
blocks, preventing mining progress.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove ineff assign (lint)
Signed-off-by: meows <b5c6@protonmail.com>
* miner: update test re downloader events
Signed-off-by: meows <b5c6@protonmail.com>
* Revert "miner: remove ineff assign (lint)"
This reverts commit eaefcd34ab4862ebc936fb8a07578aa2744bc058.
* Revert "miner: exit loop when downloader Done or Failed"
This reverts commit 23abd34265aa246c38fc390bb72572ad6ae9fe3b.
* miner: add test showing imprecise TestMiner
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix waitForMiningState precision
This helper function would return an affirmation
on the first positive match on a desired bool.
This was imprecise; it return false positives
by not waiting initially for an 'updated' value.
This fix causes TestMiner_2 to fail, which is
expected.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove TestMiner_2 demonstrating broken test
This test demonstrated the imprecision of the test
helper function waitForMiningState. This function
has been fixed with 6d365c2851, and this test test
may now be removed.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix test regarding downloader event/mining expectations
See comment for logic.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: add test describing expectations for downloader/mining events
We expect that once the downloader emits a DoneEvent,
signaling a successful sync, that subsequent StartEvents
are not longer permitted to stop the miner.
This prevents a security vulnerability where forced syncs via
fake high blocks would stall mining operation.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: use 'canStop' state to fix downloader event handling
- Break downloader event handling into event
separating Done and Failed events. We need to
treat these cases differently since a DoneEvent
should prevent the miner from being stopped on
subsequent downloader Start events.
- Use canStop state to handle the one-off
case when a downloader first succeeds.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: improve comment wording
Signed-off-by: meows <b5c6@protonmail.com>
* miner: start mining on downloader events iff not already mining
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor miner update logic w/r/t downloader events
This makes mining pause/start logic regarding downloader
events more explicit. Instead of eternally handling downloader
events after the first done event, the subscription is closed
when downloader events are no longer actionable.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix handling downloader events on subcription closed
Signed-off-by: meows <b5c6@protonmail.com>
* miner: (lint:gosimple) use range over chan instead of for/select
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor update loop to remove race condition
The go routine handling the downloader events handling
vars in parallel with the parent routine, causing a
race condition.
This change, though ugly, remove the condition while
still allowing the downloader event subscription to be
closed when the miner has no further use for it (ie DoneEvent).
* miner: alternate fix for miner-flaw
Co-authored-by: meows <b5c6@protonmail.com>
2020-10-13 15:12:06 +03:00
|
|
|
|
|
|
|
// Since the downloader hasn't yet emitted a successful DoneEvent,
|
|
|
|
// we expect the miner to stop on next StartEvent.
|
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
|
|
|
|
// Downloader finally succeeds.
|
|
|
|
mux.Post(downloader.DoneEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
|
|
|
|
// Downloader starts again.
|
|
|
|
// Since it has achieved a DoneEvent once, we expect miner
|
|
|
|
// state to be unchanged.
|
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
|
|
|
|
mux.Post(downloader.FailedEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinerStartStopAfterDownloaderEvents(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2022-01-21 14:22:44 +03:00
|
|
|
miner, mux, cleanup := createMiner(t)
|
|
|
|
defer cleanup(false)
|
2023-01-20 19:26:01 +03:00
|
|
|
|
|
|
|
miner.Start()
|
miner: don't interrupt mining after successful sync (#21701)
* miner: exit loop when downloader Done or Failed
Following the logic of the comment at the method,
this fixes a regression introduced at 7cf56d6f064869cb62b1673f9ee437020c595391
, which would allow external parties to DoS with
blocks, preventing mining progress.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove ineff assign (lint)
Signed-off-by: meows <b5c6@protonmail.com>
* miner: update test re downloader events
Signed-off-by: meows <b5c6@protonmail.com>
* Revert "miner: remove ineff assign (lint)"
This reverts commit eaefcd34ab4862ebc936fb8a07578aa2744bc058.
* Revert "miner: exit loop when downloader Done or Failed"
This reverts commit 23abd34265aa246c38fc390bb72572ad6ae9fe3b.
* miner: add test showing imprecise TestMiner
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix waitForMiningState precision
This helper function would return an affirmation
on the first positive match on a desired bool.
This was imprecise; it return false positives
by not waiting initially for an 'updated' value.
This fix causes TestMiner_2 to fail, which is
expected.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove TestMiner_2 demonstrating broken test
This test demonstrated the imprecision of the test
helper function waitForMiningState. This function
has been fixed with 6d365c2851, and this test test
may now be removed.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix test regarding downloader event/mining expectations
See comment for logic.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: add test describing expectations for downloader/mining events
We expect that once the downloader emits a DoneEvent,
signaling a successful sync, that subsequent StartEvents
are not longer permitted to stop the miner.
This prevents a security vulnerability where forced syncs via
fake high blocks would stall mining operation.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: use 'canStop' state to fix downloader event handling
- Break downloader event handling into event
separating Done and Failed events. We need to
treat these cases differently since a DoneEvent
should prevent the miner from being stopped on
subsequent downloader Start events.
- Use canStop state to handle the one-off
case when a downloader first succeeds.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: improve comment wording
Signed-off-by: meows <b5c6@protonmail.com>
* miner: start mining on downloader events iff not already mining
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor miner update logic w/r/t downloader events
This makes mining pause/start logic regarding downloader
events more explicit. Instead of eternally handling downloader
events after the first done event, the subscription is closed
when downloader events are no longer actionable.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix handling downloader events on subcription closed
Signed-off-by: meows <b5c6@protonmail.com>
* miner: (lint:gosimple) use range over chan instead of for/select
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor update loop to remove race condition
The go routine handling the downloader events handling
vars in parallel with the parent routine, causing a
race condition.
This change, though ugly, remove the condition while
still allowing the downloader event subscription to be
closed when the miner has no further use for it (ie DoneEvent).
* miner: alternate fix for miner-flaw
Co-authored-by: meows <b5c6@protonmail.com>
2020-10-13 15:12:06 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
// Start the downloader
|
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
|
|
|
|
// Downloader finally succeeds.
|
|
|
|
mux.Post(downloader.DoneEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
|
|
|
|
miner.Stop()
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
|
2023-01-20 19:26:01 +03:00
|
|
|
miner.Start()
|
miner: don't interrupt mining after successful sync (#21701)
* miner: exit loop when downloader Done or Failed
Following the logic of the comment at the method,
this fixes a regression introduced at 7cf56d6f064869cb62b1673f9ee437020c595391
, which would allow external parties to DoS with
blocks, preventing mining progress.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove ineff assign (lint)
Signed-off-by: meows <b5c6@protonmail.com>
* miner: update test re downloader events
Signed-off-by: meows <b5c6@protonmail.com>
* Revert "miner: remove ineff assign (lint)"
This reverts commit eaefcd34ab4862ebc936fb8a07578aa2744bc058.
* Revert "miner: exit loop when downloader Done or Failed"
This reverts commit 23abd34265aa246c38fc390bb72572ad6ae9fe3b.
* miner: add test showing imprecise TestMiner
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix waitForMiningState precision
This helper function would return an affirmation
on the first positive match on a desired bool.
This was imprecise; it return false positives
by not waiting initially for an 'updated' value.
This fix causes TestMiner_2 to fail, which is
expected.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove TestMiner_2 demonstrating broken test
This test demonstrated the imprecision of the test
helper function waitForMiningState. This function
has been fixed with 6d365c2851, and this test test
may now be removed.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix test regarding downloader event/mining expectations
See comment for logic.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: add test describing expectations for downloader/mining events
We expect that once the downloader emits a DoneEvent,
signaling a successful sync, that subsequent StartEvents
are not longer permitted to stop the miner.
This prevents a security vulnerability where forced syncs via
fake high blocks would stall mining operation.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: use 'canStop' state to fix downloader event handling
- Break downloader event handling into event
separating Done and Failed events. We need to
treat these cases differently since a DoneEvent
should prevent the miner from being stopped on
subsequent downloader Start events.
- Use canStop state to handle the one-off
case when a downloader first succeeds.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: improve comment wording
Signed-off-by: meows <b5c6@protonmail.com>
* miner: start mining on downloader events iff not already mining
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor miner update logic w/r/t downloader events
This makes mining pause/start logic regarding downloader
events more explicit. Instead of eternally handling downloader
events after the first done event, the subscription is closed
when downloader events are no longer actionable.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix handling downloader events on subcription closed
Signed-off-by: meows <b5c6@protonmail.com>
* miner: (lint:gosimple) use range over chan instead of for/select
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor update loop to remove race condition
The go routine handling the downloader events handling
vars in parallel with the parent routine, causing a
race condition.
This change, though ugly, remove the condition while
still allowing the downloader event subscription to be
closed when the miner has no further use for it (ie DoneEvent).
* miner: alternate fix for miner-flaw
Co-authored-by: meows <b5c6@protonmail.com>
2020-10-13 15:12:06 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
|
|
|
|
miner.Stop()
|
|
|
|
waitForMiningState(t, miner, false)
|
2020-09-10 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:17:09 +03:00
|
|
|
func TestStartWhileDownload(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2022-01-21 14:22:44 +03:00
|
|
|
miner, mux, cleanup := createMiner(t)
|
|
|
|
defer cleanup(false)
|
2020-09-11 19:17:09 +03:00
|
|
|
waitForMiningState(t, miner, false)
|
2023-01-20 19:26:01 +03:00
|
|
|
miner.Start()
|
2020-09-11 19:17:09 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
// Stop the downloader and wait for the update loop to run
|
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
// Starting the miner after the downloader should not work
|
2023-01-20 19:26:01 +03:00
|
|
|
miner.Start()
|
2020-09-11 19:17:09 +03:00
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:27:42 +03:00
|
|
|
func TestStartStopMiner(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2022-01-21 14:22:44 +03:00
|
|
|
miner, _, cleanup := createMiner(t)
|
|
|
|
defer cleanup(false)
|
2020-09-10 20:27:42 +03:00
|
|
|
waitForMiningState(t, miner, false)
|
2023-01-20 19:26:01 +03:00
|
|
|
miner.Start()
|
2020-09-10 20:27:42 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
miner.Stop()
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseMiner(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2022-01-21 14:22:44 +03:00
|
|
|
miner, _, cleanup := createMiner(t)
|
|
|
|
defer cleanup(true)
|
2020-09-10 20:27:42 +03:00
|
|
|
waitForMiningState(t, miner, false)
|
2023-01-20 19:26:01 +03:00
|
|
|
miner.Start()
|
2020-09-10 20:27:42 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
// Terminate the miner and wait for the update loop to run
|
|
|
|
miner.Close()
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:59:11 +03:00
|
|
|
// TestMinerSetEtherbase checks that etherbase becomes set even if mining isn't
|
|
|
|
// possible at the moment
|
|
|
|
func TestMinerSetEtherbase(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2022-01-21 14:22:44 +03:00
|
|
|
miner, mux, cleanup := createMiner(t)
|
|
|
|
defer cleanup(false)
|
2023-01-20 19:26:01 +03:00
|
|
|
miner.Start()
|
2020-10-14 12:59:11 +03:00
|
|
|
waitForMiningState(t, miner, true)
|
|
|
|
// Start the downloader
|
|
|
|
mux.Post(downloader.StartEvent{})
|
|
|
|
waitForMiningState(t, miner, false)
|
|
|
|
// Now user tries to configure proper mining address
|
2023-01-20 19:26:01 +03:00
|
|
|
miner.Start()
|
2020-10-14 12:59:11 +03:00
|
|
|
// Stop the downloader and wait for the update loop to run
|
|
|
|
mux.Post(downloader.DoneEvent{})
|
|
|
|
waitForMiningState(t, miner, true)
|
2023-01-20 19:26:01 +03:00
|
|
|
|
|
|
|
coinbase := common.HexToAddress("0xdeedbeef")
|
|
|
|
miner.SetEtherbase(coinbase)
|
|
|
|
if addr := miner.worker.etherbase(); addr != coinbase {
|
|
|
|
t.Fatalf("Unexpected etherbase want %x got %x", coinbase, addr)
|
2020-10-14 12:59:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:27:42 +03:00
|
|
|
// waitForMiningState waits until either
|
|
|
|
// * the desired mining state was reached
|
|
|
|
// * a timeout was reached which fails the test
|
|
|
|
func waitForMiningState(t *testing.T, m *Miner, mining bool) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var state bool
|
|
|
|
for i := 0; i < 100; i++ {
|
miner: don't interrupt mining after successful sync (#21701)
* miner: exit loop when downloader Done or Failed
Following the logic of the comment at the method,
this fixes a regression introduced at 7cf56d6f064869cb62b1673f9ee437020c595391
, which would allow external parties to DoS with
blocks, preventing mining progress.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove ineff assign (lint)
Signed-off-by: meows <b5c6@protonmail.com>
* miner: update test re downloader events
Signed-off-by: meows <b5c6@protonmail.com>
* Revert "miner: remove ineff assign (lint)"
This reverts commit eaefcd34ab4862ebc936fb8a07578aa2744bc058.
* Revert "miner: exit loop when downloader Done or Failed"
This reverts commit 23abd34265aa246c38fc390bb72572ad6ae9fe3b.
* miner: add test showing imprecise TestMiner
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix waitForMiningState precision
This helper function would return an affirmation
on the first positive match on a desired bool.
This was imprecise; it return false positives
by not waiting initially for an 'updated' value.
This fix causes TestMiner_2 to fail, which is
expected.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: remove TestMiner_2 demonstrating broken test
This test demonstrated the imprecision of the test
helper function waitForMiningState. This function
has been fixed with 6d365c2851, and this test test
may now be removed.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix test regarding downloader event/mining expectations
See comment for logic.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: add test describing expectations for downloader/mining events
We expect that once the downloader emits a DoneEvent,
signaling a successful sync, that subsequent StartEvents
are not longer permitted to stop the miner.
This prevents a security vulnerability where forced syncs via
fake high blocks would stall mining operation.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: use 'canStop' state to fix downloader event handling
- Break downloader event handling into event
separating Done and Failed events. We need to
treat these cases differently since a DoneEvent
should prevent the miner from being stopped on
subsequent downloader Start events.
- Use canStop state to handle the one-off
case when a downloader first succeeds.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: improve comment wording
Signed-off-by: meows <b5c6@protonmail.com>
* miner: start mining on downloader events iff not already mining
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor miner update logic w/r/t downloader events
This makes mining pause/start logic regarding downloader
events more explicit. Instead of eternally handling downloader
events after the first done event, the subscription is closed
when downloader events are no longer actionable.
Signed-off-by: meows <b5c6@protonmail.com>
* miner: fix handling downloader events on subcription closed
Signed-off-by: meows <b5c6@protonmail.com>
* miner: (lint:gosimple) use range over chan instead of for/select
Signed-off-by: meows <b5c6@protonmail.com>
* miner: refactor update loop to remove race condition
The go routine handling the downloader events handling
vars in parallel with the parent routine, causing a
race condition.
This change, though ugly, remove the condition while
still allowing the downloader event subscription to be
closed when the miner has no further use for it (ie DoneEvent).
* miner: alternate fix for miner-flaw
Co-authored-by: meows <b5c6@protonmail.com>
2020-10-13 15:12:06 +03:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2020-09-10 20:27:42 +03:00
|
|
|
if state = m.Mining(); state == mining {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Fatalf("Mining() == %t, want %t", state, mining)
|
|
|
|
}
|
|
|
|
|
2023-07-06 11:42:34 +03:00
|
|
|
func minerTestGenesisBlock(period uint64, gasLimit uint64, faucet common.Address) *core.Genesis {
|
|
|
|
config := *params.AllCliqueProtocolChanges
|
|
|
|
config.Clique = ¶ms.CliqueConfig{
|
|
|
|
Period: period,
|
|
|
|
Epoch: config.Clique.Epoch,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble and return the genesis with the precompiles and faucet pre-funded
|
|
|
|
return &core.Genesis{
|
|
|
|
Config: &config,
|
|
|
|
ExtraData: append(append(make([]byte, 32), faucet[:]...), make([]byte, crypto.SignatureLength)...),
|
|
|
|
GasLimit: gasLimit,
|
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
|
|
Difficulty: big.NewInt(1),
|
2024-02-16 21:05:33 +03:00
|
|
|
Alloc: map[common.Address]types.Account{
|
2023-07-06 11:42:34 +03:00
|
|
|
common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
|
|
|
|
common.BytesToAddress([]byte{2}): {Balance: big.NewInt(1)}, // SHA256
|
|
|
|
common.BytesToAddress([]byte{3}): {Balance: big.NewInt(1)}, // RIPEMD
|
|
|
|
common.BytesToAddress([]byte{4}): {Balance: big.NewInt(1)}, // Identity
|
|
|
|
common.BytesToAddress([]byte{5}): {Balance: big.NewInt(1)}, // ModExp
|
|
|
|
common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd
|
|
|
|
common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul
|
|
|
|
common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
|
|
|
|
common.BytesToAddress([]byte{9}): {Balance: big.NewInt(1)}, // BLAKE2b
|
|
|
|
faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 14:22:44 +03:00
|
|
|
func createMiner(t *testing.T) (*Miner, *event.TypeMux, func(skipMiner bool)) {
|
2020-09-10 20:27:42 +03:00
|
|
|
// Create Ethash config
|
|
|
|
config := Config{
|
|
|
|
Etherbase: common.HexToAddress("123456789"),
|
|
|
|
}
|
|
|
|
// Create chainConfig
|
2022-11-28 16:31:28 +03:00
|
|
|
chainDB := rawdb.NewMemoryDatabase()
|
2024-02-13 16:49:53 +03:00
|
|
|
triedb := triedb.NewDatabase(chainDB, nil)
|
2023-07-06 11:42:34 +03:00
|
|
|
genesis := minerTestGenesisBlock(15, 11_500_000, common.HexToAddress("12345"))
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
chainConfig, _, err := core.SetupGenesisBlock(chainDB, triedb, genesis)
|
2020-09-10 20:27:42 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't create new chain config: %v", err)
|
|
|
|
}
|
|
|
|
// Create consensus engine
|
2020-10-20 11:58:26 +03:00
|
|
|
engine := clique.New(chainConfig.Clique, chainDB)
|
2020-09-10 20:27:42 +03:00
|
|
|
// Create Ethereum backend
|
2022-08-30 19:22:28 +03:00
|
|
|
bc, err := core.NewBlockChain(chainDB, nil, genesis, nil, engine, vm.Config{}, nil, nil)
|
2020-09-10 20:27:42 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't create new chain %v", err)
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
statedb, _ := state.New(bc.Genesis().Root(), bc.StateCache(), nil)
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 10:00:53 +03:00
|
|
|
blockchain := &testBlockChain{bc.Genesis().Root(), chainConfig, statedb, 10000000, new(event.Feed)}
|
2023-06-16 15:29:40 +03:00
|
|
|
|
|
|
|
pool := legacypool.New(testTxPoolConfig, blockchain)
|
2024-02-13 12:10:11 +03:00
|
|
|
txpool, _ := txpool.New(testTxPoolConfig.PriceLimit, blockchain, []txpool.SubPool{pool})
|
2020-09-10 20:27:42 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
backend := NewMockBackend(bc, txpool)
|
2020-10-20 11:58:26 +03:00
|
|
|
// Create event Mux
|
|
|
|
mux := new(event.TypeMux)
|
2020-09-10 20:27:42 +03:00
|
|
|
// Create Miner
|
2022-01-24 10:19:52 +03:00
|
|
|
miner := New(backend, &config, chainConfig, mux, engine, nil)
|
2022-01-21 14:22:44 +03:00
|
|
|
cleanup := func(skipMiner bool) {
|
|
|
|
bc.Stop()
|
|
|
|
engine.Close()
|
2023-06-16 15:29:40 +03:00
|
|
|
txpool.Close()
|
2022-01-21 14:22:44 +03:00
|
|
|
if !skipMiner {
|
|
|
|
miner.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return miner, mux, cleanup
|
2020-09-10 20:27:42 +03:00
|
|
|
}
|