Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mining Test to E3 primitives + fixed bug with coinbase #11385

Merged
merged 67 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
7b5f91d
save
Jul 19, 2024
69ce848
trying to fix
Jul 24, 2024
737017c
save
AskAlexSharov Jul 28, 2024
917abde
save
AskAlexSharov Jul 28, 2024
48f83e2
Merge branch 'main' into uncomment_TestTxPoolContent
AskAlexSharov Jul 28, 2024
ff52286
save
AskAlexSharov Jul 28, 2024
16d2e94
Merge branch 'refs/heads/main' into uncomment_TestTxPoolContent
Jul 28, 2024
d6955be
Merge branch 'refs/heads/uncomment_TestTxPoolContent' into mining-fix…
Jul 28, 2024
a295164
save
Jul 29, 2024
29f6477
Merge branch 'main' into mining-fix-testing
AskAlexSharov Jul 29, 2024
cb85cfd
save
Jul 29, 2024
f4a2080
Merge remote-tracking branch 'origin/mining-fix-testing' into mining-…
Jul 29, 2024
5f69ef0
Merge branch 'main' into mining-fix-testing
AskAlexSharov Jul 30, 2024
64efcd7
more logging
Jul 30, 2024
50895bd
more logging
Jul 30, 2024
ef03b6e
more logging
Jul 30, 2024
1cde72a
Merge branch 'refs/heads/main' into mining-fix-testing
Jul 31, 2024
924cf67
more logging
Jul 31, 2024
47b6089
more logging
Aug 1, 2024
b375767
more logging
Aug 1, 2024
3c5d9bd
more logging
Aug 1, 2024
81f0246
more logging
Aug 2, 2024
4ff526a
save
AskAlexSharov Aug 2, 2024
075f097
save
AskAlexSharov Aug 2, 2024
d205e43
more logging
Aug 2, 2024
fa836eb
added afterrun hook
Aug 2, 2024
a72a4ae
added unwind
Aug 2, 2024
fe426e9
argueble edit
Aug 2, 2024
4ebc86e
more logging
Aug 5, 2024
8572564
save
AskAlexSharov Aug 6, 2024
25a3ce7
save
AskAlexSharov Aug 6, 2024
64deda3
a lot of logging
Aug 7, 2024
c1a24e0
a lot of logging removed + fix of nonces
Aug 7, 2024
f85ec99
added mining flag
Aug 7, 2024
8f6c232
fix of tests
Aug 7, 2024
c54525f
fix
Aug 7, 2024
7a145d3
fix
Aug 8, 2024
5819480
removed unused err logs
Aug 8, 2024
7b86a8a
fix
Aug 8, 2024
262aef2
added coinbase from header to evm + fixed test + removed useless logs
Aug 10, 2024
92672e5
Merge branch 'refs/heads/main' into mining-fix-testing
Aug 10, 2024
eadb027
small erasing
Aug 10, 2024
2e20b32
small erasing
Aug 10, 2024
c263671
lint fix
Aug 10, 2024
fdaccbc
small fixes
Aug 11, 2024
e365544
small fixes
Aug 11, 2024
85e64d2
small fixes
Aug 12, 2024
3326b13
Merge branch 'refs/heads/main' into mining-fix-testing
Aug 13, 2024
393b73d
small fixes
Aug 13, 2024
350b708
add logging to integration
Aug 13, 2024
d666be0
Merge branch 'refs/heads/main' into mining-fix-testing
Aug 13, 2024
1abf7d4
add logging to integration
Aug 13, 2024
e166af9
fix hooks
Aug 14, 2024
4abfc6a
Merge branch 'refs/heads/main' into mining-fix-testing
Aug 14, 2024
134913c
merged main
Aug 14, 2024
714505b
Merge branch 'refs/heads/main' into mining-fix-testing
Aug 15, 2024
702d085
fix
Aug 15, 2024
b830807
fix
Aug 15, 2024
25d7886
Merge branch 'refs/heads/main' into mining-fix-testing
Aug 16, 2024
5c72991
fix
Aug 16, 2024
0b08db0
fix
Aug 16, 2024
b1285db
fix
Aug 16, 2024
4e2ea38
fix
Aug 16, 2024
a7c0fa2
fix
Aug 16, 2024
5d5cf08
fix
Aug 16, 2024
1c57c4b
fix
Aug 16, 2024
b6d3334
Merge branch 'refs/heads/main' into mining-fix-testing
Aug 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd/integration/commands/state_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
stateLib "github.com/erigontech/erigon-lib/state"
"os"
"time"

Expand Down Expand Up @@ -165,6 +166,12 @@ func syncBySmallSteps(db kv.RwDB, miningConfig params.MiningConfig, ctx context.
}
defer tx.Rollback()

sd, err := stateLib.NewSharedDomains(tx, logger1)
if err != nil {
return err
}
defer sd.Close()

quit := ctx.Done()

var batchSize datasize.ByteSize
Expand Down Expand Up @@ -270,7 +277,7 @@ func syncBySmallSteps(db kv.RwDB, miningConfig params.MiningConfig, ctx context.

stateStages.MockExecFunc(stages.Execution, execUntilFunc(execToBlock))
_ = stateStages.SetCurrentStage(stages.Execution)
if _, err := stateStages.Run(db, wrap.TxContainer{Tx: tx}, false /* firstCycle */, false); err != nil {
if _, err := stateStages.Run(db, wrap.TxContainer{Tx: tx, Doms: sd}, false /* firstCycle */, false); err != nil {
return err
}

Expand Down Expand Up @@ -300,7 +307,7 @@ func syncBySmallSteps(db kv.RwDB, miningConfig params.MiningConfig, ctx context.
miner.MiningConfig.Etherbase = nextBlock.Coinbase()
miner.MiningConfig.ExtraData = nextBlock.Extra()
miningStages.MockExecFunc(stages.MiningCreateBlock, func(badBlockUnwind bool, s *stagedsync.StageState, u stagedsync.Unwinder, txc wrap.TxContainer, logger log.Logger) error {
err = stagedsync.SpawnMiningCreateBlockStage(s, txc.Tx,
err = stagedsync.SpawnMiningCreateBlockStage(s, txc,
stagedsync.StageMiningCreateBlockCfg(db, miner, *chainConfig, engine, nil, nil, dirs.Tmp, br),
quit, logger)
if err != nil {
Expand All @@ -322,7 +329,7 @@ func syncBySmallSteps(db kv.RwDB, miningConfig params.MiningConfig, ctx context.
//})

_ = miningStages.SetCurrentStage(stages.MiningCreateBlock)
if _, err := miningStages.Run(db, wrap.TxContainer{Tx: tx}, false /* firstCycle */, false); err != nil {
if _, err := miningStages.Run(db, wrap.TxContainer{Tx: tx, Doms: sd}, false /* firstCycle */, false); err != nil {
return err
}
tx.Rollback()
Expand Down
26 changes: 17 additions & 9 deletions cmd/state/exec3/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ type Worker struct {
vmCfg vm.Config

dirs datadir.Dirs

isMining bool
}

func NewWorker(lock sync.Locker, logger log.Logger, ctx context.Context, background bool, chainDb kv.RoDB, in *state.QueueWithRetry, blockReader services.FullBlockReader, chainConfig *chain.Config, genesis *types.Genesis, results *state.ResultsQueue, engine consensus.Engine, dirs datadir.Dirs) *Worker {
func NewWorker(lock sync.Locker, logger log.Logger, ctx context.Context, background bool, chainDb kv.RoDB, in *state.QueueWithRetry, blockReader services.FullBlockReader, chainConfig *chain.Config, genesis *types.Genesis, results *state.ResultsQueue, engine consensus.Engine, dirs datadir.Dirs, isMining bool) *Worker {
w := &Worker{
lock: lock,
logger: logger,
Expand All @@ -91,6 +93,8 @@ func NewWorker(lock sync.Locker, logger log.Logger, ctx context.Context, backgro
taskGasPool: new(core.GasPool),

dirs: dirs,

isMining: isMining,
}
w.taskGasPool.AddBlobGas(chainConfig.GetMaxBlobGasPerBlock())
w.vmCfg = vm.Config{Debug: true, Tracer: w.callTracer}
Expand Down Expand Up @@ -126,18 +130,18 @@ func (rw *Worker) ResetTx(chainTx kv.Tx) {

func (rw *Worker) Run() error {
for txTask, ok := rw.in.Next(rw.ctx); ok; txTask, ok = rw.in.Next(rw.ctx) {
rw.RunTxTask(txTask)
rw.RunTxTask(txTask, rw.isMining)
if err := rw.resultCh.Add(rw.ctx, txTask); err != nil {
return err
}
}
return nil
}

func (rw *Worker) RunTxTask(txTask *state.TxTask) {
func (rw *Worker) RunTxTask(txTask *state.TxTask, isMining bool) {
rw.lock.Lock()
defer rw.lock.Unlock()
rw.RunTxTaskNoLock(txTask)
rw.RunTxTaskNoLock(txTask, isMining)
}

// Needed to set history reader when need to offset few txs from block beginning and does not break processing,
Expand All @@ -159,7 +163,7 @@ func (rw *Worker) SetReader(reader state.ResettableStateReader) {
}
}

func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask) {
func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask, isMining bool) {
if txTask.HistoryExecution && !rw.historyMode {
// in case if we cancelled execution and commitment happened in the middle of the block, we have to process block
// from the beginning until committed txNum and only then disable history mode.
Expand Down Expand Up @@ -228,7 +232,11 @@ func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask) {
return core.SysCallContract(contract, data, rw.chainConfig, ibs, header, rw.engine, false /* constCall */)
}

_, _, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger)
if isMining {
_, txTask.Txs, txTask.BlockReceipts, err = rw.engine.FinalizeAndAssemble(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, nil, rw.logger)
} else {
_, _, _, err = rw.engine.Finalize(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger)
}
if err != nil {
txTask.Error = err
} else {
Expand Down Expand Up @@ -291,7 +299,7 @@ func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask) {
}
}

func NewWorkersPool(lock sync.Locker, accumulator *shards.Accumulator, logger log.Logger, ctx context.Context, background bool, chainDb kv.RoDB, rs *state.StateV3, in *state.QueueWithRetry, blockReader services.FullBlockReader, chainConfig *chain.Config, genesis *types.Genesis, engine consensus.Engine, workerCount int, dirs datadir.Dirs) (reconWorkers []*Worker, applyWorker *Worker, rws *state.ResultsQueue, clear func(), wait func()) {
func NewWorkersPool(lock sync.Locker, accumulator *shards.Accumulator, logger log.Logger, ctx context.Context, background bool, chainDb kv.RoDB, rs *state.StateV3, in *state.QueueWithRetry, blockReader services.FullBlockReader, chainConfig *chain.Config, genesis *types.Genesis, engine consensus.Engine, workerCount int, dirs datadir.Dirs, isMining bool) (reconWorkers []*Worker, applyWorker *Worker, rws *state.ResultsQueue, clear func(), wait func()) {
reconWorkers = make([]*Worker, workerCount)

resultChSize := workerCount * 8
Expand All @@ -302,7 +310,7 @@ func NewWorkersPool(lock sync.Locker, accumulator *shards.Accumulator, logger lo
ctx, cancel := context.WithCancel(ctx)
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < workerCount; i++ {
reconWorkers[i] = NewWorker(lock, logger, ctx, background, chainDb, in, blockReader, chainConfig, genesis, rws, engine, dirs)
reconWorkers[i] = NewWorker(lock, logger, ctx, background, chainDb, in, blockReader, chainConfig, genesis, rws, engine, dirs, isMining)
reconWorkers[i].ResetState(rs, accumulator)
}
if background {
Expand All @@ -329,7 +337,7 @@ func NewWorkersPool(lock sync.Locker, accumulator *shards.Accumulator, logger lo
//applyWorker.ResetTx(nil)
}
}
applyWorker = NewWorker(lock, logger, ctx, false, chainDb, in, blockReader, chainConfig, genesis, rws, engine, dirs)
applyWorker = NewWorker(lock, logger, ctx, false, chainDb, in, blockReader, chainConfig, genesis, rws, engine, dirs, isMining)

return reconWorkers, applyWorker, rws, clear, wait
}
8 changes: 6 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func FinalizeBlockExecution(
newBlock, newTxs, newReceipt, err = engine.FinalizeAndAssemble(cc, header, ibs, txs, uncles, receipts, withdrawals, requests, chainReader, syscall, nil, logger)
} else {
var rss types.Requests
_, _, rss, err = engine.Finalize(cc, header, ibs, txs, uncles, receipts, withdrawals, requests, chainReader, syscall, logger)
newTxs, newReceipt, rss, err = engine.Finalize(cc, header, ibs, txs, uncles, receipts, withdrawals, requests, chainReader, syscall, logger)

if !reflect.DeepEqual(rss, requests) {
return nil, nil, nil, fmt.Errorf("invalid requests for block %d", header.Number.Uint64())
Expand Down Expand Up @@ -367,7 +367,7 @@ func InitializeBlockExecution(engine consensus.Engine, chain consensus.ChainHead
return nil
}

func BlockPostValidation(gasUsed, blobGasUsed uint64, checkReceipts bool, receipts types.Receipts, h *types.Header) error {
func BlockPostValidation(gasUsed, blobGasUsed uint64, checkReceipts bool, receipts types.Receipts, h *types.Header, isMining bool) error {
if gasUsed != h.GasUsed {
return fmt.Errorf("gas used by execution: %d, in header: %d, headerNum=%d, %x",
gasUsed, h.GasUsed, h.Number.Uint64(), h.Hash())
Expand All @@ -383,6 +383,10 @@ func BlockPostValidation(gasUsed, blobGasUsed uint64, checkReceipts bool, receip
}
receiptHash := types.DeriveSha(receipts)
if receiptHash != h.ReceiptHash {
if isMining {
h.ReceiptHash = receiptHash
return nil
}
return fmt.Errorf("receiptHash mismatch: %x != %x, headerNum=%d, %x",
receiptHash, h.ReceiptHash, h.Number.Uint64(), h.Hash())
}
Expand Down
6 changes: 2 additions & 4 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ package state
import (
"errors"
"fmt"
"sort"

"github.com/holiman/uint256"

"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
types2 "github.com/erigontech/erigon-lib/types"
Expand All @@ -37,6 +33,8 @@ import (
"github.com/erigontech/erigon/core/vm/evmtypes"
"github.com/erigontech/erigon/crypto"
"github.com/erigontech/erigon/turbo/trie"
"github.com/holiman/uint256"
"sort"
)

var _ evmtypes.IntraBlockState = new(IntraBlockState) // compile-time interface-check
Expand Down
1 change: 0 additions & 1 deletion core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func newObject(db *IntraBlockState, address libcommon.Address, data, original *a
so.data.Root = trie.EmptyRoot
}
so.original.Copy(original)

return &so
}

Expand Down
1 change: 0 additions & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func applyTransaction(config *chain.Config, engine consensus.EngineReader, gp *G

// Update the evm with the new transaction context.
evm.Reset(txContext, ibs)

result, err := ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */)
if err != nil {
return nil, nil, err
Expand Down
1 change: 0 additions & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ func (st *StateTransition) preCheck(gasBailout bool) error {
// nil evm execution result.
func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*evmtypes.ExecutionResult, error) {
coinbase := st.evm.Context.Coinbase

senderInitBalance := st.state.GetBalance(st.msg.From()).Clone()
coinbaseInitBalance := st.state.GetBalance(coinbase).Clone()

Expand Down
21 changes: 14 additions & 7 deletions erigon-lib/kv/membatchwithdb/memory_mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,28 +731,35 @@ func (m *MemoryMutation) AggTx() any {
}

func (m *MemoryMutation) DomainGet(name kv.Domain, k, k2 []byte) (v []byte, step uint64, err error) {
return m.db.(kv.TemporalTx).DomainGet(name, k, k2)
panic("not supported")
//return m.db.(kv.TemporalTx).DomainGet(name, k, k2)
}

func (m *MemoryMutation) DomainGetAsOf(name kv.Domain, k, k2 []byte, ts uint64) (v []byte, ok bool, err error) {
return m.db.(kv.TemporalTx).DomainGetAsOf(name, k, k2, ts)
panic("not supported")
//return m.db.(kv.TemporalTx).DomainGetAsOf(name, k, k2, ts)
}
func (m *MemoryMutation) HistorySeek(name kv.History, k []byte, ts uint64) (v []byte, ok bool, err error) {
return m.db.(kv.TemporalTx).HistorySeek(name, k, ts)
panic("not supported")
//return m.db.(kv.TemporalTx).HistorySeek(name, k, ts)
}

func (m *MemoryMutation) IndexRange(name kv.InvertedIdx, k []byte, fromTs, toTs int, asc order.By, limit int) (timestamps stream.U64, err error) {
return m.db.(kv.TemporalTx).IndexRange(name, k, fromTs, toTs, asc, limit)
panic("not supported")
//return m.db.(kv.TemporalTx).IndexRange(name, k, fromTs, toTs, asc, limit)
}

func (m *MemoryMutation) HistoryRange(name kv.History, fromTs, toTs int, asc order.By, limit int) (it stream.KV, err error) {
return m.db.(kv.TemporalTx).HistoryRange(name, fromTs, toTs, asc, limit)
panic("not supported")
//return m.db.(kv.TemporalTx).HistoryRange(name, fromTs, toTs, asc, limit)
}

func (m *MemoryMutation) DomainRange(name kv.Domain, fromKey, toKey []byte, ts uint64, asc order.By, limit int) (it stream.KV, err error) {
return m.db.(kv.TemporalTx).DomainRange(name, fromKey, toKey, ts, asc, limit)
panic("not supported")
//return m.db.(kv.TemporalTx).DomainRange(name, fromKey, toKey, ts, asc, limit)
}

func (m *MemoryMutation) AppendableGet(name kv.Appendable, ts kv.TxnId) ([]byte, bool, error) {
return m.db.(kv.TemporalTx).AppendableGet(name, ts)
panic("not supported")
//return m.db.(kv.TemporalTx).AppendableGet(name, ts)
}
20 changes: 11 additions & 9 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,7 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, stateDiffClient
defer streamCancel()

mineEvery := time.NewTicker(miner.MiningConfig.Recommit)

defer mineEvery.Stop()

s.logger.Info("Starting to mine", "etherbase", eb)
Expand All @@ -1221,9 +1222,6 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, stateDiffClient
hasWork := true // Start mining immediately
errc := make(chan error, 1)

workCtx, workCancel := context.WithCancel(ctx)
defer workCancel()

for {
// Only reset if some work was done previously as we'd like to rely
// on the `miner.recommit` as backup.
Expand All @@ -1243,12 +1241,15 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, stateDiffClient
hasWork = true

case <-s.notifyMiningAboutNewTxs:
//log.Warn("[dbg] notifyMiningAboutNewTxs")

// Skip mining based on new txn notif for bor consensus
hasWork = s.chainConfig.Bor == nil
if hasWork {
s.logger.Debug("Start mining based on txpool notif")
}
case <-mineEvery.C:
//log.Warn("[dbg] mineEvery", "working", working, "waiting", waiting.Load())
if !(working || waiting.Load()) {
s.logger.Debug("Start mining based on miner.recommit", "duration", miner.MiningConfig.Recommit)
}
Expand All @@ -1272,12 +1273,13 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, stateDiffClient
hasWork = false
mineEvery.Reset(miner.MiningConfig.Recommit)
go func() {
err := stages2.MiningStep(ctx, db, mining, tmpDir, logger)
err = stages2.MiningStep(ctx, db, mining, tmpDir, logger)

waiting.Store(true)
defer waiting.Store(false)

errc <- err
defer func() {
waiting.Store(false)
errc <- err
}()

if err != nil {
return
Expand All @@ -1291,8 +1293,8 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, stateDiffClient
s.minedBlocks <- block.Block
}
return
case <-workCtx.Done():
errc <- workCtx.Err()
case <-ctx.Done():
errc <- ctx.Err()
return
}
}
Expand Down
Loading
Loading