forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 219
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 lru for eth_call #230
Closed
Closed
Add lru for eth_call #230
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
509ae7c
add lru for eth_call
giskook 9c156ee
make key with transaction
giskook 58d1a4c
add go mod
giskook 17598b6
use lru 1.0.2
giskook c4c802f
add logs
giskook b5dcad2
add logs
giskook d2e4c81
add log
giskook 3c22634
add logs
giskook 8346be6
add metric
giskook 4bbff5f
fix conflict
giskook 854204a
format file
giskook 9c06e3d
add docs
giskook 573ebce
fix lint
giskook 183a2b6
trun on when e2e test
giskook 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package jsonrpc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/lru_xlayer" | ||
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types" | ||
"github.com/0xPolygonHermez/zkevm-node/log" | ||
"github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
func getCallKey(blockNumber *uint64, sender common.Address, tx *ethtypes.Transaction) (string, string) { | ||
baseKey := fmt.Sprintf("%d-%s-%s", blockNumber, sender.String(), tx.Hash().String()) | ||
return baseKey + "ret", baseKey + "err" | ||
} | ||
|
||
func getCallResultFromLRU(blockNumber *uint64, sender common.Address, tx *ethtypes.Transaction) (interface{}, types.Error, bool) { | ||
retKey, errKey := getCallKey(blockNumber, sender, tx) | ||
value, ok := lru_xlayer.GetLRU().Get(retKey) | ||
if !ok { | ||
return nil, nil, false | ||
} | ||
errValue, ok := lru_xlayer.GetLRU().Get(errKey) | ||
if !ok { | ||
return nil, nil, false | ||
} | ||
if errValue == nil { | ||
return value, nil, true | ||
} | ||
v, ok := errValue.(types.Error) | ||
if !ok { | ||
return nil, nil, false | ||
} | ||
|
||
return value, v, true | ||
} | ||
|
||
func setCallResultToLRU(blockNumber *uint64, sender common.Address, tx *ethtypes.Transaction, value interface{}, errValue types.Error) { | ||
retKey, errKey := getCallKey(blockNumber, sender, tx) | ||
err := lru_xlayer.GetLRU().Set(retKey, value) | ||
if err != nil { | ||
log.Debugf("Failed to set value to LRU cache call ret: %v", err) | ||
return | ||
} | ||
err = lru_xlayer.GetLRU().Set(errKey, errValue) | ||
if err != nil { | ||
log.Debugf("Failed to set value to LRU cache call err: %v", err) | ||
} | ||
} |
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,21 @@ | ||
package lru_xlayer | ||
|
||
var ( | ||
gConfigure Config | ||
) | ||
|
||
// Config is the configuration for the LRU cache | ||
type Config struct { | ||
Enable bool | ||
Size int | ||
} | ||
|
||
// SetConfig sets the configuration for the LRU cache | ||
func SetConfig(c Config) { | ||
gConfigure = c | ||
} | ||
|
||
// GetConfig returns the configuration for the LRU cache | ||
func GetConfig() Config { | ||
return gConfigure | ||
} |
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,53 @@ | ||
package lru_xlayer | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/0xPolygonHermez/zkevm-node/log" | ||
lru "github.com/hashicorp/golang-lru" | ||
) | ||
|
||
var ( | ||
once sync.Once | ||
instance *LRUCache | ||
) | ||
|
||
// LRUCache is a simple interface for a LRU cache. | ||
type LRUCache struct { | ||
cache *lru.Cache | ||
} | ||
|
||
func newLRUCache() *LRUCache { | ||
c, err := lru.New(GetConfig().Size) | ||
if err != nil { | ||
log.Fatal("Failed to create LRU cache", "err", err) | ||
return nil | ||
} | ||
return &LRUCache{cache: c} | ||
} | ||
|
||
// Get retrieves a value from the cache. | ||
func (l *LRUCache) Get(key string) (interface{}, bool) { | ||
if l == nil || !GetConfig().Enable { | ||
return nil, false | ||
} | ||
|
||
return l.cache.Get(key) | ||
} | ||
|
||
// Set adds a value to the cache. | ||
func (l *LRUCache) Set(key string, value interface{}) error { | ||
if l == nil || !GetConfig().Enable { | ||
return nil | ||
} | ||
l.cache.Add(key, value) | ||
return nil | ||
} | ||
|
||
// GetLRU returns the LRU cache instance. | ||
func GetLRU() *LRUCache { | ||
once.Do(func() { | ||
instance = newLRUCache() | ||
}) | ||
return instance | ||
} |
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.
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 cause nil panic
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.
No idea about the nil panic