-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[EIP-4399 2/3] Added total difficulty stage and transition logic #2964
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b0b11b0
added diff stage
Giulio2002 ba7486b
added td stage
Giulio2002 8a07f77
improvements + more tests
Giulio2002 e0097e8
added err check
Giulio2002 57d7ee2
added err check
Giulio2002 06345b0
Merge remote-tracking branch 'origin/devel' into td-stage
Giulio2002 28b39c1
transition raw interfaces
Giulio2002 8605036
interface for transition
Giulio2002 9355efd
unwind updates
Giulio2002 22301cd
maybe this fix
Giulio2002 c4906bb
now fixed?
Giulio2002 c09dfac
fixes
Giulio2002 1f3fb7a
better db-format
Giulio2002 1895fe0
better db-format
Giulio2002 d2256c7
sum and mod
Giulio2002 1e68f17
sum and mod
Giulio2002 d20b92c
fixed headers unwind
Giulio2002 6f28fe7
fixed headers unwind
Giulio2002 5de7b14
Merge branch 'devel' into td-stage
Giulio2002 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package stagedsync | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/binary" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/ledgerwatch/erigon-lib/etl" | ||
| "github.com/ledgerwatch/erigon-lib/kv" | ||
| "github.com/ledgerwatch/erigon/core/rawdb" | ||
| "github.com/ledgerwatch/erigon/core/types" | ||
| "github.com/ledgerwatch/erigon/eth/stagedsync/stages" | ||
| "github.com/ledgerwatch/erigon/rlp" | ||
| ) | ||
|
|
||
| type DifficultyCfg struct { | ||
| tmpDir string | ||
| terminalTotalDifficulty *big.Int | ||
| db kv.RwDB | ||
| } | ||
|
|
||
| func StageDifficultyCfg(db kv.RwDB, tmpDir string, terminalTotalDifficulty *big.Int) DifficultyCfg { | ||
| return DifficultyCfg{ | ||
| db: db, | ||
| tmpDir: tmpDir, | ||
| terminalTotalDifficulty: terminalTotalDifficulty, | ||
| } | ||
| } | ||
|
|
||
| func SpawnDifficultyStage(s *StageState, tx kv.RwTx, cfg DifficultyCfg, ctx context.Context) (err error) { | ||
| useExternalTx := tx != nil | ||
|
|
||
| if !useExternalTx { | ||
| var err error | ||
| tx, err = cfg.db.BeginRw(context.Background()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer tx.Rollback() | ||
| } | ||
|
|
||
| quit := ctx.Done() | ||
| headNumber, err := stages.GetStageProgress(tx, stages.Headers) | ||
| if err != nil { | ||
| return fmt.Errorf("getting headers progress: %w", err) | ||
| } | ||
|
|
||
| td := big.NewInt(0) | ||
| if s.BlockNumber > 0 { | ||
| td, err = rawdb.ReadTd(tx, rawdb.ReadHeaderByNumber(tx, s.BlockNumber).Hash(), s.BlockNumber) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| // If the chain does not have a proof of stake config or has reached terminalTotalDifficulty then we can skip this stage | ||
| if cfg.terminalTotalDifficulty == nil || td.Cmp(cfg.terminalTotalDifficulty) >= 0 { | ||
| if err = s.Update(tx, headNumber); err != nil { | ||
| return err | ||
| } | ||
| if !useExternalTx { | ||
| if err = tx.Commit(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| startKey := make([]byte, 8) | ||
| binary.BigEndian.PutUint64(startKey, s.BlockNumber) | ||
|
|
||
| header := new(types.Header) | ||
| if err := etl.Transform( | ||
| s.LogPrefix(), | ||
| tx, | ||
| kv.Headers, | ||
| kv.HeaderTD, | ||
| cfg.tmpDir, | ||
| func(k []byte, v []byte, next etl.ExtractNextFunc) error { | ||
| if len(k) != 40 { | ||
| return nil | ||
| } | ||
|
|
||
| blockNum := binary.BigEndian.Uint64(k) | ||
| canonical, err := rawdb.ReadCanonicalHash(tx, blockNum) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !bytes.Equal(k[8:], canonical[:]) { | ||
| return nil | ||
| } | ||
| if err := rlp.Decode(bytes.NewReader(v), header); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| td.Add(td, header.Difficulty) | ||
| if td.Cmp(cfg.terminalTotalDifficulty) > 0 { | ||
| return rawdb.MarkTransition(tx, blockNum) | ||
| } | ||
| data, err := rlp.EncodeToBytes(td) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to RLP encode block total difficulty: %w", err) | ||
| } | ||
| return next(k, k, data) | ||
| }, | ||
| etl.IdentityLoadFunc, | ||
| etl.TransformArgs{ | ||
| ExtractStartKey: startKey, | ||
| Quit: quit, | ||
| }, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
| if err = s.Update(tx, headNumber); err != nil { | ||
| return err | ||
| } | ||
| if !useExternalTx { | ||
| if err = tx.Commit(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func UnwindDifficultyStage(u *UnwindState, tx kv.RwTx, ctx context.Context) (err error) { | ||
| useExternalTx := tx != nil | ||
|
|
||
| if err = u.Done(tx); err != nil { | ||
| return fmt.Errorf(" reset: %w", err) | ||
| } | ||
| if !useExternalTx { | ||
| if err = tx.Commit(); err != nil { | ||
| return fmt.Errorf("failed to write db commit: %w", err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func PruneDifficultyStage(p *PruneState, tx kv.RwTx, ctx context.Context) (err error) { | ||
| useExternalTx := tx != nil | ||
|
|
||
| if !useExternalTx { | ||
| if err = tx.Commit(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.