-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add blockhashes stage * unwindOrder specified * fixed typo * added common.copybytes * fixed little typo * better progress * better progress * refactoring with etl * added blockhashes to disabled stages in cmd/integration * added migraations * fixed go.mod * better migrations * rename unwind * simplified migrations * added onloadcommit
- Loading branch information
1 parent
81c4878
commit 0578949
Showing
11 changed files
with
221 additions
and
7 deletions.
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) | ||
} |