-
Notifications
You must be signed in to change notification settings - Fork 829
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 tools to scan and compute hash for IAVL db #2059
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4020d98
Add tools to scan and compute hash for IAVL db
yzang2019 58f2f4d
Fix lint
yzang2019 a490cb9
Fix lint
yzang2019 b387896
Fix
yzang2019 9ddec69
Fix
yzang2019 4f8d32c
Fix
yzang2019 f466989
Pebble scanner (#2061)
Kbhat1 947514f
Merge branch 'main' into yzang/SEI-8920
Kbhat1 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/sei-protocol/sei-chain/tools/hash_verification/iavl" | ||
"github.com/sei-protocol/sei-chain/tools/hash_verification/pebbledb" | ||
"github.com/sei-protocol/sei-db/config" | ||
sstypes "github.com/sei-protocol/sei-db/ss" | ||
"github.com/tendermint/tendermint/libs/log" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
func GenerateIavlHashCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generate-iavl-hash", | ||
Short: "A tool to scan full IAVL archive database and generate a hash for every N blocks per module", | ||
Run: generateIavlHash, | ||
} | ||
cmd.PersistentFlags().String("home-dir", "/root/.sei", "Sei home directory") | ||
cmd.PersistentFlags().Int64("blocks-interval", 1_000_000, "Generate a hash every N blocks") | ||
return cmd | ||
} | ||
|
||
func generateIavlHash(cmd *cobra.Command, _ []string) { | ||
homeDir, _ := cmd.Flags().GetString("home-dir") | ||
blocksInterval, _ := cmd.Flags().GetInt64("blocks-interval") | ||
dataDir := filepath.Join(homeDir, "data") | ||
db, err := dbm.NewGoLevelDB("application", dataDir) | ||
if err != nil { | ||
panic(err) | ||
} | ||
scanner := iavl.NewHashScanner(db, blocksInterval) | ||
scanner.ScanAllModules() | ||
} | ||
|
||
func GeneratePebbleHashCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generate-pebble-hash", | ||
Short: "A tool to scan full Pebble archive database and generate a hash for every N blocks per module", | ||
Run: generatePebbleHash, | ||
} | ||
cmd.PersistentFlags().String("home-dir", "/root/.sei", "Sei home directory") | ||
cmd.PersistentFlags().Int64("blocks-interval", 1_000_000, "Generate a hash every N blocks") | ||
return cmd | ||
} | ||
|
||
func generatePebbleHash(cmd *cobra.Command, _ []string) { | ||
homeDir, _ := cmd.Flags().GetString("home-dir") | ||
blocksInterval, _ := cmd.Flags().GetInt64("blocks-interval") | ||
|
||
ssConfig := config.DefaultStateStoreConfig() | ||
ssConfig.Enable = true | ||
ssConfig.KeepRecent = 0 | ||
stateStore, err := sstypes.NewStateStore(log.NewNopLogger(), homeDir, ssConfig) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
scanner := pebbledb.NewHashScanner(stateStore, blocksInterval) | ||
scanner.ScanAllModules() | ||
} |
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,8 @@ | ||
package hasher | ||
|
||
// HashCalculator defines the interface for calculating chained state hash. | ||
type HashCalculator interface { | ||
HashSingle(data []byte) []byte | ||
HashTwo(dataA []byte, dataB []byte) []byte | ||
ComputeHashes() [][]byte | ||
} |
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,103 @@ | ||
package hasher | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"sync" | ||
|
||
"github.com/sei-protocol/sei-db/ss/types" | ||
) | ||
|
||
var _ HashCalculator = (*XorHashCalculator)(nil) | ||
|
||
// XorHashCalculator is the hash calculator backed by XoR hash. | ||
type XorHashCalculator struct { | ||
NumBlocksPerWorker int64 | ||
NumOfWorkers int | ||
DataCh chan types.RawSnapshotNode | ||
} | ||
|
||
// NewXorHashCalculator create a new XorHashCalculator. | ||
func NewXorHashCalculator(numBlocksPerWorker int64, numWorkers int, data chan types.RawSnapshotNode) XorHashCalculator { | ||
return XorHashCalculator{ | ||
NumBlocksPerWorker: numBlocksPerWorker, | ||
NumOfWorkers: numWorkers, | ||
DataCh: data, | ||
} | ||
} | ||
|
||
// HashSingle computes the hash of a single data element. | ||
func (x XorHashCalculator) HashSingle(data []byte) []byte { | ||
hash := sha256.Sum256(data) | ||
return hash[:] | ||
} | ||
|
||
// HashTwo computes the hash of a two data elements, performs XOR between two byte slices of equal size. | ||
func (x XorHashCalculator) HashTwo(dataA []byte, dataB []byte) []byte { | ||
if len(dataA) != len(dataB) { | ||
panic("Expecting both data to have equal length for computing a XoR hash") | ||
} | ||
result := make([]byte, len(dataA)) | ||
for i := range dataA { | ||
result[i] = dataA[i] ^ dataB[i] | ||
} | ||
return result | ||
} | ||
|
||
func (x XorHashCalculator) ComputeHashes() [][]byte { | ||
var wg sync.WaitGroup | ||
allChannels := make([]chan types.RawSnapshotNode, x.NumOfWorkers) | ||
allHashes := make([][]byte, x.NumOfWorkers) | ||
// First calculate each sub hash in a separate goroutine | ||
for i := 0; i < x.NumOfWorkers; i++ { | ||
wg.Add(1) | ||
subsetChan := make(chan types.RawSnapshotNode, 1000) | ||
go func(index int, data chan types.RawSnapshotNode) { | ||
defer wg.Done() | ||
var hashResult []byte | ||
for item := range subsetChan { | ||
entryHash := x.HashSingle(Serialize(item)) | ||
if hashResult == nil { | ||
hashResult = entryHash | ||
} else { | ||
hashResult = x.HashTwo(hashResult, entryHash) | ||
} | ||
} | ||
allHashes[index] = hashResult | ||
}(i, subsetChan) | ||
allChannels[i] = subsetChan | ||
} | ||
// Push all the data to its corresponding channel based on version | ||
for data := range x.DataCh { | ||
index := data.Version / x.NumBlocksPerWorker | ||
allChannels[index] <- data | ||
} | ||
// Close all sub channels | ||
for _, subChan := range allChannels { | ||
close(subChan) | ||
} | ||
// Wait for all workers to complete | ||
wg.Wait() | ||
// Now modify sub hashes to hash again with previous hash | ||
for i := 1; i < len(allHashes); i++ { | ||
if len(allHashes[i-1]) > 0 && len(allHashes[i]) > 0 { | ||
allHashes[i] = x.HashTwo(allHashes[i-1], allHashes[i]) | ||
} else if len(allHashes[i-1]) > 0 && len(allHashes[i]) == 0 { | ||
allHashes[i] = allHashes[i-1] | ||
} | ||
} | ||
return allHashes | ||
} | ||
|
||
func Serialize(node types.RawSnapshotNode) []byte { | ||
keySize := len(node.Key) | ||
valueSize := len(node.Value) | ||
versionSize := 8 | ||
buf := make([]byte, keySize+valueSize+versionSize) | ||
copy(buf[:keySize], node.Key) | ||
offset := keySize | ||
copy(buf[offset:offset+valueSize], node.Value) | ||
offset += valueSize | ||
binary.LittleEndian.PutUint64(buf[offset:offset+versionSize], uint64(node.Version)) | ||
return buf | ||
} |
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,82 @@ | ||
package iavl | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
"github.com/cosmos/iavl" | ||
"github.com/sei-protocol/sei-chain/tools/hash_verification/hasher" | ||
"github.com/sei-protocol/sei-chain/tools/utils" | ||
"github.com/sei-protocol/sei-db/ss/types" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
type HashScanner struct { | ||
db dbm.DB | ||
latestVersion int64 | ||
blocksInterval int64 | ||
hashResult map[string][][]byte | ||
} | ||
|
||
func NewHashScanner(db dbm.DB, blocksInterval int64) *HashScanner { | ||
latestVersion := rootmulti.GetLatestVersion(db) | ||
fmt.Printf("Detected IAVL latest version: %d\n", latestVersion) | ||
return &HashScanner{ | ||
db: db, | ||
latestVersion: latestVersion, | ||
blocksInterval: blocksInterval, | ||
hashResult: make(map[string][][]byte), | ||
} | ||
} | ||
|
||
func (s *HashScanner) ScanAllModules() { | ||
for _, moduleName := range utils.Modules { | ||
result := s.scanAllHeights(moduleName) | ||
for i, hashResult := range result { | ||
fmt.Printf("Module %s height %d hash is: %X\n", moduleName, s.blocksInterval*(int64(i)+1), hashResult) | ||
} | ||
} | ||
} | ||
|
||
func (s *HashScanner) scanAllHeights(module string) [][]byte { | ||
dataCh := make(chan types.RawSnapshotNode, 10000) | ||
hashCalculator := hasher.NewXorHashCalculator(s.blocksInterval, int(s.latestVersion/s.blocksInterval+1), dataCh) | ||
fmt.Printf("Starting to scan module: %s\n", module) | ||
go func() { | ||
prefixDB := dbm.NewPrefixDB(s.db, []byte(utils.BuildRawPrefix(module))) | ||
itr, err := prefixDB.Iterator(nil, nil) | ||
count := 0 | ||
if err != nil { | ||
panic(fmt.Errorf("failed to create iterator: %w", err)) | ||
} | ||
defer itr.Close() | ||
for ; itr.Valid(); itr.Next() { | ||
value := bytes.Clone(itr.Value()) | ||
node, err := iavl.MakeNode(value) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to parse iavl node: %w", err)) | ||
} | ||
|
||
// Only scan leaf nodes | ||
if node.GetHeight() != 0 { | ||
continue | ||
} | ||
snapshotNode := types.RawSnapshotNode{ | ||
StoreKey: module, | ||
Key: node.GetNodeKey(), | ||
Value: node.GetValue(), | ||
Version: node.GetVersion(), | ||
} | ||
dataCh <- snapshotNode | ||
count++ | ||
if count%1000000 == 0 { | ||
fmt.Printf("Scanned %d items for module %s\n", count, module) | ||
} | ||
} | ||
close(dataCh) | ||
}() | ||
allHashes := hashCalculator.ComputeHashes() | ||
s.hashResult[module] = allHashes | ||
return allHashes | ||
} |
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,70 @@ | ||
package pebbledb | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sei-protocol/sei-chain/tools/hash_verification/hasher" | ||
"github.com/sei-protocol/sei-chain/tools/utils" | ||
"github.com/sei-protocol/sei-db/ss/types" | ||
) | ||
|
||
type HashScanner struct { | ||
db types.StateStore | ||
latestVersion int64 | ||
blocksInterval int64 | ||
hashResult map[string][][]byte | ||
} | ||
|
||
func NewHashScanner(db types.StateStore, blocksInterval int64) *HashScanner { | ||
latestVersion, err := db.GetLatestVersion() | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("Detected Pebbledb latest version: %d\n", latestVersion) | ||
return &HashScanner{ | ||
db: db, | ||
latestVersion: latestVersion, | ||
blocksInterval: blocksInterval, | ||
hashResult: make(map[string][][]byte), | ||
} | ||
} | ||
|
||
func (s *HashScanner) ScanAllModules() { | ||
for _, moduleName := range utils.Modules { | ||
result := s.scanAllHeights(moduleName) | ||
for i, hashResult := range result { | ||
fmt.Printf("Module %s height %d hash is: %X\n", moduleName, s.blocksInterval*(int64(i)+1), hashResult) | ||
} | ||
} | ||
} | ||
|
||
func (s *HashScanner) scanAllHeights(module string) [][]byte { | ||
dataCh := make(chan types.RawSnapshotNode, 10000) | ||
hashCalculator := hasher.NewXorHashCalculator(s.blocksInterval, int(s.latestVersion/s.blocksInterval+1), dataCh) | ||
fmt.Printf("Starting to scan module: %s\n", module) | ||
go func() { | ||
count := 0 | ||
_, err := s.db.RawIterate(module, func(key, value []byte, version int64) bool { | ||
dataCh <- types.RawSnapshotNode{ | ||
StoreKey: module, | ||
Key: key, | ||
Value: value, | ||
Version: version, | ||
} | ||
|
||
count++ | ||
if count%1000000 == 0 { | ||
fmt.Printf("Scanned %d items for module %s\n", count, module) | ||
} | ||
|
||
return false | ||
}) | ||
if err != nil { | ||
panic(fmt.Errorf("RawIterate error: %w", err)) | ||
} | ||
close(dataCh) | ||
}() | ||
Comment on lines
+45
to
+66
Check notice Code scanning / CodeQL Spawning a Go routine Note
Spawning a Go routine may be a possible source of non-determinism
|
||
allHashes := hashCalculator.ComputeHashes() | ||
s.hashResult[module] = allHashes | ||
return allHashes | ||
} |
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
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.
Check notice
Code scanning / CodeQL
Spawning a Go routine Note