-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR: refactor tx signature cache (#1595)
* refactor tx signature cache * fix ut * optimize code Co-authored-by: Zhong Qiu <[email protected]>
- Loading branch information
1 parent
4abdcfb
commit 4ceaea0
Showing
9 changed files
with
149 additions
and
88 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
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,122 @@ | ||
package types | ||
|
||
import ( | ||
"sync/atomic" | ||
|
||
"github.com/spf13/viper" | ||
|
||
ethcmn "github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
|
||
lru "github.com/hashicorp/golang-lru" | ||
) | ||
|
||
var ( | ||
signatureCache *Cache | ||
) | ||
|
||
const FlagSigCacheSize = "signature-cache-size" | ||
|
||
func init() { | ||
// used for ut | ||
defaultCache := &Cache{ | ||
data: nil, | ||
readCount: 0, | ||
hitCount: 0, | ||
} | ||
signatureCache = defaultCache | ||
} | ||
|
||
func InitSignatureCache() { | ||
lruCache, err := lru.New(viper.GetInt(FlagSigCacheSize)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
signatureCache = &Cache{ | ||
data: lruCache, | ||
} | ||
} | ||
|
||
func SignatureCache() *Cache { | ||
return signatureCache | ||
} | ||
|
||
type Cache struct { | ||
data *lru.Cache | ||
readCount int64 | ||
hitCount int64 | ||
} | ||
|
||
func (c *Cache) Get(key string) (*TxSigCache, bool) { | ||
// validate | ||
if !c.validate(key) { | ||
return nil, false | ||
} | ||
atomic.AddInt64(&c.readCount, 1) | ||
// get cache | ||
value, ok := c.data.Get(key) | ||
if ok { | ||
sigCache, ok := value.(*TxSigCache) | ||
if ok { | ||
atomic.AddInt64(&c.hitCount, 1) | ||
return sigCache, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func (c *Cache) Add(key string, value *TxSigCache) { | ||
// validate | ||
if !c.validate(key) { | ||
return | ||
} | ||
// add cache | ||
c.data.Add(key, value) | ||
} | ||
|
||
func (c *Cache) Remove(key string) { | ||
// validate | ||
if !c.validate(key) { | ||
return | ||
} | ||
c.data.Remove(key) | ||
} | ||
|
||
func (c *Cache) ReadCount() int64 { | ||
return atomic.LoadInt64(&c.readCount) | ||
} | ||
|
||
func (c *Cache) HitCount() int64 { | ||
return atomic.LoadInt64(&c.hitCount) | ||
} | ||
|
||
func (c *Cache) validate(key string) bool { | ||
// validate key | ||
if key == "" { | ||
return false | ||
} | ||
// validate lru cache | ||
if c.data == nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// TxSignatureCache is used to cache the derived sender and contains the signer used | ||
// to derive it. | ||
type TxSigCache struct { | ||
Signer ethtypes.Signer | ||
From ethcmn.Address | ||
} | ||
|
||
func (s TxSigCache) GetFrom() ethcmn.Address { | ||
return s.From | ||
} | ||
|
||
func (s TxSigCache) GetSigner() ethtypes.Signer { | ||
return s.Signer | ||
} | ||
|
||
func (s TxSigCache) EqualSiger(siger ethtypes.Signer) bool { | ||
return s.Signer.Equal(siger) | ||
} |
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 was deleted.
Oops, something went wrong.