-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11e4c33
commit 52d8b3e
Showing
11 changed files
with
192 additions
and
212 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/eko/gocache/lib/v4/cache" | ||
gethlog "github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"github.com/obscuronet/go-obscuro/go/common/log" | ||
) | ||
|
||
func getCachedValue[V any](cache *cache.Cache[[]byte], logger gethlog.Logger, key any, onFailed func(any) (V, error)) (V, error) { | ||
value, err := cache.Get(context.Background(), key) | ||
if err != nil { | ||
// todo metrics for cache misses | ||
b, err := onFailed(key) | ||
if err != nil { | ||
return b, err | ||
} | ||
cacheValue(cache, logger, key, b) | ||
return b, err | ||
} | ||
|
||
v := new(V) | ||
err = rlp.DecodeBytes(value, v) | ||
return *v, err | ||
} | ||
|
||
func cacheValue(cache *cache.Cache[[]byte], logger gethlog.Logger, key any, v any) { | ||
encoded, err := rlp.EncodeToBytes(v) | ||
if err != nil { | ||
logger.Error("Could not encode value to store in cache", log.ErrKey, err) | ||
return | ||
} | ||
err = cache.Set(context.Background(), key, encoded) | ||
if err != nil { | ||
logger.Error("Could not store value in cache", log.ErrKey, 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
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,16 @@ | ||
package enclavedb | ||
|
||
import gethcommon "github.com/ethereum/go-ethereum/common" | ||
|
||
const truncHash = 16 | ||
|
||
func truncTo16(hash gethcommon.Hash) []byte { | ||
return truncBTo16(hash.Bytes()) | ||
} | ||
|
||
func truncBTo16(bytes []byte) []byte { | ||
b := bytes[0:truncHash] | ||
c := make([]byte, truncHash) | ||
copy(c, b) | ||
return c | ||
} |
Oops, something went wrong.