Skip to content

Commit

Permalink
make key with transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
giskook committed Jul 1, 2024
1 parent 509ae7c commit 9c156ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func (e *EthEndpoints) Call(arg *types.TxArgs, blockArg *types.BlockNumberOrHash

// X Layer LRU
if lru_xlayer.GetConfig().Enable {
ret, errValue, ok := getCallResultFromLRU(blockToProcess, tx.To(), tx.Data())
ret, errValue, ok := getCallResultFromLRU(blockToProcess, sender, tx)
if ok {
log.Infof("Call result from LRU cache: %v, %v", ret, errValue)
return ret, errValue
}
defer func() {
setCallResultToLRU(blockToProcess, tx.To(), tx.Data(), respRet, errRet)
setCallResultToLRU(blockToProcess, sender, tx, respRet, errRet)
}()
}

Expand Down
15 changes: 8 additions & 7 deletions jsonrpc/endpoints_eth_lru_xlayer.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package jsonrpc

import (
"crypto/md5"
"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, to *common.Address, input []byte) (string, string) {
baseKey := fmt.Sprintf("%d-%s-%s", blockNumber, to.Hex(), md5.Sum(input))
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, to *common.Address, input []byte) (interface{}, types.Error, bool) {
retKey, errKey := getCallKey(blockNumber, to, input)
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
Expand All @@ -33,8 +34,8 @@ func getCallResultFromLRU(blockNumber *uint64, to *common.Address, input []byte)
return value, v, true
}

func setCallResultToLRU(blockNumber *uint64, to *common.Address, input []byte, value interface{}, errValue types.Error) {
retKey, errKey := getCallKey(blockNumber, to, input)
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)
Expand Down

0 comments on commit 9c156ee

Please sign in to comment.