2016-04-14 19:18:24 +03:00
|
|
|
// Copyright 2015 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/>.
|
|
|
|
|
2015-07-02 19:55:18 +03:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2024-03-22 17:37:47 +03:00
|
|
|
"crypto/ecdsa"
|
2015-07-02 19:55:18 +03:00
|
|
|
"math/big"
|
2020-12-14 12:27:15 +03:00
|
|
|
"sort"
|
|
|
|
"sync"
|
2024-03-22 17:37:47 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/kzg4844"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2015-07-02 19:55:18 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
2017-04-05 01:16:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-09-24 15:57:49 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-06-16 15:29:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-01-17 14:19:50 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2020-12-14 12:27:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2016-07-08 20:59:11 +03:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2024-02-17 14:37:14 +03:00
|
|
|
"github.com/holiman/uint256"
|
2015-07-02 19:55:18 +03:00
|
|
|
)
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
var (
|
|
|
|
// testKey is a private key to use for funding a tester account.
|
|
|
|
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// testAddr is the Ethereum address of the tester account.
|
|
|
|
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
|
|
|
)
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// testTxPool is a mock transaction pool that blindly accepts all transactions.
|
|
|
|
// Its goal is to get around setting up a valid statedb for the balance and nonce
|
|
|
|
// checks.
|
|
|
|
type testTxPool struct {
|
|
|
|
pool map[common.Hash]*types.Transaction // Hash map of collected transactions
|
2015-12-15 19:22:48 +02:00
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
txFeed event.Feed // Notification feed to allow waiting for inclusion
|
|
|
|
reannoTxFeed event.Feed // Notification feed to trigger reannouce
|
|
|
|
lock sync.RWMutex // Protects the transaction pool
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// newTestTxPool creates a mock transaction pool.
|
|
|
|
func newTestTxPool() *testTxPool {
|
|
|
|
return &testTxPool{
|
|
|
|
pool: make(map[common.Hash]*types.Transaction),
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// Has returns an indicator whether txpool has a transaction
|
|
|
|
// cached with the given hash.
|
|
|
|
func (p *testTxPool) Has(hash common.Hash) bool {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
return p.pool[hash] != nil
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// Get retrieves the transaction from local txpool with given
|
|
|
|
// tx hash.
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 11:13:34 +03:00
|
|
|
func (p *testTxPool) Get(hash common.Hash) *types.Transaction {
|
2020-12-14 12:27:15 +03:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 11:13:34 +03:00
|
|
|
return p.pool[hash]
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
2016-07-08 20:59:11 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
// Add appends a batch of transactions to the pool, and notifies any
|
2020-12-14 12:27:15 +03:00
|
|
|
// listeners if the addition channel is non nil
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 11:13:34 +03:00
|
|
|
func (p *testTxPool) Add(txs []*types.Transaction, local bool, sync bool) []error {
|
2020-12-14 12:27:15 +03:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2019-04-16 13:20:38 +03:00
|
|
|
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 11:13:34 +03:00
|
|
|
for _, tx := range txs {
|
2020-12-14 12:27:15 +03:00
|
|
|
p.pool[tx.Hash()] = tx
|
2016-07-08 20:59:11 +03:00
|
|
|
}
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 11:13:34 +03:00
|
|
|
p.txFeed.Send(core.NewTxsEvent{Txs: txs})
|
|
|
|
return make([]error, len(txs))
|
2019-04-16 13:20:38 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// ReannouceTransactions announce the transactions to some peers.
|
|
|
|
func (p *testTxPool) ReannouceTransactions(txs []*types.Transaction) []error {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
for _, tx := range txs {
|
|
|
|
p.pool[tx.Hash()] = tx
|
|
|
|
}
|
|
|
|
p.reannoTxFeed.Send(core.ReannoTxsEvent{Txs: txs})
|
|
|
|
return make([]error, len(txs))
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// Pending returns all the transactions known to the pool
|
2024-02-20 12:37:23 +03:00
|
|
|
func (p *testTxPool) Pending(filter txpool.PendingFilter) map[common.Address][]*txpool.LazyTransaction {
|
2020-12-14 12:27:15 +03:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
2019-04-16 13:20:38 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
batches := make(map[common.Address][]*types.Transaction)
|
2020-12-14 12:27:15 +03:00
|
|
|
for _, tx := range p.pool {
|
|
|
|
from, _ := types.Sender(types.HomesteadSigner{}, tx)
|
|
|
|
batches[from] = append(batches[from], tx)
|
2019-04-16 13:20:38 +03:00
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
for _, batch := range batches {
|
|
|
|
sort.Sort(types.TxByNonce(batch))
|
2016-07-08 20:59:11 +03:00
|
|
|
}
|
2023-07-27 13:45:35 +03:00
|
|
|
pending := make(map[common.Address][]*txpool.LazyTransaction)
|
|
|
|
for addr, batch := range batches {
|
|
|
|
for _, tx := range batch {
|
|
|
|
pending[addr] = append(pending[addr], &txpool.LazyTransaction{
|
|
|
|
Hash: tx.Hash(),
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 11:13:34 +03:00
|
|
|
Tx: tx,
|
2023-07-27 13:45:35 +03:00
|
|
|
Time: tx.Time(),
|
2024-02-19 16:59:40 +03:00
|
|
|
GasFeeCap: uint256.MustFromBig(tx.GasFeeCap()),
|
|
|
|
GasTipCap: uint256.MustFromBig(tx.GasTipCap()),
|
2023-10-04 12:36:36 +03:00
|
|
|
Gas: tx.Gas(),
|
|
|
|
BlobGas: tx.BlobGas(),
|
2023-07-27 13:45:35 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pending
|
2016-07-08 20:59:11 +03:00
|
|
|
}
|
2018-09-29 23:17:06 +03:00
|
|
|
|
2023-10-04 12:36:36 +03:00
|
|
|
// SubscribeTransactions should return an event subscription of NewTxsEvent and
|
2020-12-14 12:27:15 +03:00
|
|
|
// send events to the given channel.
|
2023-10-04 12:36:36 +03:00
|
|
|
func (p *testTxPool) SubscribeTransactions(ch chan<- core.NewTxsEvent, reorgs bool) event.Subscription {
|
2020-12-14 12:27:15 +03:00
|
|
|
return p.txFeed.Subscribe(ch)
|
2018-09-29 23:17:06 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// SubscribeReannoTxsEvent should return an event subscription of ReannoTxsEvent and
|
|
|
|
// send events to the given channel.
|
|
|
|
func (p *testTxPool) SubscribeReannoTxsEvent(ch chan<- core.ReannoTxsEvent) event.Subscription {
|
|
|
|
return p.reannoTxFeed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// testHandler is a live implementation of the Ethereum protocol handler, just
|
|
|
|
// preinitialized with some sane testing defaults and the transaction pool mocked
|
|
|
|
// out.
|
|
|
|
type testHandler struct {
|
2023-04-10 13:36:45 +03:00
|
|
|
db ethdb.Database
|
|
|
|
chain *core.BlockChain
|
|
|
|
txpool *testTxPool
|
|
|
|
votepool *testVotePool
|
|
|
|
handler *handler
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2020-02-17 13:01:03 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// newTestHandler creates a new handler for testing purposes with no blocks.
|
|
|
|
func newTestHandler() *testHandler {
|
|
|
|
return newTestHandlerWithBlocks(0)
|
2018-09-29 23:17:06 +03:00
|
|
|
}
|
2020-01-13 15:23:54 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// newTestHandlerWithBlocks creates a new handler for testing purposes, with a
|
|
|
|
// given number of initial blocks.
|
|
|
|
func newTestHandlerWithBlocks(blocks int) *testHandler {
|
|
|
|
// Create a database pre-initialize with a genesis block
|
|
|
|
db := rawdb.NewMemoryDatabase()
|
2022-08-30 19:22:28 +03:00
|
|
|
gspec := &core.Genesis{
|
2020-12-14 12:27:15 +03:00
|
|
|
Config: params.TestChainConfig,
|
2024-02-16 21:05:33 +03:00
|
|
|
Alloc: types.GenesisAlloc{testAddr: {Balance: big.NewInt(1000000)}},
|
2022-08-30 19:22:28 +03:00
|
|
|
}
|
|
|
|
chain, _ := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
|
2020-12-14 12:27:15 +03:00
|
|
|
|
2022-09-07 21:21:59 +03:00
|
|
|
_, bs, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), blocks, nil)
|
2020-12-14 12:27:15 +03:00
|
|
|
if _, err := chain.InsertChain(bs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
txpool := newTestTxPool()
|
2023-04-10 13:36:45 +03:00
|
|
|
votepool := newTestVotePool()
|
2020-12-14 12:27:15 +03:00
|
|
|
|
|
|
|
handler, _ := newHandler(&handlerConfig{
|
|
|
|
Database: db,
|
|
|
|
Chain: chain,
|
|
|
|
TxPool: txpool,
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
Merger: consensus.NewMerger(rawdb.NewMemoryDatabase()),
|
2023-04-10 13:36:45 +03:00
|
|
|
VotePool: votepool,
|
2020-12-14 12:27:15 +03:00
|
|
|
Network: 1,
|
2021-11-26 14:26:03 +03:00
|
|
|
Sync: downloader.SnapSync,
|
2020-12-14 12:27:15 +03:00
|
|
|
BloomCache: 1,
|
|
|
|
})
|
2023-05-25 03:57:02 +03:00
|
|
|
handler.Start(1000, 3)
|
2020-12-14 12:27:15 +03:00
|
|
|
|
|
|
|
return &testHandler{
|
2023-04-10 13:36:45 +03:00
|
|
|
db: db,
|
|
|
|
chain: chain,
|
|
|
|
txpool: txpool,
|
|
|
|
votepool: votepool,
|
|
|
|
handler: handler,
|
2020-01-13 15:23:54 +03:00
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2020-01-13 15:23:54 +03:00
|
|
|
|
2024-03-22 17:37:47 +03:00
|
|
|
type mockParlia struct {
|
|
|
|
consensus.Engine
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockParlia) Author(header *types.Header) (common.Address, error) {
|
|
|
|
return header.Coinbase, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockParlia) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockParlia) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockParlia) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error) {
|
|
|
|
abort := make(chan<- struct{})
|
|
|
|
results := make(chan error, len(headers))
|
|
|
|
for i := 0; i < len(headers); i++ {
|
|
|
|
results <- nil
|
|
|
|
}
|
|
|
|
return abort, results
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockParlia) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, _ *[]*types.Transaction, uncles []*types.Header, withdrawals []*types.Withdrawal,
|
|
|
|
_ *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64) (err error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
|
|
|
|
uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, []*types.Receipt, error) {
|
|
|
|
// Finalize block
|
|
|
|
c.Finalize(chain, header, state, &txs, uncles, nil, nil, nil, nil)
|
|
|
|
|
|
|
|
// Assign the final state root to header.
|
|
|
|
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
|
|
|
|
|
|
|
|
// Header seems complete, assemble into a block and return
|
|
|
|
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), receipts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockParlia) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
|
|
|
|
return big.NewInt(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestParliaHandlerAfterCancun(t *testing.T, config *params.ChainConfig, mode downloader.SyncMode, preCancunBlks, postCancunBlks uint64) *testHandler {
|
|
|
|
// Have N headers in the freezer
|
|
|
|
frdir := t.TempDir()
|
2024-05-29 10:11:52 +03:00
|
|
|
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false, false, false, false, false)
|
2024-03-22 17:37:47 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create database with ancient backend")
|
|
|
|
}
|
|
|
|
gspec := &core.Genesis{
|
|
|
|
Config: config,
|
|
|
|
Alloc: types.GenesisAlloc{testAddr: {Balance: new(big.Int).SetUint64(10 * params.Ether)}},
|
|
|
|
}
|
|
|
|
engine := &mockParlia{}
|
|
|
|
chain, _ := core.NewBlockChain(db, nil, gspec, nil, engine, vm.Config{}, nil, nil)
|
|
|
|
signer := types.LatestSigner(config)
|
|
|
|
|
|
|
|
_, bs, _ := core.GenerateChainWithGenesis(gspec, engine, int(preCancunBlks+postCancunBlks), func(i int, gen *core.BlockGen) {
|
|
|
|
if !config.IsCancun(gen.Number(), gen.Timestamp()) {
|
|
|
|
tx, _ := makeMockTx(config, signer, testKey, gen.TxNonce(testAddr), gen.BaseFee().Uint64(), eip4844.CalcBlobFee(gen.ExcessBlobGas()).Uint64(), false)
|
|
|
|
gen.AddTxWithChain(chain, tx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tx, sidecar := makeMockTx(config, signer, testKey, gen.TxNonce(testAddr), gen.BaseFee().Uint64(), eip4844.CalcBlobFee(gen.ExcessBlobGas()).Uint64(), true)
|
|
|
|
gen.AddTxWithChain(chain, tx)
|
|
|
|
gen.AddBlobSidecar(&types.BlobSidecar{
|
|
|
|
BlobTxSidecar: *sidecar,
|
|
|
|
TxIndex: 0,
|
|
|
|
TxHash: tx.Hash(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if _, err := chain.InsertChain(bs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
txpool := newTestTxPool()
|
|
|
|
votepool := newTestVotePool()
|
|
|
|
|
|
|
|
handler, _ := newHandler(&handlerConfig{
|
|
|
|
Database: db,
|
|
|
|
Chain: chain,
|
|
|
|
TxPool: txpool,
|
|
|
|
Merger: consensus.NewMerger(rawdb.NewMemoryDatabase()),
|
|
|
|
VotePool: votepool,
|
|
|
|
Network: 1,
|
|
|
|
Sync: mode,
|
|
|
|
BloomCache: 1,
|
|
|
|
})
|
|
|
|
handler.Start(1000, 3)
|
|
|
|
|
|
|
|
return &testHandler{
|
|
|
|
db: db,
|
|
|
|
chain: chain,
|
|
|
|
txpool: txpool,
|
|
|
|
votepool: votepool,
|
|
|
|
handler: handler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// close tears down the handler and all its internal constructs.
|
|
|
|
func (b *testHandler) close() {
|
|
|
|
b.handler.Stop()
|
|
|
|
b.chain.Stop()
|
2020-01-13 15:23:54 +03:00
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
|
|
|
|
// newTestVotePool creates a mock vote pool.
|
|
|
|
type testVotePool struct {
|
|
|
|
pool map[common.Hash]*types.VoteEnvelope // Hash map of collected votes
|
|
|
|
|
|
|
|
voteFeed event.Feed // Notification feed to allow waiting for inclusion
|
|
|
|
lock sync.RWMutex // Protects the vote pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestVotePool creates a mock vote pool.
|
|
|
|
func newTestVotePool() *testVotePool {
|
|
|
|
return &testVotePool{
|
|
|
|
pool: make(map[common.Hash]*types.VoteEnvelope),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testVotePool) PutVote(vote *types.VoteEnvelope) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
t.pool[vote.Hash()] = vote
|
|
|
|
t.voteFeed.Send(core.NewVoteEvent{Vote: vote})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testVotePool) FetchVoteByBlockHash(blockHash common.Hash) []*types.VoteEnvelope {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testVotePool) GetVotes() []*types.VoteEnvelope {
|
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
|
|
|
|
votes := make([]*types.VoteEnvelope, 0, len(t.pool))
|
|
|
|
for _, vote := range t.pool {
|
|
|
|
votes = append(votes, vote)
|
|
|
|
}
|
|
|
|
return votes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testVotePool) SubscribeNewVoteEvent(ch chan<- core.NewVoteEvent) event.Subscription {
|
|
|
|
return t.voteFeed.Subscribe(ch)
|
|
|
|
}
|
2024-03-22 17:37:47 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
emptyBlob = kzg4844.Blob{}
|
|
|
|
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob)
|
|
|
|
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit)
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeMockTx(config *params.ChainConfig, signer types.Signer, key *ecdsa.PrivateKey, nonce uint64, baseFee uint64, blobBaseFee uint64, isBlobTx bool) (*types.Transaction, *types.BlobTxSidecar) {
|
|
|
|
if !isBlobTx {
|
|
|
|
raw := &types.DynamicFeeTx{
|
|
|
|
ChainID: config.ChainID,
|
|
|
|
Nonce: nonce,
|
|
|
|
GasTipCap: big.NewInt(10),
|
|
|
|
GasFeeCap: new(big.Int).SetUint64(baseFee + 10),
|
|
|
|
Gas: params.TxGas,
|
|
|
|
To: &common.Address{0x00},
|
|
|
|
Value: big.NewInt(0),
|
|
|
|
}
|
|
|
|
tx, _ := types.SignTx(types.NewTx(raw), signer, key)
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
sidecar := &types.BlobTxSidecar{
|
|
|
|
Blobs: []kzg4844.Blob{emptyBlob, emptyBlob},
|
|
|
|
Commitments: []kzg4844.Commitment{emptyBlobCommit, emptyBlobCommit},
|
|
|
|
Proofs: []kzg4844.Proof{emptyBlobProof, emptyBlobProof},
|
|
|
|
}
|
|
|
|
raw := &types.BlobTx{
|
|
|
|
ChainID: uint256.MustFromBig(config.ChainID),
|
|
|
|
Nonce: nonce,
|
|
|
|
GasTipCap: uint256.NewInt(10),
|
|
|
|
GasFeeCap: uint256.NewInt(baseFee + 10),
|
|
|
|
Gas: params.TxGas,
|
|
|
|
To: common.Address{0x00},
|
|
|
|
Value: uint256.NewInt(0),
|
|
|
|
BlobFeeCap: uint256.NewInt(blobBaseFee),
|
|
|
|
BlobHashes: sidecar.BlobHashes(),
|
|
|
|
}
|
|
|
|
tx, _ := types.SignTx(types.NewTx(raw), signer, key)
|
|
|
|
return tx, sidecar
|
|
|
|
}
|