Skip to content

Commit

Permalink
[wip] TxNums asserts + ForcedWrite revise (#12234)
Browse files Browse the repository at this point in the history
closes #11211

---------

Co-authored-by: JkLondon <[email protected]>
  • Loading branch information
JkLondon and JkLondon authored Nov 1, 2024
1 parent e5665a6 commit 686df6c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func write(tx kv.RwTx, g *types.Genesis, dirs datadir.Dirs, logger log.Logger) (
if err := rawdb.WriteTd(tx, block.Hash(), block.NumberU64(), g.Difficulty); err != nil {
return nil, nil, err
}
if err := rawdbv3.TxNums.ForcedWrite(tx, 0, uint64(block.Transactions().Len()+1)); err != nil {
if err := rawdbv3.TxNums.Append(tx, 0, uint64(block.Transactions().Len()+1)); err != nil {
return nil, nil, err
}

Expand Down
14 changes: 8 additions & 6 deletions erigon-lib/kv/rawdbv3/txnum.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ func (t TxNumsReader) Append(tx kv.RwTx, blockNum, maxTxNum uint64) (err error)
}
return nil
}
func (TxNumsReader) ForcedWrite(tx kv.RwTx, blockNum, maxTxNum uint64) (err error) {
var k, v [8]byte
binary.BigEndian.PutUint64(k[:], blockNum)
binary.BigEndian.PutUint64(v[:], maxTxNum)
return tx.Put(kv.MaxTxNum, k[:], v[:])
}

func (TxNumsReader) Truncate(tx kv.RwTx, blockNum uint64) (err error) {
var seek [8]byte
binary.BigEndian.PutUint64(seek[:], blockNum)
Expand All @@ -172,13 +167,20 @@ func (TxNumsReader) Truncate(tx kv.RwTx, blockNum uint64) (err error) {
return err
}
defer c.Close()
prevBlockNum := blockNum
for k, _, err := c.Seek(seek[:]); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
currentBlockNum := binary.BigEndian.Uint64(k)
if currentBlockNum != prevBlockNum+1 /*no gaps, only growing*/ &&
currentBlockNum != blockNum /*to prevent first item error*/ {
return fmt.Errorf("bad block num: current num is %d but previous is %d", currentBlockNum, prevBlockNum)
}
if err = tx.Delete(kv.MaxTxNum, k); err != nil {
return err
}
prevBlockNum = currentBlockNum
//if err = c.DeleteCurrent(); err != nil {
// return err
//}
Expand Down
3 changes: 3 additions & 0 deletions eth/stagedsync/exec3_parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ func (pe *parallelExecutor) processResultQueue(ctx context.Context, inputTxNum u
if txTask.Error != nil {
return outputTxNum, conflicts, triggers, processedBlockNum, false, fmt.Errorf("%w: %v", consensus.ErrInvalidBlock, txTask.Error)
}
//if !pe.execStage.CurrentSyncCycle.IsInitialCycle && rand2.Int()%1500 == 0 && txTask.TxIndex == 0 && !pe.cfg.badBlockHalt {
// return outputTxNum, conflicts, triggers, processedBlockNum, false, fmt.Errorf("monkey in the datacenter: %w", consensus.ErrInvalidBlock)
//}
// TODO: post-validation of gasUsed and blobGasUsed
i++
}
Expand Down
3 changes: 3 additions & 0 deletions eth/stagedsync/exec3_serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (se *serialExecutor) execute(ctx context.Context, tasks []*state.TxTask) (c
}
}
}
//if !se.execStage.CurrentSyncCycle.IsInitialCycle && rand2.Int()%1500 == 0 && txTask.TxIndex == 0 && !se.cfg.badBlockHalt {
// return fmt.Errorf("monkey in the datacenter: %w", consensus.ErrInvalidBlock)
//}
return nil
}(); err != nil {
if errors.Is(err, context.Canceled) {
Expand Down
20 changes: 2 additions & 18 deletions eth/stagedsync/stage_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,8 @@ func FillDBFromSnapshots(logPrefix string, ctx context.Context, tx kv.RwTx, dirs
if err := rawdb.ResetSequence(tx, kv.EthTx, firstTxNum); err != nil {
return err
}
if err != nil {
return err
}

if err != nil {
return err
}
_ = tx.ClearBucket(kv.MaxTxNum)
hasInsertedAtLeastOneTxNum := true
if err := blockReader.IterateFrozenBodies(func(blockNum, baseTxNum, txAmount uint64) error {
select {
case <-ctx.Done():
Expand All @@ -500,17 +493,8 @@ func FillDBFromSnapshots(logPrefix string, ctx context.Context, tx kv.RwTx, dirs
return nil // This can actually happen as FrozenBlocks() is SegmentIdMax() and not the last .seg
}
if blockNum >= pruneMarkerBlockThreshold || blockNum == 0 {
if hasInsertedAtLeastOneTxNum {
if err := rawdbv3.TxNums.ForcedWrite(tx, blockNum, maxTxNum); err != nil {
return fmt.Errorf("%w. blockNum=%d, maxTxNum=%d", err, blockNum, maxTxNum)
}
if blockNum != 0 {
hasInsertedAtLeastOneTxNum = true
}
} else {
if err := rawdbv3.TxNums.Append(tx, blockNum, maxTxNum); err != nil {
return fmt.Errorf("%w. blockNum=%d, maxTxNum=%d", err, blockNum, maxTxNum)
}
if err := rawdbv3.TxNums.Append(tx, blockNum, maxTxNum); err != nil {
return fmt.Errorf("%w. blockNum=%d, maxTxNum=%d", err, blockNum, maxTxNum)
}
}
return nil
Expand Down

0 comments on commit 686df6c

Please sign in to comment.