forked from 0xPolygon/cdk-erigon
-
Notifications
You must be signed in to change notification settings - Fork 22
Cliff/genesis dump optimize #718
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
Open
cliff0412
wants to merge
29
commits into
dev-op
Choose a base branch
from
cliff/genesis-dump-optimize
base: dev-op
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
d362cd2
scan storage seperately
cliff0412 ea3a09e
add log
cliff0412 a581085
add log
cliff0412 2a0d494
add scalable read concurrently
cliff0412 7adbb54
add logging
cliff0412 52ec780
add logging
cliff0412 b6c774b
add concurrent scan for scalable
cliff0412 d9385c2
add concurrent scan for scalable
cliff0412 bd07b32
add concurrent scan for scalable
cliff0412 a76fa02
add concurrent scan for scalable
cliff0412 8a170db
add log
cliff0412 8e64263
refactoring
cliff0412 cb4282d
refactoring
cliff0412 27d58dd
refactoring
cliff0412 1887a7d
refactoring
cliff0412 133c140
refactoring
cliff0412 dc6f80d
refactoring
cliff0412 aa9c5f6
refactoring
cliff0412 cef3988
refactoring
cliff0412 2d79110
refactoring
cliff0412 b87cf04
refactoring
cliff0412 719a086
refactoring
cliff0412 f46e71d
refactoring
cliff0412 6e5260a
refactoring
cliff0412 c1796b2
Merge remote-tracking branch 'origin/dev-op' into cliff/genesis-dump-…
cliff0412 015323a
fixing merging conflicts
cliff0412 2f980bf
rm unused
cliff0412 3409e1b
rm unused
cliff0412 2de5d0a
rm unused
cliff0412 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -504,6 +504,127 @@ func readJsonFile(input string) (GenesisData, error) { | |
| return jsonData, nil | ||
| } | ||
|
|
||
| type KeyRange struct { | ||
| Start []byte | ||
| End []byte | ||
| } | ||
|
|
||
| // for storage key, it is of 32 bytes. we split the storage key space into chunks lexically | ||
| func generateKeyRanges(numChunks int) []KeyRange { | ||
| keyRanges := make([]KeyRange, numChunks) | ||
|
|
||
| for i := 0; i < numChunks; i++ { | ||
| startKey := make([]byte, 32) | ||
| startKey[0] = byte(i * (256 / numChunks)) | ||
|
|
||
| var endKey []byte | ||
| if i == numChunks-1 { | ||
| endKey = bytes.Repeat([]byte{0xFF}, 32) | ||
| } else { | ||
| endKey = make([]byte, 32) | ||
| endKey[0] = byte((i + 1) * (256 / numChunks)) | ||
| } | ||
|
|
||
| keyRanges[i] = KeyRange{Start: startKey, End: endKey} | ||
| } | ||
|
|
||
| return keyRanges | ||
| } | ||
|
|
||
| type StorageEntry struct { | ||
| Key string | ||
| Value string | ||
| } | ||
|
|
||
| func processScalableAddressStorageConcurrently(db kv.RwDB, prefix []byte, acct *AccInfo) (uint64, error) { | ||
|
|
||
| numWorkers := 32 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use "numWorkers := runtime.NumCPU()"? |
||
| keyRanges := generateKeyRanges(numWorkers) | ||
|
|
||
| // Create channels for results | ||
| var wg sync.WaitGroup | ||
| results := make(chan []StorageEntry, numWorkers) | ||
|
|
||
| // Start workers | ||
| for i := 0; i < numWorkers; i++ { | ||
| wg.Add(1) | ||
| go func(workerID int) { | ||
| defer wg.Done() | ||
| start := time.Now() | ||
| keyRange := keyRanges[workerID] | ||
|
|
||
| // Create a new transaction for this worker using db.View | ||
| if err := db.View(context.Background(), func(workerTx kv.Tx) error { | ||
| chunkStorage := make([]StorageEntry, 0, 1<<20) | ||
|
|
||
| // Get cursor for this worker | ||
| startKey := make([]byte, 60) | ||
| copy(startKey, prefix[0:28]) | ||
| copy(startKey[28:], keyRange.Start) | ||
|
|
||
| endKey := make([]byte, 60) | ||
| copy(endKey, prefix[0:28]) | ||
| copy(endKey[28:], keyRange.End) | ||
|
|
||
| iter, err := workerTx.Range(kv.PlainState, startKey, endKey) | ||
| if err != nil { | ||
| logger.Error("failed to create range iterator", "worker", workerID, "error", err) | ||
| results <- chunkStorage | ||
| return err | ||
| } | ||
|
|
||
| for iter.HasNext() { | ||
| keyStorage, valStorage, err := iter.Next() | ||
| if err != nil { | ||
| logger.Error("failed to read value from cursor", "worker", workerID, "error", err) | ||
| break | ||
| } | ||
| if len(keyStorage) > 28 { | ||
| chunkStorage = append(chunkStorage, StorageEntry{ | ||
| Key: hexutil.Encode(keyStorage[28:]), | ||
| Value: BytesToPaddedHex(valStorage, 64), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| results <- chunkStorage | ||
| logger.Info("worker completed", "id", workerID, "elapsed", time.Since(start)) | ||
| return nil | ||
| }); err != nil { | ||
| logger.Error("worker transaction failed", "worker", workerID, "error", err) | ||
| results <- make([]StorageEntry, 0, 1024) | ||
| } | ||
| }(i) | ||
| } | ||
|
|
||
| go func() { | ||
| wg.Wait() | ||
| close(results) | ||
| }() | ||
|
|
||
| start := time.Now() | ||
| var totalStorage uint64 | ||
| chunkCount := 0 | ||
|
|
||
| var allChunks [][]StorageEntry | ||
| for chunkStorage := range results { | ||
| allChunks = append(allChunks, chunkStorage) | ||
| totalStorage += uint64(len(chunkStorage)) | ||
| chunkCount++ | ||
| } | ||
|
|
||
| acct.Storage = make(map[string]string, int(totalStorage)) | ||
|
|
||
| for _, chunk := range allChunks { | ||
| for _, entry := range chunk { | ||
| acct.Storage[entry.Key] = entry.Value | ||
| } | ||
| } | ||
|
|
||
| logger.Info("Scalable address total storage items", "count", totalStorage, "chunk count", chunkCount, "elapsed", time.Since(start)) | ||
| return totalStorage, nil | ||
| } | ||
|
|
||
| func writeJsonFile(data GenesisData, output string) error { | ||
| startJsonMarshal := time.Now() | ||
| updatedData, err := json.MarshalIndent(data, "", " ") | ||
|
|
@@ -550,7 +671,12 @@ func scanDbGenerateGenesisData(input, chaindata string) (GenesisData, error) { | |
|
|
||
| startScanKeys := time.Now() | ||
| if err := db.View(context.Background(), func(tx kv.Tx) error { | ||
| var skipNums uint64 = 0 | ||
| return tx.ForEach(kv.PlainState, nil, func(k, v []byte) error { | ||
| if skipNums > 0 { | ||
| skipNums-- | ||
| return nil | ||
| } | ||
|
Vui-Chee marked this conversation as resolved.
|
||
| total++ | ||
| // account | ||
| if len(k) == 20 { | ||
|
|
@@ -588,7 +714,7 @@ func scanDbGenerateGenesisData(input, chaindata string) (GenesisData, error) { | |
|
|
||
| // storage | ||
| if len(k) > 28 { | ||
| storageCount++ | ||
| var acctStorageCount uint64 = 0 | ||
| acctBytes := k[:20] | ||
| acctHex := common.Bytes2Hex(acctBytes) | ||
|
|
||
|
|
@@ -600,7 +726,34 @@ func scanDbGenerateGenesisData(input, chaindata string) (GenesisData, error) { | |
| acc.Storage = make(map[string]string) | ||
| } | ||
|
|
||
| acc.Storage[hexutil.Encode(k[28:])] = BytesToPaddedHex(v, 64) | ||
| scalableAddressStr := strings.ToLower(strings.TrimPrefix(state.ADDRESS_SCALABLE_L2.Hex(), "0x")) | ||
| startAcctStorage := time.Now() | ||
| if acctHex == scalableAddressStr { | ||
|
|
||
| logger.Info("scalable acct bytes", "bytes", acctBytes, "incarnation", k[20:28]) | ||
| scalableStorageCount, err := processScalableAddressStorageConcurrently(db, k[:28], acc) | ||
| acctStorageCount = scalableStorageCount | ||
|
Comment on lines
+734
to
+735
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we omit |
||
| if err != nil { | ||
| logger.Error("processing scalable address storage", "error", err) | ||
| } | ||
|
|
||
| } else { | ||
| tx.ForPrefix(kv.PlainState, k[:20], func(storageK, storageV []byte) error { | ||
| if len(storageK) > 20 { | ||
| acc.Storage[hexutil.Encode(storageK[28:])] = BytesToPaddedHex(storageV, 64) | ||
| acctStorageCount++ | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| elapsed := time.Since(startAcctStorage) | ||
| skipNums = acctStorageCount - 1 | ||
| storageCount = storageCount + acctStorageCount | ||
| if acctHex == scalableAddressStr { | ||
| fmt.Println("scanning for scalable, number of storages", acctStorageCount, "elapsed", elapsed) | ||
| } | ||
|
|
||
| } | ||
| return nil | ||
| }) | ||
|
|
||
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.
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.
You initialize
startKeywith 32 bytes, but the the following assignment only assigns the first byte in the array. Is it becauseworkerTx.Range(kv.PlainState, startKey, endKey)requires each key to be 60 bytes long?