-
Notifications
You must be signed in to change notification settings - Fork 122
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
feat(cmd): Add offline pruning of state trie. #1564
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7e7a45e
Add offline pruning of state trie.
arijitAD 21da2e7
Add pre-generated DB for testing.
arijitAD 727e84d
Minor nits.
arijitAD 95f7d16
Minor nit.
arijitAD 8c9171f
Address comments.
arijitAD 1fcb43e
Merge remote-tracking branch 'origin/development' into prune-state-trie
arijitAD 9dd07f7
Merge remote-tracking branch 'origin/development' into prune-state-trie
arijitAD 1eaf5dc
Address comments.
arijitAD 5c45547
Merge branch 'development' into prune-state-trie
arijitAD 2bff405
Merge branch 'development' into prune-state-trie
arijitAD 43b934a
Fix tests.
arijitAD a3445b3
Fix prune CI test
arijitAD 0473235
Merge remote-tracking branch 'origin/development' into prune-state-trie
arijitAD 4e0612b
Address comments
arijitAD 57c9ff6
Merge branch 'development' into prune-state-trie
arijitAD 1ad4224
Merge branch 'development' into prune-state-trie
arijitAD 3cd1599
Fix TestMessageCache.
arijitAD 1253b89
Merge branch 'development' into prune-state-trie
arijitAD 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
log "github.com/ChainSafe/log15" | ||
bloomfilter "github.com/holiman/bloomfilter/v2" | ||
) | ||
|
||
// ErrKeySize is returned when key size does not fit | ||
var ErrKeySize = errors.New("cannot have nil keystore") | ||
|
||
type stateBloomHasher []byte | ||
|
||
func (f stateBloomHasher) Write(p []byte) (n int, err error) { panic("not implemented") } | ||
func (f stateBloomHasher) Sum(b []byte) []byte { panic("not implemented") } | ||
func (f stateBloomHasher) Reset() { panic("not implemented") } | ||
func (f stateBloomHasher) BlockSize() int { panic("not implemented") } | ||
func (f stateBloomHasher) Size() int { return 8 } | ||
func (f stateBloomHasher) Sum64() uint64 { return binary.BigEndian.Uint64(f) } | ||
|
||
// stateBloom is a wrapper for bloom filter. | ||
// The keys of all generated entries will be recorded here so that in the pruning | ||
// stage the entries belong to the specific version can be avoided for deletion. | ||
type stateBloom struct { | ||
bloom *bloomfilter.Filter | ||
} | ||
|
||
// newStateBloomWithSize creates a brand new state bloom for state generation | ||
// The bloom filter will be created by the passing bloom filter size. the parameters | ||
// are picked so that the false-positive rate for mainnet is low enough. | ||
func newStateBloomWithSize(size uint64) (*stateBloom, error) { | ||
bloom, err := bloomfilter.New(size*1024*1024*8, 4) | ||
if err != nil { | ||
return nil, err | ||
} | ||
log.Info("initialised state bloom", "size", float64(bloom.M()/8)) | ||
return &stateBloom{bloom: bloom}, nil | ||
} | ||
|
||
// put writes key to bloom filter | ||
func (sb *stateBloom) put(key []byte) error { | ||
if len(key) != common.HashLength { | ||
return ErrKeySize | ||
} | ||
|
||
sb.bloom.Add(stateBloomHasher(key)) | ||
return nil | ||
} | ||
|
||
// contain is the wrapper of the underlying contains function which | ||
// reports whether the key is contained. | ||
// - If it says yes, the key may be contained | ||
// - If it says no, the key is definitely not contained. | ||
func (sb *stateBloom) contain(key []byte) bool { | ||
return sb.bloom.Contains(stateBloomHasher(key)) | ||
} |
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 |
---|---|---|
|
@@ -35,6 +35,7 @@ const ( | |
buildSpecCommandName = "build-spec" | ||
importRuntimeCommandName = "import-runtime" | ||
importStateCommandName = "import-state" | ||
pruningStateCommandName = "prune-state" | ||
) | ||
|
||
// app is the cli application | ||
|
@@ -115,6 +116,18 @@ var ( | |
"Input can be generated by using the RPC function state_getPairs.\n" + | ||
"\tUsage: gossamer import-state --state state.json --header header.json --first-slot <first slot of network>\n", | ||
} | ||
|
||
pruningCommand = cli.Command{ | ||
Action: FixFlagOrder(pruneState), | ||
Name: pruningStateCommandName, | ||
Usage: "Prune state will prune the state trie", | ||
ArgsUsage: "<root>", | ||
Flags: PruningFlags, | ||
Description: `prune-state <retain-block> will prune historical state data. | ||
All trie nodes that do not belong to the specified version state will be deleted from the database. | ||
|
||
The default pruning target is the HEAD-256 state`, | ||
} | ||
) | ||
|
||
// init initialises the cli application | ||
|
@@ -132,6 +145,7 @@ func init() { | |
buildSpecCommand, | ||
importRuntimeCommand, | ||
importStateCommand, | ||
pruningCommand, | ||
} | ||
app.Flags = RootFlags | ||
} | ||
|
@@ -411,3 +425,40 @@ func buildSpecAction(ctx *cli.Context) error { | |
|
||
return nil | ||
} | ||
|
||
func pruneState(ctx *cli.Context) error { | ||
inputDBPath := ctx.GlobalString(BasePathFlag.Name) | ||
if inputDBPath == "" { | ||
inputDBPath = dot.GssmrConfig().Global.BasePath | ||
} | ||
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. the user should be able to specify the chain with 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. Done |
||
|
||
bloomSize := ctx.Uint64(BloomFilterSizeFlag.Name) | ||
retainBlocks := ctx.Int64(RetainBlockNumberFlag.Name) | ||
|
||
pruner, err := newPruner(inputDBPath, bloomSize, retainBlocks) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info("Pruner initialised") | ||
|
||
err = pruner.setBloomFilter() | ||
if err != nil { | ||
return fmt.Errorf("failed to set keys into bloom filter %w", err) | ||
} | ||
|
||
// close input DB so we can open reopen it for streaming, | ||
_ = pruner.inputDB.Close() | ||
|
||
prunedDBPath := ctx.String(DBPathFlag.Name) | ||
if prunedDBPath == "" { | ||
return fmt.Errorf("path not specified for badger db") | ||
} | ||
|
||
err = pruner.prune(inputDBPath, prunedDBPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to prune %w", 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
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.
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.
maybe name
retain-blocks
otherwise it could be interpreted as what block number to retainThere 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.
Done