-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
add blockhashes stage #862
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
036bad4
add blockhashes stage
Giulio2002 41e4987
Merge remote-tracking branch 'origin/master' into stage_blockhash
Giulio2002 06d5ef9
unwindOrder specified
Giulio2002 b25f20d
fixed typo
Giulio2002 bd278ce
added common.copybytes
Giulio2002 f8e4173
fixed little typo
Giulio2002 59f7a10
better progress
Giulio2002 3ce1894
better progress
Giulio2002 bb3b601
Merge remote-tracking branch 'origin/master' into stage_blockhash
Giulio2002 5f47b5a
Merge remote-tracking branch 'origin/master' into stage_blockhash
Giulio2002 2e9fbb2
refactoring with etl
Giulio2002 5e6eac0
added blockhashes to disabled stages in cmd/integration
Giulio2002 728c7a3
added migraations
Giulio2002 7e7ba3b
fixed go.mod
Giulio2002 889f078
fixed go.mod
Giulio2002 1899883
better migrations
Giulio2002 4d671ca
rename unwind
Giulio2002 97e5c82
Merge remote-tracking branch 'origin/master' into stage_blockhash
Giulio2002 17dd786
simplified migrations
Giulio2002 fca6e16
added onloadcommit
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 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 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 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 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,49 @@ | ||
package stagedsync | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"github.com/ledgerwatch/turbo-geth/common" | ||
"github.com/ledgerwatch/turbo-geth/common/dbutils" | ||
"github.com/ledgerwatch/turbo-geth/common/etl" | ||
"github.com/ledgerwatch/turbo-geth/core/rawdb" | ||
"github.com/ledgerwatch/turbo-geth/ethdb" | ||
) | ||
|
||
func extractHeaders(k []byte, v []byte, next etl.ExtractNextFunc) error { | ||
// We only want to extract entries composed by Block Number + Header Hash | ||
if len(k) != 40 { | ||
return nil | ||
} | ||
return next(k, common.CopyBytes(k[8:]), common.CopyBytes(k[:8])) | ||
} | ||
|
||
func SpawnBlockHashStage(s *StageState, stateDB ethdb.Database, quit <-chan struct{}) error { | ||
headHash := rawdb.ReadHeadHeaderHash(stateDB) | ||
headNumber := rawdb.ReadHeaderNumber(stateDB, headHash) | ||
if s.BlockNumber == *headNumber { | ||
s.Done() | ||
return nil | ||
} | ||
|
||
startKey := make([]byte, 8) | ||
binary.BigEndian.PutUint64(startKey, s.BlockNumber) | ||
endKey := dbutils.HeaderKey(*headNumber, headHash) // Make sure we stop at head | ||
|
||
if err := etl.Transform( | ||
stateDB, | ||
dbutils.HeaderPrefix, | ||
dbutils.HeaderNumberPrefix, | ||
".", | ||
extractHeaders, | ||
etl.IdentityLoadFunc, | ||
etl.TransformArgs{ | ||
ExtractStartKey: startKey, | ||
ExtractEndKey: endKey, | ||
Quit: quit, | ||
}, | ||
); err != nil { | ||
return err | ||
} | ||
return s.DoneAndUpdate(stateDB, *headNumber) | ||
} |
This file contains 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,35 @@ | ||
package stagedsync | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ledgerwatch/turbo-geth/consensus/ethash" | ||
"github.com/ledgerwatch/turbo-geth/core/rawdb" | ||
"github.com/ledgerwatch/turbo-geth/ethdb" | ||
"github.com/ledgerwatch/turbo-geth/params" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBlockHashStage(t *testing.T) { | ||
origin, headers := generateFakeBlocks(1, 4) | ||
|
||
db := ethdb.NewMemDatabase() | ||
|
||
// prepare db so it works with our test | ||
rawdb.WriteHeaderNumber(db, origin.Hash(), 0) | ||
rawdb.WriteTd(db, origin.Hash(), 0, origin.Difficulty) | ||
rawdb.WriteHeader(context.TODO(), db, origin) | ||
rawdb.WriteHeadHeaderHash(db, origin.Hash()) | ||
rawdb.WriteCanonicalHash(db, origin.Hash(), 0) | ||
|
||
_, _, err := InsertHeaderChain(db, headers, params.AllEthashProtocolChanges, ethash.NewFaker(), 0) | ||
assert.NoError(t, err) | ||
err = SpawnBlockHashStage(&StageState{}, db, nil) | ||
assert.NoError(t, err) | ||
for _, h := range headers { | ||
n := rawdb.ReadHeaderNumber(db, h.Hash()) | ||
assert.Equal(t, *n, h.Number.Uint64()) | ||
} | ||
|
||
} |
This file contains 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 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 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 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 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,51 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/ledgerwatch/turbo-geth/common/etl" | ||
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages" | ||
"github.com/ledgerwatch/turbo-geth/ethdb" | ||
) | ||
|
||
var stagedsyncToUseStageBlockhashes = Migration{ | ||
Name: "stagedsync_to_use_stage_blockhashes", | ||
Up: func(db ethdb.Database, datadir string, OnLoadCommit etl.LoadCommitHandler) error { | ||
|
||
var progress uint64 | ||
var err error | ||
if progress, _, err = stages.GetStageProgress(db, stages.Headers); err != nil { | ||
return err | ||
} | ||
|
||
if err = stages.SaveStageProgress(db, stages.BlockHashes, progress, nil); err != nil { | ||
return err | ||
} | ||
|
||
if err = OnLoadCommit(db, nil, true); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var unwindStagedsyncToUseStageBlockhashes = Migration{ | ||
Name: "unwind_stagedsync_to_use_stage_blockhashes", | ||
Up: func(db ethdb.Database, datadir string, OnLoadCommit etl.LoadCommitHandler) error { | ||
|
||
var progress uint64 | ||
var err error | ||
if progress, _, err = stages.GetStageUnwind(db, stages.Headers); err != nil { | ||
return err | ||
} | ||
|
||
if err = stages.SaveStageUnwind(db, stages.BlockHashes, progress, nil); err != nil { | ||
return err | ||
} | ||
|
||
if err = OnLoadCommit(db, nil, true); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} |
This file contains 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,46 @@ | ||
package migrations | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages" | ||
"github.com/ledgerwatch/turbo-geth/ethdb" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStagedsyncToUseStageBlockhashes(t *testing.T) { | ||
|
||
require, db := require.New(t), ethdb.NewMemDatabase() | ||
var expected uint64 = 12 | ||
|
||
err := stages.SaveStageProgress(db, stages.Headers, expected, nil) | ||
require.NoError(err) | ||
|
||
migrator := NewMigrator() | ||
migrator.Migrations = []Migration{stagedsyncToUseStageBlockhashes} | ||
err = migrator.Apply(db, "") | ||
require.NoError(err) | ||
|
||
actual, _, err := stages.GetStageProgress(db, stages.BlockHashes) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
} | ||
|
||
func TestUnwindStagedsyncToUseStageBlockhashes(t *testing.T) { | ||
|
||
require, db := require.New(t), ethdb.NewMemDatabase() | ||
var expected uint64 = 12 | ||
|
||
err := stages.SaveStageUnwind(db, stages.Headers, expected, nil) | ||
require.NoError(err) | ||
|
||
migrator := NewMigrator() | ||
migrator.Migrations = []Migration{unwindStagedsyncToUseStageBlockhashes} | ||
err = migrator.Apply(db, "") | ||
require.NoError(err) | ||
|
||
actual, _, err := stages.GetStageUnwind(db, stages.BlockHashes) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a comment here why you are comparing with 40