Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Nov 6, 2024
1 parent 6edb11b commit 0bb8754
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 81 deletions.
8 changes: 4 additions & 4 deletions internal/client/state-db/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ func (drqim *inMemDeathRowQueue[BlockHash, Key]) Import(
block, ok := drqim.deathIndex[k]
if ok {
delete(drqim.deathIndex, k)
delete(drqim.deathRows.At(int(block-base)).deleted, k)
delete(drqim.deathRows.At(int(block-base)).deleted, k) //nolint:gosec
}
}
// add new keys
importedBlock := base + uint64(drqim.deathRows.Len())
importedBlock := base + uint64(drqim.deathRows.Len()) //nolint:gosec
deletedMap := make(map[Key]any)
for _, k := range deleted {
drqim.deathIndex[k] = importedBlock
Expand All @@ -236,15 +236,15 @@ func (drqim *inMemDeathRowQueue[BlockHash, Key]) PopFront(base uint64) (*deathRo
// Check if the block at the given `index` of the queue exist
// it is the caller's responsibility to ensure `index` won't be out of bounds
func (drqim *inMemDeathRowQueue[BlockHash, Key]) HaveBlock(hash BlockHash, index uint) haveBlock {
if drqim.deathRows.At(int(index)).hash == hash {
if drqim.deathRows.At(int(index)).hash == hash { //nolint:gosec
return haveBlockYes
}
return haveBlockNo
}

// Return the number of block in the pruning window
func (drqim *inMemDeathRowQueue[BlockHash, Key]) Len(base uint64) uint64 {
return uint64(drqim.deathRows.Len())
return uint64(drqim.deathRows.Len()) //nolint:gosec
}

// Get the hash of the next pruning block
Expand Down
4 changes: 3 additions & 1 deletion internal/cost-lru/cost_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type LRU[K comparable, V any] struct {
}

// Costructor for [LRU].
func New[K comparable, V any](maxCost uint, hash freelru.HashKeyCallback[K], costFunc func(K, V) uint32) (*LRU[K, V], error) {
func New[K comparable, V any](
maxCost uint, hash freelru.HashKeyCallback[K], costFunc func(K, V) uint32,
) (*LRU[K, V], error) {
var capacity = uint32(math.MaxUint32)
if maxCost < math.MaxUint32 {
capacity = uint32(maxCost)
Expand Down
8 changes: 4 additions & 4 deletions internal/primitives/state-machine/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ type Backend[Hash runtime.Hash, H runtime.Hasher[Hash]] interface {
// Calculate the child storage root, with given delta over what is already stored in
// the backend, and produce a "transaction" that can be used to commit. The second argument
// is true if child storage root equals default storage root.
ChildStorageRoot(childInfo storage.ChildInfo, delta []Delta, stateVersion storage.StateVersion) (Hash, bool, BackendTransaction[Hash, H])
ChildStorageRoot(
childInfo storage.ChildInfo, delta []Delta, stateVersion storage.StateVersion,
) (Hash, bool, BackendTransaction[Hash, H])

// Returns a lifetimeless raw storage iterator.
RawIter(args IterArgs) (StorageIterator[Hash, H], error)
Expand All @@ -181,8 +183,6 @@ type Backend[Hash runtime.Hash, H runtime.Hasher[Hash]] interface {
// in the backend, and produce a "transaction" that can be used to commit.
// Does include child storage updates.
FullStorageRoot(
delta []Delta,
childDeltas []ChildDelta,
stateVersion storage.StateVersion,
delta []Delta, childDeltas []ChildDelta, stateVersion storage.StateVersion,
) (Hash, BackendTransaction[Hash, H])
}
8 changes: 5 additions & 3 deletions internal/primitives/state-machine/in_memory_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func (tb *MemoryDBTrieBackend[H, Hasher]) clone() MemoryDBTrieBackend[H, Hasher]
}

// Copy the state, with applied updates
func (tb *MemoryDBTrieBackend[H, Hasher]) update(changes []change, stateVersion storage.StateVersion) MemoryDBTrieBackend[H, Hasher] {
func (tb *MemoryDBTrieBackend[H, Hasher]) update(
changes []change, stateVersion storage.StateVersion,
) MemoryDBTrieBackend[H, Hasher] {
clone := tb.clone()
clone.insert(changes, stateVersion)
return clone
Expand All @@ -55,15 +57,15 @@ func (tb *MemoryDBTrieBackend[H, Hasher]) insert(changes []change, stateVersion
var deltas []Delta
for _, change := range top {
for _, skv := range change.StorageCollection {
deltas = append(deltas, Delta{skv.StorageKey, skv.StorageValue})
deltas = append(deltas, Delta{Key: skv.StorageKey, Value: skv.StorageValue})
}
}

var childDeltas []ChildDelta
for _, change := range child {
var delta []Delta
for _, skv := range change.StorageCollection {
delta = append(delta, Delta{skv.StorageKey, skv.StorageValue})
delta = append(delta, Delta{Key: skv.StorageKey, Value: skv.StorageValue})
}
childDeltas = append(childDeltas, ChildDelta{
ChildInfo: change.ChildInfo,
Expand Down
12 changes: 9 additions & 3 deletions internal/primitives/state-machine/trie_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func (tb *TrieBackend[H, Hasher]) ClosestMerkleValue(key []byte) (triedb.MerkleV
return tb.essence.ClosestMerkleValue(key)
}

func (tb *TrieBackend[H, Hasher]) ChildClosestMerkleValue(childInfo storage.ChildInfo, key []byte) (triedb.MerkleValue[H], error) {
func (tb *TrieBackend[H, Hasher]) ChildClosestMerkleValue(
childInfo storage.ChildInfo, key []byte,
) (triedb.MerkleValue[H], error) {
return tb.essence.ChildClosestMerkleValue(childInfo, key)
}

Expand Down Expand Up @@ -187,12 +189,16 @@ func (tb *TrieBackend[H, Hasher]) RawIter(args IterArgs) (StorageIterator[H, Has
return tb.essence.RawIter(args)
}

func (tb *TrieBackend[H, Hasher]) StorageRoot(delta []Delta, stateVersion storage.StateVersion) (H, BackendTransaction[H, Hasher]) {
func (tb *TrieBackend[H, Hasher]) StorageRoot(
delta []Delta, stateVersion storage.StateVersion,
) (H, BackendTransaction[H, Hasher]) {
h, pmdb := tb.essence.StorageRoot(delta, stateVersion)
return h, BackendTransaction[H, Hasher]{pmdb}
}

func (tb *TrieBackend[H, Hasher]) ChildStorageRoot(childInfo storage.ChildInfo, delta []Delta, stateVersion storage.StateVersion) (H, bool, BackendTransaction[H, Hasher]) {
func (tb *TrieBackend[H, Hasher]) ChildStorageRoot(
childInfo storage.ChildInfo, delta []Delta, stateVersion storage.StateVersion,
) (H, bool, BackendTransaction[H, Hasher]) {
h, b, pmdb := tb.essence.ChildStorageRoot(childInfo, delta, stateVersion)
return h, b, BackendTransaction[H, Hasher]{pmdb}
}
Expand Down
47 changes: 30 additions & 17 deletions internal/primitives/state-machine/trie_backend_essence.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func withTrieDB[H runtime.Hash, Hasher runtime.Hasher[H]](
}

withRecorderAndCache(tbe, &root, func(recorder triedb.TrieRecorder, cache triedb.TrieCache[H]) {
trieDB := triedb.NewTrieDB[H, Hasher](root, db, triedb.WithCache[H, Hasher](cache), triedb.WithRecorder[H, Hasher](recorder))
trieDB := triedb.NewTrieDB(
root, db, triedb.WithCache[H, Hasher](cache), triedb.WithRecorder[H, Hasher](recorder))
trieDB.SetVersion(ptrie.V1)
callback(trieDB)
})
Expand Down Expand Up @@ -299,7 +300,9 @@ func (tbe *trieBackendEssence[H, Hasher]) childRoot(childInfo storage.ChildInfo)

// Return the next key in the child trie i.e. the minimum key that is strictly superior to
// key in lexicographic order.
func (tbe *trieBackendEssence[H, Hasher]) NextChildStorageKey(childInfo storage.ChildInfo, key []byte) (StorageKey, error) {
func (tbe *trieBackendEssence[H, Hasher]) NextChildStorageKey(
childInfo storage.ChildInfo, key []byte,
) (StorageKey, error) {
childRoot, err := tbe.childRoot(childInfo)
if err != nil {
return nil, err
Expand All @@ -312,7 +315,9 @@ func (tbe *trieBackendEssence[H, Hasher]) NextChildStorageKey(childInfo storage.
}

// Return next key from main trie or child trie by providing corresponding root.
func (tbe *trieBackendEssence[H, Hasher]) NextStorageKeyFromRoot(root H, childInfo storage.ChildInfo, key []byte) (StorageKey, error) {
func (tbe *trieBackendEssence[H, Hasher]) NextStorageKeyFromRoot(
root H, childInfo storage.ChildInfo, key []byte,
) (StorageKey, error) {
var err error
var nextKey []byte
withTrieDB(tbe, root, childInfo, func(trie *triedb.TrieDB[H, Hasher]) {
Expand Down Expand Up @@ -359,7 +364,8 @@ func (tbe *trieBackendEssence[H, Hasher]) Storage(key []byte) (val StorageValue,
// Returns the hash value
func (tbe *trieBackendEssence[H, Hasher]) StorageHash(key []byte) (hash *H, err error) {
withRecorderAndCache[H, Hasher](tbe, nil, func(recorder triedb.TrieRecorder, cache triedb.TrieCache[H]) {
trieDB := triedb.NewTrieDB[H, Hasher](tbe.root, tbe, triedb.WithCache[H, Hasher](cache), triedb.WithRecorder[H, Hasher](recorder))
trieDB := triedb.NewTrieDB(
tbe.root, tbe, triedb.WithCache[H, Hasher](cache), triedb.WithRecorder[H, Hasher](recorder))
trieDB.SetVersion(ptrie.V1)
hash, err = trieDB.GetHash(key)
})
Expand Down Expand Up @@ -429,19 +435,22 @@ func (tbe *trieBackendEssence[H, Hasher]) ClosestMerkleValue(key []byte) (val tr
}

// Get the child closest merkle value at given key.
func (tbe *trieBackendEssence[H, Hasher]) ChildClosestMerkleValue(childInfo storage.ChildInfo, key []byte) (val triedb.MerkleValue[H], err error) {
func (tbe *trieBackendEssence[H, Hasher]) ChildClosestMerkleValue(
childInfo storage.ChildInfo, key []byte,
) (val triedb.MerkleValue[H], err error) {
var childRoot H
root, err := tbe.childRoot(childInfo)
if err != nil {
return nil, err
}
if root == nil {
return nil, nil
return nil, nil //nolint:nilnil
}
childRoot = *root

withRecorderAndCache(tbe, &childRoot, func(recorder triedb.TrieRecorder, cache triedb.TrieCache[H]) {
val, err = trie.ReadChildTrieFirstDescendantValue[H, Hasher](childInfo.Keyspace(), tbe, tbe.root, key, recorder, cache, triedb.V1)
val, err = trie.ReadChildTrieFirstDescendantValue[H, Hasher](
childInfo.Keyspace(), tbe, tbe.root, key, recorder, cache, triedb.V1)
})
return
}
Expand Down Expand Up @@ -506,18 +515,22 @@ func (tbe *trieBackendEssence[H, Hasher]) RawIter(args IterArgs) (*rawIter[H, Ha
}

// Return the storage root after applying the given delta.
func (tbe *trieBackendEssence[H, Hasher]) StorageRoot(delta []Delta, stateVersion storage.StateVersion) (H, *trie.PrefixedMemoryDB[H, Hasher]) {
func (tbe *trieBackendEssence[H, Hasher]) StorageRoot(
delta []Delta, stateVersion storage.StateVersion,
) (H, *trie.PrefixedMemoryDB[H, Hasher]) {
writeOverlay := trie.NewPrefixedMemoryDB[H, Hasher]()

root := withRecorderAndCacheForStorageRoot[H, Hasher, H](tbe, nil, func(recorder triedb.TrieRecorder, cache triedb.TrieCache[H]) (*H, H) {
eph := newEphemeral[H, Hasher](tbe.BackendStorage(), writeOverlay)
root, err := trie.DeltaTrieRoot[H, Hasher](eph, tbe.root, delta, recorder, cache, stateVersion.TrieLayout())
if err != nil {
log.Printf("WARN: failed to write to trie: %v", err)
return nil, tbe.root
}
return &root, root
})
root := withRecorderAndCacheForStorageRoot(
tbe, nil, func(recorder triedb.TrieRecorder, cache triedb.TrieCache[H]) (*H, H) {
eph := newEphemeral[H, Hasher](tbe.BackendStorage(), writeOverlay)
root, err := trie.DeltaTrieRoot[H, Hasher](eph, tbe.root, delta, recorder, cache, stateVersion.TrieLayout())
if err != nil {
log.Printf("WARN: failed to write to trie: %v", err)
return nil, tbe.root
}
return &root, root
},
)

return root, writeOverlay
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ func TestTrieBackendEssence(t *testing.T) {
require.Equal(t, root1.Bytes(), val)
}

essence1 := newTrieBackendEssence[hash.H256, runtime.BlakeTwo256](HashDBTrieBackendStorage[hash.H256]{mdb}, root1, nil, nil)
tb1 := TrieBackend[hash.H256, runtime.BlakeTwo256]{essence: essence1}
essence1 := newTrieBackendEssence[hash.H256, runtime.BlakeTwo256](
HashDBTrieBackendStorage[hash.H256]{mdb}, root1, nil, nil)
tb1 := TrieBackend[hash.H256, runtime.BlakeTwo256]{essence: essence1} //nolint:govet

key, err := tb1.NextStorageKey([]byte("2"))
require.NoError(t, err)
Expand All @@ -88,7 +89,8 @@ func TestTrieBackendEssence(t *testing.T) {
require.NoError(t, err)
require.Nil(t, key)

essence2 := newTrieBackendEssence[hash.H256, runtime.BlakeTwo256](HashDBTrieBackendStorage[hash.H256]{mdb}, root2, nil, nil)
essence2 := newTrieBackendEssence[hash.H256, runtime.BlakeTwo256](
HashDBTrieBackendStorage[hash.H256]{mdb}, root2, nil, nil)

key, err = essence2.NextChildStorageKey(childInfo, []byte("2"))
require.NoError(t, err)
Expand Down
29 changes: 13 additions & 16 deletions internal/primitives/trie/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (nc nodeCached[H]) ByteSize() uint {
//
// This cache should be used per state instance created by the backend. One state instance is
// referring to the state of one block. It will cache all the accesses that are done to the state
// which could not be fullfilled by the [SharedTrieCache]. These locally cached items are merged
// which could not be fulfilled by the [SharedTrieCache]. These locally cached items are merged
// back to the shared trie cache when this instance is dropped.
//
// When using [LocalTrieCache.TrieCache] or [LocalTrieCache.TrieCacheMut] it will lock mutexes.
Expand Down Expand Up @@ -193,15 +193,15 @@ type valueCache[H runtime.Hash] interface {
// This is used for example when a new trie is being build, to cache new values.
type freshValueCache[H runtime.Hash] map[string]triedb.CachedValue[H]

func (fvc *freshValueCache[H]) get(key []byte, sharedCache *SharedTrieCache[H]) triedb.CachedValue[H] {
func (fvc *freshValueCache[H]) get(key []byte, sharedCache *SharedTrieCache[H]) triedb.CachedValue[H] { //nolint:unused
val, ok := (*fvc)[string(key)]
if ok {
return val
} else {
return nil
}
}
func (fvc *freshValueCache[H]) insert(key []byte, value triedb.CachedValue[H]) {
func (fvc *freshValueCache[H]) insert(key []byte, value triedb.CachedValue[H]) { //nolint:unused
(*fvc)[string(key)] = value
}

Expand All @@ -212,7 +212,8 @@ type forStorageRootValueCache[H runtime.Hash] struct {
storageRoot H
}

func (fsrvc forStorageRootValueCache[H]) get(key []byte, sharedCache *SharedTrieCache[H]) triedb.CachedValue[H] {
func (fsrvc forStorageRootValueCache[H]) get( //nolint:unused
key []byte, sharedCache *SharedTrieCache[H]) triedb.CachedValue[H] {
// We first need to look up in the local cache and then the shared cache.
// It can happen that some value is cached in the shared cache, but the
// weak reference of the data can not be upgraded anymore. This for example
Expand All @@ -237,7 +238,7 @@ func (fsrvc forStorageRootValueCache[H]) get(key []byte, sharedCache *SharedTrie

return nil
}
func (fsrvc forStorageRootValueCache[H]) insert(key []byte, value triedb.CachedValue[H]) {
func (fsrvc forStorageRootValueCache[H]) insert(key []byte, value triedb.CachedValue[H]) { //nolint:unused
vck := ValueCacheKey[H]{
StorageKey: key,
StorageRoot: fsrvc.storageRoot,
Expand Down Expand Up @@ -285,21 +286,22 @@ func (tc *TrieCache[H]) MergeInto(local *LocalTrieCache[H], storageRoot H) {
}
}

func (tc *TrieCache[H]) GetOrInsertNode(hash H, fetchNode func() (triedb.CachedNode[H], error)) (triedb.CachedNode[H], error) {
func (tc *TrieCache[H]) GetOrInsertNode(
hash H, fetchNode func() (triedb.CachedNode[H], error),
) (triedb.CachedNode[H], error) {
var isLocalCacheHit bool = true

// First try to grab the node from the local cache.
var err error
var node *nodeCached[H]
local, ok := tc.localCache.Get(hash)
node, ok := tc.localCache.Get(hash)
if !ok {
isLocalCacheHit = false

// It was not in the local cache; try the shared cache.
shared := tc.sharedCache.PeekNode(hash)
if shared != nil {
log.Printf("TRACE: Serving node from shared cache: %s\n", hash)
node = &nodeCached[H]{Node: shared, FromSharedCache: true}
node = nodeCached[H]{Node: shared, FromSharedCache: true}
} else {
// It was not in the shared cache; try fetching it from the database.
var fetched triedb.CachedNode[H]
Expand All @@ -309,21 +311,16 @@ func (tc *TrieCache[H]) GetOrInsertNode(hash H, fetchNode func() (triedb.CachedN
return nil, err
} else {
log.Printf("TRACE: Serving node from database: %s\n", hash)
node = &nodeCached[H]{Node: fetched, FromSharedCache: false}
node = nodeCached[H]{Node: fetched, FromSharedCache: false}
}
}
tc.localCache.Add(hash, *node)
} else {
node = &local
tc.localCache.Add(hash, node)
}

if isLocalCacheHit {
log.Printf("TRACE: Serving node from local cache: %s\n", hash)
}

if node == nil {
panic("you can always insert at least one element into the local cache; qed")
}
return node.Node, nil
}

Expand Down
Loading

0 comments on commit 0bb8754

Please sign in to comment.