-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: cache for signature verification #18422
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d6f5195
add cache for sig verification
tac0turtle 4ff9d56
fix init function
tac0turtle 6b1c512
remove sha256 check
tac0turtle 20524ea
fix/cleanup
tac0turtle 46e4d58
x/auth/signing: start benchmarking
odeke-em dae49f3
fix mismatched benchmarking variables
odeke-em 984b2a9
address comments
tac0turtle a0c7a6c
remove global init and pass the cache through to where its used
tac0turtle 3a98aa1
Merge branch 'main' into marko/15780
tac0turtle 8418550
address comments and linting
tac0turtle 4c52445
add bool to handleroptions
tac0turtle 905f326
remove do once
tac0turtle 60c9759
Update x/auth/client/cli/tx_multisign.go
tac0turtle 31c3568
lint
tac0turtle dab15ee
Merge branch 'main' into marko/15780
tac0turtle 04644c6
address bot
tac0turtle a94863d
Merge branch 'main' into marko/15780
tac0turtle dc1bb85
move cache to types
tac0turtle a1af317
return error on new cache, rename cache to signatureCache
tac0turtle 9f35046
Merge branch 'main' into marko/15780
tac0turtle 42b6466
fix build
tac0turtle 74a6008
address comments
tac0turtle 34d2743
Merge branch 'main' into marko/15780
tac0turtle 8a42f45
cleanup
tac0turtle f7ac82d
simplfy validate
tac0turtle 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||||
package types | ||||||
|
||||||
import ( | ||||||
lru "github.com/hashicorp/golang-lru/v2" | ||||||
) | ||||||
|
||||||
// SignatureCache is a cache of verified signatures | ||||||
type SignatureCache struct { | ||||||
data *lru.Cache[string, []byte] | ||||||
} | ||||||
|
||||||
// NewSignatureCache initializes the signature cache. | ||||||
// signature verification is one of the most expensive parts in verification | ||||||
// by caching it we avoid needing to verify the same signature multiple times | ||||||
func NewSignatureCache() (*SignatureCache, error) { | ||||||
// 500 * (32 + 42) = 37.5KB | ||||||
cache, err := lru.New[string, []byte](500) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
return &SignatureCache{data: cache}, nil | ||||||
} | ||||||
|
||||||
// Get returns the cached signature if it exists | ||||||
func (c *SignatureCache) Get(key string) ([]byte, bool) { | ||||||
if !c.validate(key) { | ||||||
return nil, false | ||||||
} | ||||||
|
||||||
return c.data.Get(key) | ||||||
} | ||||||
|
||||||
// Add adds a signature to the cache | ||||||
func (c *SignatureCache) Add(key string, value []byte) { | ||||||
// validate | ||||||
if !c.validate(key) { | ||||||
return | ||||||
} | ||||||
c.data.Add(key, value) | ||||||
} | ||||||
|
||||||
// Remove removes a signature from the cache | ||||||
func (c *SignatureCache) Remove(key string) { | ||||||
// validate | ||||||
if !c.validate(key) { | ||||||
return | ||||||
} | ||||||
c.data.Remove(key) | ||||||
} | ||||||
|
||||||
// validate validates the key and cache | ||||||
func (c *SignatureCache) validate(key string) bool { | ||||||
return len(key) != 0 | ||||||
} | ||||||
|
||||||
// sigkey is the key used to store the signature in the cache | ||||||
type sigkey struct { | ||||||
signbytes []byte | ||||||
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.
Suggested change
|
||||||
sig []byte | ||||||
} | ||||||
|
||||||
func NewSigKey(signbytes, sig []byte) sigkey { | ||||||
return sigkey{ | ||||||
signbytes: signbytes, | ||||||
sig: sig, | ||||||
} | ||||||
} | ||||||
|
||||||
func (s sigkey) String() string { | ||||||
return string(append(s.signbytes, s.sig...)) | ||||||
} | ||||||
tac0turtle marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -20,6 +20,7 @@ type HandlerOptions struct { | |||||
SignModeHandler *txsigning.HandlerMap | ||||||
SigGasConsumer func(meter storetypes.GasMeter, sig signing.SignatureV2, params types.Params) error | ||||||
TxFeeChecker TxFeeChecker | ||||||
Cachesignature bool | ||||||
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.
Suggested change
|
||||||
} | ||||||
|
||||||
// NewAnteHandler returns an AnteHandler that checks and increments sequence | ||||||
|
@@ -48,7 +49,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { | |||||
NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), | ||||||
NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators | ||||||
NewValidateSigCountDecorator(options.AccountKeeper), | ||||||
NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.SigGasConsumer), | ||||||
NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.SigGasConsumer, options.Cachesignature), | ||||||
} | ||||||
|
||||||
return sdk.ChainAnteDecorators(anteDecorators...), 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
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.
My only gripe with this is that I think
Add
should actually take acryptotypes.SIgKey
as an argument instead of astring
. The reason I recommend this is that theSignatureCache
doesn't really pertain to a signature cache, the API suggests it can be used for any [string,[]byte] mapping. So I'd recommend either changing the argument.Note, this isn't to block, but just a suggestion.