2020-09-10 19:27:42 +02: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 (
|
2023-03-02 08:29:15 +02:00
|
|
|
"math/big"
|
2024-03-06 13:45:03 +01:00
|
|
|
"sync"
|
2020-09-10 19:27:42 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-10-20 10:58:26 +02:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/clique"
|
2020-09-10 19:27:42 +02: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 19:27:42 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2023-07-06 10:42:34 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-09-10 19:27:42 +02:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-06-16 15:29:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-09-10 19:27:42 +02:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2024-02-13 21:49:53 +08:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
2020-09-10 19:27:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockBackend struct {
|
|
|
|
bc *core.BlockChain
|
2022-10-24 16:13:55 +03:00
|
|
|
txPool *txpool.TxPool
|
2020-09-10 19:27:42 +02:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func NewMockBackend(bc *core.BlockChain, txPool *txpool.TxPool) *mockBackend {
|
2020-09-10 19:27:42 +02: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 19:27:42 +02:00
|
|
|
return m.txPool
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:00:53 +08:00
|
|
|
root common.Hash
|
2023-06-16 15:29:40 +03:00
|
|
|
config *params.ChainConfig
|
2020-09-10 19:27:42 +02: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 08:29:15 +02:00
|
|
|
func (bc *testBlockChain) CurrentBlock() *types.Header {
|
|
|
|
return &types.Header{
|
|
|
|
Number: new(big.Int),
|
2020-09-10 19:27:42 +02:00
|
|
|
GasLimit: bc.gasLimit,
|
2023-03-02 08:29:15 +02:00
|
|
|
}
|
2020-09-10 19:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
|
2024-04-30 06:55:08 -06:00
|
|
|
return types.NewBlock(bc.CurrentBlock(), nil, nil, trie.NewStackTrie(nil))
|
2020-09-10 19:27:42 +02: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 15:00:53 +08:00
|
|
|
func (bc *testBlockChain) HasState(root common.Hash) bool {
|
|
|
|
return bc.root == root
|
|
|
|
}
|
|
|
|
|
2020-09-10 19:27:42 +02:00
|
|
|
func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
|
|
|
|
return bc.chainHeadFeed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
func TestBuildPendingBlocks(t *testing.T) {
|
|
|
|
miner := createMiner(t)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
block, _, _ := miner.Pending()
|
|
|
|
if block == nil {
|
|
|
|
t.Error("Pending failed")
|
2020-09-10 19:27:42 +02:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
}()
|
|
|
|
wg.Wait()
|
2020-09-10 19:27:42 +02:00
|
|
|
}
|
|
|
|
|
2023-07-06 10:42:34 +02: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 19:05:33 +01:00
|
|
|
Alloc: map[common.Address]types.Account{
|
2023-07-06 10:42:34 +02: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))},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
|
|
|
|
func createMiner(t *testing.T) *Miner {
|
2020-09-10 19:27:42 +02:00
|
|
|
// Create Ethash config
|
|
|
|
config := Config{
|
2024-03-06 13:45:03 +01:00
|
|
|
PendingFeeRecipient: common.HexToAddress("123456789"),
|
2020-09-10 19:27:42 +02:00
|
|
|
}
|
|
|
|
// Create chainConfig
|
2022-11-28 21:31:28 +08:00
|
|
|
chainDB := rawdb.NewMemoryDatabase()
|
2024-02-13 21:49:53 +08:00
|
|
|
triedb := triedb.NewDatabase(chainDB, nil)
|
2023-07-06 10:42:34 +02:00
|
|
|
genesis := minerTestGenesisBlock(15, 11_500_000, common.HexToAddress("12345"))
|
cmd, core, miner: rework genesis setup (#30907)
This pull request refactors the genesis setup function, the major
changes are highlighted here:
**(a) Triedb is opened in verkle mode if `EnableVerkleAtGenesis` is
configured in chainConfig or the database has been initialized previously with
`EnableVerkleAtGenesis` configured**.
A new config field `EnableVerkleAtGenesis` has been added in the
chainConfig. This field must be configured with True if Geth wants to initialize
the genesis in Verkle mode.
In the verkle devnet-7, the verkle transition is activated at genesis.
Therefore, the verkle rules should be used since the genesis. In production
networks (mainnet and public testnets), verkle activation always occurs after
the genesis block. Therefore, this flag is only made for devnet and should be
deprecated later. Besides, verkle transition at non-genesis block hasn't been
implemented yet, it should be done in the following PRs.
**(b) The genesis initialization condition has been simplified**
There is a special mode supported by the Geth is that: Geth can be
initialized with an existing chain segment, which can fasten the node sync
process by retaining the chain freezer folder.
Originally, if the triedb is regarded as uninitialized and the genesis block can
be found in the chain freezer, the genesis block along with genesis state will be
committed. This condition has been simplified to checking the presence of chain
config in key-value store. The existence of chain config can represent the genesis
has been committed.
2025-01-14 18:49:30 +08:00
|
|
|
chainConfig, _, _, err := core.SetupGenesisBlock(chainDB, triedb, genesis)
|
2020-09-10 19:27:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't create new chain config: %v", err)
|
|
|
|
}
|
|
|
|
// Create consensus engine
|
2020-10-20 10:58:26 +02:00
|
|
|
engine := clique.New(chainConfig.Clique, chainDB)
|
2020-09-10 19:27:42 +02:00
|
|
|
// Create Ethereum backend
|
2024-09-04 15:03:06 +02:00
|
|
|
bc, err := core.NewBlockChain(chainDB, nil, genesis, nil, engine, vm.Config{}, nil)
|
2020-09-10 19:27:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't create new chain %v", err)
|
|
|
|
}
|
2024-09-05 18:10:47 +08:00
|
|
|
statedb, _ := state.New(bc.Genesis().Root(), bc.StateCache())
|
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 15:00:53 +08: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 17:10:11 +08:00
|
|
|
txpool, _ := txpool.New(testTxPoolConfig.PriceLimit, blockchain, []txpool.SubPool{pool})
|
2020-09-10 19:27:42 +02:00
|
|
|
|
|
|
|
// Create Miner
|
2024-03-06 13:45:03 +01:00
|
|
|
backend := NewMockBackend(bc, txpool)
|
|
|
|
miner := New(backend, config, engine)
|
|
|
|
return miner
|
2020-09-10 19:27:42 +02:00
|
|
|
}
|